1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.library; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.beans.Customizer ; 25 import java.io.File ; 26 import java.net.URL ; 27 import java.net.MalformedURLException ; 28 import java.util.Collection ; 29 import java.util.Arrays ; 30 import javax.swing.DefaultListCellRenderer ; 31 import javax.swing.JButton ; 32 import javax.swing.JComponent ; 33 import javax.swing.JFileChooser ; 34 import javax.swing.JList ; 35 import javax.swing.event.ListSelectionEvent ; 36 import javax.swing.event.ListSelectionListener ; 37 import javax.swing.filechooser.FileFilter ; 38 import org.netbeans.spi.project.libraries.support.LibrariesSupport; 39 import org.openide.DialogDisplayer; 40 import org.openide.ErrorManager; 41 import org.openide.NotifyDescriptor; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.filesystems.URLMapper; 45 import org.openide.util.NbBundle; 46 import org.netbeans.spi.project.libraries.LibraryImplementation; 47 48 53 public class J2SEVolumeCustomizer extends javax.swing.JPanel implements Customizer { 54 55 private String volumeType; 56 private LibraryImplementation impl; 57 private VolumeContentModel model; 58 59 60 J2SEVolumeCustomizer (String volumeType) { 61 this.volumeType = volumeType; 62 initComponents(); 63 postInitComponents (); 64 this.setName (NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_"+volumeType)); 65 } 66 67 public void addNotify() { 68 super.addNotify(); 69 this.addButton.requestFocus(); 70 } 71 72 public void setEnabled(boolean enabled) { 73 super.setEnabled(enabled); 74 this.addButton.setEnabled(enabled); 75 if (this.addURLButton != null) { 76 this.addURLButton.setEnabled(enabled); 77 } 78 int[] indices = content.getSelectedIndices(); 79 this.removeButton.setEnabled(enabled && indices.length > 0); 80 this.downButton.setEnabled(enabled && indices.length > 0 && indices[indices.length-1]<model.getSize()-1); 81 this.upButton.setEnabled(enabled && indices.length>0 && indices[0]>0); 82 } 83 84 85 private void postInitComponents () { 86 this.content.setCellRenderer(new ContentRenderer()); 87 this.upButton.setEnabled (false); 88 this.downButton.setEnabled (false); 89 this.removeButton.setEnabled (false); 90 if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_CLASSPATH)) { 91 this.addButton.setText (NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_AddClassPath")); 92 this.addButton.setMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_AddClassPath").charAt(0)); 93 this.message.setText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_ContentClassPath")); 94 this.message.setDisplayedMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_ContentClassPath").charAt(0)); 95 this.addButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEVolumeCustomizer.class,"AD_AddClassPath")); 96 this.message.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEVolumeCustomizer.class,"AD_ContentClassPath")); 97 } 98 else if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_JAVADOC)) { 99 this.addButton.setText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_AddJavadoc")); 100 this.addButton.setMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_AddJavadoc").charAt(0)); 101 this.message.setText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_ContentJavadoc")); 102 this.message.setDisplayedMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_ContentJavadoc").charAt(0)); 103 this.addButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEVolumeCustomizer.class,"AD_AddJavadoc")); 104 this.message.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEVolumeCustomizer.class,"AD_ContentJavadoc")); 105 } 124 else if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_SRC)) { 125 this.addButton.setText (NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_AddSources")); 126 this.addButton.setMnemonic (NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_AddSources").charAt(0)); 127 this.message.setText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_ContentSources")); 128 this.message.setDisplayedMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_ContentSources").charAt(0)); 129 this.addButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEVolumeCustomizer.class,"AD_AddSources")); 130 this.message.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEVolumeCustomizer.class,"AD_ContentSources")); 131 } 132 this.content.addListSelectionListener(new ListSelectionListener () { 133 public void valueChanged(ListSelectionEvent e) { 134 if (e.getValueIsAdjusting()) 135 return; 136 int[] indices = content.getSelectedIndices(); 137 removeButton.setEnabled(indices.length > 0); 138 downButton.setEnabled(indices.length > 0 && indices[indices.length-1]<model.getSize()-1); 139 upButton.setEnabled(indices.length>0 && indices[0]>0); 140 } 141 }); 142 } 143 144 149 private void initComponents() { 151 java.awt.GridBagConstraints gridBagConstraints; 152 153 message = new javax.swing.JLabel (); 154 jScrollPane1 = new javax.swing.JScrollPane (); 155 content = new javax.swing.JList (); 156 addButton = new javax.swing.JButton (); 157 removeButton = new javax.swing.JButton (); 158 upButton = new javax.swing.JButton (); 159 downButton = new javax.swing.JButton (); 160 161 setLayout(new java.awt.GridBagLayout ()); 162 163 getAccessibleContext().setAccessibleDescription(null); 164 message.setLabelFor(content); 165 org.openide.awt.Mnemonics.setLocalizedText(message, org.openide.util.NbBundle.getBundle(J2SEVolumeCustomizer.class).getString("CTL_ContentMessage")); 166 gridBagConstraints = new java.awt.GridBagConstraints (); 167 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 168 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 169 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 170 gridBagConstraints.weightx = 1.0; 171 gridBagConstraints.insets = new java.awt.Insets (6, 6, 2, 6); 172 add(message, gridBagConstraints); 173 174 jScrollPane1.setViewportView(content); 175 176 gridBagConstraints = new java.awt.GridBagConstraints (); 177 gridBagConstraints.gridx = 0; 178 gridBagConstraints.gridy = 1; 179 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 180 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 181 gridBagConstraints.weightx = 1.0; 182 gridBagConstraints.weighty = 1.0; 183 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 6); 184 add(jScrollPane1, gridBagConstraints); 185 186 org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getBundle(J2SEVolumeCustomizer.class).getString("CTL_AddContent")); 187 addButton.addActionListener(new java.awt.event.ActionListener () { 188 public void actionPerformed(java.awt.event.ActionEvent evt) { 189 addResource(evt); 190 } 191 }); 192 193 gridBagConstraints = new java.awt.GridBagConstraints (); 194 gridBagConstraints.gridx = 1; 195 gridBagConstraints.gridy = 1; 196 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 197 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 198 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 199 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 6); 200 add(addButton, gridBagConstraints); 201 addButton.getAccessibleContext().setAccessibleDescription(null); 202 203 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getBundle(J2SEVolumeCustomizer.class).getString("CTL_RemoveContent")); 204 removeButton.addActionListener(new java.awt.event.ActionListener () { 205 public void actionPerformed(java.awt.event.ActionEvent evt) { 206 removeResource(evt); 207 } 208 }); 209 210 gridBagConstraints = new java.awt.GridBagConstraints (); 211 gridBagConstraints.gridx = 1; 212 gridBagConstraints.gridy = 3; 213 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 214 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 215 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 216 gridBagConstraints.insets = new java.awt.Insets (5, 6, 6, 6); 217 add(removeButton, gridBagConstraints); 218 removeButton.getAccessibleContext().setAccessibleDescription(null); 219 220 org.openide.awt.Mnemonics.setLocalizedText(upButton, org.openide.util.NbBundle.getBundle(J2SEVolumeCustomizer.class).getString("CTL_UpContent")); 221 upButton.addActionListener(new java.awt.event.ActionListener () { 222 public void actionPerformed(java.awt.event.ActionEvent evt) { 223 upResource(evt); 224 } 225 }); 226 227 gridBagConstraints = new java.awt.GridBagConstraints (); 228 gridBagConstraints.gridx = 1; 229 gridBagConstraints.gridy = 4; 230 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 231 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 232 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 233 gridBagConstraints.insets = new java.awt.Insets (5, 6, 0, 6); 234 add(upButton, gridBagConstraints); 235 upButton.getAccessibleContext().setAccessibleDescription(null); 236 237 org.openide.awt.Mnemonics.setLocalizedText(downButton, org.openide.util.NbBundle.getBundle(J2SEVolumeCustomizer.class).getString("CTL_DownContent")); 238 downButton.addActionListener(new java.awt.event.ActionListener () { 239 public void actionPerformed(java.awt.event.ActionEvent evt) { 240 downResource(evt); 241 } 242 }); 243 244 gridBagConstraints = new java.awt.GridBagConstraints (); 245 gridBagConstraints.gridx = 1; 246 gridBagConstraints.gridy = 5; 247 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 248 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 249 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 250 gridBagConstraints.insets = new java.awt.Insets (5, 6, 6, 6); 251 add(downButton, gridBagConstraints); 252 downButton.getAccessibleContext().setAccessibleDescription(null); 253 254 } 256 private void downResource(java.awt.event.ActionEvent evt) { int[] indices = this.content.getSelectedIndices(); 258 if (indices.length == 0 || indices[0] < 0 || indices[indices.length-1]>=model.getSize()-1) { 259 return; 260 } 261 this.model.moveDown(indices); 262 for (int i=0; i< indices.length; i++) { 263 indices[i] = indices[i] + 1; 264 } 265 this.content.setSelectedIndices (indices); 266 } 268 private void upResource(java.awt.event.ActionEvent evt) { int[] indices = this.content.getSelectedIndices(); 270 if (indices.length == 0 || indices[0] <= 0) { 271 return; 272 } 273 this.model.moveUp(indices); 274 for (int i=0; i< indices.length; i++) { 275 indices[i] = indices[i] - 1; 276 } 277 this.content.setSelectedIndices(indices); 278 } 280 private void removeResource(java.awt.event.ActionEvent evt) { int[] indices =this.content.getSelectedIndices(); 282 if (indices.length == 0) { 283 return; 284 } 285 this.model.removeResources(indices); 286 if (indices[indices.length-1]-indices.length+1 < this.model.getSize()) { 287 this.content.setSelectedIndex(indices[indices.length-1]-indices.length+1); 288 } 289 else if (indices[0] >= 1) { 290 this.content.setSelectedIndex (indices[0]-1); 291 } 292 } 294 private void addResource(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 297 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 298 chooser.setAcceptAllFileFilterUsed(false); 299 if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_CLASSPATH)) { 300 chooser.setMultiSelectionEnabled (true); 301 chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenClasses")); 302 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 303 chooser.setFileFilter (new SimpleFileFilter(NbBundle.getMessage( 304 J2SEVolumeCustomizer.class,"TXT_Classpath"),new String [] {"ZIP","JAR"})); chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectCP")); 306 chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectCP").charAt(0)); 307 } 308 else if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_JAVADOC)) { 309 chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenJavadoc")); 310 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 311 chooser.setFileFilter (new SimpleFileFilter(NbBundle.getMessage( 312 J2SEVolumeCustomizer.class,"TXT_Javadoc"),new String [] {"ZIP","JAR"})); chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectJD")); 314 chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectJD").charAt(0)); 315 } 316 else if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_SRC)) { 317 chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenSources")); 318 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 319 chooser.setFileFilter (new SimpleFileFilter(NbBundle.getMessage( 320 J2SEVolumeCustomizer.class,"TXT_Sources"),new String [] {"ZIP","JAR"})); chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectSRC")); 322 chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectSRC").charAt(0)); 323 } 324 if (lastFolder != null) { 325 chooser.setCurrentDirectory (lastFolder); 326 } 327 if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 328 try { 329 lastFolder = chooser.getCurrentDirectory(); 330 if (chooser.isMultiSelectionEnabled()) { 331 addFiles (chooser.getSelectedFiles()); 332 } 333 else { 334 final File selectedFile = chooser.getSelectedFile(); 335 addFiles (new File [] {selectedFile}); 336 } 337 } catch (MalformedURLException mue) { 338 ErrorManager.getDefault().notify(mue); 339 } 340 } 341 } 343 344 364 365 private void addFiles (File [] files) throws MalformedURLException { 366 int firstIndex = this.model.getSize(); 367 for (int i = 0; i < files.length; i++) { 368 File f = files[i]; 369 if (!f.exists()) { 373 File parent = f.getParentFile(); 374 if (parent != null && f.getName().equals(parent.getName()) && parent.exists()) { 375 f = parent; 376 } 377 } 378 URL url = f.toURI().toURL(); 379 if (FileUtil.isArchiveFile(url)) { 380 url = FileUtil.getArchiveRoot(url); 381 } 382 else if (!url.toExternalForm().endsWith("/")){ 383 try { 384 url = new URL (url.toExternalForm()+"/"); 385 } catch (MalformedURLException mue) { 386 ErrorManager.getDefault().notify(mue); 387 } 388 } 389 if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_JAVADOC) 390 && !PersistenceLibrarySupport.isValidLibraryJavadocRoot (url)) { 391 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 392 NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_InvalidJavadocRoot", f.getPath()), 393 NotifyDescriptor.ERROR_MESSAGE)); 394 continue; 395 } 396 this.model.addResource(url); 397 } 398 int lastIndex = this.model.getSize()-1; 399 if (firstIndex<=lastIndex) { 400 int[] toSelect = new int[lastIndex-firstIndex+1]; 401 for (int i = 0; i < toSelect.length; i++) { 402 toSelect[i] = firstIndex+i; 403 } 404 this.content.setSelectedIndices(toSelect); 405 } 406 } 407 408 public void setObject(Object bean) { 409 if (bean instanceof LibraryImplementation) { 410 LibrariesSupport.createLibraryImplementation(PersistenceLibrarySupport.LIBRARY_TYPE, PersistenceLibrarySupport.VOLUME_TYPES); 411 this.impl = (LibraryImplementation) bean; 412 this.model = new VolumeContentModel(this.impl,this.volumeType); 413 this.content.setModel(model); 414 if (this.model.getSize()>0) { 415 this.content.setSelectedIndex(0); 416 } 417 } 418 else { 419 throw new IllegalArgumentException (); 420 } 421 } 422 423 424 private static class SimpleFileFilter extends FileFilter { 425 426 private String description; 427 private Collection extensions; 428 429 430 public SimpleFileFilter(String description, String [] extensions) { 431 this.description = description; 432 this.extensions = Arrays.asList(extensions); 433 } 434 435 public boolean accept(File f) { 436 if (f.isDirectory()) 437 return true; 438 String name = f.getName(); 439 int index = name.lastIndexOf('.'); if (index <= 0 || index==name.length()-1) 441 return false; 442 String extension = name.substring(index+1).toUpperCase(); 443 return this.extensions.contains(extension); 444 } 445 446 public String getDescription() { 447 return this.description; 448 } 449 } 450 451 452 private static File lastFolder = null; 453 454 455 private javax.swing.JButton addButton; 457 private javax.swing.JList content; 458 private javax.swing.JButton downButton; 459 private javax.swing.JScrollPane jScrollPane1; 460 private javax.swing.JLabel message; 461 private javax.swing.JButton removeButton; 462 private javax.swing.JButton upButton; 463 private JButton addURLButton; 465 466 private static class ContentRenderer extends DefaultListCellRenderer { 467 468 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 469 String displayName = null; 470 Color color = null; 471 String toolTip = null; 472 473 if (value instanceof URL ) { 474 URL url = (URL ) value; 475 if ("jar".equals(url.getProtocol())) { url = FileUtil.getArchiveFile (url); 477 } 478 FileObject fo = URLMapper.findFileObject (url); 479 if (fo == null) { 480 displayName = url.toExternalForm(); 481 color = new Color (164,0,0); 482 toolTip = NbBundle.getMessage (J2SEVolumeCustomizer.class,"TXT_BrokenFile"); 483 } 484 else { 485 displayName = FileUtil.getFileDisplayName(fo); 486 } 487 } 488 Component c = super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 489 if (c instanceof JComponent ) { 490 if (color != null) { 491 ((JComponent )c).setForeground (color); 492 } 493 if (toolTip != null) { 494 ((JComponent )c).setToolTipText (toolTip); 495 } 496 } 497 return c; 498 } 499 500 } 501 502 } 503 | Popular Tags |