KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > 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.web.wizards;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import org.netbeans.api.project.SourceGroup;
32 import org.openide.util.NbBundle;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.explorer.ExplorerManager;
36 import org.openide.explorer.view.BeanTreeView;
37 import org.openide.filesystems.FileObject;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataObjectNotFoundException;
40 import org.openide.loaders.DataFolder;
41 import org.openide.nodes.Children;
42 import org.openide.nodes.Node;
43 import org.openide.nodes.AbstractNode;
44 import org.openide.nodes.FilterNode;
45 import org.openide.nodes.NodeNotFoundException;
46 import org.openide.nodes.NodeOp;
47 import org.openide.util.NbCollections;
48
49 // XXX I18N
50

51 /**
52  *
53  * @author phrebejk, mkuchtiak
54  */

55 public class BrowseFolders extends javax.swing.JPanel JavaDoc implements ExplorerManager.Provider {
56     
57     private ExplorerManager manager;
58     private SourceGroup[] folders;
59     private Class JavaDoc target;
60     BeanTreeView btv;
61     
62     private static JScrollPane JavaDoc SAMPLE_SCROLL_PANE = new JScrollPane JavaDoc();
63     
64     /** Creates new form BrowseFolders */
65     public BrowseFolders( SourceGroup[] folders, Class JavaDoc target, String JavaDoc preselectedFileName ) {
66         initComponents();
67         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, (target == DataFolder.class?"ACSD_BrowseFolders":"ACSD_BrowseFiles")));
68
69         this.folders = folders;
70         this.target = target;
71         manager = new ExplorerManager();
72         AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders ) );
73         manager.setRootContext( rootNode );
74         
75         // Create the templates view
76
btv = new BeanTreeView();
77         btv.setRootVisible( false );
78         btv.setSelectionMode( javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION );
79         btv.setBorder( SAMPLE_SCROLL_PANE.getBorder() );
80         expandSelection( preselectedFileName );
81         folderPanel.add( btv, java.awt.BorderLayout.CENTER );
82     }
83         
84     // ExplorerManager.Provider implementation ---------------------------------
85

86     public ExplorerManager getExplorerManager() {
87         return manager;
88     }
89     
90     private void expandSelection( String JavaDoc preselectedFileName ) {
91         
92         Node root = manager.getRootContext();
93         Children ch = root.getChildren();
94         if ( ch == Children.LEAF ) {
95             return;
96         }
97         Node nodes[] = ch.getNodes( true );
98         
99         Node sel = null;
100         
101         if ( preselectedFileName != null && preselectedFileName.length() > 0 ) {
102              // Try to find the node
103
for ( int i = 0; i < nodes.length; i++ ) {
104                 try {
105                     sel = NodeOp.findPath( nodes[i], NbCollections.checkedEnumerationByFilter( new java.util.StringTokenizer JavaDoc( preselectedFileName, "/" ), String JavaDoc.class, false ) );
106                     break;
107                 }
108                 catch ( NodeNotFoundException e ) {
109                     // Will select the first node
110
}
111              }
112         }
113                         
114         if ( sel == null ) {
115             // Node not found => expand first level
116
btv.expandNode( root );
117             for ( int i = 0; i < nodes.length; i++ ) {
118                 btv.expandNode( nodes[i] );
119                 if ( i == 0 ) {
120                     sel = nodes[i];
121                 }
122             }
123         }
124         
125         
126         if ( sel != null ) {
127             // Select the node
128
try {
129                 manager.setSelectedNodes( new Node[] { sel } );
130             }
131             catch ( java.beans.PropertyVetoException JavaDoc e ) {
132                 // No selection for some reason
133
}
134         }
135                 
136     }
137     
138     /** This method is called from within the constructor to
139      * initialize the form.
140      * WARNING: Do NOT modify this code. The content of this method is
141      * always regenerated by the Form Editor.
142      */

143     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
144
private void initComponents() {
145         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
146
147         jLabel1 = new javax.swing.JLabel JavaDoc();
148         folderPanel = new javax.swing.JPanel JavaDoc();
149
150         setLayout(new java.awt.GridBagLayout JavaDoc());
151
152         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 12, 12)));
153         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders"));
154         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
155         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
156         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
158         add(jLabel1, gridBagConstraints);
159
160         folderPanel.setLayout(new java.awt.BorderLayout JavaDoc());
161
162         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
163         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
164         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
165         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
166         gridBagConstraints.weightx = 1.0;
167         gridBagConstraints.weighty = 1.0;
168         add(folderPanel, gridBagConstraints);
169
170     }
171     // </editor-fold>//GEN-END:initComponents
172

173     
174     // Variables declaration - do not modify//GEN-BEGIN:variables
175
private javax.swing.JPanel JavaDoc folderPanel;
176     private javax.swing.JLabel JavaDoc jLabel1;
177     // End of variables declaration//GEN-END:variables
178

179     public static FileObject showDialog( SourceGroup[] folders, Class JavaDoc target, String JavaDoc preselectedFileName) {
180         
181         BrowseFolders bf = new BrowseFolders( folders, target, preselectedFileName);
182
183         JButton JavaDoc selectButton = new JButton JavaDoc( NbBundle.getMessage(BrowseFolders.class,(target == DataFolder.class?"LBL_SelectFolder":"LBL_SelectFile")));
184         selectButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, (target == DataFolder.class?"ACSD_SelectFolder":"ACSD_SelectFile")));
185         JButton JavaDoc cancelButton = new JButton JavaDoc( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") );
186         cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, "ACSD_Cancel"));
187         JButton JavaDoc options[] = new JButton JavaDoc[] {
188             //new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Select_Option") ), // NOI18N
189
//new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Cancel_Option") ), // NOI18N
190
selectButton,
191             cancelButton,
192         };
193                 
194         OptionsListener optionsListener = new OptionsListener( bf, target );
195         
196         options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT );
197         options[ 0 ].addActionListener( optionsListener );
198         options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL );
199         options[ 1 ].addActionListener( optionsListener );
200         
201         DialogDescriptor dialogDescriptor = new DialogDescriptor(
202             bf, // innerPane
203
NbBundle.getMessage(BrowseFolders.class, (target == DataFolder.class? "LBL_BrowseFolders":"LBL_BrowseFiles")), // displayName
204
true, // modal
205
options, // options
206
options[ 0 ], // initial value
207
DialogDescriptor.BOTTOM_ALIGN, // options align
208
null, // helpCtx
209
null ); // listener
210

