KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.ui.wizards;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.DefaultListModel JavaDoc;
27 import javax.swing.DefaultListCellRenderer JavaDoc;
28 import javax.swing.JFileChooser JavaDoc;
29 import javax.swing.JList JavaDoc;
30 import javax.swing.event.ListSelectionListener JavaDoc;
31 import javax.swing.event.ListSelectionEvent JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.SourceGroup;
34 import org.netbeans.api.project.Sources;
35 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientSourceRootsUi;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.netbeans.api.project.FileOwnerQuery;
39 import org.netbeans.api.project.Project;
40
41 /**
42  * List of source/test roots
43  * @author tzezula
44  */

45 public final class FolderList extends javax.swing.JPanel JavaDoc {
46     
47     public static final String JavaDoc PROP_FILES = "files"; //NOI18N
48
public static final String JavaDoc PROP_LAST_USED_DIR = "lastUsedDir"; //NOI18N
49

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

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

169         removeButton.setText(org.openide.util.NbBundle.getMessage(FolderList.class, "CTL_RemoveFolder")); // NOI18N
170
removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
171             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
172                 removeButtonActionPerformed(evt);
173             }
174         });
175         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
176         gridBagConstraints.gridx = 1;
177         gridBagConstraints.gridy = 2;
178         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
179         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
180         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
181         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
182         add(removeButton, gridBagConstraints);
183         removeButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FolderList.class, "AD_RemoveFolder")); // NOI18N
184
}// </editor-fold>//GEN-END:initComponents
185

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

194     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
195
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
196         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
197         chooser.setDialogTitle(this.fcMessage);
198         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
199         chooser.setMultiSelectionEnabled(true);
200         if (this.lastUsedFolder != null && this.lastUsedFolder.isDirectory()) {
201             chooser.setCurrentDirectory(this.lastUsedFolder);
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<File JavaDoc> invalidRoots = new HashSet JavaDoc<File 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                 } else {
217                     int pos = model.indexOf(normalizedFile);
218                     if (pos == -1) {
219                         model.addElement(normalizedFile);
220                         indecesToSelect[i] = index;
221                     } else {
222                         indecesToSelect[i] = pos;
223                     }
224                 }
225             }
226             this.roots.setSelectedIndices(indecesToSelect);
227             this.firePropertyChange(PROP_FILES, null, null);
228             File JavaDoc cd = chooser.getCurrentDirectory();
229             if (cd != null) {
230                 this.setLastUsedDir(FileUtil.normalizeFile(cd));
231             }
232             if (invalidRoots.size()>0) {
233                 AppClientSourceRootsUi.showIllegalRootsDialog(invalidRoots);
234             }
235         }
236     }//GEN-LAST:event_addButtonActionPerformed
237

238     
239     static boolean isValidRoot(File JavaDoc file, File JavaDoc[] relatedRoots, File JavaDoc projectFolder) {
240         Project p;
241         if ((p = FileOwnerQuery.getOwner(file.toURI()))!=null
242                 && !file.getAbsolutePath().startsWith(projectFolder.getAbsolutePath()+File.separatorChar)) {
243             final Sources sources = (Sources) p.getLookup().lookup(Sources.class);
244             if (sources == null) {
245                 return false;
246             }
247             final SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
248             final SourceGroup[] javaGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
249             final SourceGroup[] groups = new SourceGroup [sourceGroups.length + javaGroups.length];
250             System.arraycopy(sourceGroups,0,groups,0,sourceGroups.length);
251             System.arraycopy(javaGroups,0,groups,sourceGroups.length,javaGroups.length);
252             final FileObject projectDirectory = p.getProjectDirectory();
253             final FileObject fileObject = FileUtil.toFileObject(file);
254             if (projectDirectory == null || fileObject == null) {
255                 return false;
256             }
257             for (int i = 0; i< groups.length; i++) {
258                 final FileObject sgRoot = groups[i].getRootFolder();
259                 if (fileObject.equals(sgRoot)) {
260                     return false;
261                 }
262                 if (!projectDirectory.equals(sgRoot) && FileUtil.isParentOf(sgRoot, fileObject)) {
263                     return false;
264                 }
265             }
266             return true;
267         } else if (contains(file, relatedRoots)) {
268             return false;
269         }
270         return true;
271     }
272     
273     private static boolean contains(File JavaDoc folder, File JavaDoc[] roots) {
274         String JavaDoc path = folder.getAbsolutePath();
275         for (int i=0; i<roots.length; i++) {
276             String JavaDoc rootPath = roots[i].getAbsolutePath();
277             if (rootPath.equals(path) || path.startsWith(rootPath + File.separatorChar)) {
278                 return true;
279             }
280         }
281         return false;
282     }
283     
284     private static class Renderer extends DefaultListCellRenderer JavaDoc {
285         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
286             File JavaDoc f = (File JavaDoc) value;
287             Project p = FileOwnerQuery.getOwner(f.toURI());
288             String JavaDoc message = f.getAbsolutePath();
289             Component JavaDoc result = super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus);
290             return result;
291         }
292         
293     }
294     
295     // Variables declaration - do not modify//GEN-BEGIN:variables
296
private javax.swing.JButton JavaDoc addButton;
297     private javax.swing.JLabel JavaDoc jLabel1;
298     private javax.swing.JScrollPane JavaDoc jScrollPane1;
299     private javax.swing.JButton JavaDoc removeButton;
300     private javax.swing.JList JavaDoc roots;
301     // End of variables declaration//GEN-END:variables
302

303 }
304
Popular Tags