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.apache.tools.ant.module.wizards.shortcut;
043:
044: import java.awt.Component;
045: import java.util.ArrayList;
046: import java.util.List;
047: import javax.swing.DefaultListCellRenderer;
048: import javax.swing.DefaultListModel;
049: import javax.swing.JList;
050: import javax.swing.JPanel;
051: import javax.swing.ListModel;
052: import javax.swing.event.ChangeListener;
053: import javax.swing.event.DocumentEvent;
054: import javax.swing.event.DocumentListener;
055: import org.openide.WizardDescriptor;
056: import org.openide.awt.Mnemonics;
057: import org.openide.loaders.DataFolder;
058: import org.openide.loaders.DataObject;
059: import org.openide.util.ChangeSupport;
060: import org.openide.util.HelpCtx;
061:
062: /**
063: * Wizard panel that lets you select a menu or toolbar folder and a display name
064: * for the menu or toolbar item.
065: */
066: final class SelectFolderPanel extends JPanel implements
067: DocumentListener {
068:
069: private final String prop;
070: private final boolean stripAmps;
071: private final SelectFolderWizardPanel wiz;
072: private final DataFolder top;
073:
074: /** Create the wizard panel component and set up some basic properties. */
075: public SelectFolderPanel(SelectFolderWizardPanel wiz, String name,
076: String hint, String displayNameLabelText, DataFolder top,
077: boolean stripAmps, String prop) {
078: this .wiz = wiz;
079: initComponents();
080: // Provide a name in the title bar.
081: setName(name);
082: hintsArea.setText(hint);
083: initAccessibility(hint);
084: //displayNameLabel.setText(displayNameLabelText);
085: displayNameLabel.getAccessibleContext()
086: .setAccessibleDescription(displayNameLabelText);
087: Mnemonics.setLocalizedText(displayNameLabel,
088: displayNameLabelText);
089: displayNameField.getAccessibleContext().setAccessibleName(
090: displayNameLabelText);
091: displayNameField.getAccessibleContext()
092: .setAccessibleDescription(displayNameLabelText);
093:
094: this .prop = prop;
095: this .top = top;
096: this .stripAmps = stripAmps;
097: DefaultListModel model = new DefaultListModel();
098: DataObject[] folders = findFolders(top);
099: for (int i = 0; i < folders.length; i++) {
100: model.addElement(folders[i]);
101: }
102: folderList.setModel(model);
103: folderList.setCellRenderer(new CellRenderer());
104: displayNameField.getDocument().addDocumentListener(this );
105: }
106:
107: ListModel getModel() {
108: return folderList.getModel();
109: }
110:
111: private String getDisplayName(DataFolder folder) {
112: String name = folder.getNodeDelegate().getDisplayName();
113: if (stripAmps) {
114: // XXX use o.o.a.Mnemonics instead
115: int idx = name.indexOf('&');
116: if (idx != -1) {
117: name = name.substring(0, idx) + name.substring(idx + 1);
118: }
119: }
120: return name;
121: }
122:
123: String getNestedDisplayName(DataFolder folder) {
124: DataFolder f = folder;
125: StringBuffer b = new StringBuffer();
126: while (f != top) {
127: if (b.length() > 0) {
128: b.insert(0, " \u2192 "); // XXX I18N? just a right-arrow
129: }
130: b.insert(0, getDisplayName(f));
131: f = f.getFolder();
132: }
133: return b.toString();
134: }
135:
136: private DataFolder getFolder() {
137: return (DataFolder) folderList.getSelectedValue();
138: }
139:
140: private void setFolder(DataFolder f) {
141: folderList.setSelectedValue(f, true);
142: }
143:
144: private static DataFolder[] findFolders(DataFolder top) {
145: List<DataFolder> folders = new ArrayList<DataFolder>();
146: // Needs to be DFS, so children(true) is no good
147: visit(folders, top);
148: folders.remove(0);
149: return folders.toArray(new DataFolder[folders.size()]);
150: }
151:
152: private static void visit(List<DataFolder> folders, DataFolder f) {
153: folders.add(f);
154: DataObject[] kids = f.getChildren();
155: for (int i = 0; i < kids.length; i++) {
156: if (kids[i] instanceof DataFolder) {
157: visit(folders, (DataFolder) kids[i]);
158: }
159: }
160: }
161:
162: private final class CellRenderer extends DefaultListCellRenderer {
163:
164: public CellRenderer() {
165: }
166:
167: @Override
168: public Component getListCellRendererComponent(JList list,
169: Object value, int index, boolean isSelected,
170: boolean cellHasFocus) {
171: DataFolder f = (DataFolder) value;
172: String display = getNestedDisplayName(f);
173: return super .getListCellRendererComponent(list, display,
174: index, isSelected, cellHasFocus);
175: }
176:
177: }
178:
179: // --- VISUAL DESIGN OF PANEL ---
180:
181: @Override
182: public void requestFocus() {
183: super .requestFocus();
184: folderList.requestFocus();
185: }
186:
187: private void initAccessibility(String hint) {
188: this .getAccessibleContext().setAccessibleDescription(hint);
189: }
190:
191: /** This method is called from within the constructor to
192: * initialize the form.
193: * WARNING: Do NOT modify this code. The content of this method is
194: * always regenerated by the Form Editor.
195: */
196: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
197: private void initComponents() {
198:
199: hintsArea = new javax.swing.JTextArea();
200: folderScrollPane = new javax.swing.JScrollPane();
201: folderList = new javax.swing.JList();
202: displayNamePanel = new javax.swing.JPanel();
203: displayNameLabel = new javax.swing.JLabel();
204: displayNameField = new javax.swing.JTextField();
205:
206: setLayout(new java.awt.BorderLayout(0, 11));
207:
208: hintsArea.setBackground(new java.awt.Color(204, 204, 204));
209: hintsArea.setEditable(false);
210: hintsArea.setFont(javax.swing.UIManager.getFont("Label.font"));
211: hintsArea.setForeground(new java.awt.Color(102, 102, 153));
212: hintsArea.setLineWrap(true);
213: hintsArea.setText("<hints>");
214: hintsArea.setWrapStyleWord(true);
215: hintsArea.setDisabledTextColor(javax.swing.UIManager
216: .getColor("Label.foreground"));
217: hintsArea.setEnabled(false);
218: hintsArea.setOpaque(false);
219: add(hintsArea, java.awt.BorderLayout.NORTH);
220:
221: folderList
222: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
223: folderList
224: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
225: public void valueChanged(
226: javax.swing.event.ListSelectionEvent evt) {
227: folderListValueChanged(evt);
228: }
229: });
230: folderScrollPane.setViewportView(folderList);
231: folderList.getAccessibleContext().setAccessibleName(
232: org.openide.util.NbBundle.getMessage(
233: SelectFolderPanel.class, "ACSN_folderList")); // NOI18N
234: folderList.getAccessibleContext().setAccessibleDescription(
235: org.openide.util.NbBundle.getMessage(
236: SelectFolderPanel.class, "ACSD_folderList")); // NOI18N
237:
238: add(folderScrollPane, java.awt.BorderLayout.CENTER);
239:
240: displayNamePanel.setLayout(new java.awt.FlowLayout(
241: java.awt.FlowLayout.LEFT));
242:
243: displayNameLabel.setLabelFor(displayNameField);
244: displayNameLabel.setText("<set display name>");
245: displayNamePanel.add(displayNameLabel);
246:
247: displayNameField.setColumns(30);
248: displayNamePanel.add(displayNameField);
249:
250: add(displayNamePanel, java.awt.BorderLayout.SOUTH);
251: }// </editor-fold>//GEN-END:initComponents
252:
253: private void folderListValueChanged(
254: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_folderListValueChanged
255: wiz.fireChangeEvent();
256: }//GEN-LAST:event_folderListValueChanged
257:
258: public void insertUpdate(DocumentEvent e) {
259: // From displayNameField.
260: wiz.fireChangeEvent();
261: }
262:
263: public void removeUpdate(DocumentEvent e) {
264: // From displayNameField.
265: wiz.fireChangeEvent();
266: }
267:
268: public void changedUpdate(DocumentEvent e) {
269: // ignore
270: }
271:
272: // Variables declaration - do not modify//GEN-BEGIN:variables
273: private javax.swing.JTextField displayNameField;
274: private javax.swing.JLabel displayNameLabel;
275: private javax.swing.JPanel displayNamePanel;
276: private javax.swing.JList folderList;
277: private javax.swing.JScrollPane folderScrollPane;
278: private javax.swing.JTextArea hintsArea;
279:
280: // End of variables declaration//GEN-END:variables
281:
282: public static class SelectFolderWizardPanel implements
283: WizardDescriptor.Panel<ShortcutWizard> {
284:
285: private SelectFolderPanel panel;
286:
287: private String namePanel;
288: private String hintPanel;
289: private String displayNameLabelText;
290: private DataFolder topPanel;
291: private boolean stripAmpsPanel;
292: private String propPanel;
293:
294: public SelectFolderWizardPanel(String name, String hint,
295: String displayNameLabelText, DataFolder top,
296: boolean stripAmps, String prop) {
297: this .namePanel = name;
298: this .hintPanel = hint;
299: this .displayNameLabelText = displayNameLabelText;
300: this .topPanel = top;
301: this .stripAmpsPanel = stripAmps;
302: this .propPanel = prop;
303: }
304:
305: public Component getComponent() {
306: return getPanel();
307: }
308:
309: SelectFolderPanel getPanel() {
310: if (panel == null) {
311: panel = new SelectFolderPanel(this , namePanel,
312: hintPanel, displayNameLabelText, topPanel,
313: stripAmpsPanel, propPanel);
314: }
315: return panel;
316: }
317:
318: public HelpCtx getHelp() {
319: return HelpCtx.DEFAULT_HELP;
320: }
321:
322: public boolean isValid() {
323: return getPanel().getFolder() != null
324: && getPanel().displayNameField.getText().length() > 0;
325: }
326:
327: private final ChangeSupport cs = new ChangeSupport(this );
328:
329: public final void addChangeListener(ChangeListener l) {
330: cs.addChangeListener(l);
331: }
332:
333: public final void removeChangeListener(ChangeListener l) {
334: cs.removeChangeListener(l);
335: }
336:
337: protected final void fireChangeEvent() {
338: cs.fireChange();
339: }
340:
341: public void readSettings(ShortcutWizard wiz) {
342: getPanel().setFolder(
343: (DataFolder) wiz.getProperty(getPanel().prop));
344: String dn = (String) wiz
345: .getProperty(ShortcutWizard.PROP_DISPLAY_NAME);
346: getPanel().displayNameField.setText(dn != null ? dn : ""); // NOI18N
347: }
348:
349: public void storeSettings(ShortcutWizard wiz) {
350: DataFolder folder = getPanel().getFolder();
351: wiz.putProperty(getPanel().prop, folder);
352: wiz.putProperty(ShortcutWizard.PROP_DISPLAY_NAME,
353: getPanel().displayNameField.getText());
354: }
355:
356: }
357: }
|