KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > wizards > shortcut > SelectFolderPanel


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.apache.tools.ant.module.wizards.shortcut;
21
22 import java.awt.Component JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.DefaultListCellRenderer JavaDoc;
28 import javax.swing.DefaultListModel JavaDoc;
29 import javax.swing.JList JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.ListModel JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
36 import org.openide.WizardDescriptor;
37 import org.openide.loaders.DataFolder;
38 import org.openide.loaders.DataObject;
39 import org.openide.util.HelpCtx;
40
41 /**
42  * Wizard panel that lets you select a menu or toolbar folder and a display name
43  * for the menu or toolbar item.
44  */

45 final class SelectFolderPanel extends JPanel JavaDoc implements DocumentListener JavaDoc {
46
47     private final String JavaDoc prop;
48     private final boolean stripAmps;
49     private final SelectFolderWizardPanel wiz;
50     private final DataFolder top;
51     
52     /** Create the wizard panel component and set up some basic properties. */
53     public SelectFolderPanel(SelectFolderWizardPanel wiz, String JavaDoc name, String JavaDoc hint, String JavaDoc displayNameLabelText, DataFolder top, boolean stripAmps, String JavaDoc prop) {
54         this.wiz = wiz;
55         initComponents ();
56         // Provide a name in the title bar.
57
setName (name);
58         hintsArea.setText (hint);
59         initAccessibility (hint);
60         displayNameLabel.setText(displayNameLabelText);
61         this.prop = prop;
62         this.top = top;
63         this.stripAmps = stripAmps;
64         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
65         DataObject[] folders = findFolders(top);
66         for (int i = 0; i < folders.length; i++) {
67             model.addElement(folders[i]);
68         }
69         folderList.setModel(model);
70         folderList.setCellRenderer(new CellRenderer JavaDoc());
71         displayNameField.getDocument().addDocumentListener(this);
72     }
73     
74     ListModel JavaDoc getModel() {
75         return folderList.getModel();
76     }
77     
78     private String JavaDoc getDisplayName(DataFolder folder) {
79         String JavaDoc name = folder.getNodeDelegate().getDisplayName();
80         if (stripAmps) {
81             // XXX use o.o.a.Mnemonics instead
82
int idx = name.indexOf('&');
83             if (idx != -1) {
84                 name = name.substring(0, idx) + name.substring(idx + 1);
85             }
86         }
87         return name;
88     }
89     
90     String JavaDoc getNestedDisplayName(DataFolder folder) {
91         DataFolder f = folder;
92         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
93         while (f != top) {
94             if (b.length() > 0) {
95                 b.insert(0, " \u2192 "); // XXX I18N? just a right-arrow
96
}
97             b.insert(0, getDisplayName(f));
98             f = f.getFolder();
99         }
100         return b.toString();
101     }
102     
103     private DataFolder getFolder() {
104         return (DataFolder)folderList.getSelectedValue();
105     }
106     
107     private void setFolder(DataFolder f) {
108         folderList.setSelectedValue(f, true);
109     }
110     
111     private static DataFolder[] findFolders(DataFolder top) {
112         List JavaDoc<DataFolder> folders = new ArrayList JavaDoc<DataFolder>();
113         // Needs to be DFS, so children(true) is no good
114
visit(folders, top);
115         folders.remove(0);
116         return folders.toArray(new DataFolder[folders.size()]);
117     }
118     
119     private static void visit(List JavaDoc<DataFolder> folders, DataFolder f) {
120         folders.add(f);
121         DataObject[] kids = f.getChildren();
122         for (int i = 0; i < kids.length; i++) {
123             if (kids[i] instanceof DataFolder) {
124                 visit(folders, (DataFolder)kids[i]);
125             }
126         }
127     }
128     
129     private final class CellRenderer extends DefaultListCellRenderer JavaDoc {
130         
131         public CellRenderer() {}
132         
133         @Override JavaDoc
134         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
135             DataFolder f = (DataFolder)value;
136             String JavaDoc display = getNestedDisplayName(f);
137             return super.getListCellRendererComponent(list, display, index, isSelected, cellHasFocus);
138         }
139         
140     }
141     
142     // --- VISUAL DESIGN OF PANEL ---
143

144     @Override JavaDoc
145     public void requestFocus() {
146         super.requestFocus();
147         folderList.requestFocus();
148     }
149     
150     private void initAccessibility(String JavaDoc hint) {
151         this.getAccessibleContext().setAccessibleDescription(hint); }
152     
153     /** This method is called from within the constructor to
154      * initialize the form.
155      * WARNING: Do NOT modify this code. The content of this method is
156      * always regenerated by the Form Editor.
157      */

