1 19 20 package org.netbeans.modules.web.project.ui.wizards; 21 import java.awt.Component ; 22 import java.io.File ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 import javax.swing.DefaultListModel ; 26 import javax.swing.DefaultListCellRenderer ; 27 import javax.swing.JFileChooser ; 28 import javax.swing.JList ; 29 import javax.swing.event.ListSelectionListener ; 30 import javax.swing.event.ListSelectionEvent ; 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 41 public final class FolderList extends javax.swing.JPanel { 42 43 public static final String PROP_FILES = "files"; public static final String PROP_LAST_USED_DIR = "lastUsedDir"; 46 private String fcMessage; 47 private File projectFolder; 48 private File lastUsedFolder; 49 private FolderList relatedFolderList; 50 51 52 public FolderList (String label, char mnemonic, String accessibleDesc, String fcMessage, 53 char addButtonMnemonic, String addButtonAccessibleDesc, 54 char removeButtonMnemonic,String 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 ()); 61 this.roots.setModel (new DefaultListModel ()); 62 this.roots.addListSelectionListener(new ListSelectionListener () { 63 public void valueChanged(ListSelectionEvent 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 [] getFiles () { 81 Object [] files = ((DefaultListModel )this.roots.getModel()).toArray(); 82 File [] result = new File [files.length]; 83 System.arraycopy(files, 0, result, 0, files.length); 84 return result; 85 } 86 87 public void setProjectFolder (File projectFolder) { 88 this.projectFolder = projectFolder; 89 } 90 91 public void setFiles (File [] files) { 92 DefaultListModel model = ((DefaultListModel )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 lastUsedDir) { 103 if (this.lastUsedFolder == null ? lastUsedDir != null : !this.lastUsedFolder.equals(lastUsedDir)) { 104 File oldValue = this.lastUsedFolder; 105 this.lastUsedFolder = lastUsedDir; 106 this.firePropertyChange(PROP_LAST_USED_DIR, oldValue, this.lastUsedFolder); 107 } 108 } 109 110 public File getLastUsedDir () { 111 return this.lastUsedFolder; 112 } 113 114 119 private void initComponents() { 121 java.awt.GridBagConstraints gridBagConstraints; 122 123 jLabel1 = new javax.swing.JLabel (); 124 jScrollPane1 = new javax.swing.JScrollPane (); 125 roots = new javax.swing.JList (); 126 addButton = new javax.swing.JButton (); 127 removeButton = new javax.swing.JButton (); 128 129 setLayout(new java.awt.GridBagLayout ()); 130 131 jLabel1.setLabelFor(roots); 132 jLabel1.setText("jLabel1"); 133 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (); 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 (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 () { 155 public void actionPerformed(java.awt.event.ActionEvent evt) { 156 addButtonActionPerformed(evt); 157 } 158 }); 159 160 gridBagConstraints = new java.awt.GridBagConstraints (); 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 () { 168 public void actionPerformed(java.awt.event.ActionEvent evt) { 169 removeButtonActionPerformed(evt); 170 } 171 }); 172 173 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (12, 0, 0, 0); 180 add(removeButton, gridBagConstraints); 181 182 } 183 185 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] selection = this.roots.getSelectedValues (); 187 for (int i=0; i<selection.length; i++) { 188 ((DefaultListModel )this.roots.getModel()).removeElement (selection[i]); 189 } 190 this.firePropertyChange(PROP_FILES, null, null); 191 } 193 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 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 [] files = chooser.getSelectedFiles(); 207 int[] indecesToSelect = new int[files.length]; 208 DefaultListModel model = (DefaultListModel )this.roots.getModel(); 209 Set invalidRoots = new HashSet (); 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 } 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 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 } 240 static boolean isValidRoot (File file, File [] relatedRoots, File 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 folder, File [] roots) { 252 String path = folder.getAbsolutePath (); 253 for (int i=0; i<roots.length; i++) { 254 String 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 { 263 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 264 File f = (File ) value; 265 String message = f.getAbsolutePath(); 266 Component result = super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus); 267 return result; 268 } 269 } 270 271 private javax.swing.JButton addButton; 273 private javax.swing.JLabel jLabel1; 274 private javax.swing.JScrollPane jScrollPane1; 275 private javax.swing.JButton removeButton; 276 private javax.swing.JList roots; 277 279 } 280 | Popular Tags |