001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.palette;
043:
044: import java.awt.datatransfer.Transferable;
045: import java.util.Collections;
046: import java.util.List;
047: import javax.swing.Action;
048: import org.netbeans.spi.palette.PaletteActions;
049: import org.netbeans.spi.palette.PaletteFilter;
050:
051: import org.openide.*;
052: import org.openide.filesystems.FileObject;
053: import org.openide.loaders.*;
054: import org.openide.nodes.*;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.datatransfer.NewType;
057: import org.openide.util.datatransfer.PasteType;
058: import org.openide.util.Lookup;
059: import org.openide.util.lookup.AbstractLookup;
060: import org.openide.util.lookup.InstanceContent;
061: import org.openide.util.lookup.ProxyLookup;
062:
063: /**
064: * The root node representing the Component Palette content.
065: *
066: * @author S. Aubrecht
067: */
068: public final class RootNode extends FilterNode {
069:
070: static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0];
071:
072: private Action[] actions;
073:
074: // --------
075:
076: public RootNode(Node originalRoot, Lookup lkp) {
077: this (originalRoot, new InstanceContent(), lkp);
078: }
079:
080: private RootNode(Node originalRoot, InstanceContent content,
081: Lookup lkp) {
082: super (originalRoot, new Children(originalRoot, lkp),
083: new ProxyLookup(new Lookup[] { lkp,
084: new AbstractLookup(content),
085: originalRoot.getLookup() }));
086: DataFolder df = (DataFolder) getOriginal().getCookie(
087: DataFolder.class);
088: if (null != df) {
089: content.add(new DataFolder.Index(df, this ));
090: }
091: content.add(this );
092: setDisplayName(Utils.getBundleString("CTL_Component_palette")); // NOI18N
093: }
094:
095: // --------
096:
097: public NewType[] getNewTypes() {
098: NewType[] res = super .getNewTypes();
099: if (null == res || res.length == 0)
100: res = new NewType[] { new NewCategory() };
101: return res;
102: }
103:
104: public Action[] getActions(boolean context) {
105: if (actions == null) {
106: actions = new Action[] { new Utils.NewCategoryAction(this ),
107: null, new Utils.SortCategoriesAction(this ), null,
108: new Utils.RefreshPaletteAction() };
109: }
110: PaletteActions customActions = (PaletteActions) getLookup()
111: .lookup(PaletteActions.class);
112: if (null != customActions) {
113: return Utils.mergeActions(actions, customActions
114: .getCustomPaletteActions());
115: }
116: return actions;
117: }
118:
119: public Node.PropertySet[] getPropertySets() {
120: return NO_PROPERTIES;
121: }
122:
123: public PasteType getDropType(Transferable t, int action, int index) {
124: //no drop is accepted in palette's root node
125: return null;
126: }
127:
128: public void refreshChildren() {
129: ((Children) getChildren()).refreshNodes();
130: }
131:
132: // ------------
133:
134: void createNewCategory() throws java.io.IOException {
135: java.util.ResourceBundle bundle = Utils.getBundle();
136: NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
137: bundle.getString("CTL_NewCategoryName"), // NOI18N
138: bundle.getString("CTL_NewCategoryTitle")); // NOI18N
139: input.setInputText(bundle.getString("CTL_NewCategoryValue")); // NOI18N
140:
141: while (DialogDisplayer.getDefault().notify(input) == NotifyDescriptor.OK_OPTION) {
142: String categoryName = input.getInputText();
143: if (CategoryNode
144: .checkCategoryName(this , categoryName, null)) {
145: DataFolder paletteFolder = (DataFolder) getCookie(DataFolder.class);
146: FileObject parentFolder = paletteFolder
147: .getPrimaryFile();
148: String folderName = CategoryNode
149: .convertCategoryToFolderName(parentFolder,
150: categoryName, null);
151: FileObject folder = parentFolder
152: .createFolder(folderName);
153: if (!folderName.equals(categoryName))
154: folder.setAttribute(CategoryNode.CAT_NAME,
155: categoryName);
156: break;
157: }
158: }
159: }
160:
161: public boolean canCut() {
162: return false;
163: }
164:
165: public boolean canDestroy() {
166: return false;
167: }
168:
169: public HelpCtx getHelpCtx() {
170: return Utils.getHelpCtx(this , super .getHelpCtx());
171: }
172:
173: // --------------
174:
175: /** Children for the PaletteNode. Creates PaletteCategoryNode instances
176: * as filter subnodes. */
177: private static class Children extends FilterNode.Children {
178:
179: private PaletteFilter filter;
180: private Lookup lkp;
181:
182: public Children(Node original, Lookup lkp) {
183: super (original);
184: this .lkp = lkp;
185: filter = (PaletteFilter) lkp.lookup(PaletteFilter.class);
186: }
187:
188: protected Node copyNode(Node node) {
189: return new CategoryNode(node, lkp);
190: }
191:
192: protected Node[] createNodes(Node key) {
193: if (null == filter
194: || filter.isValidCategory(key.getLookup())) {
195: return new Node[] { copyNode(key) };
196: }
197:
198: return null;
199: }
200:
201: public void refreshNodes() {
202: Node[] nodes = original.getChildren().getNodes();
203: List<Node> empty = Collections.emptyList();
204: setKeys(empty);
205: setKeys(nodes);
206: }
207: }
208:
209: // -------
210:
211: // -------
212: /**
213: * New type for creation of new palette category.
214: */
215: final class NewCategory extends NewType {
216:
217: public String getName() {
218: return Utils.getBundleString("CTL_NewCategory"); // NOI18N
219: }
220:
221: public HelpCtx getHelpCtx() {
222: return new HelpCtx(NewCategory.class);
223: }
224:
225: public void create() throws java.io.IOException {
226: RootNode.this.createNewCategory();
227: }
228: }
229: }
|