KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > dialogs > 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.struts.dialogs;
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
46 // XXX I18N
47

48 /**
49  *
50  * @author phrebejk, mkuchtiak
51  */

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

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

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

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

123     public static FileObject showDialog( SourceGroup[] folders ) {
124         
125         BrowseFolders bf = new BrowseFolders( folders );
126         
127         JButton JavaDoc options[] = new JButton JavaDoc[] {
128             //new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Select_Option") ), // NOI18N
129
//new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Cancel_Option") ), // NOI18N
130
new JButton JavaDoc( NbBundle.getMessage(BrowseFolders.class,"LBL_SelectFile")),
131             new JButton JavaDoc( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") ),
132         };
133         options[0].getAccessibleContext().setAccessibleDescription(
134                 NbBundle.getMessage(BrowseFolders.class, "ACSD_SelectFile"));
135         options[1].getAccessibleContext().setAccessibleDescription(
136                 NbBundle.getMessage(BrowseFolders.class, "ACSD_Cancel"));
137         
138         OptionsListener optionsListener = new OptionsListener( bf );
139         
140         options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT );
141         options[ 0 ].addActionListener( optionsListener );
142         options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL );
143         options[ 1 ].addActionListener( optionsListener );
144         
145         DialogDescriptor dialogDescriptor = new DialogDescriptor(
146             bf, // innerPane
147
NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
148
true, // modal
149
options, // options
150
options[ 0 ], // initial value
151
DialogDescriptor.BOTTOM_ALIGN, // options align
152
null, // helpCtx
153
null ); // listener
154

155         dialogDescriptor.setClosingOptions( new Object JavaDoc[] { options[ 0 ], options[ 1 ] } );
156             
157         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
158         dialog.getAccessibleContext().setAccessibleDescription(
159                 NbBundle.getMessage(BrowseFolders.class, "ACSD_BrowseFoldersDialog"));
160         dialog.setVisible(true);
161         
162         return optionsListener.getResult();
163                 
164     }
165     
166     
167     // Innerclasses ------------------------------------------------------------
168

169     /** Children to be used to show FileObjects from given SourceGroups
170      */

171      
172     private final class SourceGroupsChildren extends Children.Keys {
173         
174         private SourceGroup[] groups;
175         private SourceGroup group;
176         private FileObject fo;
177         
178         public SourceGroupsChildren( SourceGroup[] groups ) {
179             this.groups = groups;
180         }
181         
182         public SourceGroupsChildren( FileObject fo, SourceGroup group ) {
183             this.fo = fo;
184             this.group = group;
185         }
186         
187         protected void addNotify() {
188             super.addNotify();
189             setKeys( getKeys() );
190         }
191         
192         protected void removeNotify() {
193             setKeys( Collections.EMPTY_SET );
194             super.removeNotify();
195         }
196         
197         protected Node[] createNodes(Object JavaDoc key) {
198             
199             FileObject fObj = null;
200             SourceGroup group = null;
201             boolean isFile=false;
202             
203             if ( key instanceof SourceGroup ) {
204                 fObj = ((SourceGroup)key).getRootFolder();
205                 group = (SourceGroup)key;
206             }
207             else if ( key instanceof Key ) {
208                 fObj = ((Key)key).folder;
209                 group = ((Key)key).group;
210                 if (!fObj.isFolder()) isFile=true;
211             }
212
213             try {
214                 DataObject dobj = DataObject.find( fObj );
215                 FilterNode fn = (isFile?new SimpleFilterNode(dobj.getNodeDelegate(),Children.LEAF):
216                                         new SimpleFilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren( fObj, group )));
217                 if ( key instanceof SourceGroup ) {
218                     fn.setDisplayName( group.getDisplayName() );
219                 }
220             
221                 return new Node[] { fn };
222             }
223             catch ( DataObjectNotFoundException e ) {
224                 return null;
225             }
226         }
227                 
228         private Collection JavaDoc getKeys() {
229             
230             if ( groups != null ) {
231                 return Arrays.asList( groups );
232             }
233             else {
234                 FileObject files[] = fo.getChildren();
235                 Arrays.sort(files,new BrowseFolders.FileObjectComparator());
236                 ArrayList JavaDoc children = new ArrayList JavaDoc( files.length );
237                 /*
238                 if (BrowseFolders.this.target==org.openide.loaders.DataFolder.class)
239                     for( int i = 0; i < files.length; i++ ) {
240                         if ( files[i].isFolder() && group.contains( files[i] ) ) {
241                             children.add( new Key( files[i], group ) );
242                         }
243                     }*/

244                 //else {
245
// add folders
246
for( int i = 0; i < files.length; i++ ) {
247                         if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) );
248                     }
249                     // add files
250
for( int i = 0; i < files.length; i++ ) {
251                         if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) );
252                     }
253                 //}
254

255                 return children;
256             }
257             
258         }
259         
260         private class Key {
261             
262             private FileObject folder;
263             private SourceGroup group;
264             
265             private Key ( FileObject folder, SourceGroup group ) {
266                 this.folder = folder;
267                 this.group = group;
268             }
269             
270             
271         }
272         
273     }
274
275     private class FileObjectComparator implements java.util.Comparator JavaDoc {
276         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
277             FileObject fo1 = (FileObject)o1;
278             FileObject fo2 = (FileObject)o2;
279             return fo1.getName().compareTo(fo2.getName());
280         }
281     }
282     
283     private static final class OptionsListener implements ActionListener JavaDoc {
284     
285         public static final String JavaDoc COMMAND_SELECT = "SELECT"; //NOI18N
286
public static final String JavaDoc COMMAND_CANCEL = "CANCEL"; //NOI18N
287

288         private BrowseFolders browsePanel;
289         
290         private FileObject result;
291         //private Class target;
292

293         public OptionsListener( BrowseFolders browsePanel ) {
294             this.browsePanel = browsePanel;
295         }
296         
297         public void actionPerformed( ActionEvent JavaDoc e ) {
298             String JavaDoc command = e.getActionCommand();
299
300             if ( COMMAND_SELECT.equals( command ) ) {
301                 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes();
302                 
303                 if ( selection != null && selection.length > 0 ) {
304                     DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class );
305                     //if (dobj!=null && dobj.getClass().isAssignableFrom(target)) {
306
result = dobj.getPrimaryFile();
307                     //}
308
/*
309                     if ( dobj != null ) {
310                         FileObject fo = dobj.getPrimaryFile();
311                         if ( fo.isFolder() ) {
312                             result = fo;
313                         }
314                     }
315                     */

316                 }
317                 
318                 
319             }
320         }
321         
322         public FileObject getResult() {
323             return result;
324         }
325     }
326     
327     class SimpleFilterNode extends FilterNode {
328         
329         public SimpleFilterNode(org.openide.nodes.Node node, org.openide.nodes.Children children) {
330             super(node, children);
331             
332         }
333         
334         public org.openide.util.actions.SystemAction[] getActions() {
335             return new org.openide.util.actions.SystemAction[]{};
336         }
337         public org.openide.util.actions.SystemAction getDefaultAction() {
338            return null;
339         }
340     }
341     
342 }
343
Popular Tags