KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > wizards > FolderList


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.web.project.ui.wizards;
21 import java.awt.Component JavaDoc;
22 import java.io.File JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.swing.DefaultListModel JavaDoc;
26 import javax.swing.DefaultListCellRenderer JavaDoc;
27 import javax.swing.JFileChooser JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.event.ListSelectionListener JavaDoc;
30 import javax.swing.event.ListSelectionEvent JavaDoc;
31 import org.netbeans.api.project.FileOwnerQuery;
32 import org.netbeans.modules.web.project.ui.customizer.WebSourceRootsUi;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.NbBundle;
35
36
37 /**
38  * List of source/test roots
39  * @author tzezula
40  */

41 public final class FolderList extends javax.swing.JPanel JavaDoc {
42
43     public static final String JavaDoc PROP_FILES = "files"; //NOI18N
44
public static final String JavaDoc PROP_LAST_USED_DIR = "lastUsedDir"; //NOI18N
45

46     private String JavaDoc fcMessage;
47     private File JavaDoc projectFolder;
48     private File JavaDoc lastUsedFolder;
49     private FolderList relatedFolderList;
50
51     /** Creates new form FolderList */
52     public FolderList (String JavaDoc label, char mnemonic, String JavaDoc accessibleDesc, String JavaDoc fcMessage,
53                        char addButtonMnemonic, String JavaDoc addButtonAccessibleDesc,
54                        char removeButtonMnemonic,String JavaDoc removeButtonAccessibleDesc) {
55         this.fcMessage = fcMessage;
56         initComponents();
57         this.jLabel1.setText(label);
58         this.jLabel1.setDisplayedMnemonic(mnemonic);
59         this.roots.getAccessibleContext().setAccessibleName(accessibleDesc);
60         this.roots.setCellRenderer(new Renderer JavaDoc());
61         this.roots.setModel (new DefaultListModel JavaDoc());
62         this.roots.addListSelectionListener(new ListSelectionListener JavaDoc() {
63             public void valueChanged(ListSelectionEvent JavaDoc e) {
64                 if (!e.getValueIsAdjusting()) {
65                     removeButton.setEnabled(roots.getSelectedIndices().length != 0);
66                 }
67             }
68         });
69         this.addButton.getAccessibleContext().setAccessibleDescription(addButtonAccessibleDesc);
70         this.addButton.setMnemonic (addButtonMnemonic);
71         this.removeButton.getAccessibleContext().setAccessibleDescription(removeButtonAccessibleDesc);
72         this.removeButton.setMnemonic (removeButtonMnemonic);
73         this.removeButton.setEnabled(false);
74     }
75     
76     public void setRelatedFolderList (FolderList relatedFolderList) {
77         this.relatedFolderList = relatedFolderList;
78     }
79
80     public File JavaDoc[] getFiles () {
81         Object JavaDoc[] files = ((DefaultListModel JavaDoc)this.roots.getModel()).toArray();
82         File JavaDoc[] result = new File JavaDoc[files.length];
83         System.arraycopy(files, 0, result, 0, files.length);
84         return result;
85     }
86
87     public void setProjectFolder (File JavaDoc projectFolder) {
88         this.projectFolder = projectFolder;
89     }
90
91     public void setFiles (File JavaDoc[] files) {
92         DefaultListModel JavaDoc model = ((DefaultListModel JavaDoc)this.roots.getModel());
93         model.clear();
94         for (int i=0; i<files.length; i++) {
95             model.addElement (files[i]);
96         }
97         if (files.length>0) {
98             this.roots.setSelectedIndex(0);
99         }
100     }
101
102     public void setLastUsedDir (File JavaDoc lastUsedDir) {
103         if (this.lastUsedFolder == null ? lastUsedDir != null : !this.lastUsedFolder.equals(lastUsedDir)) {
104             File JavaDoc oldValue = this.lastUsedFolder;
105             this.lastUsedFolder = lastUsedDir;
106             this.firePropertyChange(PROP_LAST_USED_DIR, oldValue, this.lastUsedFolder);
107         }
108     }
109     
110     public File JavaDoc getLastUsedDir () {
111         return this.lastUsedFolder;
112     }
113
114     /** This method is called from within the constructor to
115      * initialize the form.
116      * WARNING: Do NOT modify this code. The content of this method is
117      * always regenerated by the Form Editor.
118      */

119     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
120
private void initComponents() {
121         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
122
123         jLabel1 = new javax.swing.JLabel JavaDoc();
124         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
125         roots = new javax.swing.JList JavaDoc();
126         addButton = new javax.swing.JButton JavaDoc();
127         removeButton = new javax.swing.JButton JavaDoc();
128
129         setLayout(new java.awt.GridBagLayout JavaDoc());
130
131         jLabel1.setLabelFor(roots);
132         jLabel1.setText("jLabel1");
133         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
134         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
135         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
136         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
137         gridBagConstraints.weightx = 1.0;
138         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
139         add(jLabel1, gridBagConstraints);
140
141         jScrollPane1.setViewportView(roots);
142         roots.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FolderList.class, "ACSD_FolderList"));
143
144         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
146         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
147         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
148         gridBagConstraints.weightx = 1.0;
149         gridBagConstraints.weighty = 1.0;
150         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
151         add(jScrollPane1, gridBagConstraints);
152
153         addButton.setText(NbBundle.getMessage(FolderList.class, "CTL_AddFolder"));
154         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
155             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
156                 addButtonActionPerformed(evt);
157             }
158         });
159
160         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
161         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
162         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
163         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
164         add(addButton, gridBagConstraints);
165
166         removeButton.setText(NbBundle.getMessage(FolderList.class, "CTL_RemoveFolder"));
167         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
168             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
169                 removeButtonActionPerformed(evt);
170             }
171         });
172
173         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
174         gridBagConstraints.gridx = 1;
175         gridBagConstraints.gridy = 2;
176         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
177         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
178         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
179         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
180         add(removeButton, gridBagConstraints);
181
182     }
183     // </editor-fold>//GEN-END:initComponents
184

