1 19 20 package org.netbeans.modules.palette; 21 22 import java.awt.Image ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.dnd.DropTargetDragEvent ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import javax.swing.Action ; 30 import org.netbeans.spi.palette.DragAndDropHandler; 31 import org.openide.ErrorManager; 32 import org.openide.nodes.*; 33 import org.openide.util.Lookup; 34 35 40 public class DefaultCategory implements Category, NodeListener { 41 42 private Node categoryNode; 43 private ArrayList <CategoryListener> categoryListeners = new ArrayList <CategoryListener>( 3 ); 44 private Item[] items; 45 46 51 public DefaultCategory( Node categoryNode ) { 52 this.categoryNode = categoryNode; 53 this.categoryNode.addNodeListener( this ); 54 this.categoryNode.addPropertyChangeListener( new PropertyChangeListener () { 55 public void propertyChange(PropertyChangeEvent evt) { 56 notifyListeners(); 57 } 58 }); 59 } 60 61 public Image getIcon(int type) { 62 return categoryNode.getIcon( type ); 63 } 64 65 public void addCategoryListener( CategoryListener listener ) { 66 synchronized( categoryListeners ) { 67 categoryListeners.add( listener ); 68 } 69 } 70 71 public void removeCategoryListener( CategoryListener listener ) { 72 synchronized( categoryListeners ) { 73 categoryListeners.remove( listener ); 74 } 75 } 76 77 public Action [] getActions() { 78 return categoryNode.getActions( false ); 79 } 80 81 public String getShortDescription() { 82 return categoryNode.getShortDescription(); 83 } 84 85 public Item[] getItems() { 86 if( null == items ) { 87 Node[] children = categoryNode.getChildren().getNodes( DefaultModel.canBlock() ); 88 items = new Item[children.length]; 89 for( int i=0; i<children.length; i++ ) { 90 items[i] = new DefaultItem( children[i] ); 91 } 92 } 93 return items; 94 } 95 96 public String getName() { 97 return categoryNode.getName(); 98 } 99 100 public String getDisplayName() { 101 return categoryNode.getDisplayName(); 102 } 103 104 protected void notifyListeners() { 105 CategoryListener[] listeners; 106 synchronized( categoryListeners ) { 107 listeners = new CategoryListener[categoryListeners.size()]; 108 listeners = categoryListeners.toArray( listeners ); 109 } 110 for( int i=0; i<listeners.length; i++ ) { 111 listeners[i].categoryModified( this ); 112 } 113 } 114 115 118 public void childrenAdded(NodeMemberEvent ev) { 119 items = null; 120 notifyListeners(); 121 } 122 123 126 public void childrenRemoved(NodeMemberEvent ev) { 127 items = null; 128 notifyListeners(); 129 } 130 131 134 public void childrenReordered(NodeReorderEvent ev) { 135 items = null; 136 notifyListeners(); 137 } 138 139 142 public void nodeDestroyed(NodeEvent ev) { 143 categoryNode.removeNodeListener( this ); 144 } 145 146 public void propertyChange(PropertyChangeEvent evt) { 147 if( Node.PROP_DISPLAY_NAME.equals( evt.getPropertyName() ) ) { 148 notifyListeners(); 149 } 150 } 151 152 public boolean equals(Object obj) { 153 if( !(obj instanceof DefaultCategory) ) 154 return false; 155 156 return categoryNode.equals( ((DefaultCategory) obj).categoryNode ); 157 } 158 159 public Transferable getTransferable() { 160 try { 161 return categoryNode.drag(); 162 } catch( IOException ioE ) { 163 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ioE ); 164 } 165 return null; 166 } 167 168 public Lookup getLookup() { 169 return categoryNode.getLookup(); 170 } 171 172 private int itemToIndex( Item item ) { 173 if( null == item ) { 174 return -1; 175 } 176 Node node = (Node)item.getLookup().lookup( Node.class ); 177 if( null != node ) { 178 Index order = (Index)categoryNode.getCookie( Index.class ); 179 if( null != order ) { 180 return order.indexOf( node ); 181 } 182 } 183 return -1; 184 } 185 186 public boolean dragOver( DropTargetDragEvent e ) { 187 DragAndDropHandler handler = getDragAndDropHandler(); 188 return handler.canDrop( getLookup(), e.getCurrentDataFlavors(), e.getDropAction() ); 189 } 190 191 public boolean dropItem( Transferable dropItem, int dndAction, Item target, boolean dropBefore ) { 192 int targetIndex = itemToIndex( target ); 193 if( !dropBefore ) { 194 targetIndex++; 195 } 196 DragAndDropHandler handler = getDragAndDropHandler(); 197 boolean res = handler.doDrop( getLookup(), dropItem, dndAction, targetIndex ); 198 items = null; 199 return res; 200 } 201 202 private DragAndDropHandler getDragAndDropHandler() { 203 return (DragAndDropHandler)categoryNode.getLookup().lookup( DragAndDropHandler.class ); 204 } 205 206 public String toString() { 207 return categoryNode.getDisplayName(); 208 } 209 } 210 | Popular Tags |