158     private void initComponents() {//GEN-BEGIN:initComponents
159
hintsArea = new javax.swing.JTextArea JavaDoc();
160         folderScrollPane = new javax.swing.JScrollPane JavaDoc();
161         folderList = new javax.swing.JList JavaDoc();
162         displayNamePanel = new javax.swing.JPanel JavaDoc();
163         displayNameLabel = new javax.swing.JLabel JavaDoc();
164         displayNameField = new javax.swing.JTextField JavaDoc();
165
166         setLayout(new java.awt.BorderLayout JavaDoc(0, 11));
167
168         hintsArea.setEditable(false);
169         hintsArea.setFont(javax.swing.UIManager.getFont ("Label.font"));
170         hintsArea.setText("<hints>");
171         hintsArea.setBackground(new java.awt.Color JavaDoc(204, 204, 204));
172         hintsArea.setLineWrap(true);
173         hintsArea.setForeground(new java.awt.Color JavaDoc(102, 102, 153));
174         hintsArea.setWrapStyleWord(true);
175         hintsArea.setDisabledTextColor(javax.swing.UIManager.getColor ("Label.foreground"));
176         hintsArea.setOpaque(false);
177         hintsArea.setEnabled(false);
178         add(hintsArea, java.awt.BorderLayout.NORTH);
179
180         folderList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
181         folderList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
182             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
183                 folderListValueChanged(evt);
184             }
185         });
186
187         folderScrollPane.setViewportView(folderList);
188         folderList.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SelectFolderPanel.class, "ACSN_folderList"));
189         folderList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SelectFolderPanel.class, "ACSD_folderList"));
190
191         add(folderScrollPane, java.awt.BorderLayout.CENTER);
192
193         displayNamePanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT));
194
195         displayNameLabel.setText("<set display name>");
196         displayNameLabel.setLabelFor(displayNameField);
197         displayNamePanel.add(displayNameLabel);
198
199         displayNameField.setColumns(30);
200         displayNamePanel.add(displayNameField);
201
202         add(displayNamePanel, java.awt.BorderLayout.SOUTH);
203
204     }//GEN-END:initComponents
205

206     private void folderListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_folderListValueChanged
207
wiz.fireChangeEvent();
208     }//GEN-LAST:event_folderListValueChanged
209

210     public void insertUpdate(DocumentEvent JavaDoc e) {
211         // From displayNameField.
212
wiz.fireChangeEvent();
213     }
214     
215     public void removeUpdate(DocumentEvent JavaDoc e) {
216         // From displayNameField.
217
wiz.fireChangeEvent();
218     }
219
220     public void changedUpdate(DocumentEvent JavaDoc e) {
221         // ignore
222
}
223     
224     // Variables declaration - do not modify//GEN-BEGIN:variables
225
private javax.swing.JTextField JavaDoc displayNameField;
226     private javax.swing.JLabel JavaDoc displayNameLabel;
227     private javax.swing.JPanel JavaDoc displayNamePanel;
228     private javax.swing.JList JavaDoc folderList;
229     private javax.swing.JScrollPane JavaDoc folderScrollPane;
230     private javax.swing.JTextArea JavaDoc hintsArea;
231     // End of variables declaration//GEN-END:variables
232

233     public static class SelectFolderWizardPanel implements WizardDescriptor.Panel {
234
235         private SelectFolderPanel panel;
236
237         private String JavaDoc namePanel;
238         private String JavaDoc hintPanel;
239         private String JavaDoc displayNameLabelText;
240         private DataFolder topPanel;
241         private boolean stripAmpsPanel;
242         private String JavaDoc propPanel;
243         
244         public SelectFolderWizardPanel(String JavaDoc name, String JavaDoc hint, String JavaDoc displayNameLabelText, DataFolder top, boolean stripAmps, String JavaDoc prop) {
245             this.namePanel = name;
246             this.hintPanel = hint;
247             this.displayNameLabelText = displayNameLabelText;
248             this.topPanel = top;
249             this.stripAmpsPanel = stripAmps;
250             this.propPanel = prop;
251         }
252         
253         public Component JavaDoc getComponent () {
254             return getPanel();
255         }
256         
257         SelectFolderPanel getPanel() {
258             if (panel == null) {
259                 panel = new SelectFolderPanel(this, namePanel, hintPanel, displayNameLabelText, topPanel, stripAmpsPanel, propPanel);
260             }
261             return panel;
262         }
263
264         public HelpCtx getHelp () {
265             return HelpCtx.DEFAULT_HELP;
266         }
267
268         public boolean isValid () {
269             return getPanel().getFolder() != null &&
270                 getPanel().displayNameField.getText().length() > 0;
271         }
272
273         private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
274         public final void addChangeListener (ChangeListener JavaDoc l) {
275             synchronized (listeners) {
276                 listeners.add (l);
277             }
278         }
279         public final void removeChangeListener (ChangeListener JavaDoc l) {
280             synchronized (listeners) {
281                 listeners.remove (l);
282             }
283         }
284         protected final void fireChangeEvent () {
285             ChangeListener JavaDoc[] ls;
286             synchronized (listeners) {
287                 ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
288             }
289             ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc (this);
290             for (ChangeListener JavaDoc l : ls) {
291                 l.stateChanged(ev);
292             }
293         }
294
295         public void readSettings (Object JavaDoc settings) {
296             WizardDescriptor wiz = (WizardDescriptor) settings;
297             getPanel().setFolder((DataFolder)wiz.getProperty(getPanel().prop));
298             String JavaDoc dn = (String JavaDoc)wiz.getProperty(ShortcutWizard.PROP_DISPLAY_NAME);
299             getPanel().displayNameField.setText(dn != null ? dn : ""); // NOI18N
300
}
301         
302         public void storeSettings (Object JavaDoc settings) {
303             DataFolder folder = getPanel().getFolder();
304             WizardDescriptor wiz = (WizardDescriptor) settings;
305             wiz.putProperty(getPanel().prop, folder);
306             wiz.putProperty(ShortcutWizard.PROP_DISPLAY_NAME, getPanel().displayNameField.getText());
307         }
308
309     }
310 }
311
Popular Tags