KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > html > palette > 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.html.palette;
21 import java.io.File JavaDoc;
22 import java.io.FileFilter JavaDoc;
23 import org.netbeans.api.project.SourceGroup;
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.filesystems.FileUtil;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32 import org.openide.nodes.AbstractNode;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.FilterNode;
35 import org.openide.nodes.Node;
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.awt.event.MouseAdapter JavaDoc;
44 import java.awt.event.MouseEvent JavaDoc;
45 import java.awt.event.MouseListener JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Arrays JavaDoc;
48 import java.util.Collection JavaDoc;
49 import java.util.Collections JavaDoc;
50 import org.openide.util.NbBundle;
51
52 /**
53  * @author pfiala
54  */

55 public class BrowseFolders extends JPanel implements ExplorerManager.Provider {
56
57     private ExplorerManager manager;
58
59     private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane();
60     private FileFilter JavaDoc filter;
61
62     public static final FileFilter JavaDoc imageFileFilter = new FileFilter JavaDoc() {
63         public boolean accept(File JavaDoc pathname) {
64             return FileUtil.toFileObject(pathname).getMIMEType().startsWith("image/"); // NOI18N
65
}
66     };
67     
68     
69     /**
70      * Creates new form BrowseFolders
71      */

72     public BrowseFolders(SourceGroup[] folders, FileFilter JavaDoc filter) {
73         initComponents();
74         
75         this.filter = filter;
76         if (this.filter == null) {
77             this.filter = new FileFilter JavaDoc() {
78                 public boolean accept(File JavaDoc pathname) {
79                     return true;
80                 }
81             };
82         }
83         
84         manager = new ExplorerManager();
85         AbstractNode rootNode = new AbstractNode(new SourceGroupsChildren(folders));
86         manager.setRootContext(rootNode);
87         
88         // Create the templates view
89
BeanTreeView btv = new BeanTreeView();
90         btv.setRootVisible(false);
91         btv.setSelectionMode(javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION);
92         btv.setBorder(SAMPLE_SCROLL_PANE.getBorder());
93         btv.setDefaultActionAllowed(false);
94         expandFirstLevel(btv);
95         folderPanel.add(btv, java.awt.BorderLayout.CENTER);
96     }
97         
98     // ExplorerManager.Provider implementation ---------------------------------
99

100     public ExplorerManager getExplorerManager() {
101         return manager;
102     }
103
104     private void expandFirstLevel(BeanTreeView btv) {
105         
106         Node root = manager.getRootContext();
107         Children ch = root.getChildren();
108         if ( ch == Children.LEAF ) {
109             return;
110         }
111         Node nodes[] = ch.getNodes(true);
112
113         btv.expandNode(root);
114         for ( int i = 0; i < nodes.length; i++ ) {
115             btv.expandNode( nodes[i] );
116             if ( i == 0 ) {
117                 try {
118                     manager.setSelectedNodes( new Node[] { nodes[i] } );
119                 }
120                 catch ( java.beans.PropertyVetoException JavaDoc e ) {
121                     // No selection for some reason
122
}
123             }
124         }
125     }
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     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
134
private void initComponents() {
135         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
136
137         jLabel1 = new javax.swing.JLabel JavaDoc();
138         folderPanel = new javax.swing.JPanel JavaDoc();
139
140         setLayout(new java.awt.GridBagLayout JavaDoc());
141
142         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 12, 12)));
143         jLabel1.setText(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders"));
144         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
146         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
147         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
148         add(jLabel1, gridBagConstraints);
149
150         folderPanel.setLayout(new java.awt.BorderLayout JavaDoc());
151
152         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
153         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
154         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
155         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
156         gridBagConstraints.weightx = 1.0;
157         gridBagConstraints.weighty = 1.0;
158         add(folderPanel, gridBagConstraints);
159
160     }
161     // </editor-fold>//GEN-END:initComponents
162

163     // Variables declaration - do not modify//GEN-BEGIN:variables
164
private javax.swing.JPanel JavaDoc folderPanel;
165     private javax.swing.JLabel JavaDoc jLabel1;
166     // End of variables declaration//GEN-END:variables
167

168     public static FileObject showDialog(SourceGroup[] folders) {
169         return showDialog(folders, null);
170     }
171
172     static class TreeMouseAdapter extends MouseAdapter JavaDoc {
173         
174         JTree tree;
175         BrowseFolders bf;
176         JButton[] options;
177         
178         TreeMouseAdapter(JTree tree, BrowseFolders bf, JButton[] options) {
179             this.tree = tree;
180             this.bf = bf;
181             this.options = options;
182         }
183         
184         public void mouseClicked(MouseEvent JavaDoc e) {
185             int selRow = tree.getRowForLocation(e.getX(), e.getY());
186
187             if ((selRow != -1) && SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() % 2) == 0) {
188                 FileObject fileObject = bf.getSelectedFileObject();
189                 if (fileObject != null && !fileObject.isFolder())
190                     options[0].doClick();
191             }
192         }
193     }
194
195     public static FileObject showDialog(SourceGroup[] folders, FileFilter JavaDoc filter) {
196
197         final BrowseFolders bf = new BrowseFolders(folders, filter);
198
199         final JButton options[] = new JButton[]{
200             new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_SelectFile")),
201             new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_Cancel")),
202         };
203
204         options[0].setEnabled(false);
205
206         JTree tree = HTMLPaletteUtilities.findTreeComponent(bf);
207         tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener JavaDoc() {
208             public void valueChanged(TreeSelectionEvent JavaDoc e) {
209                 FileObject fileObject = bf.getSelectedFileObject();
210                 options[0].setEnabled(fileObject != null && !fileObject.isFolder());
211             }
212         });
213         
214         OptionsListener optionsListener = new OptionsListener(bf);
215
216         options[0].setActionCommand(OptionsListener.COMMAND_SELECT);
217         options[0].addActionListener(optionsListener);
218         options[1].setActionCommand(OptionsListener.COMMAND_CANCEL);
219         options[1].addActionListener(optionsListener);
220         
221         MouseListener JavaDoc ml = new TreeMouseAdapter(tree, bf, options);
222         tree.addMouseListener(ml);
223
224
225         DialogDescriptor dialogDescriptor = new DialogDescriptor(bf, // innerPane
226
NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
227
true, // modal
228
options, // options
229
options[0], // initial value
230
DialogDescriptor.BOTTOM_ALIGN, // options align
231
null, // helpCtx
232
null); // listener
233