185     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
186
Object JavaDoc[] selection = this.roots.getSelectedValues ();
187         for (int i=0; i<selection.length; i++) {
188             ((DefaultListModel JavaDoc)this.roots.getModel()).removeElement (selection[i]);
189         }
190         this.firePropertyChange(PROP_FILES, null, null);
191     }//GEN-LAST:event_removeButtonActionPerformed
192

193     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
194
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
195         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
196         chooser.setDialogTitle(this.fcMessage);
197         chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
198         chooser.setMultiSelectionEnabled(true);
199         if (this.lastUsedFolder != null && this.lastUsedFolder.isDirectory()) {
200             chooser.setCurrentDirectory (this.lastUsedFolder);
201         }
202         else if (this.projectFolder != null && this.projectFolder.isDirectory()) {
203             chooser.setCurrentDirectory (this.projectFolder);
204         }
205         if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
206             File JavaDoc[] files = chooser.getSelectedFiles();
207             int[] indecesToSelect = new int[files.length];
208             DefaultListModel JavaDoc model = (DefaultListModel JavaDoc)this.roots.getModel();
209             Set JavaDoc invalidRoots = new HashSet JavaDoc ();
210             File JavaDoc[] relatedFolders = this.relatedFolderList == null ?
211                 new File JavaDoc[0] : this.relatedFolderList.getFiles();
212             for (int i=0, index=model.size(); i<files.length; i++, index++) {
213                 File JavaDoc normalizedFile = FileUtil.normalizeFile(files[i]);
214                 if (!isValidRoot(normalizedFile, relatedFolders, this.projectFolder)) {
215                     invalidRoots.add (normalizedFile);
216                 }
217                 else {
218                     int pos = model.indexOf (normalizedFile);
219                     if (pos == -1) {
220                         model.addElement (normalizedFile);
221                         indecesToSelect[i] = index;
222                     }
223                     else {
224                         indecesToSelect[i] = pos;
225                     }
226                 }
227             }
228             this.roots.setSelectedIndices(indecesToSelect);
229             this.firePropertyChange(PROP_FILES, null, null);
230             File JavaDoc cd = chooser.getCurrentDirectory();
231             if (cd != null) {
232                 this.setLastUsedDir(FileUtil.normalizeFile(cd));
233             }
234             if (invalidRoots.size()>0) {
235                 WebSourceRootsUi.showIllegalRootsDialog(invalidRoots);
236             }
237         }
238     }//GEN-LAST:event_addButtonActionPerformed
239

240     static boolean isValidRoot (File JavaDoc file, File JavaDoc[] relatedRoots, File JavaDoc projectFolder) {
241         if (FileOwnerQuery.getOwner(file.toURI())!=null
242             && !file.getAbsolutePath().startsWith(projectFolder.getAbsolutePath()+File.separatorChar)) {
243             return false;
244         }
245         else if (contains (file, relatedRoots)) {
246             return false;
247         }
248         return true;
249     }
250     
251     private static boolean contains (File JavaDoc folder, File JavaDoc[] roots) {
252         String JavaDoc path = folder.getAbsolutePath ();
253         for (int i=0; i<roots.length; i++) {
254             String JavaDoc rootPath = roots[i].getAbsolutePath();
255             if (rootPath.equals (path) || path.startsWith (rootPath + File.separatorChar)) {
256                 return true;
257             }
258         }
259         return false;
260     }
261
262     private static class Renderer extends DefaultListCellRenderer JavaDoc {
263         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
264             File JavaDoc f = (File JavaDoc) value;
265             String JavaDoc message = f.getAbsolutePath();
266             Component JavaDoc result = super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus);
267             return result;
268         }
269     }
270     
271     // Variables declaration - do not modify//GEN-BEGIN:variables
272
private javax.swing.JButton JavaDoc addButton;
273     private javax.swing.JLabel JavaDoc jLabel1;
274     private javax.swing.JScrollPane JavaDoc jScrollPane1;
275     private javax.swing.JButton JavaDoc removeButton;
276     private javax.swing.JList JavaDoc roots;
277     // End of variables declaration//GEN-END:variables
278

279 }
280
Popular Tags