1 19 20 package org.netbeans.api.nodes2looks; 21 22 import java.awt.Component ; 23 import java.awt.datatransfer.Transferable ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import org.netbeans.spi.looks.Look; 32 import org.openide.nodes.Node; 33 import org.openide.nodes.NodeEvent; 34 import org.openide.nodes.NodeListener; 35 import org.openide.nodes.NodeMemberEvent; 36 import org.openide.nodes.NodeReorderEvent; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.NbBundle; 39 import org.openide.util.Lookup; 40 import org.openide.util.datatransfer.NewType; 41 import org.openide.util.datatransfer.PasteType; 42 43 61 final class NodeProxyLook extends Look { 62 63 private static final Lookup.Template TEMPLATE_ALL = 64 new Lookup.Template( Object .class ); 65 66 private NodeEventTranslator eventTranslator = new NodeEventTranslator(); 67 68 69 public NodeProxyLook( String name ) { 70 super( name ); 71 } 73 74 76 83 protected Node getNodeDelegate( Object representedObject ) { 84 85 if ( representedObject instanceof Node ) { 86 return (Node)representedObject; 87 } 88 else { 89 return null; 90 } 91 } 92 93 95 public void attachTo(Object representedObject) { 96 97 if ( representedObject instanceof Node ) { 98 Node dn = (Node)representedObject; 99 dn.addNodeListener( eventTranslator ); 101 dn.addPropertyChangeListener( eventTranslator.propertyChangeListener ); 102 } 103 else { 104 throw new IllegalArgumentException ( "Represented object has to be a node" ); 105 } 106 } 107 108 109 public void detachFrom( Object representedObject ) { 110 if ( representedObject instanceof Node ) { 111 Node dn = (Node)representedObject; 112 dn.removeNodeListener( eventTranslator ); 114 dn.removePropertyChangeListener( eventTranslator.propertyChangeListener ); 115 } 116 } 117 118 120 public Collection getLookupItems(Object representedObject, Lookup oldEnv ) { 121 if ( representedObject instanceof Node ) { 122 Node dn = (Node)representedObject; 123 Lookup lookup = dn.getLookup(); 124 Lookup.Result result = lookup.lookup( TEMPLATE_ALL ); 125 return result.allItems(); 126 } 127 else { 128 throw new IllegalArgumentException ( "Represented object has to be a node: " + representedObject ); 129 } 130 } 131 132 134 public String getDisplayName( Object representedObject, Lookup env ) { 135 Node dn = getNodeDelegate( representedObject ); 136 if (dn == null) { 137 return null; 138 } else { 139 return dn.getDisplayName(); 140 } 141 } 142 143 public String getName( Object representedObject, Lookup env ) { 144 Node dn = getNodeDelegate( representedObject ); 145 if (dn == null) { 146 return null; 147 } else { 148 return dn.getName(); 149 } 150 } 151 152 public void rename( Object representedObject, String newName, Lookup env ) { 153 Node dn = getNodeDelegate( representedObject ); 154 if (dn != null) { 155 dn.setName( newName ); 156 } 157 } 158 159 public String getShortDescription ( Object representedObject, Lookup env ) { 160 Node dn = getNodeDelegate( representedObject ); 161 if (dn == null) { 162 return null; 163 } else { 164 return dn.getShortDescription(); 165 } 166 } 167 168 public java.awt.Image getIcon( Object representedObject, int type, Lookup env ) { 169 Node dn = getNodeDelegate( representedObject ); 170 if (dn == null) { 171 return null; 172 } else { 173 return dn.getIcon( type ); 174 } 175 } 176 177 public java.awt.Image getOpenedIcon( Object representedObject, int type, Lookup env ) { 178 Node dn = getNodeDelegate( representedObject ); 179 if (dn == null) { 180 return null; 181 } else { 182 return dn.getOpenedIcon( type ); 183 } 184 } 185 186 public HelpCtx getHelpCtx( Object representedObject, Lookup env ) { 187 Node dn = getNodeDelegate( representedObject ); 188 if (dn == null) { 189 return null; 190 } else { 191 return dn.getHelpCtx(); 192 } 193 } 194 195 197 public List getChildObjects( Object representedObject, Lookup env ) { 198 Node dn = getNodeDelegate( representedObject ); 199 if (dn == null) { 200 return null; 201 } else { 202 return Arrays.asList( dn.getChildren().getNodes() ); 203 } 204 } 205 206 public boolean isLeaf( Object representedObject, Lookup env ) { 207 Node dn = getNodeDelegate( representedObject ); 208 if (dn == null) { 209 return true; 210 } else { 211 return dn.isLeaf(); 212 } 213 } 214 215 217 public NewType[] getNewTypes( Object representedObject, Lookup env ) { 218 Node dn = getNodeDelegate( representedObject ); 219 if ( dn == null ) { 220 return null; 221 } 222 else { 223 return dn.getNewTypes(); 224 } 225 } 226 227 public Action [] getActions(Object representedObject, Lookup env ) { 228 Node dn = getNodeDelegate( representedObject ); 229 if ( dn == null ) { 230 return null; 231 } 232 else { 233 return dn.getActions(false); 234 } 235 } 236 237 public Action [] getContextActions(Object representedObject, Lookup env ) { 238 Node dn = getNodeDelegate( representedObject ); 239 if ( dn == null ) { 240 return null; 241 } 242 else { 243 return dn.getActions(true); 244 } 245 } 246 247 public Action getDefaultAction(Object representedObject, Lookup env ) { 248 Node dn = getNodeDelegate( representedObject ); 249 if ( dn == null ) { 250 return null; 251 } 252 else { 253 return dn.getDefaultAction(); 254 } 255 } 256 257 259 public Node.PropertySet[] getPropertySets( Object representedObject, Lookup env ) { 260 Node dn = getNodeDelegate( representedObject ); 261 if ( dn == null ) { 262 return null; 263 } 264 else { 265 return dn.getPropertySets(); 266 } 267 } 268 269 public Component getCustomizer(Object representedObject, Lookup env ) { 270 Node dn = getNodeDelegate( representedObject ); 271 if ( dn == null || !dn.hasCustomizer() ) { 272 return null; 273 } 274 else { 275 return dn.getCustomizer(); 276 } 277 } 278 279 public boolean hasCustomizer( Object representedObject, Lookup env ) { 280 Node dn = getNodeDelegate( representedObject ); 281 return dn != null && dn.hasCustomizer (); 282 } 283 284 286 public boolean canRename( Object representedObject, Lookup env ) { 287 Node dn = getNodeDelegate( representedObject ); 288 if ( dn == null ) { 289 return false; 290 } 291 else { 292 return dn.canRename(); 293 } 294 } 295 296 public boolean canDestroy( Object representedObject, Lookup env ) { 297 Node dn = getNodeDelegate( representedObject ); 298 if ( dn == null ) { 299 return false; 300 } 301 else { 302 return dn.canDestroy(); 303 } 304 } 305 306 public boolean canCopy( Object representedObject, Lookup env ) { 307 Node dn = getNodeDelegate( representedObject ); 308 if ( dn == null ) { 309 return false; 310 } 311 else { 312 return dn.canCopy(); 313 } 314 } 315 316 public boolean canCut( Object representedObject, Lookup env ) { 317 Node dn = getNodeDelegate( representedObject ); 318 if ( dn == null ) { 319 return false; 320 } 321 else { 322 return dn.canCut(); 323 } 324 } 325 326 public PasteType[] getPasteTypes( Object representedObject, Transferable t, Lookup env ) { 327 Node dn = getNodeDelegate( representedObject ); 328 if ( dn == null ) { 329 return null; 330 } 331 else { 332 return dn.getPasteTypes( t ); 333 } 334 } 335 336 public PasteType getDropType( Object representedObject, Transferable t, int action, int index, Lookup env ) { 337 Node dn = getNodeDelegate( representedObject ); 338 if ( dn == null ) { 339 return null; 340 } 341 else { 342 return dn.getDropType( t, action, index ); 343 } 344 } 345 346 public Transferable clipboardCopy( Object representedObject, Lookup env ) throws IOException { 347 Node dn = getNodeDelegate( representedObject ); 348 if ( dn == null ) { 349 return null; 350 } 351 else { 352 return dn.clipboardCopy(); 353 } 354 } 355 356 public Transferable clipboardCut( Object representedObject, Lookup env ) throws IOException { 357 Node dn = getNodeDelegate( representedObject ); 358 if ( dn == null ) { 359 return null; 360 } 361 else { 362 return dn.clipboardCut(); 363 } 364 } 365 366 public Transferable drag( Object representedObject, Lookup env ) throws IOException { 367 Node dn = getNodeDelegate( representedObject ); 368 if ( dn == null ) { 369 return null; 370 } 371 else { 372 return dn.drag(); 373 } 374 } 375 376 public void destroy( Object representedObject, Lookup env ) throws IOException { 377 Node dn = getNodeDelegate( representedObject ); 378 if ( dn != null ) { 379 dn.destroy(); 380 } 381 } 382 383 386 public String getDisplayName() { 387 return NbBundle.getMessage (NodeProxyLook.class, "LAB_NodeProxyLook"); } 389 390 391 393 private class NodeEventTranslator implements NodeListener { 394 395 private NodePropertyChangeTranslator propertyChangeListener = new NodePropertyChangeTranslator(); 396 397 403 404 public void childrenAdded( NodeMemberEvent e ) { 405 fireChange( e.getSource(), Look.GET_CHILD_OBJECTS ); 406 } 407 408 public void childrenRemoved( NodeMemberEvent e ) { 409 fireChange( e.getSource(), Look.GET_CHILD_OBJECTS ); 410 } 411 412 public void childrenReordered( NodeReorderEvent e ) { 413 fireChange( e.getSource(), Look.GET_CHILD_OBJECTS ); 414 } 415 416 public void nodeDestroyed( NodeEvent e ) { 417 fireChange( e.getSource(), Look.DESTROY ); 418 } 419 420 public void propertyChange( PropertyChangeEvent e ) { 421 422 if ( Node.PROP_NAME.equals( e.getPropertyName() ) ) { 423 fireChange( e.getSource(), Look.GET_NAME ); 424 } 425 else if ( Node.PROP_DISPLAY_NAME.equals( e.getPropertyName() ) ) { 426 fireChange( e.getSource(), Look.GET_DISPLAY_NAME ); 427 } 428 else if ( Node.PROP_SHORT_DESCRIPTION.equals( e.getPropertyName() ) ) { 429 fireChange( e.getSource(), Look.GET_SHORT_DESCRIPTION ); 430 } 431 else if ( Node.PROP_ICON.equals( e.getPropertyName() ) ) { 432 fireChange( e.getSource(), Look.GET_ICON ); 433 } 434 else if ( Node.PROP_OPENED_ICON.equals( e.getPropertyName() ) ) { 435 fireChange( e.getSource(), Look.GET_OPENED_ICON ); 436 } 437 else if ( Node.PROP_PROPERTY_SETS.equals( e.getPropertyName() ) ) { 441 fireChange( e.getSource(), Look.GET_PROPERTY_SETS ); 442 } 443 else if ( Node.PROP_LEAF.equals( e.getPropertyName() ) ) { 444 fireChange( e.getSource(), GET_CHILD_OBJECTS ); 445 } 446 else if ( Node.PROP_COOKIE.equals( e.getPropertyName() ) ) { 447 fireChange( e.getSource(), GET_LOOKUP_ITEMS ); 448 } 449 } 450 451 private class NodePropertyChangeTranslator implements PropertyChangeListener { 452 453 454 public void propertyChange( PropertyChangeEvent e ) { 455 firePropertyChange( e.getSource(), e.getPropertyName() ); 456 } 457 } 458 } 459 460 } 461 | Popular Tags |