234         dialogDescriptor.setClosingOptions(new Object JavaDoc[]{options[0], options[1]});
235
236         Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
237         dialog.setVisible(true);
238
239         return optionsListener.getResult();
240
241     }
242
243
244     private FileObject getSelectedFileObject() {
245         Node selection[] = getExplorerManager().getSelectedNodes();
246         if (selection != null && selection.length > 0) {
247             DataObject dobj = (DataObject) selection[0].getLookup().lookup(DataObject.class);
248             return dobj.getPrimaryFile();
249         }
250         return null;
251     }
252
253     // Innerclasses ------------------------------------------------------------
254

255     /**
256      * Children to be used to show FileObjects from given SourceGroups
257      */

258
259     private final class SourceGroupsChildren extends Children.Keys {
260
261         private SourceGroup[] groups;
262         private SourceGroup group;
263         private FileObject fo;
264
265         public SourceGroupsChildren(SourceGroup[] groups) {
266             this.groups = groups;
267         }
268
269         public SourceGroupsChildren(FileObject fo, SourceGroup group) {
270             this.fo = fo;
271             this.group = group;
272         }
273
274         protected void addNotify() {
275             super.addNotify();
276             setKeys(getKeys());
277         }
278
279         protected void removeNotify() {
280             setKeys(Collections.EMPTY_SET);
281             super.removeNotify();
282         }
283
284         protected Node[] createNodes(Object JavaDoc key) {
285
286             FileObject fObj = null;
287             SourceGroup group = null;
288             boolean isFile = false;
289
290             if (key instanceof SourceGroup) {
291                 fObj = ((SourceGroup) key).getRootFolder();
292                 group = (SourceGroup) key;
293             } else if (key instanceof Key) {
294                 fObj = ((Key) key).folder;
295                 group = ((Key) key).group;
296                 if (!fObj.isFolder()) {
297                     isFile = true;
298                 }
299             }
300
301             try {
302                 DataObject dobj = DataObject.find(fObj);
303                 FilterNode fn = (isFile ?
304                         new FilterNode(dobj.getNodeDelegate(), Children.LEAF) :
305                         new FilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren(fObj, group)));
306
307                 if (key instanceof SourceGroup) {
308                     fn.setDisplayName(group.getDisplayName());
309                 }
310
311                 return new Node[]{fn};
312             } catch (DataObjectNotFoundException e) {
313                 return null;
314             }
315         }
316
317         private Collection JavaDoc getKeys() {
318
319             if (groups != null) {
320                 return Arrays.asList(groups);
321             } else {
322                 FileObject files[] = fo.getChildren();
323                 Arrays.sort(files, new FileObjectComparator());
324                 ArrayList JavaDoc children = new ArrayList JavaDoc(files.length);
325                 for (int i = 0; i < files.length; i++) {
326                     FileObject file = files[i];
327                     if (group.contains(files[i]) && file.isFolder()) {
328                         children.add(new Key(files[i], group));
329                     }
330                 }
331                 // add files
332
for (int i = 0; i < files.length; i++) {
333                     FileObject file = files[i];
334                     if (group.contains(file) && !files[i].isFolder()) {
335                         if (filter.accept(FileUtil.toFile(file))) {
336                             children.add(new Key(files[i], group));
337                         }
338                     }
339                 }
340                 //}
341

342                 return children;
343             }
344
345         }
346
347         private class Key {
348
349             private FileObject folder;
350             private SourceGroup group;
351
352             private Key(FileObject folder, SourceGroup group) {
353                 this.folder = folder;
354                 this.group = group;
355             }
356
357         }
358
359     }
360
361     private class FileObjectComparator implements java.util.Comparator JavaDoc {
362         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
363             FileObject fo1 = (FileObject) o1;
364             FileObject fo2 = (FileObject) o2;
365             return fo1.getName().compareTo(fo2.getName());
366         }
367     }
368
369     private static final class OptionsListener implements ActionListener JavaDoc {
370
371         public static final String JavaDoc COMMAND_SELECT = "SELECT"; //NOI18N
372
public static final String JavaDoc COMMAND_CANCEL = "CANCEL"; //NOI18N
373

374         private BrowseFolders browsePanel;
375
376         private FileObject result;
377         //private Class target;
378

379         public OptionsListener(BrowseFolders browsePanel) {
380             this.browsePanel = browsePanel;
381         }
382
383         public void actionPerformed(ActionEvent JavaDoc e) {
384             String JavaDoc command = e.getActionCommand();
385
386             if (COMMAND_SELECT.equals(command)) {
387                 this.result = browsePanel.getSelectedFileObject();
388             }
389         }
390
391         public FileObject getResult() {
392             return result;
393         }
394     }
395
396 }
397
Popular Tags