KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.ejbjarproject.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.api.project.Project;
33 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarSourceRootsUi;
34 import org.openide.filesystems.FileUtil;
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 setProjectFolder (File JavaDoc projectFolder) {
77         this.projectFolder = projectFolder;
78     }
79
80     public void setRelatedFolderList (FolderList relatedFolderList) {
81         this.relatedFolderList = relatedFolderList;
82     }
83
84     public File JavaDoc[] getFiles () {
85         Object JavaDoc[] files = ((DefaultListModel JavaDoc)this.roots.getModel()).toArray();
86         File JavaDoc[] result = new File JavaDoc[files.length];
87         System.arraycopy(files, 0, result, 0, files.length);
88         return result;
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
143         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
144         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
145         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
146         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
147         gridBagConstraints.weightx = 1.0;
148         gridBagConstraints.weighty = 1.0;
149         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
150         add(jScrollPane1, gridBagConstraints);
151
152         addButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("CTL_AddFolder"));
153         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
154             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
155                 addButtonActionPerformed(evt);
156             }
157         });
158
159         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
160         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
161         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
162         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
163         add(addButton, gridBagConstraints);
164
165         removeButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("CTL_RemoveFolder"));
166         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
167             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
168                 removeButtonActionPerformed(evt);
169             }
170         });
171
172         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
173         gridBagConstraints.gridx = 1;
174         gridBagConstraints.gridy = 2;
175         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
176         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
177         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
178         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
179         add(removeButton, gridBagConstraints);
180
181     }
182     // </editor-fold>//GEN-END:initComponents
183

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

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

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

278 }
279
Popular Tags