KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.palette;
22
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import org.netbeans.spi.palette.PaletteActions;
28 import org.netbeans.spi.palette.PaletteFilter;
29
30 import org.openide.*;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.*;
33 import org.openide.nodes.*;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.datatransfer.NewType;
36 import org.openide.util.datatransfer.PasteType;
37 import org.openide.util.Lookup;
38 import org.openide.util.lookup.AbstractLookup;
39 import org.openide.util.lookup.InstanceContent;
40 import org.openide.util.lookup.ProxyLookup;
41
42 /**
43  * The root node representing the Component Palette content.
44  *
45  * @author S. Aubrecht
46  */

47 public final class RootNode extends FilterNode {
48     
49     static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0];
50
51     private Action JavaDoc[] actions;
52     
53
54     // --------
55

56     public RootNode( Node originalRoot, Lookup lkp ) {
57         this( originalRoot, new InstanceContent(), lkp );
58     }
59
60     private RootNode( Node originalRoot, InstanceContent content, Lookup lkp ) {
61         super( originalRoot,
62                 new Children( originalRoot, lkp ),
63                 new ProxyLookup( new Lookup[] { lkp, new AbstractLookup( content ), originalRoot.getLookup() } ) );
64         DataFolder df = (DataFolder)getOriginal().getCookie( DataFolder.class );
65         if( null != df ) {
66             content.add( new DataFolder.Index( df, this ) );
67         }
68         content.add( this );
69         setDisplayName(Utils.getBundleString("CTL_Component_palette")); // NOI18N
70
}
71     
72     // --------
73

74     public NewType[] getNewTypes() {
75         NewType[] res = super.getNewTypes();
76         if( null == res || res.length == 0 )
77             res = new NewType[] { new NewCategory() };
78         return res;
79     }
80
81     public Action JavaDoc[] getActions(boolean context) {
82         if (actions == null) {
83             actions = new Action JavaDoc[] {
84                 new Utils.NewCategoryAction( this ),
85                 null,
86                 new Utils.SortCategoriesAction( this ),
87                 null,
88                 new Utils.RefreshPaletteAction()
89             };
90         }
91         PaletteActions customActions = (PaletteActions)getLookup().lookup( PaletteActions.class );
92         if( null != customActions ) {
93             return Utils.mergeActions( actions, customActions.getCustomPaletteActions() );
94         }
95         return actions;
96     }
97
98     public Node.PropertySet[] getPropertySets() {
99         return NO_PROPERTIES;
100     }
101
102     public PasteType getDropType(Transferable JavaDoc t, int action, int index) {
103         //no drop is accepted in palette's root node
104
return null;
105     }
106
107
108     public void refreshChildren() {
109         ((Children)getChildren()).refreshNodes();
110     }
111
112     // ------------
113

114     void createNewCategory() throws java.io.IOException JavaDoc {
115         java.util.ResourceBundle JavaDoc bundle = Utils.getBundle();
116         NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
117             bundle.getString("CTL_NewCategoryName"), // NOI18N
118
bundle.getString("CTL_NewCategoryTitle")); // NOI18N
119
input.setInputText(bundle.getString("CTL_NewCategoryValue")); // NOI18N
120

121         while (DialogDisplayer.getDefault().notify(input)
122                                               == NotifyDescriptor.OK_OPTION)
123         {
124             String JavaDoc categoryName = input.getInputText();
125             if( CategoryNode.checkCategoryName( this, categoryName, null ) ) {
126                 DataFolder paletteFolder = (DataFolder)getCookie( DataFolder.class );
127                 FileObject parentFolder = paletteFolder.getPrimaryFile();
128                 String JavaDoc folderName = CategoryNode.convertCategoryToFolderName( parentFolder, categoryName, null );
129                 FileObject folder = parentFolder.createFolder(folderName);
130                 if (!folderName.equals(categoryName))
131                     folder.setAttribute( CategoryNode.CAT_NAME, categoryName );
132                 break;
133             }
134         }
135     }
136
137     public boolean canCut() {
138         return false;
139     }
140
141     public boolean canDestroy() {
142         return false;
143     }
144
145     public HelpCtx getHelpCtx() {
146         return Utils.getHelpCtx( this, super.getHelpCtx() );
147     }
148
149     // --------------
150

151     /** Children for the PaletteNode. Creates PaletteCategoryNode instances
152      * as filter subnodes. */

153     private static class Children extends FilterNode.Children {
154
155         private PaletteFilter filter;
156         private Lookup lkp;
157         
158         public Children(Node original, Lookup lkp) {
159             super(original);
160             this.lkp = lkp;
161             filter = (PaletteFilter)lkp.lookup( PaletteFilter.class );
162         }
163
164         protected Node copyNode(Node node) {
165             return new CategoryNode( node, lkp );
166         }
167         
168         protected Node[] createNodes(Node key) {
169             if( null == filter || filter.isValidCategory( key.getLookup() ) ) {
170                 return new Node[] { copyNode(key) };
171             }
172
173             return null;
174         }
175         
176         public void refreshNodes() {
177             Node[] nodes = original.getChildren().getNodes();
178             List JavaDoc<Node> empty = Collections.emptyList();
179             setKeys( empty );
180             setKeys( nodes );
181         }
182     }
183
184     // -------
185

186
187     // -------
188
/**
189      * New type for creation of new palette category.
190      */

191     final class NewCategory extends NewType {
192
193         public String JavaDoc getName() {
194             return Utils.getBundleString("CTL_NewCategory"); // NOI18N
195
}
196
197         public HelpCtx getHelpCtx() {
198             return new HelpCtx(NewCategory.class);
199         }
200
201         public void create() throws java.io.IOException JavaDoc {
202             RootNode.this.createNewCategory();
203         }
204     }
205 }
206
Popular Tags