~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/ddloaders/src/org/netbeans/modules/j2ee/ddloaders/web/multiview/BrowseFolders.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.ddloaders.web.multiview;
 
43
 
 
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;
 
67
 
 
68
// XXX I18N
 
69
 
 
70
/**
 
71
 *
 
72
 * @author  phrebejk, mkuchtiak
 
73
 */
 
74
public class BrowseFolders extends javax.swing.JPanel implements ExplorerManager.Provider {
 
75
    
 
76
    private ExplorerManager manager;
 
77
    private SourceGroup[] folders;
 
78
    
 
79
    private static JScrollPane SAMPLE_SCROLL_PANE = new JScrollPane();
 
80
    
 
81
    /** Creates new form BrowseFolders */
 
82
    public BrowseFolders( SourceGroup[] folders) {
 
83
        initComponents();
 
84
        this.folders = folders;
 
85
        manager = new ExplorerManager();        
 
86
        AbstractNode rootNode = new AbstractNode( new SourceGroupsChildren( folders ) );
 
87
        manager.setRootContext( rootNode );
 
88
        
 
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 );
 
95
    }
 
96
        
 
97
    // ExplorerManager.Provider implementation ---------------------------------
 
98
    
 
99
    public ExplorerManager getExplorerManager() {
 
100
        return manager;
 
101
    }
 
102
        
 
103
    
 
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.
 
108
     */
 
109
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 
110
    private void initComponents() {
 
111
        java.awt.GridBagConstraints gridBagConstraints;
 
112
 
 
113
        jLabel1 = new javax.swing.JLabel();
 
114
        folderPanel = new javax.swing.JPanel();
 
115
 
 
116
        setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
 
117
        setLayout(new java.awt.GridBagLayout());
 
118
 
 
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);
 
125
 
 
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);
 
134
 
 
135
        getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BrowseFolders.class, "ACSD_browse_dialog")); // NOI18N
 
136
    }// </editor-fold>//GEN-END:initComponents
 
137
    
 
138
    
 
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
 
143
        
 
144
    public static FileObject showDialog( SourceGroup[] folders ) {
 
145
        
 
146
        BrowseFolders bf = new BrowseFolders( folders );
 
147
        
 
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") ), 
 
153
        };
 
154
                
 
155
        OptionsListener optionsListener = new OptionsListener( bf );
 
156
        
 
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"));
 
163
 
 
164
        DialogDescriptor dialogDescriptor = new DialogDescriptor( 
 
165
            bf,                                     // innerPane
 
166
            NbBundle.getMessage(BrowseFolders.class, "LBL_BrowseFiles"), // displayName
 
167
            true,                                   // modal
 
168
            options,                                // options
 
169
            options[ 0 ],                           // initial value
 
170
            DialogDescriptor.BOTTOM_ALIGN,          // options align
 
171
            null,                                   // helpCtx
 
172
            null );                                 // listener 
 
173
 
 
174
        dialogDescriptor.setClosingOptions( new Object[] { options[ 0 ], options[ 1 ] } );
 
175
            
 
176
        Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
 
177
        dialog.setVisible(true);
 
178
        
 
179
        return optionsListener.getResult();
 
180
                
 
181
    }
 
182
    
 
183
    
 
184
    // Innerclasses ------------------------------------------------------------
 
185
    
 
186
    /** Children to be used to show FileObjects from given SourceGroups
 
187
     */
 
