1 19 20 package org.netbeans.modules.dbschema.nodes; 21 22 import java.awt.datatransfer.Transferable ; 23 import java.awt.datatransfer.DataFlavor ; 24 import java.beans.*; 25 import java.io.IOException ; 26 import java.lang.reflect.InvocationTargetException ; 27 28 import org.openide.nodes.*; 29 import org.openide.util.HelpCtx; 30 import org.openide.util.NbBundle; 31 import org.openide.util.WeakListeners; 32 import org.openide.util.actions.SystemAction; 33 import org.openide.util.datatransfer.ExTransferable; 34 35 import org.netbeans.modules.dbschema.*; 36 37 50 public abstract class DBElementNode extends AbstractNode implements IconStrings, DBElementProperties, Node.Cookie { 51 52 53 private static final String [] ICON_AFFECTING_PROPERTIES = new String []{}; 54 55 56 protected DBElement element; 57 58 59 protected boolean writeable; 60 61 62 private transient PropertyChangeListener listener; 63 64 72 public DBElementNode(DBElement element, Children children, boolean writeable) { 73 super(children); 74 this.element = element; 75 this.writeable = writeable; 76 setIconBase(resolveIconBase()); 77 setName(element.getName().getName()); 78 setDisplayName(element.getName().getName()); 79 listener = new ElementListener(); 80 element.addPropertyChangeListener(WeakListeners.propertyChange(listener, element)); 81 } 82 83 91 abstract protected String resolveIconBase(); 92 93 97 protected String [] getIconAffectingProperties() { 98 return ICON_AFFECTING_PROPERTIES; 99 } 100 101 public HelpCtx getHelpCtx () { 102 return new HelpCtx("dbschema_ctxhelp_wizard"); } 104 105 110 public boolean canRename () { 111 return writeable; 112 } 113 114 119 public boolean canDestroy () { 120 return writeable; 121 } 122 123 129 public Transferable clipboardCopy () throws IOException { 130 ExTransferable ex = ExTransferable.create(super.clipboardCopy()); 131 132 ex.put(new ElementStringTransferable()); 133 134 return ex; 135 } 136 137 143 public Transferable clipboardCut () throws IOException { 144 if (!writeable) 145 throw new IOException (); 146 147 ExTransferable ex = ExTransferable.create(super.clipboardCut()); 148 ex.put(new ElementStringTransferable()); 149 150 return ex; 151 } 152 153 154 class ElementStringTransferable extends ExTransferable.Single { 155 156 ElementStringTransferable() { super(DataFlavor.stringFlavor); } 157 158 159 protected Object getData() { return element.toString(); } 160 } 161 162 166 public boolean canCopy() { 167 return true; 168 } 169 170 175 public boolean canCut() { 176 return writeable; 177 } 178 179 182 void superFireCookieChange() { 183 fireCookieChange(); 184 } 185 186 199 public Node.Cookie getCookie (Class type) { 200 Node.Cookie c = super.getCookie(type); 201 202 if (c == null && (type.isAssignableFrom(DBElementProvider.class) || 203 type.isAssignableFrom(DBElement.class))) 204 { 205 c = new DBElementProvider(element); 206 } 207 208 return c; 209 } 210 211 214 public boolean equals (Object o) { 215 return ((o instanceof DBElementNode) && (element.equals(((DBElementNode)o).element))); 216 } 217 218 221 public int hashCode() { 222 return element.hashCode(); 223 } 224 225 229 protected Node.Property createNameProperty (boolean canW) { 230 return new ElementProp(Node.PROP_NAME, String .class,canW) { 231 232 public Object getValue () { 233 return ((DBElement) element).getName().getFullName(); 234 } 235 }; 236 } 237 238 void superSetName(String name) { 239 super.setName(name); 240 } 241 242 244 248 private class ElementListener implements PropertyChangeListener { 249 public ElementListener () {} 250 251 253 public void propertyChange (PropertyChangeEvent evt) { 254 String propName = evt.getPropertyName(); 255 256 if (propName == null) { 257 setIconBase(resolveIconBase()); 258 } else { 259 String [] iconProps = getIconAffectingProperties(); 261 for (int i = 0; i < iconProps.length; i++) 262 if (iconProps[i].equals(propName)) { 263 setIconBase(resolveIconBase()); 264 break; 265 } 266 267 if (propName.equals(Node.PROP_NAME)) { 268 try { 273 superSetName(((DBMemberElement)DBElementNode.this.element).getName().toString()); 274 } catch (ClassCastException e) { 275 } 277 } else { 278 if (propName.equals(Node.PROP_COOKIE)) { 279 superFireCookieChange(); 281 return; 282 } 283 } 284 } 285 DBElementNode.this.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue()); 286 } 287 } 288 289 291 293 static abstract class ElementProp extends PropertySupport { 294 301 public ElementProp (String name, Class type, boolean canW) { 302 super(name, type, NbBundle.getMessage(DBElementNode.class, "PROP_" + name), NbBundle.getMessage(DBElementNode.class, "HINT_" + name), true, canW); } 304 305 312 public void setValue (Object val) throws IllegalArgumentException , IllegalAccessException , InvocationTargetException { 313 if (!canWrite()) 314 throw new IllegalAccessException (NbBundle.getMessage(DBElementNode.class, "MSG_Cannot_Write")); } 316 317 } 318 } 319 | Popular Tags |