KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > DefaultCategory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.palette;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.dnd.DropTargetDragEvent JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import javax.swing.Action JavaDoc;
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 /**
36  * PaletteCategory implementation based on Nodes.
37  *
38  * @author S. Aubrecht
39  */

40 public class DefaultCategory implements Category, NodeListener {
41     
42     private Node categoryNode;
43     private ArrayList JavaDoc<CategoryListener> categoryListeners = new ArrayList JavaDoc<CategoryListener>( 3 );
44     private Item[] items;
45     
46     /**
47      * Creates a new instance of DefaultPaletteCategory
48      *
49      * @param categoryNode Node representing the category.
50      */

51     public DefaultCategory( Node categoryNode ) {
52         this.categoryNode = categoryNode;
53         this.categoryNode.addNodeListener( this );
54         this.categoryNode.addPropertyChangeListener( new PropertyChangeListener JavaDoc() {
55             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
56                 notifyListeners();
57             }
58         });
59     }
60
61     public Image JavaDoc 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 JavaDoc[] getActions() {
78         return categoryNode.getActions( false );
79     }
80
81     public String JavaDoc 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 JavaDoc getName() {
97         return categoryNode.getName();
98     }
99
100     public String JavaDoc 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     /** Fired when a set of new children is added.
116     * @param ev event describing the action
117     */

118     public void childrenAdded(NodeMemberEvent ev) {
119         items = null;
120         notifyListeners();
121     }
122
123     /** Fired when a set of children is removed.
124     * @param ev event describing the action
125     */

126     public void childrenRemoved(NodeMemberEvent ev) {
127         items = null;
128         notifyListeners();
129     }
130
131     /** Fired when the order of children is changed.
132     * @param ev event describing the change
133     */

134     public void childrenReordered(NodeReorderEvent ev) {
135         items = null;
136         notifyListeners();
137     }
138
139     /** Fired when the node is deleted.
140     * @param ev event describing the node
141     */

142     public void nodeDestroyed(NodeEvent ev) {
143         categoryNode.removeNodeListener( this );
144     }
145
146     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
147         if( Node.PROP_DISPLAY_NAME.equals( evt.getPropertyName() ) ) {
148             notifyListeners();
149         }
150     }
151
152     public boolean equals(Object JavaDoc obj) {
153         if( !(obj instanceof DefaultCategory) )
154             return false;
155         
156         return categoryNode.equals( ((DefaultCategory) obj).categoryNode );
157     }
158
159     public Transferable JavaDoc getTransferable() {
160         try {
161             return categoryNode.drag();
162         } catch( IOException JavaDoc 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 JavaDoc e ) {
187         DragAndDropHandler handler = getDragAndDropHandler();
188         return handler.canDrop( getLookup(), e.getCurrentDataFlavors(), e.getDropAction() );
189     }
190
191     public boolean dropItem( Transferable JavaDoc 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 JavaDoc toString() {
207         return categoryNode.getDisplayName();
208     }
209 }
210
Popular Tags