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