1 19 20 package org.netbeans.modules.dbschema.nodes; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeEvent ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.LinkedList ; 27 import org.openide.ErrorManager; 28 29 import org.openide.nodes.Children; 30 import org.openide.nodes.Node; 31 import org.openide.util.RequestProcessor; 32 import org.openide.util.WeakListeners; 33 34 import org.netbeans.modules.dbschema.*; 35 import org.netbeans.modules.dbschema.jdbcimpl.*; 36 37 53 public class SchemaRootChildren extends Children.Keys { 54 55 56 static final Object NOT_KEY = new Object (); 57 58 static final Object ERROR_KEY = new Object (); 59 60 61 protected SchemaElement element; 62 63 protected DBElementNodeFactory factory; 64 65 private PropertyChangeListener wPropL; 66 68 private DBElementListener propL; 69 70 private boolean nodesInited = false; 71 72 73 private boolean parseStatus = false; 74 private Object parseLock = new Object (); 75 76 private org.netbeans.modules.dbschema.jdbcimpl.DBschemaDataObject obj; 77 78 82 public SchemaRootChildren(final DBElementNodeFactory factory, org.netbeans.modules.dbschema.jdbcimpl.DBschemaDataObject obj) { 83 super(); 84 this.factory = factory; 85 this.obj = obj; 86 } 87 88 90 93 protected void addNotify () { 94 SchemaElement el = getElement(); 95 96 if (el != null) { 97 if (wPropL == null) { 99 propL = new DBElementListener(); 100 wPropL = WeakListeners.propertyChange(propL, el); 101 } 102 el.addPropertyChangeListener (wPropL); 103 } 104 105 refreshKeys (); 106 } 107 108 protected void removeNotify () { 109 setKeys (java.util.Collections.EMPTY_SET); 110 nodesInited = false; 111 } 112 113 116 protected Node[] createNodes (final Object key) { 117 if (key instanceof SchemaElement) 118 return new Node[] { factory.createSchemaNode((SchemaElement) key) }; 119 if (NOT_KEY.equals(key)) 120 return new Node[] { factory.createWaitNode() }; 121 122 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 124 new Exception ("DbSchema: Error node created for object " + key + 125 " (class " + ((key == null) ? "null" : key.getClass().getName()) + ")")); 126 return new Node[] { factory.createErrorNode() }; 127 } 128 129 130 132 135 public SchemaElement getElement () { 136 if (element == null && !parseStatus) { 137 refreshKeys2(); 138 139 RequestProcessor.getDefault().post(new Runnable () { 140 public void run () { 141 synchronized (parseLock) { 142 if (!parseStatus) { 143 nodesInited = true; 144 setElement(obj.getSchema()); 145 parseStatus = true; 146 } 147 } 148 } 149 }, 0); 150 } 151 152 return element; 153 } 154 155 158 public void setElement (final SchemaElement element) { 159 if (this.element != null) 160 this.element.removePropertyChangeListener(wPropL); 161 162 this.element = element; 163 if (this.element != null) { 164 if (wPropL == null) { 165 propL = new DBElementListener(); 166 wPropL = WeakListeners.propertyChange(propL, this.element); 167 } 168 else { 169 wPropL = WeakListeners.propertyChange(propL, this.element); 171 } 172 173 this.element.addPropertyChangeListener(wPropL); 174 } 175 176 if (nodesInited) 178 refreshKeys (); 179 } 180 181 183 192 public void refreshKeys () { 193 int status; 194 195 SchemaElement el = getElement(); 196 197 if (parseStatus) 198 status = (el == null) ? SchemaElement.STATUS_ERROR : el.getStatus(); 199 else 200 status = (el == null) ? SchemaElement.STATUS_NOT : el.getStatus(); 201 202 switch (status) { 203 case SchemaElement.STATUS_NOT: 204 setKeys(new Object [] { NOT_KEY }); 205 break; 206 case SchemaElement.STATUS_ERROR: 207 setKeys(new Object [] { ERROR_KEY }); 208 break; 209 case SchemaElement.STATUS_PARTIAL: 210 case SchemaElement.STATUS_OK: 211 refreshAllKeys(); 212 break; 213 } 214 } 215 216 private void refreshKeys2() { 217 setKeys(new Object [] {NOT_KEY}); 218 } 219 220 222 private void refreshAllKeys () { 223 int[] order = SchemaElementFilter.DEFAULT_ORDER; 224 225 final LinkedList keys = new LinkedList (); 226 for (int i = 0; i < order.length; i++) 228 addKeysOfType(keys, order[i]); 229 230 javax.swing.SwingUtilities.invokeLater(new Runnable () { 232 public void run() { 233 setKeys2(Collections.EMPTY_SET); 238 setKeys2(keys); 239 } 240 }); 241 } 242 243 246 private void addKeysOfType (Collection keys, final int elementType) { 247 SchemaElement schemaElement = (SchemaElement) getElement(); 248 if (elementType != 0) 249 keys.add (schemaElement); 250 } 251 252 private void setKeys2(Collection c) { 253 setKeys(c); 254 } 255 256 258 260 private final class DBElementListener implements PropertyChangeListener { 261 public DBElementListener () {} 262 263 public void propertyChange (PropertyChangeEvent evt) { 264 boolean refresh = DBElementProperties.PROP_SCHEMA.equals(evt.getPropertyName()); 265 if (!refresh && DBElementProperties.PROP_STATUS.equals(evt.getPropertyName())) { 266 Integer val = (Integer ) evt.getNewValue(); 267 refresh = ((val == null) || (val.intValue() != SchemaElement.STATUS_NOT)); 268 } 269 270 if (refresh) 271 javax.swing.SwingUtilities.invokeLater(new Runnable () { 272 public void run() { 273 refreshKeys(); 274 } 275 }); 276 } 277 } } 279 | Popular Tags |