1 19 20 package org.netbeans.modules.j2ee.clientproject.ui.wizards; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import javax.swing.DefaultListModel ; 27 import javax.swing.DefaultListCellRenderer ; 28 import javax.swing.JFileChooser ; 29 import javax.swing.JList ; 30 import javax.swing.event.ListSelectionListener ; 31 import javax.swing.event.ListSelectionEvent ; 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 45 public final class FolderList extends javax.swing.JPanel { 46 47 public static final String PROP_FILES = "files"; public static final String PROP_LAST_USED_DIR = "lastUsedDir"; 50 private String fcMessage; 51 private File projectFolder; 52 private File lastUsedFolder; 53 private FolderList relatedFolderList; 54 55 56 public FolderList(String label, char mnemonic, String accessibleDesc, String fcMessage, 57 char addButtonMnemonic, String addButtonAccessibleDesc, 58 char removeButtonMnemonic,String 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 ()); 65 this.roots.setModel(new DefaultListModel ()); 66 this.roots.addListSelectionListener(new ListSelectionListener () { 67 public void valueChanged(ListSelectionEvent 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 projectFolder) { 81 this.projectFolder = projectFolder; 82 } 83 84 public void setRelatedFolderList(FolderList relatedFolderList) { 85 this.relatedFolderList = relatedFolderList; 86 } 87 88 public File [] getFiles() { 89 Object [] files = ((DefaultListModel )this.roots.getModel()).toArray(); 90 File [] result = new File [files.length]; 91 System.arraycopy(files, 0, result, 0, files.length); 92 return result; 93 } 94 95 public void setFiles(File [] files) { 96 DefaultListModel model = ((DefaultListModel )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 lastUsedDir) { 107 if (this.lastUsedFolder == null ? lastUsedDir != null : !this.lastUsedFolder.equals(lastUsedDir)) { 108 File oldValue = this.lastUsedFolder; 109 this.lastUsedFolder = lastUsedDir; 110 this.firePropertyChange(PROP_LAST_USED_DIR, oldValue, this.lastUsedFolder); 111 } 112 } 113 114 public File getLastUsedDir() { 115 return this.lastUsedFolder; 116 } 117 118 123 private void initComponents() { 125 java.awt.GridBagConstraints gridBagConstraints; 126 127 jLabel1 = new javax.swing.JLabel (); 128 jScrollPane1 = new javax.swing.JScrollPane (); 129 roots = new javax.swing.JList (); 130 addButton = new javax.swing.JButton (); 131 removeButton = new javax.swing.JButton (); 132 133 setLayout(new java.awt.GridBagLayout ()); 134 135 jLabel1.setLabelFor(roots); 136 jLabel1.setText("jLabel1"); 137 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 0, 6, 0); 143 add(jLabel1, gridBagConstraints); 144 145 jScrollPane1.setViewportView(roots); 146 147 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 0, 0, 12); 154 add(jScrollPane1, gridBagConstraints); 155 156 addButton.setText(org.openide.util.NbBundle.getMessage(FolderList.class, "CTL_AddFolder")); addButton.addActionListener(new java.awt.event.ActionListener () { 158 public void actionPerformed(java.awt.event.ActionEvent evt) { 159 addButtonActionPerformed(evt); 160 } 161 }); 162 gridBagConstraints = new java.awt.GridBagConstraints (); 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")); 169 removeButton.setText(org.openide.util.NbBundle.getMessage(FolderList.class, "CTL_RemoveFolder")); removeButton.addActionListener(new java.awt.event.ActionListener () { 171 public void actionPerformed(java.awt.event.ActionEvent evt) { 172 removeButtonActionPerformed(evt); 173 } 174 }); 175 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (12, 0, 0, 0); 182 add(removeButton, gridBagConstraints); 183 removeButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(FolderList.class, "AD_RemoveFolder")); } 186 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] selection = this.roots.getSelectedValues(); 188 for (int i=0; i<selection.length; i++) { 189 ((DefaultListModel )this.roots.getModel()).removeElement(selection[i]); 190 } 191 this.firePropertyChange(PROP_FILES, null, null); 192 } 194 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 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 [] files = chooser.getSelectedFiles(); 207 int[] indecesToSelect = new int[files.length]; 208 DefaultListModel model = (DefaultListModel )this.roots.getModel(); 209 Set <File > invalidRoots = new HashSet <File >(); 210 File [] relatedFolders = this.relatedFolderList == null ? 211 new File [0] : this.relatedFolderList.getFiles(); 212 for (int i=0, index=model.size(); i<files.length; i++, index++) { 213 File 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 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 } 238 239 static boolean isValidRoot(File file, File [] relatedRoots, File 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 folder, File [] roots) { 274 String path = folder.getAbsolutePath(); 275 for (int i=0; i<roots.length; i++) { 276 String 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 { 285 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 286 File f = (File ) value; 287 Project p = FileOwnerQuery.getOwner(f.toURI()); 288 String message = f.getAbsolutePath(); 289 Component result = super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus); 290 return result; 291 } 292 293 } 294 295 private javax.swing.JButton addButton; 297 private javax.swing.JLabel jLabel1; 298 private javax.swing.JScrollPane jScrollPane1; 299 private javax.swing.JButton removeButton; 300 private javax.swing.JList roots; 301 303 } 304 | Popular Tags |