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

« back to all changes in this revision

Viewing changes to j2ee/ejbjarproject/src/org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/FolderList.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.ejbjarproject.ui.wizards;
 
43
import java.awt.Component;
 
44
import java.io.File;
 
45
import java.util.HashSet;
 
46
import java.util.Set;
 
47
import javax.swing.DefaultListModel;
 
48
import javax.swing.DefaultListCellRenderer;
 
49
import javax.swing.JFileChooser;
 
50
import javax.swing.JList;
 
51
import javax.swing.event.ListSelectionListener;
 
52
import javax.swing.event.ListSelectionEvent;
 
53
import org.netbeans.api.project.FileOwnerQuery;
 
54
import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarSourceRootsUi;
 
55
import org.openide.filesystems.FileUtil;
 
56
 
 
57
 
 
58
/**
 
59
 * List of source/test roots
 
60
 * @author tzezula
 
61
 */
 
62
public final class FolderList extends javax.swing.JPanel {
 
63
 
 
64
    public static final String PROP_FILES = "files";    //NOI18N
 
65
    public static final String PROP_LAST_USED_DIR = "lastUsedDir";  //NOI18N
 
66
 
 
67
    private String fcMessage;
 
68
    private File projectFolder;
 
69
    private File lastUsedFolder;
 
70
    private FolderList relatedFolderList;
 
71
 
 
72
    /** Creates new form FolderList */
 
73
    public FolderList (String label, char mnemonic, String accessibleDesc, String fcMessage,
 
74
                       char addButtonMnemonic, String addButtonAccessibleDesc,
 
75
                       char removeButtonMnemonic,String removeButtonAccessibleDesc) {
 
76
        this.fcMessage = fcMessage;
 
77
        initComponents();
 
78
        this.jLabel1.setText(label);
 
79
        this.jLabel1.setDisplayedMnemonic(mnemonic);
 
80
        this.roots.getAccessibleContext().setAccessibleName(accessibleDesc);
 
81
        this.roots.setCellRenderer(new Renderer());
 
82
        this.roots.setModel (new DefaultListModel());
 
83
        this.roots.addListSelectionListener(new ListSelectionListener() {
 
84
            public void valueChanged(ListSelectionEvent e) {
 
85
                if (!e.getValueIsAdjusting()) {
 
86
                    removeButton.setEnabled(roots.getSelectedIndices().length != 0);
 
87
                }
 
88
            }
 
89
        });
 
90
        this.addButton.getAccessibleContext().setAccessibleDescription(addButtonAccessibleDesc);
 
91
        this.addButton.setMnemonic (addButtonMnemonic);        
 
92
        this.removeButton.getAccessibleContext().setAccessibleDescription(removeButtonAccessibleDesc);
 
93
        this.removeButton.setMnemonic (removeButtonMnemonic);
 
94
        this.removeButton.setEnabled(false);
 
95
    }
 
96
 
 
97
    public void setProjectFolder (File projectFolder) {
 
98
        this.projectFolder = projectFolder;
 
99
    }
 
100
 
 
101
    public void setRelatedFolderList (FolderList relatedFolderList) {
 
102
        this.relatedFolderList = relatedFolderList;
 
103
    }    
 
104
 
 
105
    public File[] getFiles () {
 
106
        Object[] files = ((DefaultListModel)this.roots.getModel()).toArray();
 
107
        File[] result = new File[files.length];
 
108
        System.arraycopy(files, 0, result, 0, files.length);
 
109
        return result;
 
110
    }
 
111
 
 
112
    public void setFiles (File[] files) {
 
113
        DefaultListModel model = ((DefaultListModel)this.roots.getModel());
 
114
        model.clear();
 
115
        for (int i=0; i<files.length; i++) {
 
116
            model.addElement (files[i]);
 
117
        }
 
118
        if (files.length>0) {
 
119
            this.roots.setSelectedIndex(0);
 
120
        }
 
121
    }
 
122
 
 
123
    public void setLastUsedDir (File lastUsedDir) {
 
124
        if (this.lastUsedFolder == null ? lastUsedDir != null : !this.lastUsedFolder.equals(lastUsedDir)) {
 
125
            File oldValue = this.lastUsedFolder;
 
126
            this.lastUsedFolder = lastUsedDir;
 
127
            this.firePropertyChange(PROP_LAST_USED_DIR, oldValue, this.lastUsedFolder);
 
128
        }
 
129
    }
 
130
    
 
131
    public File getLastUsedDir () {
 
132
        return this.lastUsedFolder;
 
133
    }
 
134
 
 
135
    /** This method is called from within the constructor to
 
136
     * initialize the form.
 
137
     * WARNING: Do NOT modify this code. The content of this method is
 
138
     * always regenerated by the Form Editor.
 
139
     */
 
140
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 
141
    private void initComponents() {
 
142
        java.awt.GridBagConstraints gridBagConstraints;
 
143
 
 
144
        jLabel1 = new javax.swing.JLabel();
 
145
        jScrollPane1 = new javax.swing.JScrollPane();
 
146
        roots = new javax.swing.JList();
 
147
        addButton = new javax.swing.JButton();
 
148
        removeButton = new javax.swing.JButton();
 
149
 
 
150
        setLayout(new java.awt.GridBagLayout());
 
151
 
 
152
        jLabel1.setLabelFor(roots);
 
153
        jLabel1.setText("jLabel1");
 
154
        gridBagConstraints = new java.awt.GridBagConstraints();
 
155
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
156
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
157
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
 
158
        gridBagConstraints.weightx = 1.0;
 
159
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
 
160
        add(jLabel1, gridBagConstraints);
 
161
 
 
162
        jScrollPane1.setViewportView(roots);
 
163
 
 
164
        gridBagConstraints = new java.awt.GridBagConstraints();
 
165
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
 
166
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
 
167
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
 
168
        gridBagConstraints.weightx = 1.0;
 
169
        gridBagConstraints.weighty = 1.0;
 
170
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
 
171
        add(jScrollPane1, gridBagConstraints);
 
172
 
 
173
        addButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("CTL_AddFolder"));
 
174
        addButton.addActionListener(new java.awt.event.ActionListener() {
 
175
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
176
                addButtonActionPerformed(evt);
 
177
            }
 
178
        });
 