188
         
 
189
    private final class SourceGroupsChildren extends Children.Keys {
 
190
        
 
191
        private SourceGroup[] groups;
 
192
        private SourceGroup group;
 
193
        private FileObject fo;
 
194
        
 
195
        public SourceGroupsChildren( SourceGroup[] groups ) {
 
196
            this.groups = groups;
 
197
        }
 
198
        
 
199
        public SourceGroupsChildren( FileObject fo, SourceGroup group ) {            
 
200
            this.fo = fo;
 
201
            this.group = group;
 
202
        }
 
203
        
 
204
        protected void addNotify() {
 
205
            super.addNotify();
 
206
            setKeys( getKeys() );
 
207
        }
 
208
        
 
209
        protected void removeNotify() {
 
210
            setKeys( Collections.EMPTY_SET );
 
211
            super.removeNotify();
 
212
        }
 
213
        
 
214
        protected Node[] createNodes(Object key) {
 
215
            
 
216
            FileObject fObj = null;
 
217
            SourceGroup group = null;
 
218
            boolean isFile=false;
 
219
            
 
220
            if ( key instanceof SourceGroup ) {
 
221
                fObj = ((SourceGroup)key).getRootFolder();
 
222
                group = (SourceGroup)key;
 
223
            }
 
224
            else if ( key instanceof Key ) {
 
225
                fObj = ((Key)key).folder;
 
226
                group = ((Key)key).group;
 
227
                if (!fObj.isFolder()) isFile=true;
 
228
            }
 
229
 
 
230
            try {
 
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() );
 
236
                }
 
237
            
 
238
                return new Node[] { fn };            
 
239
            }
 
240
            catch ( DataObjectNotFoundException e ) {
 
241
                return null;
 
242
            }
 
243
        }
 
244
                
 
245
        private Collection getKeys() {
 
246
                        
 
247
            if ( groups != null ) {
 
248
                return Arrays.asList( groups );                
 
249
            }
 
250
            else {
 
251
                FileObject files[] = fo.getChildren();
 
252
                Arrays.sort(files,new BrowseFolders.FileObjectComparator());
 
253
                ArrayList children = new ArrayList( files.length );
 
254
                /*
 
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 ) );
 
259
                        }
 
260
                    }*/
 
261
                //else {
 
262
                    // add folders
 
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 ) );
 
265
                    }
 
266
                    // add files
 
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 ) );
 
269
                    }
 
270
                //}
 
271
                
 
272
                return children;
 
273
            }
 
274
                        
 
275
        }
 
276
        
 
277
        private class Key {
 
278
            
 
279
            private FileObject folder;
 
280
            private SourceGroup group;
 
281
            
 
282
            private Key ( FileObject folder, SourceGroup group ) {
 
283
                this.folder = folder;
 
284
                this.group = group;
 
285
            }
 
286
            
 
287
            
 
288
        }
 
289
        
 
290
    }
 
291
 
 
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());
 
297
        }
 
298
    }
 
299
    
 
300
    private static final class OptionsListener implements ActionListener {
 
301
    
 
302
        public static final String COMMAND_SELECT = "SELECT"; //NOI18N
 
303
        public static final String COMMAND_CANCEL = "CANCEL"; //NOI18N
 
304
            
 
305
        private BrowseFolders browsePanel;
 
306
        
 
307
        private FileObject result;
 
308
        //private Class target;
 
309
        
 
310
        public OptionsListener( BrowseFolders browsePanel ) {
 
311
            this.browsePanel = browsePanel;
 
312
        }
 
313
        
 
314
        public void actionPerformed( ActionEvent e ) {
 
315
            String command = e.getActionCommand();
 
316
 
 
317
            if ( COMMAND_SELECT.equals( command ) ) {
 
318
                Node selection[] = browsePanel.getExplorerManager().getSelectedNodes();
 
319
                
 
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();
 
324
                    //}
 
325
                    /*
 
326
                    if ( dobj != null ) {
 
327
                        FileObject fo = dobj.getPrimaryFile();
 
328
                        if ( fo.isFolder() ) {
 
329
                            result = fo;
 
330
                        }
 
331
                    }
 
332
                    */
 
333
                }
 
334
                
 
335
                
 
336
            }
 
337
        }
 
338
        
 
339
        public FileObject getResult() {
 
340
            return result;
 
341
        }
 
342
    }
 
343
    
 
344
    class SimpleFilterNode extends FilterNode {
 
345
        
 
346
        public SimpleFilterNode(org.openide.nodes.Node node, org.openide.nodes.Children children) {
 
347
            super(node, children);
 
348
            
 
349
        }
 
350
        
 
351
        public org.openide.util.actions.SystemAction[] getActions() {
 
352
            return new org.openide.util.actions.SystemAction[]{};
 
353
        }
 
354
        public org.openide.util.actions.SystemAction getDefaultAction() {
 
355
           return null;
 
356
        }
 
357
    }
 
358
    
 
359
}