KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > 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.project.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JScrollPane JavaDoc;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.SourceGroup;
34 import org.netbeans.api.queries.VisibilityQuery;
35 import org.openide.DialogDescriptor;
36 import org.openide.DialogDisplayer;
37 import org.openide.awt.Mnemonics;
38 import org.openide.explorer.ExplorerManager;
39 import org.openide.explorer.view.BeanTreeView;
40 import org.openide.filesystems.FileObject;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43 import org.openide.nodes.Children;
44 import org.openide.nodes.Node;
45 import org.openide.nodes.AbstractNode;
46 import org.openide.nodes.FilterNode;
47 import org.openide.nodes.NodeNotFoundException;
48 import org.openide.nodes.NodeOp;
49 import org.openide.util.NbBundle;
50 import org.openide.util.NbCollections;
51
52 /**
53  *
54  * @author phrebejk
55  */

56 public class BrowseFolders extends javax.swing.JPanel JavaDoc implements ExplorerManager.Provider {
57     
58     private ExplorerManager manager;
59     private SourceGroup[] folders;
60     private BeanTreeView btv;
61     private Project project;
62     
63     private static JScrollPane JavaDoc SAMPLE_SCROLL_PANE = new JScrollPane JavaDoc();
64     
65     /** Creates new form BrowseFolders */
66     public BrowseFolders( SourceGroup[] folders, Project project, String JavaDoc preselectedFileName ) {
67         initComponents();
68         this.folders = folders;
69         this.project = project;
70         
71         manager = new ExplorerManager();
72         AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders, project ) );
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         btv.setPopupAllowed( false );
81         btv.getAccessibleContext ().setAccessibleName (NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders_folderPanel"));
82         btv.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(BrowseFolders.class, "ACSD_BrowseFolders_folderPanel"));
83         expandSelection( preselectedFileName );
84         //expandAllNodes( btv, manager.getRootContext() );
85
folderPanel.add( btv, java.awt.BorderLayout.CENTER );
86     }
87         
88     // ExplorerManager.Provider implementation ---------------------------------
89

90     public ExplorerManager getExplorerManager() {
91         return manager;
92     }
93         
94     
95     /** This method is called from within the constructor to
96      * initialize the form.
97      * WARNING: Do NOT modify this code. The content of this method is
98      * always regenerated by the Form Editor.
99      */

100     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
101
private void initComponents() {
102         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
103
104         jLabel1 = new javax.swing.JLabel JavaDoc();
105         folderPanel = new javax.swing.JPanel JavaDoc();
106
107         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
108         setLayout(new java.awt.GridBagLayout JavaDoc());
109
110         jLabel1.setLabelFor(folderPanel);
111         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFolders_jLabel1")); // NOI18N
112
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
113         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
114         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
115         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
116         add(jLabel1, gridBagConstraints);
117         jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders_jLabel1")); // NOI18N
118

119         folderPanel.setLayout(new java.awt.BorderLayout JavaDoc());
120         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
121         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
122         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
123         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
124         gridBagConstraints.weightx = 1.0;
125         gridBagConstraints.weighty = 1.0;
126         add(folderPanel, gridBagConstraints);
127
128         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders")); // NOI18N
129
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSN_BrowseFolders")); // NOI18N
130
}// </editor-fold>//GEN-END:initComponents
131

132     
133     // Variables declaration - do not modify//GEN-BEGIN:variables
134
private javax.swing.JPanel JavaDoc folderPanel;
135     private javax.swing.JLabel JavaDoc jLabel1;
136     // End of variables declaration//GEN-END:variables
137

138     public static FileObject showDialog( SourceGroup[] folders, Project project, String JavaDoc preselectedFileName ) {
139         
140         BrowseFolders bf = new BrowseFolders( folders, project, preselectedFileName );
141         
142         JButton JavaDoc options[] = new JButton JavaDoc[] {
143             new JButton JavaDoc(),
144             new JButton JavaDoc( NbBundle.getMessage( BrowseFolders.class, "BTN_BrowseFolders_Cancel_Option") ), // NOI18N
145
};
146                 
147         OptionsListener optionsListener = new OptionsListener( bf );
148         
149         options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT );
150         options[ 0 ].addActionListener( optionsListener );
151         Mnemonics.setLocalizedText(options[0], NbBundle.getMessage( BrowseFolders.class, "BTN_BrowseFolders_Select_Option") );
152         options[ 0 ].getAccessibleContext ().setAccessibleName (NbBundle.getMessage( BrowseFolders.class, "ACSN_BrowseFolders_Select_Option"));
153         options[ 0 ].getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage( BrowseFolders.class, "ACSD_BrowseFolders_Select_Option"));
154         options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL );
155         options[ 1 ].addActionListener( optionsListener );
156         options[ 1 ].getAccessibleContext ().setAccessibleName (NbBundle.getMessage( BrowseFolders.class, "ACSN_BrowseFolders_Cancel_Option"));
157         options[ 1 ].getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage( BrowseFolders.class, "ACSD_BrowseFolders_Cancel_Option"));
158         
159         DialogDescriptor dialogDescriptor = new DialogDescriptor(
160             bf, // innerPane
161
NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Dialog"), // displayName
162
true, // modal
163
options, // options
164
options[ 0 ], // initial value
165
DialogDescriptor.BOTTOM_ALIGN, // options align
166
null, // helpCtx
167
null ); // listener
168

