1 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 ; 29 import java.beans.PropertyChangeListener ; 30 31 public class ColumnItem extends Hashtable { 32 public static final String NAME = "name"; public static final String TYPE = "type"; public static final String SIZE = "size"; public static final String SCALE = "scale"; public static final String PRIMARY_KEY = "pkey"; public static final String INDEX = "idx"; public static final String NULLABLE = "nullable"; public static final String COMMENT = "comment"; public static final String DEFVAL = "defval"; public static final String UNIQUE = "unique"; public static final String CHECK = "check"; public static final String CHECK_CODE = "checkcode"; 45 private PropertyChangeSupport 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"); } 56 57 public static final Vector getProperties(String 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"); } 73 74 public static final Vector getColumnTitles() 75 { 76 return getProperties("columntitle"); } 78 79 public static final Vector getColumnClasses() 80 { 81 return getProperties("columnclass"); } 83 84 static final long serialVersionUID =-6638535249384813829L; 85 public ColumnItem() 86 { 87 Vector vec = getProperties(); 88 Enumeration evec = vec.elements(); 89 propertySupport = new PropertyChangeSupport (this); 90 while (evec.hasMoreElements()) { 91 Map pmap = (Map)evec.nextElement(); 92 Object pdv = pmap.get("default"); if (pdv != null) { 94 String pclass = (String )pmap.get("columnclass"); if (pclass.equals("java.lang.Boolean")) pdv = Boolean.valueOf((String )pdv); put(pmap.get("name"), pdv); } 98 } 99 } 100 101 106 public void addPropertyChangeListener (PropertyChangeListener l) { 107 propertySupport.addPropertyChangeListener (l); 108 } 109 110 113 public void removePropertyChangeListener (PropertyChangeListener l) { 114 propertySupport.removePropertyChangeListener (l); 115 } 116 117 public Object getProperty(String pname) 118 { 119 return get(pname); 120 } 121 122 public void setProperty(String pname, Object value) { 123 if (pname == null) 124 return; 125 126 Object old = get(pname); 127 if (old != null) { 128 Class oldc = old.getClass(); 129 if (old.equals(value)) 130 return; 131 132 try { 133 if (!oldc.equals(value.getClass())) 134 if (oldc.equals(Integer .class)) 135 if ("".equals((String ) value)) 136 value = new Integer (0); 137 else 138 value = Integer.valueOf((String ) value); 139 } catch (NumberFormatException e) { 140 } 142 } 143 144 put(pname, value); 145 propertySupport.firePropertyChange(pname, old, value); 146 } 147 148 public String getName() 149 { 150 return (String )get(NAME); 151 } 152 153 public TypeElement getType() { 154 return (TypeElement) get(TYPE); 155 } 156 157 public int getSize() { 158 Object size = get(SIZE); 159 160 if (size instanceof String ) { 161 if ("".equals(size)) 162 size = "0"; 163 return Integer.valueOf((String ) size).intValue(); 164 } 165 166 return ((Integer ) size).intValue(); 167 } 168 169 public boolean isPrimaryKey() 170 { 171 Boolean val = (Boolean )get(PRIMARY_KEY); 172 if (val != null) return val.booleanValue(); 173 return false; 174 } 175 176 public boolean isUnique() 177 { 178 Boolean val = (Boolean )get(UNIQUE); 179 if (val != null) return val.booleanValue(); 180 return false; 181 } 182 183 public boolean isIndexed() 184 { 185 Boolean val = (Boolean )get(INDEX); 186 if (val != null) return val.booleanValue(); 187 return false; 188 } 189 190 public boolean allowsNull() 191 { 192 Boolean val = (Boolean )get(NULLABLE); 193 if (val != null) return val.booleanValue(); 194 return false; 195 } 196 197 public boolean hasCheckConstraint() 198 { 199 Boolean val = (Boolean )get(CHECK); 200 if (val != null) return val.booleanValue(); 201 return false; 202 } 203 204 public String getCheckConstraint() 205 { 206 return (String )get(CHECK_CODE); 207 } 208 209 public boolean hasDefaultValue() 210 { 211 String dv = getDefaultValue(); 212 if (dv != null && dv.length()>0) return true; 213 return false; 214 } 215 216 public String getDefaultValue() 217 { 218 return (String )get(DEFVAL); 219 } 220 221 public boolean validate() 222 { 223 String 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 237 public int getScale() { 238 return ((Integer )get(SCALE)).intValue(); 239 } 240 } 241 | Popular Tags |