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.form.palette;
043:
044: import javax.swing.*;
045:
046: import org.openide.nodes.Node;
047: import org.openide.explorer.view.ListView;
048: import org.openide.explorer.*;
049: import org.openide.*;
050: import org.openide.nodes.AbstractNode;
051: import org.openide.nodes.Children;
052: import org.openide.nodes.FilterNode;
053:
054: /**
055: * A simple panel allowing the user to choose one of the palette categories.
056: * Used by ChooseCategoryWizardPanel in the "Add to Palette" wizard for
057: * choosing the target category for added components.
058: */
059:
060: class CategorySelector extends JPanel implements
061: ExplorerManager.Provider {
062:
063: private ExplorerManager explorerManager;
064:
065: CategorySelector() {
066: explorerManager = new ExplorerManager();
067: explorerManager.setRootContext(getCategoryRootNode());
068:
069: ListView listView = new ListView();
070: // Issue 50703 - restore the default scroll pane's border
071: JScrollPane scrollPane = new JScrollPane();
072: listView.setBorder(scrollPane.getBorder());
073: listView.getAccessibleContext().setAccessibleDescription(
074: PaletteUtils
075: .getBundleString("ACSD_CTL_PaletteCategories")); // NOI18N
076: listView.setPopupAllowed(false);
077: listView.setTraversalAllowed(false);
078:
079: JLabel categoryLabel = new JLabel();
080: org.openide.awt.Mnemonics.setLocalizedText(categoryLabel,
081: PaletteUtils.getBundleString("CTL_PaletteCategories")); // NOI18N
082: if ((listView.getViewport() != null)
083: && (listView.getViewport().getView() != null)) {
084: categoryLabel.setLabelFor(listView.getViewport().getView());
085: } else {
086: categoryLabel.setLabelFor(listView);
087: }
088:
089: getAccessibleContext()
090: .setAccessibleDescription(
091: PaletteUtils
092: .getBundleString("ACSD_PaletteCategoriesSelector")); // NOI18N
093:
094: setLayout(new java.awt.BorderLayout(0, 5));
095: add(categoryLabel, java.awt.BorderLayout.NORTH);
096: add(listView, java.awt.BorderLayout.CENTER);
097: }
098:
099: private Node getCategoryRootNode() {
100: Node root = new AbstractNode(new Children.Array());
101:
102: Node[] paleteCategories = PaletteUtils.getCategoryNodes(
103: PaletteUtils.getPaletteNode(), false);
104: Node[] categoryNodes = new Node[paleteCategories.length];
105:
106: for (int i = 0; i < paleteCategories.length; i++) {
107: categoryNodes[i] = new FilterNode(paleteCategories[i],
108: Children.LEAF);
109: }
110:
111: root.getChildren().add(categoryNodes);
112: getExplorerManager().setRootContext(root);
113:
114: return root;
115: }
116:
117: public static String selectCategory() {
118: CategorySelector selector = new CategorySelector();
119: selector.setBorder(new javax.swing.border.EmptyBorder(12, 12,
120: 0, 11));
121: DialogDescriptor dd = new DialogDescriptor(selector,
122: PaletteUtils
123: .getBundleString("CTL_SelectCategory_Title"), // NOI18N
124: true, null);
125: DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
126:
127: return dd.getValue() == DialogDescriptor.OK_OPTION ? selector
128: .getSelectedCategory() : null;
129: }
130:
131: String getSelectedCategory() {
132: Node[] selected = explorerManager.getSelectedNodes();
133: return selected.length == 1 ? selected[0].getName() : null;
134: }
135:
136: // ExplorerManager.Provider
137: public ExplorerManager getExplorerManager() {
138: return explorerManager;
139: }
140:
141: @Override
142: public java.awt.Dimension getPreferredSize() {
143: return new java.awt.Dimension(400, 300);
144: }
145: }
|