KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > palette > CategorySelector


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.form.palette;
21
22 import javax.swing.*;
23
24 import org.openide.nodes.Node;
25 import org.openide.explorer.view.ListView;
26 import org.openide.explorer.*;
27 import org.openide.*;
28 import org.openide.nodes.AbstractNode;
29 import org.openide.nodes.Children;
30 import org.openide.nodes.FilterNode;
31
32 /**
33  * A simple panel allowing the user to choose one of the palette categories.
34  * Used by ChooseCategoryWizardPanel in the "Add to Palette" wizard for
35  * choosing the target category for added components.
36  */

37
38 class CategorySelector extends JPanel implements ExplorerManager.Provider {
39
40     private ExplorerManager explorerManager;
41
42     CategorySelector() {
43         explorerManager = new ExplorerManager();
44         explorerManager.setRootContext(getCategoryRootNode());
45     
46         ListView listView = new ListView();
47         // Issue 50703 - restore the default scroll pane's border
48
JScrollPane scrollPane = new JScrollPane();
49         listView.setBorder(scrollPane.getBorder());
50         listView.getAccessibleContext().setAccessibleDescription(
51             PaletteUtils.getBundleString("ACSD_CTL_PaletteCategories")); // NOI18N
52
listView.setPopupAllowed(false);
53         listView.setTraversalAllowed(false);
54     
55         JLabel categoryLabel = new JLabel();
56         org.openide.awt.Mnemonics.setLocalizedText(categoryLabel,
57                 PaletteUtils.getBundleString("CTL_PaletteCategories")); // NOI18N
58
categoryLabel.setLabelFor(listView);
59
60         getAccessibleContext().setAccessibleDescription(
61             PaletteUtils.getBundleString("ACSD_PaletteCategoriesSelector")); // NOI18N
62

63         setLayout(new java.awt.BorderLayout JavaDoc(0, 5));
64         add(categoryLabel, java.awt.BorderLayout.NORTH);
65         add(listView, java.awt.BorderLayout.CENTER);
66     }
67
68     private Node getCategoryRootNode() {
69     Node root = new AbstractNode(new Children.Array());
70     
71     Node[] paleteCategories = PaletteUtils.getCategoryNodes(PaletteUtils.getPaletteNode(), false);
72     Node[] categoryNodes = new Node[paleteCategories.length];
73     
74     for (int i = 0; i < paleteCategories.length; i++) {
75         categoryNodes[i] = new FilterNode(paleteCategories[i], Children.LEAF);
76     }
77     
78     root.getChildren().add(categoryNodes);
79     getExplorerManager().setRootContext(root);
80
81     return root;
82     }
83     
84     public static String JavaDoc selectCategory() {
85         CategorySelector selector = new CategorySelector();
86         selector.setBorder(new javax.swing.border.EmptyBorder JavaDoc(12, 12, 0, 11));
87         DialogDescriptor dd = new DialogDescriptor(
88             selector,
89             PaletteUtils.getBundleString("CTL_SelectCategory_Title"), // NOI18N
90
true,
91             null);
92         DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
93
94         return dd.getValue() == DialogDescriptor.OK_OPTION ?
95             selector.getSelectedCategory() : null;
96     }
97
98     String JavaDoc getSelectedCategory() {
99         Node[] selected = explorerManager.getSelectedNodes();
100         return selected.length == 1 ? selected[0].getName() : null;
101     }
102
103     // ExplorerManager.Provider
104
public ExplorerManager getExplorerManager() {
105         return explorerManager;
106     }
107
108     public java.awt.Dimension JavaDoc getPreferredSize() {
109         return new java.awt.Dimension JavaDoc(400, 300);
110     }
111 }
112
Popular Tags