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