KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > 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.web.multiview;
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         folderPanel.add( btv, java.awt.BorderLayout.CENTER );
73     }
74         
75     // ExplorerManager.Provider implementation ---------------------------------
76

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

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

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         
123         BrowseFolders bf = new BrowseFolders( folders );
124         
125         JButton JavaDoc options[] = new JButton JavaDoc[] {
126             //new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Select_Option") ), // NOI18N
127
//new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Cancel_Option") ), // NOI18N
128
new JButton JavaDoc( NbBundle.getMessage(BrowseFolders.class,"LBL_SelectFile")),
129             new JButton JavaDoc( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") ),
130         };
131                 
132         OptionsListener optionsListener = new OptionsListener( bf );
133         
134         options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT );
135         options[ 0 ].addActionListener( optionsListener );
136         options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL );
137         options[ 1 ].addActionListener( optionsListener );
138         
139         DialogDescriptor dialogDescriptor = new DialogDescriptor(
140             bf, // innerPane
141
NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
142
true, // modal
143
options, // options
144
options[ 0 ], // initial value
145
DialogDescriptor.BOTTOM_ALIGN, // options align
146
null, // helpCtx
147
null ); // listener
148

149         dialogDescriptor.setClosingOptions( new Object JavaDoc[] { options[ 0 ], options[ 1 ] } );
150             
151         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
152         dialog.setVisible(true);
153         
154         return optionsListener.getResult();
155                 
156     }
157     
158     
159     // Innerclasses ------------------------------------------------------------
160

161     /** Children to be used to show FileObjects from given SourceGroups
162      */

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

236                 //else {
237
// add folders
238
for( int i = 0; i < files.length; i++ ) {
239                         if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) );
240                     }
241                     // add files
242
for( int i = 0; i < files.length; i++ ) {
243                         if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) );
244                     }
245                 //}
246

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

280         private BrowseFolders browsePanel;
281         
282         private FileObject result;
283         //private Class target;
284

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

308                 }
309                 
310                 
311             }
312         }
313         
314         public FileObject getResult() {
315             return result;
316         }
317     }
318     
319     class SimpleFilterNode extends FilterNode {
320         
321         public SimpleFilterNode(org.openide.nodes.Node node, org.openide.nodes.Children children) {
322             super(node, children);
323             
324         }
325         
326         public org.openide.util.actions.SystemAction[] getActions() {
327             return new org.openide.util.actions.SystemAction[]{};
328         }
329         public org.openide.util.actions.SystemAction getDefaultAction() {
330            return null;
331         }
332     }
333     
334 }
335
Popular Tags