KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > ui > BrowseFolders


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.multiview.ui;
21
22 import org.netbeans.api.project.SourceGroup;
23 import org.netbeans.modules.j2ee.ddloaders.multiview.Utils;
24 import org.openide.DialogDescriptor;
25 import org.openide.DialogDisplayer;
26 import org.openide.explorer.ExplorerManager;
27 import org.openide.explorer.view.BeanTreeView;
28 import org.openide.filesystems.FileObject;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.FilterNode;
34 import org.openide.nodes.Node;
35 import org.openide.util.NbBundle;
36
37 import javax.swing.*;
38 import javax.swing.event.TreeSelectionEvent JavaDoc;
39 import javax.swing.event.TreeSelectionListener JavaDoc;
40 import java.awt.*;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Arrays JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.Collections JavaDoc;
47
48 /**
49  * @author pfiala
50  */

51 public class BrowseFolders extends JPanel implements ExplorerManager.Provider {
52
53     private ExplorerManager manager;
54
55     private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane();
56     private FileObjectFilter filter;
57
58     /**
59      * Creates new form BrowseFolders
60      */

61     public BrowseFolders(SourceGroup[] folders, FileObjectFilter filter) {
62         initComponents();
63         this.filter = filter == null ? new FileObjectFilter() : filter;
64         manager = new ExplorerManager();
65         AbstractNode rootNode = new AbstractNode(new SourceGroupsChildren(folders));
66         manager.setRootContext(rootNode);
67         
68         // Create the templates view
69
BeanTreeView btv = new BeanTreeView();
70         btv.setRootVisible(false);
71         btv.setSelectionMode(javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION);
72         btv.setBorder(SAMPLE_SCROLL_PANE.getBorder());
73         folderPanel.add(btv, java.awt.BorderLayout.CENTER);
74     }
75         
76     // ExplorerManager.Provider implementation ---------------------------------
77

78     public ExplorerManager getExplorerManager() {
79         return manager;
80     }
81
82     /**
83      * This method is called from within the constructor to
84      * initialize the form.
85      * WARNING: Do NOT modify this code. The content of this method is
86      * always regenerated by the Form Editor.
87      */

88     private void initComponents() {//GEN-BEGIN:initComponents
89
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
90
91         jLabel1 = new javax.swing.JLabel JavaDoc();
92         folderPanel = new javax.swing.JPanel JavaDoc();
93
94         setLayout(new java.awt.GridBagLayout JavaDoc());
95
96         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 12, 12)));
97         jLabel1.setText(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders"));
98         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
100         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
101         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
102         add(jLabel1, gridBagConstraints);
103
104         folderPanel.setLayout(new java.awt.BorderLayout JavaDoc());
105
106         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
107         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
108         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
109         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
110         gridBagConstraints.weightx = 1.0;
111         gridBagConstraints.weighty = 1.0;
112         add(folderPanel, gridBagConstraints);
113
114     }//GEN-END:initComponents
115

116     // Variables declaration - do not modify//GEN-BEGIN:variables
117
private javax.swing.JPanel JavaDoc folderPanel;
118     private javax.swing.JLabel JavaDoc jLabel1;
119     // End of variables declaration//GEN-END:variables
120

121     public static FileObject showDialog(SourceGroup[] folders) {
122         return showDialog(folders, null);
123     }
124
125     public static FileObject showDialog(SourceGroup[] folders, FileObjectFilter filter) {
126
127         final BrowseFolders bf = new BrowseFolders(folders, filter);
128
129         final JButton options[] = new JButton[]{
130             new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_SelectFile")),
131             new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_Cancel")),
132         };
133
134         options[0].setEnabled(false);
135
136         JTree tree = Utils.findTreeComponent(bf);
137         tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener JavaDoc() {
138             public void valueChanged(TreeSelectionEvent JavaDoc e) {
139                 FileObject fileObject = bf.getSelectedFileObject();
140                 options[0].setEnabled(fileObject != null && !fileObject.isFolder());
141             }
142         });
143
144         OptionsListener optionsListener = new OptionsListener(bf);
145
146         options[0].setActionCommand(OptionsListener.COMMAND_SELECT);
147         options[0].addActionListener(optionsListener);
148         options[1].setActionCommand(OptionsListener.COMMAND_CANCEL);
149         options[1].addActionListener(optionsListener);
150
151         DialogDescriptor dialogDescriptor = new DialogDescriptor(bf, // innerPane
152
NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
153
true, // modal
154
options, // options
155
options[0], // initial value
156
DialogDescriptor.BOTTOM_ALIGN, // options align
157
null, // helpCtx
158
null); // listener
159

