001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.dlg;
043:
044: import java.awt.*;
045: import java.awt.event.*;
046: import java.util.*;
047: import javax.swing.*;
048: import org.netbeans.lib.ddl.*;
049: import org.netbeans.modules.db.explorer.*;
050: import java.beans.PropertyChangeSupport;
051: import java.beans.PropertyChangeListener;
052:
053: public class ColumnItem extends Hashtable {
054: public static final String NAME = "name"; //NOI18N
055: public static final String TYPE = "type"; //NOI18N
056: public static final String SIZE = "size"; //NOI18N
057: public static final String SCALE = "scale"; //NOI18N
058: public static final String PRIMARY_KEY = "pkey"; //NOI18N
059: public static final String INDEX = "idx"; //NOI18N
060: public static final String NULLABLE = "nullable"; //NOI18N
061: public static final String COMMENT = "comment"; //NOI18N
062: public static final String DEFVAL = "defval"; //NOI18N
063: public static final String UNIQUE = "unique"; //NOI18N
064: public static final String CHECK = "check"; //NOI18N
065: public static final String CHECK_CODE = "checkcode"; //NOI18N
066:
067: private PropertyChangeSupport propertySupport;
068:
069: public static final Map getColumnProperty(int idx) {
070: return (Map) getProperties().elementAt(idx);
071: }
072:
073: public static final Vector getProperties() {
074: return (Vector) CreateTableDialog.getProperties()
075: .get("columns"); //NOI18N
076: }
077:
078: public static final Vector getProperties(String pname) {
079: Vector vec = getProperties(), cnames = new Vector(vec.size());
080: Enumeration evec = vec.elements();
081: while (evec.hasMoreElements()) {
082: Map pmap = (Map) evec.nextElement();
083: cnames.add(pmap.get(pname));
084: }
085:
086: return cnames;
087: }
088:
089: public static final Vector getColumnNames() {
090: return getProperties("name"); //NOI18N
091: }
092:
093: public static final Vector getColumnTitles() {
094: return getProperties("columntitle"); //NOI18N
095: }
096:
097: public static final Vector getColumnClasses() {
098: return getProperties("columnclass"); //NOI18N
099: }
100:
101: static final long serialVersionUID = -6638535249384813829L;
102:
103: public ColumnItem() {
104: Vector vec = getProperties();
105: Enumeration evec = vec.elements();
106: propertySupport = new PropertyChangeSupport(this );
107: while (evec.hasMoreElements()) {
108: Map pmap = (Map) evec.nextElement();
109: Object pdv = pmap.get("default"); //NOI18N
110: if (pdv != null) {
111: String pclass = (String) pmap.get("columnclass"); //NOI18N
112: if (pclass.equals("java.lang.Boolean"))
113: pdv = Boolean.valueOf((String) pdv); //NOI18N
114: put(pmap.get("name"), pdv); //NOI18N
115: }
116: }
117: }
118:
119: /** Add property change listener
120: * Registers a listener for the PropertyChange event. The connection object
121: * should fire a PropertyChange event whenever somebody changes driver, database,
122: * login name or password.
123: */
124: public void addPropertyChangeListener(PropertyChangeListener l) {
125: propertySupport.addPropertyChangeListener(l);
126: }
127:
128: /** Remove property change listener
129: * Remove a listener for the PropertyChange event.
130: */
131: public void removePropertyChangeListener(PropertyChangeListener l) {
132: propertySupport.removePropertyChangeListener(l);
133: }
134:
135: public Object getProperty(String pname) {
136: return get(pname);
137: }
138:
139: public void setProperty(String pname, Object value) {
140: if (pname == null)
141: return;
142:
143: Object old = get(pname);
144: if (old != null) {
145: Class oldc = old.getClass();
146: if (old.equals(value))
147: return;
148:
149: try {
150: if (!oldc.equals(value.getClass()))
151: if (oldc.equals(Integer.class))
152: if ("".equals((String) value))
153: value = new Integer(0);
154: else
155: value = Integer.valueOf((String) value);
156: } catch (NumberFormatException e) {
157: //PENDING
158: }
159: }
160:
161: put(pname, value);
162: propertySupport.firePropertyChange(pname, old, value);
163: }
164:
165: public String getName() {
166: return (String) get(NAME);
167: }
168:
169: public TypeElement getType() {
170: return (TypeElement) get(TYPE);
171: }
172:
173: public int getSize() {
174: Object size = get(SIZE);
175:
176: if (size instanceof String) {
177: if ("".equals(size))
178: size = "0";
179: return Integer.valueOf((String) size).intValue();
180: }
181:
182: return ((Integer) size).intValue();
183: }
184:
185: public boolean isPrimaryKey() {
186: Boolean val = (Boolean) get(PRIMARY_KEY);
187: if (val != null)
188: return val.booleanValue();
189: return false;
190: }
191:
192: public boolean isUnique() {
193: Boolean val = (Boolean) get(UNIQUE);
194: if (val != null)
195: return val.booleanValue();
196: return false;
197: }
198:
199: public boolean isIndexed() {
200: Boolean val = (Boolean) get(INDEX);
201: if (val != null)
202: return val.booleanValue();
203: return false;
204: }
205:
206: public boolean allowsNull() {
207: Boolean val = (Boolean) get(NULLABLE);
208: if (val != null)
209: return val.booleanValue();
210: return false;
211: }
212:
213: public boolean hasCheckConstraint() {
214: Boolean val = (Boolean) get(CHECK);
215: if (val != null)
216: return val.booleanValue();
217: return false;
218: }
219:
220: public String getCheckConstraint() {
221: return (String) get(CHECK_CODE);
222: }
223:
224: public boolean hasDefaultValue() {
225: String dv = getDefaultValue();
226: if (dv != null && dv.length() > 0)
227: return true;
228: return false;
229: }
230:
231: public String getDefaultValue() {
232: return (String) get(DEFVAL);
233: }
234:
235: public boolean validate() {
236: String name = getName();
237: int size = getSize();
238: int scale = getScale();
239:
240: if (size < scale)
241: return false;
242: if (name == null || name.length() == 0)
243: return false;
244:
245: return true;
246: }
247:
248: /** Getter for property scale.
249: * @return Value of property scale.
250: */
251: public int getScale() {
252: return ((Integer) get(SCALE)).intValue();
253: }
254: }
|