169         dialogDescriptor.setClosingOptions( new Object JavaDoc[] { options[ 0 ], options[ 1 ] } );
170             
171         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
172         dialog.setVisible(true);
173         
174         return optionsListener.getResult();
175                 
176     }
177     
178     private void expandSelection( String JavaDoc preselectedFileName ) {
179         
180         Node root = manager.getRootContext();
181         Children ch = root.getChildren();
182         if ( ch == Children.LEAF ) {
183             return;
184         }
185         Node nodes[] = ch.getNodes( true );
186         
187         Node sel = null;
188         
189         if ( preselectedFileName != null && preselectedFileName.length() > 0 ) {
190              // Try to find the node
191
for ( int i = 0; i < nodes.length; i++ ) {
192                 try {
193                     sel = NodeOp.findPath(nodes[i], NbCollections.checkedEnumerationByFilter(new StringTokenizer JavaDoc(preselectedFileName, "/"), String JavaDoc.class, true));
194                     break;
195                 }
196                 catch ( NodeNotFoundException e ) {
197                     System.out.println( e.getMissingChildName() );
198                     // Will select the first node
199
}
200              }
201         }
202                         
203         if ( sel == null ) {
204             // Node not found => expand first level
205
btv.expandNode( root );
206             for ( int i = 0; i < nodes.length; i++ ) {
207                 btv.expandNode( nodes[i] );
208                 if ( i == 0 ) {
209                     sel = nodes[i];
210                 }
211             }
212         }
213         
214         
215         if ( sel != null ) {
216             // Select the node
217
try {
218                 manager.setSelectedNodes( new Node[] { sel } );
219             }
220             catch ( PropertyVetoException JavaDoc e ) {
221                 // No selection for some reason
222
}
223         }
224                 
225     }
226     
227     
228     /*
229     private static void expandAllNodes( BeanTreeView btv, Node node ) {
230         
231         btv.expandNode( node );
232         
233         Children ch = node.getChildren();
234         if ( ch == Children.LEAF ) {
235             return;
236         }
237         Node nodes[] = ch.getNodes( true );
238         
239         for ( int i = 0; i < nodes.length; i++ ) {
240             expandAllNodes( btv, nodes[i] );
241         }
242         
243     }
244     */

245     
246     // Innerclasses ------------------------------------------------------------
247

248     private static final class SourceGroupsChildren extends Children.Keys<SourceGroup> {
249
250         private final SourceGroup[] groups;
251         private final Project project;
252
253         public SourceGroupsChildren(SourceGroup[] groups, Project project) {
254             assert groups != null;
255             assert project != null;
256             this.groups = groups;
257             this.project = project;
258         }
259
260         @Override JavaDoc
261         protected void addNotify() {
262             super.addNotify();
263             setKeys(groups);
264         }
265
266         @Override JavaDoc
267         protected void removeNotify() {
268             setKeys(Collections.<SourceGroup>emptySet());
269             super.removeNotify();
270         }
271
272         @Override JavaDoc
273         protected Node[] createNodes(SourceGroup g) {
274             FileObject folder = g.getRootFolder();
275             FilterNode fn = new FilterNode(
276                     new PhysicalView.GroupNode(project, g, folder.equals(project.getProjectDirectory()), DataFolder.findFolder(folder)),
277                     new SourceGroupChildren(folder, g));
278             return new Node[] { fn };
279         }
280
281     }
282
283     private static final class SourceGroupChildren extends Children.Keys<FileObject> {
284
285         private final SourceGroup group;
286         private final FileObject fo;
287
288         public SourceGroupChildren(FileObject fo, SourceGroup group) {
289             assert fo != null;
290             assert group != null;
291             this.fo = fo;
292             this.group = group;
293         }
294
295         @Override JavaDoc
296         protected void addNotify() {
297             super.addNotify();
298             List JavaDoc<FileObject> l = new ArrayList JavaDoc<FileObject>();
299             for (FileObject f : fo.getChildren()) {
300                 if (f.isFolder() && group.contains(f) && VisibilityQuery.getDefault().isVisible(f)) {
301                     l.add(f);
302                 }
303             }
304             setKeys(l);
305         }
306
307         @Override JavaDoc
308         protected void removeNotify() {
309             setKeys(Collections.<FileObject>emptySet());
310             super.removeNotify();
311         }
312
313         @Override JavaDoc
314         protected Node[] createNodes(FileObject folder) {
315             FilterNode fn = new FilterNode(
316                     DataFolder.findFolder(folder).getNodeDelegate(),
317                     new SourceGroupChildren(folder, group));
318             return new Node[] { fn };
319         }
320
321     }
322
323     
324     private static final class OptionsListener implements ActionListener JavaDoc {
325     
326         public static final String JavaDoc COMMAND_SELECT = "SELECT";
327         public static final String JavaDoc COMMAND_CANCEL = "CANCEL";
328             
329         private BrowseFolders browsePanel;
330         
331         private FileObject result;
332         
333         public OptionsListener( BrowseFolders browsePanel ) {
334             this.browsePanel = browsePanel;
335         }
336         
337         public void actionPerformed( ActionEvent JavaDoc e ) {
338             String JavaDoc command = e.getActionCommand();
339
340             if ( COMMAND_SELECT.equals( command ) ) {
341                 Node selection[] = browsePanel.getExplorerManager().getSelectedNodes();
342                 
343                 if ( selection != null && selection.length > 0 ) {
344                     DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class );
345                     if ( dobj != null ) {
346                         FileObject fo = dobj.getPrimaryFile();
347                         if ( fo.isFolder() ) {
348                             result = fo;
349                         }
350                     }
351                 }
352                 
353                 
354             }
355         }
356         
357         public FileObject getResult() {
358             return result;
359         }
360     }
361     
362     
363 }
364
Popular Tags