179
 
 
180
        gridBagConstraints = new java.awt.GridBagConstraints();
 
181
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
182
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
183
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
 
184
        add(addButton, gridBagConstraints);
 
185
 
 
186
        removeButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("CTL_RemoveFolder"));
 
187
        removeButton.addActionListener(new java.awt.event.ActionListener() {
 
188
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
189
                removeButtonActionPerformed(evt);
 
190
            }
 
191
        });
 
192
 
 
193
        gridBagConstraints = new java.awt.GridBagConstraints();
 
194
        gridBagConstraints.gridx = 1;
 
195
        gridBagConstraints.gridy = 2;
 
196
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
197
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
198
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
 
199
        gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
 
200
        add(removeButton, gridBagConstraints);
 
201
 
 
202
    }
 
203
    // </editor-fold>//GEN-END:initComponents
 
204
 
 
205
    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
 
206
        Object[] selection = this.roots.getSelectedValues ();
 
207
        for (int i=0; i<selection.length; i++) {
 
208
            ((DefaultListModel)this.roots.getModel()).removeElement (selection[i]);
 
209
        }
 
210
        this.firePropertyChange(PROP_FILES, null, null);
 
211
    }//GEN-LAST:event_removeButtonActionPerformed
 
212
 
 
213
    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
 
214
        JFileChooser chooser = new JFileChooser();
 
215
        FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
 
216
        chooser.setDialogTitle(this.fcMessage);
 
217
        chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
 
218
        chooser.setMultiSelectionEnabled(true);
 
219
        if (this.lastUsedFolder != null && this.lastUsedFolder.isDirectory()) {
 
220
            chooser.setCurrentDirectory (this.lastUsedFolder);
 
221
        }        
 
222
        else if (this.projectFolder != null && this.projectFolder.isDirectory()) {
 
223
            chooser.setCurrentDirectory (this.projectFolder);            
 
224
        }                        
 
225
        if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
 
226
            File[] files = chooser.getSelectedFiles();
 
227
            int[] indecesToSelect = new int[files.length];
 
228
            DefaultListModel model = (DefaultListModel)this.roots.getModel();
 
229
            Set invalidRoots = new HashSet ();
 
230
            File[] relatedFolders = this.relatedFolderList == null ? 
 
231
                new File[0] : this.relatedFolderList.getFiles();
 
232
            for (int i=0, index=model.size(); i<files.length; i++, index++) {
 
233
                File normalizedFile = FileUtil.normalizeFile(files[i]);
 
234
                if (!isValidRoot(normalizedFile, relatedFolders, this.projectFolder)) {
 
235
                    invalidRoots.add (normalizedFile);
 
236
                }
 
237
                else {
 
238
                    int pos = model.indexOf (normalizedFile);                
 
239
                    if (pos == -1) {
 
240
                        model.addElement (normalizedFile);
 
241
                        indecesToSelect[i] = index;
 
242
                    }
 
243
                    else {
 
244
                        indecesToSelect[i] = pos;
 
245
                    }
 
246
                }
 
247
            }
 
248
            this.roots.setSelectedIndices(indecesToSelect);
 
249
            this.firePropertyChange(PROP_FILES, null, null);
 
250
            File cd = chooser.getCurrentDirectory();
 
251
            if (cd != null) {
 
252
                this.setLastUsedDir(FileUtil.normalizeFile(cd));
 
253
            }
 
254
            if (invalidRoots.size()>0) {
 
255
                EjbJarSourceRootsUi.showIllegalRootsDialog(invalidRoots);
 
256
            }
 
257
        }
 
258
    }//GEN-LAST:event_addButtonActionPerformed
 
259
    
 
260
    static boolean isValidRoot (File file, File[] relatedRoots, File projectFolder) {        
 
261
        if (FileOwnerQuery.getOwner(file.toURI())!=null 
 
262
            && !file.getAbsolutePath().startsWith(projectFolder.getAbsolutePath()+File.separatorChar)) {
 
263
            return false;
 
264
        }                
 
265
        else if (contains (file, relatedRoots)) {
 
266
            return false;
 
267
        }
 
268
        return true;
 
269
    }
 
270
    
 
271
    private static boolean contains (File folder, File[] roots) {
 
272
        String path = folder.getAbsolutePath ();
 
273
        for (int i=0; i<roots.length; i++) {
 
274
            String rootPath = roots[i].getAbsolutePath();
 
275
            if (rootPath.equals (path) || path.startsWith (rootPath + File.separatorChar)) {
 
276
                return true;
 
277
            }
 
278
        }
 
279
        return false;
 
280
    }
 
281
 
 
282
    private static class Renderer extends DefaultListCellRenderer {
 
283
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 
284
            File f = (File) value;
 
285
            String message = f.getAbsolutePath();
 
286
            Component result = super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus);
 
287
            return result;
 
288
        }        
 
289
    }
 
290
    
 
291
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
292
    private javax.swing.JButton addButton;
 
293
    private javax.swing.JLabel jLabel1;
 
294
    private javax.swing.JScrollPane jScrollPane1;
 
295
    private javax.swing.JButton removeButton;
 
296
    private javax.swing.JList roots;
 
297
    // End of variables declaration//GEN-END:variables
 
298
    
 
299
}