KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > FileSelector


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.core.windows.services;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.beans.BeanInfo JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyVetoException JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JComboBox JavaDoc;
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.JList JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.ListCellRenderer JavaDoc;
39 import javax.swing.UIManager JavaDoc;
40 import javax.swing.border.Border JavaDoc;
41 import javax.swing.border.EmptyBorder JavaDoc;
42 import javax.swing.border.LineBorder JavaDoc;
43 import org.openide.awt.Mnemonics;
44 import org.openide.explorer.ExplorerManager;
45 import org.openide.explorer.view.BeanTreeView;
46 import org.openide.nodes.Node;
47 import org.openide.nodes.NodeAcceptor;
48 import org.openide.util.NbBundle;
49
50 /** File Selector
51  *
52  * @author Ales Novak, Jaroslav Tulach, Ian Formanek, Petr Hamernik, Jan Jancura
53  */

54 final class FileSelector extends JPanel JavaDoc implements PropertyChangeListener JavaDoc, ExplorerManager.Provider {
55     //XXX AFAIK nothing in NetBeans uses NodeOperation.select(). Probably this class can be deleted and NodeOperation.select deprecated. - Tim
56

57     /** generated Serialized Version UID */
58     static final long serialVersionUID = 6524404012203099065L;
59     /** manages tree */
60     private final ExplorerManager manager = new ExplorerManager();
61     /** tree */
62     private BeanTreeView tree;
63
64     /** The OK Button */
65     private JButton JavaDoc okButton;
66     /** The Cancel Button */
67     private JButton JavaDoc cancelButton;
68     private JButton JavaDoc[] buttons;
69
70     /** aceptor */
71     private NodeAcceptor acceptor;
72
73     /** reference to Frame that keeps our selected nodes synchronized with nodes actions */
74     // static TopFrameHack hack;
75

76     /**
77      * @param title is a title of the dialog
78      * @param rootLabel label for the root node
79      * @param root the base object to start browsing from
80      * @param acceptor decides whether we have valid selection or not
81      * @param top is a <code>Component</code> we just place on the top of the dialog
82      * it can be <code>null</code>
83      */

84     public FileSelector ( String JavaDoc rootLabel, Node root, final NodeAcceptor acceptor, Component JavaDoc top) {
85         super ();
86
87         this.acceptor = acceptor;
88         
89         ResourceBundle JavaDoc bundle = NbBundle.getBundle(FileSelector.class);
90
91
92         okButton = new JButton JavaDoc(bundle.getString("CTL_FileSelectorOkButton"));
93         cancelButton = new JButton JavaDoc(bundle.getString("CTL_FileSelectorCancelButton"));
94         okButton.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACSD_FileSelectorOkButton"));
95         cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACSD_FileSelectorCancelButton"));
96         buttons = new JButton JavaDoc[] { okButton, cancelButton };
97         
98
99         manager.setRootContext (root);//s[0]);
100

101         // Center
102
tree = new BeanTreeView ();
103         tree.setPopupAllowed (false);
104         tree.setDefaultActionAllowed (false);
105         // install proper border for tree
106
tree.setBorder((Border JavaDoc)UIManager.get("Nb.ScrollPane.border")); // NOI18N
107
tree.getAccessibleContext().setAccessibleName(NbBundle.getBundle(FileSelector.class).getString("ACSN_FileSelectorTreeView"));
108         tree.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FileSelector.class).getString("ACSD_FileSelectorTreeView"));
109         this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(FileSelector.class).getString("ACSD_FileSelectorDialog"));
110         setLayout(new BorderLayout JavaDoc());
111         add(tree, BorderLayout.CENTER);
112
113         // component to place at the top
114
try {
115             Node[] roots;
116             if (
117                 root instanceof org.netbeans.core.DataSystem &&
118                 (roots = root.getChildren ().getNodes ()).length > 0
119             ) {
120                 final JComboBox JavaDoc combo = new JComboBox JavaDoc(roots);
121                 combo.setRenderer(new FileSelectRenderer());
122                 combo.addActionListener(new ActionListener JavaDoc() {
123                     public void actionPerformed(ActionEvent JavaDoc evt) {
124                         Node o = (Node) combo.getSelectedItem();
125                         manager.setRootContext(o);
126                     }
127                 });
128                 combo.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACSD_FileSelectorComboBox"));
129                 manager.setSelectedNodes (new Node[] { roots[0] });
130
131                 // North - "Create In" // NOI18N
132
JPanel JavaDoc comboPanel = new JPanel JavaDoc();
133                 // Space between label and drop-down list
134
comboPanel.setLayout(new BorderLayout JavaDoc(5, 0));
135
136                 JLabel JavaDoc label = new JLabel JavaDoc();
137                 Mnemonics.setLocalizedText(label, rootLabel);
138                 label.setLabelFor(combo);
139                 comboPanel.add(label, BorderLayout.WEST);
140                 comboPanel.add(combo, BorderLayout.CENTER);
141                 add(comboPanel, BorderLayout.NORTH);
142             } else {
143                 manager.setSelectedNodes (new Node[] { root });
144                 JLabel JavaDoc label = new JLabel JavaDoc(rootLabel.replace('&', ' '));
145                 label.setLabelFor(tree);
146                 add(label, BorderLayout.NORTH);
147             }
148         } catch(PropertyVetoException JavaDoc pve) {
149             throw new IllegalStateException JavaDoc(pve.getMessage());
150         }
151
152
153
154         // South
155
if (top != null) {
156             add(top, BorderLayout.SOUTH);
157         }
158
159         manager.addPropertyChangeListener (this);
160
161 // if (top != null) top.requestFocus ();
162