160         dialogDescriptor.setClosingOptions(new Object JavaDoc[]{options[0], options[1]});
161
162         Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
163         dialog.setVisible(true);
164
165         return optionsListener.getResult();
166
167     }
168
169
170     private FileObject getSelectedFileObject() {
171         Node selection[] = getExplorerManager().getSelectedNodes();
172         if (selection != null && selection.length > 0) {
173             DataObject dobj = (DataObject) selection[0].getLookup().lookup(DataObject.class);
174             return dobj.getPrimaryFile();
175         }
176         return null;
177     }
178
179     // Innerclasses ------------------------------------------------------------
180

181     /**
182      * Children to be used to show FileObjects from given SourceGroups
183      */

184
185     private final class SourceGroupsChildren extends Children.Keys {
186
187         private SourceGroup[] groups;
188         private SourceGroup group;
189         private FileObject fo;
190
191         public SourceGroupsChildren(SourceGroup[] groups) {
192             this.groups = groups;
193         }
194
195         public SourceGroupsChildren(FileObject fo, SourceGroup group) {
196             this.fo = fo;
197             this.group = group;
198         }
199
200         protected void addNotify() {
201             super.addNotify();
202             setKeys(getKeys());
203         }
204
205         protected void removeNotify() {
206             setKeys(Collections.EMPTY_SET);
207             super.removeNotify();
208         }
209
210         protected Node[] createNodes(Object JavaDoc key) {
211
212             FileObject fObj = null;
213             SourceGroup group = null;
214             boolean isFile = false;
215
216             if (key instanceof SourceGroup) {
217                 fObj = ((SourceGroup) key).getRootFolder();
218                 group = (SourceGroup) key;
219             } else if (key instanceof Key) {
220                 fObj = ((Key) key).folder;
221                 group = ((Key) key).group;
222                 if (!fObj.isFolder()) {
223                     isFile = true;
224                 }
225             }
226
227             try {
228                 DataObject dobj = DataObject.find(fObj);
229                 FilterNode fn = (isFile ?
230                         new FilterNode(dobj.getNodeDelegate(), Children.LEAF) :
231                         new FilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren(fObj, group)));
232
233                 if (key instanceof SourceGroup) {
234                     fn.setDisplayName(group.getDisplayName());
235                 }
236
237                 return new Node[]{fn};
238             } catch (DataObjectNotFoundException e) {
239                 return null;
240             }
241         }
242
243         private Collection JavaDoc getKeys() {
244
245             if (groups != null) {
246                 return Arrays.asList(groups);
247             } else {
248                 FileObject files[] = fo.getChildren();
249                 Arrays.sort(files, new FileObjectComparator());
250                 ArrayList JavaDoc children = new ArrayList JavaDoc(files.length);
251                 for (int i = 0; i < files.length; i++) {
252                     FileObject file = files[i];
253                     if (group.contains(files[i]) && file.isFolder()) {
254                         children.add(new Key(files[i], group));
255                     }
256                 }
257                 // add files
258
for (int i = 0; i < files.length; i++) {
259                     FileObject file = files[i];
260                     if (group.contains(file) && !files[i].isFolder()) {
261                         if (filter.accept(file)) {
262                             children.add(new Key(files[i], group));
263                         }
264                     }
265                 }
266                 //}
267

268                 return children;
269             }
270
271         }
272
273         private class Key {
274
275             private FileObject folder;
276             private SourceGroup group;
277
278             private Key(FileObject folder, SourceGroup group) {
279                 this.folder = folder;
280                 this.group = group;
281             }
282
283         }
284
285     }
286
287     private class FileObjectComparator implements java.util.Comparator JavaDoc {
288         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
289             FileObject fo1 = (FileObject) o1;
290             FileObject fo2 = (FileObject) o2;
291             return fo1.getName().compareTo(fo2.getName());
292         }
293     }
294
295     private static final class OptionsListener implements ActionListener JavaDoc {
296
297         public static final String JavaDoc COMMAND_SELECT = "SELECT"; //NOI18N
298
public static final String JavaDoc COMMAND_CANCEL = "CANCEL"; //NOI18N
299

300         private BrowseFolders browsePanel;
301
302         private FileObject result;
303         //private Class target;
304

305         public OptionsListener(BrowseFolders browsePanel) {
306             this.browsePanel = browsePanel;
307         }
308
309         public void actionPerformed(ActionEvent JavaDoc e) {
310             String JavaDoc command = e.getActionCommand();
311
312             if (COMMAND_SELECT.equals(command)) {
313                 this.result = browsePanel.getSelectedFileObject();
314             }
315         }
316
317         public FileObject getResult() {
318             return result;
319         }
320     }
321
322     public static class FileObjectFilter {
323         public boolean accept(FileObject fileObject) {
324             return true;
325         }
326     }
327
328 }
329
Popular Tags