2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.j2ee.ddloaders.web.multiview;
44
import java.awt.Dialog;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.util.ArrayList;
48
import java.util.Arrays;
49
import java.util.Collection;
50
import java.util.Collections;
51
import javax.swing.JButton;
52
import javax.swing.JScrollPane;
53
import org.netbeans.api.project.SourceGroup;
54
import org.openide.util.NbBundle;
55
import org.openide.DialogDescriptor;
56
import org.openide.DialogDisplayer;
57
import org.openide.explorer.ExplorerManager;
58
import org.openide.explorer.view.BeanTreeView;
59
import org.openide.filesystems.FileObject;
60
import org.openide.loaders.DataObject;
61
import org.openide.loaders.DataObjectNotFoundException;
62
import org.openide.loaders.DataFolder;
63
import org.openide.nodes.Children;
64
import org.openide.nodes.Node;
65
import org.openide.nodes.AbstractNode;
66
import org.openide.nodes.FilterNode;
72
* @author phrebejk, mkuchtiak
74
public class BrowseFolders extends javax.swing.JPanel implements ExplorerManager.Provider {
76
private ExplorerManager manager;
77
private SourceGroup[] folders;
79
private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane();
81
/** Creates new form BrowseFolders */
82
public BrowseFolders( SourceGroup[] folders) {
84
this.folders = folders;
85
manager = new ExplorerManager();
86
AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders ) );
87
manager.setRootContext( rootNode );
89
// Create the templates view
90
BeanTreeView btv = new BeanTreeView();
91
btv.setRootVisible( false );
92
btv.setSelectionMode( javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION );
93
btv.setBorder( SAMPLE_SCROLL_PANE.getBorder() );
94
folderPanel.add( btv, java.awt.BorderLayout.CENTER );
97
// ExplorerManager.Provider implementation ---------------------------------
99
public ExplorerManager getExplorerManager() {
104
/** This method is called from within the constructor to
105
* initialize the form.
106
* WARNING: Do NOT modify this code. The content of this method is
107
* always regenerated by the Form Editor.
109
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
110
private void initComponents() {
111
java.awt.GridBagConstraints gridBagConstraints;
113
jLabel1 = new javax.swing.JLabel();
114
folderPanel = new javax.swing.JPanel();
116
setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
117
setLayout(new java.awt.GridBagLayout());
119
jLabel1.setText(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "LBL_Folders")); // NOI18N
120
gridBagConstraints = new java.awt.GridBagConstraints();
121
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
122
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
123
gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
124
add(jLabel1, gridBagConstraints);
126
folderPanel.setLayout(new java.awt.BorderLayout());
127
gridBagConstraints = new java.awt.GridBagConstraints();
128
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
129
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
130
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
131
gridBagConstraints.weightx = 1.0;
132
gridBagConstraints.weighty = 1.0;
133
add(folderPanel, gridBagConstraints);
135
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSD_browse_dialog")); // NOI18N
136
}// </editor-fold>//GEN-END:initComponents
139
// Variables declaration - do not modify//GEN-BEGIN:variables
140
private javax.swing.JPanel folderPanel;
141
private javax.swing.JLabel jLabel1;
142
// End of variables declaration//GEN-END:variables
144
public static FileObject showDialog( SourceGroup[] folders ) {
146
BrowseFolders bf = new BrowseFolders( folders );
148
JButton options[] = new JButton[] {
149
//new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Select_Option") ), // NOI18N
150
//new JButton( NbBundle.getMessage( BrowseFolders.class, "LBL_BrowseFolders_Cancel_Option") ), // NOI18N
151
new JButton( NbBundle.getMessage(BrowseFolders.class,"LBL_SelectFile")),
152
new JButton( NbBundle.getMessage(BrowseFolders.class,"LBL_Cancel") ),
155
OptionsListener optionsListener = new OptionsListener( bf );
157
options[ 0 ].setActionCommand( OptionsListener.COMMAND_SELECT );
158
options[ 0 ].addActionListener( optionsListener );
159
options[ 0 ].getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(BrowseFolders.class,"ACSD_SelectFile"));
160
options[ 1 ].setActionCommand( OptionsListener.COMMAND_CANCEL );
161
options[ 1 ].addActionListener( optionsListener );
162
options[ 1 ].getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(BrowseFolders.class,"ACSD_Cancel"));
164
DialogDescriptor dialogDescriptor = new DialogDescriptor(
166
NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
169
options[ 0 ], // initial value
170
DialogDescriptor.BOTTOM_ALIGN, // options align
174
dialogDescriptor.setClosingOptions( new Object[] { options[ 0 ], options[ 1 ] } );
176
Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
177
dialog.setVisible(true);
179
return optionsListener.getResult();
184
// Innerclasses ------------------------------------------------------------
186
/** Children to be used to show FileObjects from given SourceGroups
189
private final class SourceGroupsChildren extends Children.Keys {
191
private SourceGroup[] groups;
192
private SourceGroup group;
193
private FileObject fo;
195
public SourceGroupsChildren( SourceGroup[] groups ) {
196
this.groups = groups;
199
public SourceGroupsChildren( FileObject fo, SourceGroup group ) {
204
protected void addNotify() {
206
setKeys( getKeys() );
209
protected void removeNotify() {
210
setKeys( Collections.EMPTY_SET );
211
super.removeNotify();
214
protected Node[] createNodes(Object key) {
216
FileObject fObj = null;
217
SourceGroup group = null;
218
boolean isFile=false;
220
if ( key instanceof SourceGroup ) {
221
fObj = ((SourceGroup)key).getRootFolder();
222
group = (SourceGroup)key;
224
else if ( key instanceof Key ) {
225
fObj = ((Key)key).folder;
226
group = ((Key)key).group;
227
if (!fObj.isFolder()) isFile=true;
231
DataObject dobj = DataObject.find( fObj );
232
FilterNode fn = (isFile?new SimpleFilterNode(dobj.getNodeDelegate(),Children.LEAF):
233
new SimpleFilterNode(dobj.getNodeDelegate(), new SourceGroupsChildren( fObj, group )));
234
if ( key instanceof SourceGroup ) {
235
fn.setDisplayName( group.getDisplayName() );
238
return new Node[] { fn };
240
catch ( DataObjectNotFoundException e ) {
245
private Collection getKeys() {
247
if ( groups != null ) {
248
return Arrays.asList( groups );
251
FileObject files[] = fo.getChildren();
252
Arrays.sort(files,new BrowseFolders.FileObjectComparator());
253
ArrayList children = new ArrayList( files.length );
255
if (BrowseFolders.this.target==org.openide.loaders.DataFolder.class)
256
for( int i = 0; i < files.length; i++ ) {
257
if ( files[i].isFolder() && group.contains( files[i] ) ) {
258
children.add( new Key( files[i], group ) );
263
for( int i = 0; i < files.length; i++ ) {
264
if ( group.contains( files[i]) && files[i].isFolder() ) children.add( new Key( files[i], group ) );
267
for( int i = 0; i < files.length; i++ ) {
268
if ( group.contains( files[i]) && !files[i].isFolder() ) children.add( new Key( files[i], group ) );
279
private FileObject folder;
280
private SourceGroup group;
282
private Key ( FileObject folder, SourceGroup group ) {
283
this.folder = folder;
292
private class FileObjectComparator implements java.util.Comparator {
293
public int compare(Object o1, Object o2) {
294
FileObject fo1 = (FileObject)o1;
295
FileObject fo2 = (FileObject)o2;
296
return fo1.getName().compareTo(fo2.getName());
300
private static final class OptionsListener implements ActionListener {
302
public static final String COMMAND_SELECT = "SELECT"; //NOI18N
303
public static final String COMMAND_CANCEL = "CANCEL"; //NOI18N
305
private BrowseFolders browsePanel;
307
private FileObject result;
308
//private Class target;
310
public OptionsListener( BrowseFolders browsePanel ) {
311
this.browsePanel = browsePanel;
314
public void actionPerformed( ActionEvent e ) {
315
String command = e.getActionCommand();
317
if ( COMMAND_SELECT.equals( command ) ) {
318
Node selection[] = browsePanel.getExplorerManager().getSelectedNodes();
320
if ( selection != null && selection.length > 0 ) {
321
DataObject dobj = (DataObject)selection[0].getLookup().lookup( DataObject.class );
322
//if (dobj!=null && dobj.getClass().isAssignableFrom(target)) {
323
result = dobj.getPrimaryFile();
326
if ( dobj != null ) {
327
FileObject fo = dobj.getPrimaryFile();
328
if ( fo.isFolder() ) {
339
public FileObject getResult() {
344
class SimpleFilterNode extends FilterNode {
346
public SimpleFilterNode(org.openide.nodes.Node node, org.openide.nodes.Children children) {
347
super(node, children);
351
public org.openide.util.actions.SystemAction[] getActions() {
352
return new org.openide.util.actions.SystemAction[]{};
354
public org.openide.util.actions.SystemAction getDefaultAction() {