163         if (acceptor.acceptNodes (manager.getSelectedNodes())) {
164             enableButton ();
165         } else {
166             disableButton ();
167         }
168
169     }
170     
171     Object JavaDoc[] getOptions() {
172         return buttons;
173     }
174     
175     Object JavaDoc getSelectOption() {
176         return okButton;
177     }
178     
179     
180
181     /** Changing properties. Implements <code>PropertyChangeListener</code>. */
182     public void propertyChange (PropertyChangeEvent JavaDoc ev) {
183         if (ev.getPropertyName().equals (ExplorerManager.PROP_SELECTED_NODES)) {
184             if (acceptor.acceptNodes (manager.getSelectedNodes())) {
185                 enableButton ();
186             } else {
187                 disableButton ();
188             }
189         }
190     }
191
192
193     /** Gets preferred size. Overrides superclass method. Height is adjusted
194      * to 1/2 screen. */

195     public Dimension JavaDoc getPreferredSize() {
196         Dimension JavaDoc dim = super.getPreferredSize();
197         dim.height = Math.max(dim.height, org.openide.util.Utilities.getUsableScreenBounds().height / 2);
198         return dim;
199     }
200
201     /**
202     * @return selected nodes
203     */

204     public Node[] getNodes() {
205         return manager.getSelectedNodes();
206     }
207
208     /** enables ok button */
209     void enableButton () {
210         okButton.setEnabled(true);
211     }
212
213     /** disables ok button */
214     void disableButton () {
215         okButton.setEnabled(false);
216     }
217
218     public ExplorerManager getExplorerManager() {
219         return manager;
220     }
221     
222
223     /** Renderer used in list box of exit dialog */
224     private static class FileSelectRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc {
225         /** Generated Serialized Version UID. */
226         static final long serialVersionUID = -7071698027341621636L;
227
228         protected static Border JavaDoc hasFocusBorder;
229         protected static Border JavaDoc noFocusBorder;
230
231         public FileSelectRenderer() {
232             setOpaque(true);
233             setBorder(noFocusBorder);
234             hasFocusBorder = new LineBorder JavaDoc(UIManager.getColor("List.focusCellHighlight")); // NOI18N
235
noFocusBorder = new EmptyBorder JavaDoc(1, 1, 1, 1);
236         }
237
238         /** Implements <code>ListCellRenderer</code>. */
239         public Component JavaDoc getListCellRendererComponent(
240             JList JavaDoc list,
241             Object JavaDoc value, // value to display
242
int index, // cell index
243
boolean isSelected, // is the cell selected
244
boolean cellHasFocus) // the list and the cell have the focus
245
{
246             if (!(value instanceof Node)) return this;
247
248             Node node = (Node)value;
249
250             ImageIcon JavaDoc icon = new ImageIcon JavaDoc(node.getIcon(BeanInfo.ICON_COLOR_16x16));
251             setIcon(icon);
252
253             setText(node.getDisplayName());
254             if (isSelected){
255                 super.setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
256
super.setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
257
}
258             else {
259                 super.setBackground(list.getBackground());
260                 super.setForeground(list.getForeground());
261             }
262
263             setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
264
265             return this;
266         }
267     } // End of class FileSelectRenderer.
268
}
269
Popular Tags