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