1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.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.api.project.Project; 33 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarSourceRootsUi; 34 import org.openide.filesystems.FileUtil; 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 setProjectFolder (File projectFolder) { 77 this.projectFolder = projectFolder; 78 } 79 80 public void setRelatedFolderList (FolderList relatedFolderList) { 81 this.relatedFolderList = relatedFolderList; 82 } 83 84 public File [] getFiles () { 85 Object [] files = ((DefaultListModel )this.roots.getModel()).toArray(); 86 File [] result = new File [files.length]; 87 System.arraycopy(files, 0, result, 0, files.length); 88 return result; 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 143 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 () { 154 public void actionPerformed(java.awt.event.ActionEvent evt) { 155 addButtonActionPerformed(evt); 156 } 157 }); 158 159 gridBagConstraints = new java.awt.GridBagConstraints (); 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 () { 167 public void actionPerformed(java.awt.event.ActionEvent evt) { 168 removeButtonActionPerformed(evt); 169 } 170 }); 171 172 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (12, 0, 0, 0); 179 add(removeButton, gridBagConstraints); 180 181 } 182 184 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] selection = this.roots.getSelectedValues (); 186 for (int i=0; i<selection.length; i++) { 187 ((DefaultListModel )this.roots.getModel()).removeElement (selection[i]); 188 } 189 this.firePropertyChange(PROP_FILES, null, null); 190 } 192 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 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 [] files = chooser.getSelectedFiles(); 206 int[] indecesToSelect = new int[files.length]; 207 DefaultListModel model = (DefaultListModel )this.roots.getModel(); 208 Set invalidRoots = new HashSet (); 209 File [] relatedFolders = this.relatedFolderList == null ? 210 new File [0] : this.relatedFolderList.getFiles(); 211 for (int i=0, index=model.size(); i<files.length; i++, index++) { 212 File 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 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 } 239 static boolean isValidRoot (File file, File [] relatedRoots, File 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 folder, File [] roots) { 251 String path = folder.getAbsolutePath (); 252 for (int i=0; i<roots.length; i++) { 253 String 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 { 262 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 263 File f = (File ) value; 264 String message = f.getAbsolutePath(); 265 Component result = super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus); 266 return result; 267 } 268 } 269 270 private javax.swing.JButton addButton; 272 private javax.swing.JLabel jLabel1; 273 private javax.swing.JScrollPane jScrollPane1; 274 private javax.swing.JButton removeButton; 275 private javax.swing.JList roots; 276 278 } 279 | Popular Tags |