1 19 package org.netbeans.modules.java.j2seplatform.platformdefinition; 20 21 22 import java.awt.*; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.ActionEvent ; 25 import java.io.File ; 26 import java.util.Collection ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 import java.net.URL ; 31 import java.net.URI ; 32 import java.net.MalformedURLException ; 33 import javax.swing.*; 34 import javax.swing.filechooser.FileFilter ; 35 import javax.swing.event.ListSelectionListener ; 36 import javax.swing.event.ListSelectionEvent ; 37 import org.openide.NotifyDescriptor; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.NbBundle; 41 import org.netbeans.api.java.classpath.ClassPath; 42 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 43 44 45 46 public class J2SEPlatformCustomizer extends JTabbedPane { 47 48 private static final int CLASSPATH = 0; 49 private static final int SOURCES = 1; 50 private static final int JAVADOC = 2; 51 52 private J2SEPlatformImpl platform; 53 54 public J2SEPlatformCustomizer (J2SEPlatformImpl platform) { 55 this.platform = platform; 56 this.initComponents (); 57 } 58 59 60 private void initComponents () { 61 this.getAccessibleContext().setAccessibleName (NbBundle.getMessage(J2SEPlatformCustomizer.class,"AN_J2SEPlatformCustomizer")); 62 this.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(J2SEPlatformCustomizer.class,"AD_J2SEPlatformCustomizer")); 63 this.addTab(NbBundle.getMessage(J2SEPlatformCustomizer.class,"TXT_Classes"), createPathTab(CLASSPATH)); 64 this.addTab(NbBundle.getMessage(J2SEPlatformCustomizer.class,"TXT_Sources"), createPathTab(SOURCES)); 65 this.addTab(NbBundle.getMessage(J2SEPlatformCustomizer.class,"TXT_Javadoc"), createPathTab(JAVADOC)); 66 } 67 68 69 private JComponent createPathTab (int type) { 70 return new PathView (this.platform, type); 71 } 72 73 74 private static class PathView extends JPanel { 75 76 private JList resources; 77 private JButton addButton; 78 private JButton removeButton; 80 private JButton moveUpButton; 81 private JButton moveDownButton; 82 private int type; 83 private File currentDir; 84 85 public PathView (J2SEPlatformImpl platform, int type) { 86 this.type = type; 87 this.initComponents (platform); 88 } 89 90 private void initComponents (J2SEPlatformImpl platform) { 91 this.setLayout(new GridBagLayout()); 92 JLabel label = new JLabel (); 93 String key = null; 94 String mneKey = null; 95 String ad = null; 96 switch (type) { 97 case CLASSPATH: 98 key = "TXT_JDKClasspath"; mneKey = "MNE_JDKClasspath"; ad = "AD_JDKClasspath"; break; 102 case SOURCES: 103 key = "TXT_JDKSources"; mneKey = "MNE_JDKSources"; ad = "AD_JDKSources"; break; 107 case JAVADOC: 108 key = "TXT_JDKJavadoc"; mneKey = "MNE_JDKJavadoc"; ad = "AD_JDKJavadoc"; break; 112 default: 113 assert false : "Illegal type of panel"; return; 115 } 116 label.setText (NbBundle.getMessage(J2SEPlatformCustomizer.class,key)); 117 label.setDisplayedMnemonic(NbBundle.getMessage(J2SEPlatformCustomizer.class,mneKey).charAt(0)); 118 GridBagConstraints c = new GridBagConstraints(); 119 c.gridx = GridBagConstraints.RELATIVE; 120 c.gridy = GridBagConstraints.RELATIVE; 121 c.gridwidth = GridBagConstraints.REMAINDER; 122 c.insets = new Insets (6,6,2,6); 123 c.fill = GridBagConstraints.HORIZONTAL; 124 c.weightx = 1.0; 125 ((GridBagLayout)this.getLayout()).setConstraints(label,c); 126 this.add (label); 127 this.resources = new JList(new PathModel(platform,type)); 128 label.setLabelFor (this.resources); 129 this.resources.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEPlatformCustomizer.class,ad)); 130 this.resources.addListSelectionListener(new ListSelectionListener () { 131 public void valueChanged(ListSelectionEvent e) { 132 selectionChanged (); 133 } 134 }); 135 JScrollPane spane = new JScrollPane (this.resources); 136 c = new GridBagConstraints(); 137 c.gridx = GridBagConstraints.RELATIVE; 138 c.gridy = GridBagConstraints.RELATIVE; 139 c.gridwidth = 1; 140 c.gridheight = 5; 141 c.insets = new Insets (0,6,6,6); 142 c.fill = GridBagConstraints.BOTH; 143 c.weightx = 1.0; 144 c.weighty = 1.0; 145 ((GridBagLayout)this.getLayout()).setConstraints(spane,c); 146 this.add (spane); 147 if (type == SOURCES || type == JAVADOC) { 148 this.addButton = new JButton (); 149 String text; 150 char mne; 151 if (type == SOURCES) { 152 text = NbBundle.getMessage(J2SEPlatformCustomizer.class, "CTL_Add"); 153 mne = NbBundle.getMessage(J2SEPlatformCustomizer.class, "MNE_Add").charAt(0); 154 ad = NbBundle.getMessage(J2SEPlatformCustomizer.class, "AD_Add"); 155 } 156 else { 157 text = NbBundle.getMessage(J2SEPlatformCustomizer.class, "CTL_AddZip"); 158 mne = NbBundle.getMessage(J2SEPlatformCustomizer.class, "MNE_AddZip").charAt(0); 159 ad = NbBundle.getMessage(J2SEPlatformCustomizer.class, "AD_AddZip"); 160 } 161 this.addButton.setText(text); 162 this.addButton.setMnemonic(mne); 163 this.addButton.getAccessibleContext().setAccessibleDescription (ad); 164 addButton.addActionListener( new ActionListener () { 165 public void actionPerformed(ActionEvent e) { 166 addPathElement (); 167 } 168 }); 169 c = new GridBagConstraints(); 170 c.gridx = 1; 171 c.gridy = 1; 172 c.gridwidth = GridBagConstraints.REMAINDER; 173 c.fill = GridBagConstraints.HORIZONTAL; 174 c.anchor = GridBagConstraints.NORTHWEST; 175 c.insets = new Insets (0,6,0,6); 176 ((GridBagLayout)this.getLayout()).setConstraints(addButton,c); 177 this.add (addButton); 178 removeButton = new JButton (NbBundle.getMessage(J2SEPlatformCustomizer.class, "CTL_Remove")); 197 removeButton.setMnemonic(NbBundle.getMessage(J2SEPlatformCustomizer.class, "MNE_Remove").charAt(0)); 198 removeButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(J2SEPlatformCustomizer.class,"AD_Remove")); 199 removeButton.addActionListener( new ActionListener () { 200 public void actionPerformed(ActionEvent e) { 201 removePathElement (); 202 } 203 }); 204 removeButton.setEnabled(false); 205 c = new GridBagConstraints(); 206 c.gridx = 1; 207 c.gridy = 3; 208 c.gridwidth = GridBagConstraints.REMAINDER; 209 c.fill = GridBagConstraints.HORIZONTAL; 210 c.anchor = GridBagConstraints.NORTHWEST; 211 c.insets = new Insets (12,6,0,6); 212 ((GridBagLayout)this.getLayout()).setConstraints(removeButton,c); 213 this.add (removeButton); 214 moveUpButton = new JButton (NbBundle.getMessage(J2SEPlatformCustomizer.class, "CTL_Up")); 215 moveUpButton.setMnemonic(NbBundle.getMessage(J2SEPlatformCustomizer.class, "MNE_Up").charAt(0)); 216 moveUpButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(J2SEPlatformCustomizer.class,"AD_Up")); 217 moveUpButton.addActionListener( new ActionListener () { 218 public void actionPerformed(ActionEvent e) { 219 moveUpPathElement (); 220 } 221 }); 222 moveUpButton.setEnabled(false); 223 c = new GridBagConstraints(); 224 c.gridx = 1; 225 c.gridy = 4; 226 c.gridwidth = GridBagConstraints.REMAINDER; 227 c.fill = GridBagConstraints.HORIZONTAL; 228 c.anchor = GridBagConstraints.NORTHWEST; 229 c.insets = new Insets (12,6,0,6); 230 ((GridBagLayout)this.getLayout()).setConstraints(moveUpButton,c); 231 this.add (moveUpButton); 232 moveDownButton = new JButton (NbBundle.getMessage(J2SEPlatformCustomizer.class, "CTL_Down")); 233 moveDownButton.setMnemonic (NbBundle.getMessage(J2SEPlatformCustomizer.class, "MNE_Down").charAt(0)); 234 moveDownButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEPlatformCustomizer.class,"AD_Down")); 235 moveDownButton.addActionListener( new ActionListener () { 236 public void actionPerformed(ActionEvent e) { 237 moveDownPathElement (); 238 } 239 }); 240 moveDownButton.setEnabled(false); 241 c = new GridBagConstraints(); 242 c.gridx = 1; 243 c.gridy = 5; 244 c.gridwidth = GridBagConstraints.REMAINDER; 245 c.fill = GridBagConstraints.HORIZONTAL; 246 c.anchor = GridBagConstraints.NORTHWEST; 247 c.insets = new Insets (5,6,6,6); 248 ((GridBagLayout)this.getLayout()).setConstraints(moveDownButton,c); 249 this.add (moveDownButton); 250 } 251 } 252 253 303 private void addPathElement () { 304 JFileChooser chooser = new JFileChooser (); 305 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 306 chooser.setMultiSelectionEnabled (true); 307 String title = null; 308 String message = null; 309 String approveButtonName = null; 310 String approveButtonNameMne = null; 311 if (this.type == SOURCES) { 312 title = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenSources"); 313 message = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_Sources"); 314 approveButtonName = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenSources"); 315 approveButtonNameMne = NbBundle.getMessage (J2SEPlatformCustomizer.class,"MNE_OpenSources"); 316 } 317 else if (this.type == JAVADOC) { 318 title = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenJavadoc"); 319 message = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_Javadoc"); 320 approveButtonName = NbBundle.getMessage (J2SEPlatformCustomizer.class,"TXT_OpenJavadoc"); 321 approveButtonNameMne = NbBundle.getMessage (J2SEPlatformCustomizer.class,"MNE_OpenJavadoc"); 322 } 323 chooser.setDialogTitle(title); 324 chooser.setApproveButtonText(approveButtonName); 325 chooser.setApproveButtonMnemonic (approveButtonNameMne.charAt(0)); 326 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 327 chooser.setAcceptAllFileFilterUsed( false ); 329 chooser.setFileFilter (new SimpleFileFilter(message,new String [] {"ZIP","JAR"})); if (this.currentDir != null) { 331 chooser.setCurrentDirectory(this.currentDir); 332 } 333 if (chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) { 334 File [] fs = chooser.getSelectedFiles(); 335 PathModel model = (PathModel) this.resources.getModel(); 336 boolean addingFailed = false; 337 int firstIndex = this.resources.getModel().getSize(); 338 for (int i = 0; i < fs.length; i++) { 339 File f = fs[i]; 340 if (!f.exists()) { 344 File parent = f.getParentFile(); 345 if (parent != null && f.getName().equals(parent.getName()) && parent.exists()) { 346 f = parent; 347 } 348 } 349 addingFailed|=!model.addPath (f); 350 } 351 if (addingFailed) { 352 new NotifyDescriptor.Message (NbBundle.getMessage(J2SEPlatformCustomizer.class,"TXT_CanNotAddResolve"), 353 NotifyDescriptor.ERROR_MESSAGE); 354 } 355 int lastIndex = this.resources.getModel().getSize()-1; 356 if (firstIndex<=lastIndex) { 357 int[] toSelect = new int[lastIndex-firstIndex+1]; 358 for (int i = 0; i < toSelect.length; i++) { 359 toSelect[i] = firstIndex+i; 360 } 361 this.resources.setSelectedIndices(toSelect); 362 } 363 this.currentDir = FileUtil.normalizeFile(chooser.getCurrentDirectory()); 364 } 365 } 366 367 private void removePathElement () { 368 int[] indices = this.resources.getSelectedIndices(); 369 if (indices.length == 0) { 370 return; 371 } 372 PathModel model = (PathModel) this.resources.getModel(); 373 model.removePath (indices); 374 if ( indices[indices.length-1]-indices.length+1 < this.resources.getModel().getSize()) { 375 this.resources.setSelectedIndex (indices[indices.length-1]-indices.length+1); 376 } 377 else if (indices[0]>0) { 378 this.resources.setSelectedIndex (indices[0]-1); 379 } 380 } 381 382 private void moveDownPathElement () { 383 int[] indices = this.resources.getSelectedIndices(); 384 if (indices.length == 0) { 385 return; 386 } 387 PathModel model = (PathModel) this.resources.getModel(); 388 model.moveDownPath (indices); 389 for (int i=0; i< indices.length; i++) { 390 indices[i] = indices[i] + 1; 391 } 392 this.resources.setSelectedIndices (indices); 393 } 394 395 private void moveUpPathElement () { 396 int[] indices = this.resources.getSelectedIndices(); 397 if (indices.length == 0) { 398 return; 399 } 400 PathModel model = (PathModel) this.resources.getModel(); 401 model.moveUpPath (indices); 402 for (int i=0; i< indices.length; i++) { 403 indices[i] = indices[i] - 1; 404 } 405 this.resources.setSelectedIndices (indices); 406 } 407 408 private void selectionChanged () { 409 if (this.type == CLASSPATH) { 410 return; 411 } 412 int indices[] = this.resources.getSelectedIndices(); 413 this.removeButton.setEnabled (indices.length > 0); 414 this.moveUpButton.setEnabled (indices.length > 0 && indices[0]>0); 415 this.moveDownButton.setEnabled(indices.length > 0 && indices[indices.length-1]<this.resources.getModel().getSize()-1); 416 } 417 418 } 419 420 421 private static class PathModel extends AbstractListModel { 422 423 private J2SEPlatformImpl platform; 424 private int type; 425 private java.util.List data; 426 427 public PathModel (J2SEPlatformImpl platform, int type) { 428 this.platform = platform; 429 this.type = type; 430 } 431 432 public int getSize() { 433 return this.getData().size(); 434 } 435 436 public Object getElementAt(int index) { 437 java.util.List list = this.getData(); 438 URL url = (URL )list.get(index); 439 if ("jar".equals(url.getProtocol())) { URL fileURL = FileUtil.getArchiveFile (url); 441 if (FileUtil.getArchiveRoot(fileURL).equals(url)) { 442 url = fileURL; 444 } else { 445 return url.toExternalForm(); 447 } 448 } 449 if ("file".equals(url.getProtocol())) { 450 File f = new File (URI.create(url.toExternalForm())); 451 return f.getAbsolutePath(); 452 } 453 else { 454 return url.toExternalForm(); 455 } 456 } 457 458 private void removePath (int[] indices) { 459 java.util.List data = getData(); 460 for (int i=indices.length-1; i>=0; i--) { 461 data.remove(indices[i]); 462 } 463 updatePlatform (); 464 fireIntervalRemoved(this,indices[0],indices[indices.length-1]); 465 } 466 467 private void moveUpPath (int[] indices) { 468 java.util.List data = getData (); 469 for (int i=0; i<indices.length; i++) { 470 Object p2 = data.get (indices[i]); 471 Object p1 = data.set (indices[i]-1,p2); 472 data.set (indices[i],p1); 473 } 474 updatePlatform (); 475 fireContentsChanged(this,indices[0]-1,indices[indices.length-1]); 476 } 477 478 private void moveDownPath (int[] indices) { 479 java.util.List data = getData (); 480 for (int i=indices.length-1; i>=0; i--) { 481 Object p1 = data.get (indices[i]); 482 Object p2 = data.set (indices[i]+1,p1); 483 data.set (indices[i],p2); 484 } 485 updatePlatform(); 486 fireContentsChanged(this,indices[0],indices[indices.length-1]+1); 487 } 488 489 private boolean addPath (File f) { 490 try { 491 URL url = f.toURI().toURL(); 492 return this.addPath (url); 493 } catch (MalformedURLException mue) { 494 return false; 495 } 496 } 497 498 private boolean addPath (URL url) { 499 if (FileUtil.isArchiveFile(url)) { 500 url = FileUtil.getArchiveRoot (url); 501 } 502 else if (!url.toExternalForm().endsWith("/")){ 503 try { 504 url = new URL (url.toExternalForm()+"/"); 505 } catch (MalformedURLException mue) { 506 ErrorManager.getDefault().notify(mue); 507 } 508 } 509 java.util.List data = getData(); 510 int oldSize = data.size (); 511 data.add (url); 512 updatePlatform(); 513 fireIntervalAdded(this,oldSize,oldSize); 514 return true; 515 } 516 517 private synchronized java.util.List getData () { 518 if (this.data == null) { 519 switch (this.type) { 520 case CLASSPATH: 521 this.data = getPathList (this.platform.getBootstrapLibraries()); 522 break; 523 case SOURCES: 524 this.data = getPathList (this.platform.getSourceFolders()); 525 break; 526 case JAVADOC: 527 this.data = new ArrayList (this.platform.getJavadocFolders()); 528 break; 529 } 530 } 531 return this.data; 532 } 533 534 private static java.util.List getPathList (ClassPath cp) { 535 java.util.List result = new ArrayList (); 536 for (Iterator it = cp.entries().iterator(); it.hasNext();) { 537 ClassPath.Entry entry = (ClassPath.Entry) it.next (); 538 result.add (entry.getURL()); 539 } 540 return result; 541 } 542 543 private static ClassPath createClassPath (java.util.List roots) { 544 java.util.List resources = new ArrayList (); 545 for (Iterator it = roots.iterator(); it.hasNext();) { 546 URL url = (URL ) it.next (); 547 resources.add (ClassPathSupport.createResource(url)); 548 } 549 return ClassPathSupport.createClassPath(resources); 550 } 551 552 private void updatePlatform () { 553 switch (this.type) { 554 case SOURCES: 555 this.platform.setSourceFolders(createClassPath(data)); 556 break; 557 case JAVADOC: 558 this.platform.setJavadocFolders (data); 559 break; 560 default: 561 assert false : "Trying to update unknown property"; } 563 } 564 } 565 566 567 private static class SimpleFileFilter extends FileFilter { 568 569 private String description; 570 private Collection extensions; 571 572 573 public SimpleFileFilter (String description, String [] extensions) { 574 this.description = description; 575 this.extensions = Arrays.asList(extensions); 576 } 577 578 public boolean accept(File f) { 579 if (f.isDirectory()) 580 return true; 581 String name = f.getName(); 582 int index = name.lastIndexOf('.'); if (index <= 0 || index==name.length()-1) 584 return false; 585 String extension = name.substring (index+1).toUpperCase(); 586 return this.extensions.contains(extension); 587 } 588 589 public String getDescription() { 590 return this.description; 591 } 592 } 593 594 595 } 596 | Popular Tags |