211         dialogDescriptor.setClosingOptions( new Object JavaDoc[] { options[ 0 ], options[ 1 ] } );
212             
213         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
214         dialog.show();
215         
216         return optionsListener.getResult();
217                 
218     }
219     
220     
221     // Innerclasses ------------------------------------------------------------
222

223     /** Children to be used to show FileObjects from given SourceGroups
224      */

225      
226     private final class SourceGroupsChildren extends Children.Keys {
227         
228         private SourceGroup[] groups;
229         private SourceGroup group;
230         private FileObject fo;
231         
232         public SourceGroupsChildren( SourceGroup[] groups ) {
233             this.groups = groups;
234         }
235         
236         public SourceGroupsChildren( FileObject fo, SourceGroup group ) {
237             this.fo = fo;
238             this.group = group;
239         }
240         
241         protected void addNotify() {
242             super.addNotify();
243             setKeys( getKeys() );
244         }
245         
246         protected void removeNotify() {
247             setKeys( Collections.EMPTY_SET );
248             super.removeNotify();
249         }
250         
251         protected Node[] createNodes(Object JavaDoc key) {
252             
253             FileObject fObj = null;
254             SourceGroup group = null;
255             boolean isFile=false;
256             
257             if ( key instanceof SourceGroup ) {
258                 fObj = ((SourceGroup)key).getRootFolder();
259                 group = (SourceGroup)key;
260             }
261             else if ( key instanceof Key ) {
262                 fObj = ((Key)key).folder;
263                 group = ((Key)key).group;
264                 if (!fObj.isFolder()) isFile=true;
265             }
266
267             try {
268                 DataObject dobj = DataObject.find( fObj );
269                 FilterNode fn = (isFile?new FilterNode(dobj.getNodeDelegate(),Children.LEAF):
270                                         new FilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren( fObj, group )));
271             
272                 if ( key instanceof SourceGroup ) {
273                     fn.setDisplayName( group.getDisplayName() );
274                 }
275             
276                 return new Node[] { fn };
277             }
278             catch ( DataObjectNotFoundException e ) {
279                 return null;
280             }
281         }
282                 
283         private Collection JavaDoc getKeys() {
284             
285             if ( groups != null ) {
286                 return Arrays.asList( groups );
287             }
288             else {
289                 FileObject files[] = fo.getChildren();
290                 Arrays.sort(files,new BrowseFolders.FileObjectComparator());
291                 ArrayList JavaDoc children = new ArrayList JavaDoc( files.length );
292                 
293                 if (BrowseFolders.this.target==org.openide.loaders.DataFolder.class)
294                     for( int i = 0; i < files.length; i++ ) {
295                         if ( files[i].isFolder() && group.contains( files[i] ) ) {
296                             children.add( new Key( files[i], group ) );
297                         }
298                     }
299                 else {
300                     // add folders
301
for( int i = 0; i < files.length; i++ ) {
302                         if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) );
303                     }
304                     // add files
305
for( int i = 0; i < files.length; i++ ) {
306                         if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) );
307                     }
308                 }
309                 
310                 return children;
311             }
312             
313         }
314         
315         private class Key {
316             
317             private FileObject folder;
318             private SourceGroup group;
319             
320             private Key ( FileObject folder, SourceGroup group ) {
321                 this.folder = folder;
322                 this.group = group;
323             }
324             
325             
326         }
327         
328     }
329
330     private class FileObjectComparator implements java.util.Comparator JavaDoc {
331         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
332             FileObject fo1 = (FileObject)o1;
333             FileObject fo2 = (FileObject)o2;
334             return fo1.getName().compareTo(fo2.getName());
335         }
336     }
337     
338     private static final class OptionsListener implements ActionListener JavaDoc {
339     
340         public static final String JavaDoc COMMAND_SELECT = "SELECT"; //NOI18N
341
public static final String JavaDoc COMMAND_CANCEL = "CANCEL"; //NOI18N
342

343         private BrowseFolders browsePanel;
344         
345         private FileObject result;
346         private Class JavaDoc target;
347         
348         public OptionsListener( BrowseFolders browsePanel, Class JavaDoc target ) {
349             this.browsePanel = browsePanel;
350             this.target=target;
351         }
352         
353         public void actionPerformed( ActionEvent JavaDoc e ) {
354             String JavaDoc command = e.getActionCommand();
355
356             if ( COMMAND_SELECT.equals( command ) ) {
357                 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes();
358                 
359                 if ( selection != null && selection.length > 0 ) {
360                     DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class );
361                     if (dobj!=null && dobj.getClass().isAssignableFrom(target)) {
362                         result = dobj.getPrimaryFile();
363                     }
364                     /*
365                     if ( dobj != null ) {
366                         FileObject fo = dobj.getPrimaryFile();
367                         if ( fo.isFolder() ) {
368                             result = fo;
369                         }
370                     }
371                     */

372                 }
373                 
374                 
375             }
376         }
377         
378         public FileObject getResult() {
379             return result;
380         }
381     }
382     
383     
384 }
385
Popular Tags