KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > dlg > ColumnItem


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer.dlg;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.util.*;
25 import javax.swing.*;
26 import org.netbeans.lib.ddl.*;
27 import org.netbeans.modules.db.explorer.*;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30
31 public class ColumnItem extends Hashtable {
32     public static final String JavaDoc NAME = "name"; //NOI18N
33
public static final String JavaDoc TYPE = "type"; //NOI18N
34
public static final String JavaDoc SIZE = "size"; //NOI18N
35
public static final String JavaDoc SCALE = "scale"; //NOI18N
36
public static final String JavaDoc PRIMARY_KEY = "pkey"; //NOI18N
37
public static final String JavaDoc INDEX = "idx"; //NOI18N
38
public static final String JavaDoc NULLABLE = "nullable"; //NOI18N
39
public static final String JavaDoc COMMENT = "comment"; //NOI18N
40
public static final String JavaDoc DEFVAL = "defval"; //NOI18N
41
public static final String JavaDoc UNIQUE = "unique"; //NOI18N
42
public static final String JavaDoc CHECK = "check"; //NOI18N
43
public static final String JavaDoc CHECK_CODE = "checkcode"; //NOI18N
44

45     private PropertyChangeSupport JavaDoc propertySupport;
46
47     public static final Map getColumnProperty(int idx)
48     {
49         return (Map)getProperties().elementAt(idx);
50     }
51
52     public static final Vector getProperties()
53     {
54         return (Vector)CreateTableDialog.getProperties().get("columns"); //NOI18N
55
}
56
57     public static final Vector getProperties(String JavaDoc pname)
58     {
59         Vector vec = getProperties(), cnames = new Vector(vec.size());
60         Enumeration evec = vec.elements();
61         while (evec.hasMoreElements()) {
62             Map pmap = (Map)evec.nextElement();
63             cnames.add(pmap.get(pname));
64         }
65
66         return cnames;
67     }
68
69     public static final Vector getColumnNames()
70     {
71         return getProperties("name"); //NOI18N
72
}
73
74     public static final Vector getColumnTitles()
75     {
76         return getProperties("columntitle"); //NOI18N
77
}
78
79     public static final Vector getColumnClasses()
80     {
81         return getProperties("columnclass"); //NOI18N
82
}
83
84     static final long serialVersionUID =-6638535249384813829L;
85     public ColumnItem()
86     {
87         Vector vec = getProperties();
88         Enumeration evec = vec.elements();
89         propertySupport = new PropertyChangeSupport JavaDoc(this);
90         while (evec.hasMoreElements()) {
91             Map pmap = (Map)evec.nextElement();
92             Object JavaDoc pdv = pmap.get("default"); //NOI18N
93
if (pdv != null) {
94                 String JavaDoc pclass = (String JavaDoc)pmap.get("columnclass"); //NOI18N
95
if (pclass.equals("java.lang.Boolean")) pdv = Boolean.valueOf((String JavaDoc)pdv); //NOI18N
96
put(pmap.get("name"), pdv); //NOI18N
97
}
98         }
99     }
100
101     /** Add property change listener
102     * Registers a listener for the PropertyChange event. The connection object
103     * should fire a PropertyChange event whenever somebody changes driver, database,
104     * login name or password.
105     */

106     public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
107         propertySupport.addPropertyChangeListener (l);
108     }
109
110     /** Remove property change listener
111     * Remove a listener for the PropertyChange event.
112     */

113     public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
114         propertySupport.removePropertyChangeListener (l);
115     }
116
117     public Object JavaDoc getProperty(String JavaDoc pname)
118     {
119         return get(pname);
120     }
121
122     public void setProperty(String JavaDoc pname, Object JavaDoc value) {
123         if (pname == null)
124             return;
125         
126         Object JavaDoc old = get(pname);
127         if (old != null) {
128             Class JavaDoc oldc = old.getClass();
129             if (old.equals(value))
130                 return;
131
132             try {
133                 if (!oldc.equals(value.getClass()))
134                     if (oldc.equals(Integer JavaDoc.class))
135                         if ("".equals((String JavaDoc) value))
136                             value = new Integer JavaDoc(0);
137                         else
138                             value = Integer.valueOf((String JavaDoc) value);
139             } catch (NumberFormatException JavaDoc e) {
140                 //PENDING
141
}
142         }
143
144         put(pname, value);
145         propertySupport.firePropertyChange(pname, old, value);
146     }
147
148     public String JavaDoc getName()
149     {
150         return (String JavaDoc)get(NAME);
151     }
152
153     public TypeElement getType() {
154         return (TypeElement) get(TYPE);
155     }
156
157     public int getSize() {
158         Object JavaDoc size = get(SIZE);
159         
160         if (size instanceof String JavaDoc) {
161             if ("".equals(size))
162                 size = "0";
163             return Integer.valueOf((String JavaDoc) size).intValue();
164         }
165         
166         return ((Integer JavaDoc) size).intValue();
167     }
168
169     public boolean isPrimaryKey()
170     {
171         Boolean JavaDoc val = (Boolean JavaDoc)get(PRIMARY_KEY);
172         if (val != null) return val.booleanValue();
173         return false;
174     }
175
176     public boolean isUnique()
177     {
178         Boolean JavaDoc val = (Boolean JavaDoc)get(UNIQUE);
179         if (val != null) return val.booleanValue();
180         return false;
181     }
182
183     public boolean isIndexed()
184     {
185         Boolean JavaDoc val = (Boolean JavaDoc)get(INDEX);
186         if (val != null) return val.booleanValue();
187         return false;
188     }
189
190     public boolean allowsNull()
191     {
192         Boolean JavaDoc val = (Boolean JavaDoc)get(NULLABLE);
193         if (val != null) return val.booleanValue();
194         return false;
195     }
196
197     public boolean hasCheckConstraint()
198     {
199         Boolean JavaDoc val = (Boolean JavaDoc)get(CHECK);
200         if (val != null) return val.booleanValue();
201         return false;
202     }
203
204     public String JavaDoc getCheckConstraint()
205     {
206         return (String JavaDoc)get(CHECK_CODE);
207     }
208
209     public boolean hasDefaultValue()
210     {
211         String JavaDoc dv = getDefaultValue();
212         if (dv != null && dv.length()>0) return true;
213         return false;
214     }
215
216     public String JavaDoc getDefaultValue()
217     {
218         return (String JavaDoc)get(DEFVAL);
219     }
220
221     public boolean validate()
222     {
223         String JavaDoc name = getName();
224         int size = getSize();
225         int scale = getScale();
226
227         if (size < scale)
228             return false;
229         if (name == null || name.length() == 0)
230             return false;
231
232         return true;
233     }
234     /** Getter for property scale.
235      * @return Value of property scale.
236      */

237     public int getScale() {
238         return ((Integer JavaDoc)get(SCALE)).intValue();
239     }
240 }
241
Popular Tags