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.jmeter.module.wizards;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.util.ArrayList;
048: import java.util.Arrays;
049: import java.util.Collection;
050: import java.util.Collections;
051: import javax.swing.JButton;
052: import javax.swing.JScrollPane;
053: import org.netbeans.api.project.SourceGroup;
054: import org.openide.util.NbBundle;
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.explorer.ExplorerManager;
058: import org.openide.explorer.view.BeanTreeView;
059: import org.openide.filesystems.FileObject;
060: import org.openide.loaders.DataObject;
061: import org.openide.loaders.DataObjectNotFoundException;
062: import org.openide.loaders.DataFolder;
063: import org.openide.nodes.Children;
064: import org.openide.nodes.Node;
065: import org.openide.nodes.AbstractNode;
066: import org.openide.nodes.FilterNode;
067: import org.openide.nodes.NodeNotFoundException;
068: import org.openide.nodes.NodeOp;
069: import org.openide.util.NbCollections;
070:
071: // XXX I18N
072:
073: /**
074: *
075: * @author phrebejk, mkuchtiak
076: */
077: public class BrowseFolders extends javax.swing.JPanel implements
078: ExplorerManager.Provider {
079:
080: private ExplorerManager manager;
081: private SourceGroup[] folders;
082: private Class target;
083: BeanTreeView btv;
084:
085: private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane();
086:
087: /** Creates new form BrowseFolders */
088: public BrowseFolders(SourceGroup[] folders, Class target,
089: String preselectedFileName) {
090: initComponents();
091: getAccessibleContext()
092: .setAccessibleDescription(
093: NbBundle
094: .getMessage(
095: BrowseFolders.class,
096: (target == DataFolder.class ? "ACSD_BrowseFolders"
097: : "ACSD_BrowseFiles")));
098:
099: this .folders = folders;
100: this .target = target;
101: manager = new ExplorerManager();
102: AbstractNode rootNode = new AbstractNode(
103: new SourceGroupsChildren(folders));
104: manager.setRootContext(rootNode);
105:
106: // Create the templates view
107: btv = new BeanTreeView();
108: btv.setRootVisible(false);
109: btv
110: .setSelectionMode(javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION);
111: btv.setBorder(SAMPLE_SCROLL_PANE.getBorder());
112: expandSelection(preselectedFileName);
113: folderPanel.add(btv, java.awt.BorderLayout.CENTER);
114: }
115:
116: // ExplorerManager.Provider implementation ---------------------------------
117:
118: public ExplorerManager getExplorerManager() {
119: return manager;
120: }
121:
122: private void expandSelection(String preselectedFileName) {
123:
124: Node root = manager.getRootContext();
125: Children ch = root.getChildren();
126: if (ch == Children.LEAF) {
127: return;
128: }
129: Node nodes[] = ch.getNodes(true);
130:
131: Node sel = null;
132:
133: if (preselectedFileName != null
134: && preselectedFileName.length() > 0) {
135: // Try to find the node
136: for (int i = 0; i < nodes.length; i++) {
137: try {
138: sel = NodeOp.findPath(nodes[i], NbCollections
139: .checkedEnumerationByFilter(
140: new java.util.StringTokenizer(
141: preselectedFileName, "/"),
142: String.class, false));
143: break;
144: } catch (NodeNotFoundException e) {
145: // Will select the first node
146: }
147: }
148: }
149:
150: if (sel == null) {
151: // Node not found => expand first level
152: btv.expandNode(root);
153: for (int i = 0; i < nodes.length; i++) {
154: btv.expandNode(nodes[i]);
155: if (i == 0) {
156: sel = nodes[i];
157: }
158: }
159: }
160:
161: if (sel != null) {
162: // Select the node
163: try {
164: manager.setSelectedNodes(new Node[] { sel });
165: } catch (java.beans.PropertyVetoException e) {
166: // No selection for some reason
167: }
168: }
169:
170: }
171:
172: /** This method is called from within the constructor to
173: * initialize the form.
174: * WARNING: Do NOT modify this code. The content of this method is
175: * always regenerated by the Form Editor.
176: */
177: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
178: private void initComponents() {
179: java.awt.GridBagConstraints gridBagConstraints;
180:
181: jLabel1 = new javax.swing.JLabel();
182: folderPanel = new javax.swing.JPanel();
183:
184: setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12,
185: 12, 12));
186: setLayout(new java.awt.GridBagLayout());
187:
188: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
189: org.openide.util.NbBundle.getMessage(
190: BrowseFolders.class, "LBL_Folders")); // NOI18N
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
193: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
194: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
195: add(jLabel1, gridBagConstraints);
196:
197: folderPanel.setLayout(new java.awt.BorderLayout());
198: gridBagConstraints = new java.awt.GridBagConstraints();
199: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
200: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
201: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
202: gridBagConstraints.weightx = 1.0;
203: gridBagConstraints.weighty = 1.0;
204: add(folderPanel, gridBagConstraints);
205: }// </editor-fold>//GEN-END:initComponents
206:
207: // Variables declaration - do not modify//GEN-BEGIN:variables
208: private javax.swing.JPanel folderPanel;
209: private javax.swing.JLabel jLabel1;
210:
211: // End of variables declaration//GEN-END:variables
212:
213: public static FileObject showDialog(SourceGroup[] folders,
214: Class target, String preselectedFileName) {
215:
216: BrowseFolders bf = new BrowseFolders(folders, target,
217: preselectedFileName);
218:
219: JButton selectButton = new JButton(NbBundle.getMessage(
220: BrowseFolders.class,
221: (target == DataFolder.class ? "LBL_SelectFolder"
222: : "LBL_SelectFile")));
223: selectButton
224: .getAccessibleContext()
225: .setAccessibleDescription(
226: NbBundle
227: .getMessage(
228: BrowseFolders.class,
229: (target == DataFolder.class ? "ACSD_SelectFolder"
230: : "ACSD_SelectFile")));
231: JButton cancelButton = new JButton(NbBundle.getMessage(
232: BrowseFolders.class, "LBL_Cancel"));
233: cancelButton.getAccessibleContext()
234: .setAccessibleDescription(
235: NbBundle.getMessage(BrowseFolders.class,
236: "ACSD_Cancel"));
237: JButton options[] = new JButton[] {
238: //new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Select_Option") ), // NOI18N
239: //new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Cancel_Option") ), // NOI18N
240: selectButton, cancelButton, };
241:
242: OptionsListener optionsListener = new OptionsListener(bf,
243: target);
244:
245: options[0].setActionCommand(OptionsListener.COMMAND_SELECT);
246: options[0].addActionListener(optionsListener);
247: options[1].setActionCommand(OptionsListener.COMMAND_CANCEL);
248: options[1].addActionListener(optionsListener);
249:
250: DialogDescriptor dialogDescriptor = new DialogDescriptor(
251: bf, // innerPane
252: NbBundle
253: .getMessage(
254: BrowseFolders.class,
255: (target == DataFolder.class ? "LBL_BrowseFolders"
256: : "LBL_BrowseFiles")), // displayName
257: true, // modal
258: options, // options
259: options[0], // initial value
260: DialogDescriptor.BOTTOM_ALIGN, // options align
261: null, // helpCtx
262: null); // listener
263:
264: dialogDescriptor.setClosingOptions(new Object[] { options[0],
265: options[1] });
266:
267: Dialog dialog = DialogDisplayer.getDefault().createDialog(
268: dialogDescriptor);
269: dialog.setVisible(true);
270:
271: return optionsListener.getResult();
272:
273: }
274:
275: // Innerclasses ------------------------------------------------------------
276:
277: /** Children to be used to show FileObjects from given SourceGroups
278: */
279:
280: private final class SourceGroupsChildren extends Children.Keys {
281:
282: private SourceGroup[] groups;
283: private SourceGroup group;
284: private FileObject fo;
285:
286: public SourceGroupsChildren(SourceGroup[] groups) {
287: this .groups = groups;
288: }
289:
290: public SourceGroupsChildren(FileObject fo, SourceGroup group) {
291: this .fo = fo;
292: this .group = group;
293: }
294:
295: protected void addNotify() {
296: super .addNotify();
297: setKeys(getKeys());
298: }
299:
300: protected void removeNotify() {
301: setKeys(Collections.EMPTY_SET);
302: super .removeNotify();
303: }
304:
305: protected Node[] createNodes(Object key) {
306:
307: FileObject fObj = null;
308: SourceGroup group = null;
309: boolean isFile = false;
310:
311: if (key instanceof SourceGroup) {
312: fObj = ((SourceGroup) key).getRootFolder();
313: group = (SourceGroup) key;
314: } else if (key instanceof Key) {
315: fObj = ((Key) key).folder;
316: group = ((Key) key).group;
317: if (!fObj.isFolder())
318: isFile = true;
319: }
320:
321: try {
322: DataObject dobj = DataObject.find(fObj);
323: FilterNode fn = (isFile ? new FilterNode(dobj
324: .getNodeDelegate(), Children.LEAF)
325: : new FilterNode(dobj.getNodeDelegate(),
326: new SourceGroupsChildren(fObj, group)));
327:
328: if (key instanceof SourceGroup) {
329: fn.setDisplayName(group.getDisplayName());
330: }
331:
332: return new Node[] { fn };
333: } catch (DataObjectNotFoundException e) {
334: return null;
335: }
336: }
337:
338: private Collection getKeys() {
339:
340: if (groups != null) {
341: return Arrays.asList(groups);
342: } else {
343: FileObject files[] = fo.getChildren();
344: Arrays.sort(files,
345: new BrowseFolders.FileObjectComparator());
346: ArrayList children = new ArrayList(files.length);
347:
348: if (BrowseFolders.this .target == org.openide.loaders.DataFolder.class)
349: for (int i = 0; i < files.length; i++) {
350: if (files[i].isFolder()
351: && group.contains(files[i])) {
352: children.add(new Key(files[i], group));
353: }
354: }
355: else {
356: // add folders
357: for (int i = 0; i < files.length; i++) {
358: if (group.contains(files[i])
359: && files[i].isFolder())
360: children.add(new Key(files[i], group));
361: }
362: // add files
363: for (int i = 0; i < files.length; i++) {
364: if (group.contains(files[i])
365: && !files[i].isFolder())
366: children.add(new Key(files[i], group));
367: }
368: }
369:
370: return children;
371: }
372:
373: }
374:
375: private class Key {
376:
377: private FileObject folder;
378: private SourceGroup group;
379:
380: private Key(FileObject folder, SourceGroup group) {
381: this .folder = folder;
382: this .group = group;
383: }
384:
385: }
386:
387: }
388:
389: private class FileObjectComparator implements java.util.Comparator {
390: public int compare(Object o1, Object o2) {
391: FileObject fo1 = (FileObject) o1;
392: FileObject fo2 = (FileObject) o2;
393: return fo1.getName().compareTo(fo2.getName());
394: }
395: }
396:
397: private static final class OptionsListener implements
398: ActionListener {
399:
400: public static final String COMMAND_SELECT = "SELECT"; //NOI18N
401: public static final String COMMAND_CANCEL = "CANCEL"; //NOI18N
402:
403: private BrowseFolders browsePanel;
404:
405: private FileObject result;
406: private Class target;
407:
408: public OptionsListener(BrowseFolders browsePanel, Class target) {
409: this .browsePanel = browsePanel;
410: this .target = target;
411: }
412:
413: public void actionPerformed(ActionEvent e) {
414: String command = e.getActionCommand();
415:
416: if (COMMAND_SELECT.equals(command)) {
417: Node selection[] = browsePanel.getExplorerManager()
418: .getSelectedNodes();
419:
420: if (selection != null && selection.length > 0) {
421: DataObject dobj = (DataObject) selection[0]
422: .getLookup().lookup(DataObject.class);
423: if (dobj != null
424: && dobj.getClass().isAssignableFrom(target)) {
425: result = dobj.getPrimaryFile();
426: }
427: /*
428: if ( dobj != null ) {
429: FileObject fo = dobj.getPrimaryFile();
430: if ( fo.isFolder() ) {
431: result = fo;
432: }
433: }
434: */
435: }
436:
437: }
438: }
439:
440: public FileObject getResult() {
441: return result;
442: }
443: }
444:
445: }
|