1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.beans.BeanInfo ; 27 import java.io.File ; 28 import java.text.MessageFormat ; 29 import java.util.Arrays ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import javax.swing.ButtonModel ; 38 import javax.swing.DefaultListCellRenderer ; 39 import javax.swing.DefaultListModel ; 40 import javax.swing.Icon ; 41 import javax.swing.ImageIcon ; 42 import javax.swing.JButton ; 43 import javax.swing.JFileChooser ; 44 import javax.swing.JList ; 45 import javax.swing.JTable ; 46 import javax.swing.ListCellRenderer ; 47 import javax.swing.ListSelectionModel ; 48 import javax.swing.SwingUtilities ; 49 import javax.swing.event.ListSelectionEvent ; 50 import javax.swing.event.ListSelectionListener ; 51 import javax.swing.filechooser.FileFilter ; 52 import javax.swing.table.DefaultTableCellRenderer ; 53 import javax.swing.table.TableCellRenderer ; 54 import org.netbeans.api.java.project.JavaProjectConstants; 55 import org.netbeans.api.project.Project; 56 import org.netbeans.api.project.ProjectInformation; 57 import org.netbeans.api.project.ProjectUtils; 58 import org.netbeans.api.project.libraries.Library; 59 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathSupport; 60 import org.netbeans.modules.j2ee.ejbjarproject.ui.FoldersListSettings; 61 import org.netbeans.spi.project.support.ant.AntProjectHelper; 62 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 63 import org.netbeans.spi.project.support.ant.ReferenceHelper; 64 import org.openide.DialogDescriptor; 65 import org.openide.DialogDisplayer; 66 import org.openide.filesystems.FileObject; 67 import org.openide.filesystems.FileUtil; 68 import org.openide.filesystems.Repository; 69 import org.openide.loaders.DataFolder; 70 import org.openide.util.NbBundle; 71 import org.openide.util.Utilities; 72 73 77 public class EjbJarClassPathUi { 78 79 private EjbJarClassPathUi() {} 80 81 83 85 public static class ClassPathListCellRenderer extends DefaultListCellRenderer { 86 87 private static String RESOURCE_ICON_JAR = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/jar.gif"; private static String RESOURCE_ICON_LIBRARY = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/libraries.gif"; private static String RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/projectDependencies.gif"; private static String RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/referencedClasspath.gif"; private static String RESOURCE_ICON_BROKEN_BADGE = "org/netbeans/modules/j2ee/ejbjarproject/ui/resources/brokenProjectBadge.gif"; 93 94 private static ImageIcon ICON_JAR = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_JAR ) ); 95 private static ImageIcon ICON_FOLDER = null; 96 private static ImageIcon ICON_LIBRARY = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) ); 97 private static ImageIcon ICON_ARTIFACT = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) ); 98 private static ImageIcon ICON_CLASSPATH = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) ); 99 private static ImageIcon ICON_BROKEN_BADGE = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_BROKEN_BADGE ) ); 100 101 private static ImageIcon ICON_BROKEN_JAR; 102 private static ImageIcon ICON_BROKEN_LIBRARY; 103 private static ImageIcon ICON_BROKEN_ARTIFACT; 104 105 private PropertyEvaluator evaluator; 106 107 private static final Map WELL_KNOWN_PATHS_NAMES = new HashMap (); 109 static { 110 WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.JAVAC_CLASSPATH, NbBundle.getMessage( EjbJarProjectProperties.class, "LBL_JavacClasspath_DisplayName" ) ); 111 WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.JAVAC_TEST_CLASSPATH, NbBundle.getMessage( EjbJarProjectProperties.class,"LBL_JavacTestClasspath_DisplayName") ); 112 WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.RUN_TEST_CLASSPATH, NbBundle.getMessage( EjbJarProjectProperties.class, "LBL_RunTestClasspath_DisplayName" ) ); 113 WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.BUILD_CLASSES_DIR, NbBundle.getMessage( EjbJarProjectProperties.class, "LBL_BuildClassesDir_DisplayName" ) ); 114 WELL_KNOWN_PATHS_NAMES.put( EjbJarProjectProperties.BUILD_TEST_CLASSES_DIR, NbBundle.getMessage (EjbJarProjectProperties.class,"LBL_BuildTestClassesDir_DisplayName") ); 115 }; 116 117 public ClassPathListCellRenderer( PropertyEvaluator evaluator ) { 118 super(); 119 this.evaluator = evaluator; 120 } 121 122 public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 123 124 ClassPathSupport.Item item = (ClassPathSupport.Item)value; 125 126 super.getListCellRendererComponent( list, getDisplayName( item ), index, isSelected, cellHasFocus ); 127 setIcon( getIcon( item ) ); 128 setToolTipText( getToolTipText( item ) ); 129 130 return this; 131 } 132 133 134 public String getDisplayName( ClassPathSupport.Item item ) { 135 136 switch ( item.getType() ) { 137 138 case ClassPathSupport.Item.TYPE_LIBRARY: 139 if ( item.isBroken() ) { 140 return NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_MISSING_LIBRARY", getLibraryName( item ) ); 141 } 142 else { 143 return item.getLibrary().getDisplayName(); 144 } 145 case ClassPathSupport.Item.TYPE_CLASSPATH: 146 String name = (String )WELL_KNOWN_PATHS_NAMES.get( EjbJarProjectProperties.getAntPropertyName( item.getReference() ) ); 147 return name == null ? item.getReference() : name; 148 case ClassPathSupport.Item.TYPE_ARTIFACT: 149 if ( item.isBroken() ) { 150 return NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_MISSING_PROJECT", getProjectName( item ) ); 151 } 152 else { 153 Project p = item.getArtifact().getProject(); 154 String projectName; 155 ProjectInformation pi = ProjectUtils.getInformation(p); 156 projectName = pi.getDisplayName(); 157 return MessageFormat.format(NbBundle.getMessage(EjbJarClassPathUi.class,"MSG_ProjectArtifactFormat"), new Object [] { 158 projectName, 159 item.getArtifactURI().toString() 160 }); 161 } 162 case ClassPathSupport.Item.TYPE_JAR: 163 if ( item.isBroken() ) { 164 return NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_MISSING_FILE", getFileRefName( item ) ); 165 } 166 else { 167 return item.getFile().getPath(); 168 } 169 } 170 171 return item.getReference(); } 173 174 public static Icon getIcon( ClassPathSupport.Item item ) { 175 176 switch ( item.getType() ) { 177 178 case ClassPathSupport.Item.TYPE_LIBRARY: 179 if ( item.isBroken() ) { 180 if ( ICON_BROKEN_LIBRARY == null ) { 181 ICON_BROKEN_LIBRARY = new ImageIcon ( Utilities.mergeImages( ICON_LIBRARY.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) ); 182 } 183 return ICON_BROKEN_LIBRARY; 184 } 185 else { 186 return ICON_LIBRARY; 187 } 188 case ClassPathSupport.Item.TYPE_ARTIFACT: 189 if ( item.isBroken() ) { 190 if ( ICON_BROKEN_ARTIFACT == null ) { 191 ICON_BROKEN_ARTIFACT = new ImageIcon ( Utilities.mergeImages( ICON_ARTIFACT.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) ); 192 } 193 return ICON_BROKEN_ARTIFACT; 194 } 195 else { 196 Project p = item.getArtifact().getProject(); 197 if (p != null) { 198 ProjectInformation pi = ProjectUtils.getInformation(p); 199 return pi.getIcon(); 200 } 201 return ICON_ARTIFACT; 202 } 203 case ClassPathSupport.Item.TYPE_JAR: 204 if ( item.isBroken() ) { 205 if ( ICON_BROKEN_JAR == null ) { 206 ICON_BROKEN_JAR = new ImageIcon ( Utilities.mergeImages( ICON_JAR.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) ); 207 } 208 return ICON_BROKEN_JAR; 209 } 210 else { 211 File file = item.getFile(); 212 return file.isDirectory() ? getFolderIcon() : ICON_JAR; 213 } 214 case ClassPathSupport.Item.TYPE_CLASSPATH: 215 return ICON_CLASSPATH; 216 217 } 218 219 return null; } 221 222 public String getToolTipText( ClassPathSupport.Item item ) { 223 if ( item.isBroken() && 224 ( item.getType() == ClassPathSupport.Item.TYPE_JAR || 225 item.getType() == ClassPathSupport.Item.TYPE_ARTIFACT ) ) { 226 return evaluator.evaluate( item.getReference() ); 227 } 228 229 return getDisplayName( item ); } 231 232 private static ImageIcon getFolderIcon() { 233 234 if ( ICON_FOLDER == null ) { 235 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 236 DataFolder dataFolder = DataFolder.findFolder( root ); 237 ICON_FOLDER = new ImageIcon ( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) ); 238 } 239 240 return ICON_FOLDER; 241 } 242 243 private String getProjectName( ClassPathSupport.Item item ) { 244 String ID = item.getReference(); 245 return ID.substring(12, ID.indexOf(".", 12)); } 248 249 private String getLibraryName( ClassPathSupport.Item item ) { 250 String ID = item.getReference(); 251 return ID.substring(7, ID.indexOf(".classpath")); } 254 255 private String getFileRefName( ClassPathSupport.Item item ) { 256 String ID = item.getReference(); 257 return ID.substring(17, ID.length()-1); 259 } 260 261 262 263 } 264 265 public static class ClassPathTableCellItemRenderer extends DefaultTableCellRenderer { 266 267 private ClassPathListCellRenderer renderer; 268 private TableCellRenderer booleanRenderer; 269 270 public ClassPathTableCellItemRenderer(PropertyEvaluator evaluator) { 271 renderer = new ClassPathListCellRenderer(evaluator); 272 } 273 274 public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) { 275 276 if (value instanceof ClassPathSupport.Item) { 277 ClassPathSupport.Item item = (ClassPathSupport.Item)value; 278 setIcon( renderer.getIcon( item ) ); 279 setToolTipText( renderer.getToolTipText( item ) ); 280 return super.getTableCellRendererComponent(table, renderer.getDisplayName( item ), isSelected, false, row, column); 281 } else { 282 if (value instanceof Boolean && booleanRenderer != null) 283 return booleanRenderer.getTableCellRendererComponent( table, value, isSelected, false, row, column ); 284 else { 285 setIcon( null ); 286 return super.getTableCellRendererComponent( table, value, isSelected, false, row, column ); 287 } 288 } 289 } 290 291 public void setBooleanRenderer(TableCellRenderer booleanRenderer) { 292 this.booleanRenderer = booleanRenderer; 293 } 294 } 295 296 297 public static class EditMediator implements ActionListener , ListSelectionListener { 298 299 private final Project project; 300 private final ListComponent list; 301 private final DefaultListModel listModel; 302 private final ListSelectionModel selectionModel; 303 private final ButtonModel addJar; 304 private final ButtonModel addLibrary; 305 private final ButtonModel addAntArtifact; 306 private final ButtonModel remove; 307 private final ButtonModel moveUp; 308 private final ButtonModel moveDown; 309 private final boolean includeNewFilesInDeployment; 310 311 public EditMediator( Project project, 312 ListComponent list, 313 ButtonModel addJar, 314 ButtonModel addLibrary, 315 ButtonModel addAntArtifact, 316 ButtonModel remove, 317 ButtonModel moveUp, 318 ButtonModel moveDown, 319 boolean includeNewFilesInDeployment) { 320 321 323 this.list = list; 324 325 if ( !( list.getModel() instanceof DefaultListModel ) ) { 326 throw new IllegalArgumentException ( "The list's model has to be of class DefaultListModel" ); } 328 329 this.listModel = (DefaultListModel )list.getModel(); 330 this.selectionModel = list.getSelectionModel(); 331 332 this.addJar = addJar; 333 this.addLibrary = addLibrary; 334 this.addAntArtifact = addAntArtifact; 335 this.remove = remove; 336 this.moveUp = moveUp; 337 this.moveDown = moveDown; 338 339 this.project = project; 340 this.includeNewFilesInDeployment = includeNewFilesInDeployment; 341 } 342 343 public static void register(Project project, 344 ListComponent list, 345 ButtonModel addJar, 346 ButtonModel addLibrary, 347 ButtonModel addAntArtifact, 348 ButtonModel remove, 349 ButtonModel moveUp, 350 ButtonModel moveDown, 351 boolean includeNewFilesInDeployment) { 352 353 EditMediator em = new EditMediator( project, 354 list, 355 addJar, 356 addLibrary, 357 addAntArtifact, 358 remove, 359 moveUp, 360 moveDown, 361 includeNewFilesInDeployment ); 362 363 addJar.addActionListener( em ); 365 addLibrary.addActionListener( em ); 366 addAntArtifact.addActionListener( em ); 367 remove.addActionListener( em ); 368 moveUp.addActionListener( em ); 369 moveDown.addActionListener( em ); 370 em.selectionModel.addListSelectionListener( em ); 372 em.valueChanged( null ); 374 } 375 376 378 380 public void actionPerformed( ActionEvent e ) { 381 382 Object source = e.getSource(); 383 384 if ( source == addJar ) { 385 JFileChooser chooser = new JFileChooser (); 387 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 388 chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES ); 389 chooser.setMultiSelectionEnabled( true ); 390 chooser.setDialogTitle( NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_AddJar_DialogTitle" ) ); chooser.setAcceptAllFileFilterUsed( false ); 393 chooser.setFileFilter( new SimpleFileFilter( 394 NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_ZipJarFolderFilter" ), new String [] {"ZIP","JAR"} ) ); File curDir = FoldersListSettings.getDefault().getLastUsedClassPathFolder(); 397 chooser.setCurrentDirectory (curDir); 398 int option = chooser.showOpenDialog( SwingUtilities.getWindowAncestor( list.getComponent() ) ); 400 if ( option == JFileChooser.APPROVE_OPTION ) { 401 402 File files[] = chooser.getSelectedFiles(); 403 int[] newSelection = ClassPathUiSupport.addJarFiles( listModel, list.getSelectedIndices(), files, includeNewFilesInDeployment ); 404 list.setSelectedIndices( newSelection ); 405 curDir = FileUtil.normalizeFile(chooser.getCurrentDirectory()); 406 FoldersListSettings.getDefault().setLastUsedClassPathFolder(curDir); 407 } 408 } 409 else if ( source == addLibrary ) { 410 Set includedLibraries = new HashSet (); 411 for (int i=0; i< listModel.getSize(); i++) { 412 ClassPathSupport.Item item = (ClassPathSupport.Item) listModel.get(i); 413 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken() ) { 414 includedLibraries.add( item.getLibrary() ); 415 } 416 } 417 Object [] options = new Object [] { 418 new JButton (NbBundle.getMessage (EjbJarClassPathUi.class,"LBL_AddLibrary")), 419 DialogDescriptor.CANCEL_OPTION 420 }; 421 ((JButton )options[0]).setEnabled(false); 422 ((JButton )options[0]).getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (EjbJarClassPathUi.class,"AD_AddLibrary")); 423 LibrariesChooser panel = new LibrariesChooser ((JButton )options[0], includedLibraries); 424 DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( EjbJarClassPathUi.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ), 425 true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null); 426 Dialog dlg = DialogDisplayer.getDefault().createDialog(desc); 427 dlg.setVisible(true); 428 if (desc.getValue() == options[0]) { 429 int[] newSelection = ClassPathUiSupport.addLibraries( listModel, list.getSelectedIndices(), panel.getSelectedLibraries(), includedLibraries, includeNewFilesInDeployment ); 430 list.setSelectedIndices( newSelection ); 431 } 432 dlg.dispose(); 433 } 434 else if ( source == addAntArtifact ) { 435 AntArtifactChooser.ArtifactItem artifactItems[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, project, list.getComponent().getParent()); 436 if (artifactItems != null) { 437 int[] newSelection = ClassPathUiSupport.addArtifacts( listModel, list.getSelectedIndices(), artifactItems, includeNewFilesInDeployment ); 438 list.setSelectedIndices( newSelection ); 439 } 440 } 441 else if ( source == remove ) { 442 int[] newSelection = ClassPathUiSupport.remove( listModel, list.getSelectedIndices() ); 443 list.setSelectedIndices( newSelection ); 444 } 445 else if ( source == moveUp ) { 446 int[] newSelection = ClassPathUiSupport.moveUp( listModel, list.getSelectedIndices() ); 447 list.setSelectedIndices( newSelection ); 448 } 449 else if ( source == moveDown ) { 450 int[] newSelection = ClassPathUiSupport.moveDown( listModel, list.getSelectedIndices() ); 451 list.setSelectedIndices( newSelection ); 452 } 453 } 454 455 456 458 public void valueChanged( ListSelectionEvent e ) { 459 460 boolean canRemove = false; 462 if ( selectionModel.getMinSelectionIndex() != -1 ) { 464 canRemove = true; 465 int iMin = selectionModel.getMinSelectionIndex(); 466 int iMax = selectionModel.getMinSelectionIndex(); 467 for ( int i = iMin; i <= iMax; i++ ) { 468 469 if ( selectionModel.isSelectedIndex( i ) ) { 470 ClassPathSupport.Item item = (ClassPathSupport.Item)listModel.get( i ); 471 if ( item.getType() == ClassPathSupport.Item.TYPE_CLASSPATH ) { 472 canRemove = false; 473 break; 474 } 475 } 476 } 477 } 478 479 remove.setEnabled( canRemove ); 484 moveUp.setEnabled( ClassPathUiSupport.canMoveUp( selectionModel ) ); 485 moveDown.setEnabled( ClassPathUiSupport.canMoveDown( selectionModel, listModel.getSize() ) ); 486 487 } 488 489 public interface ListComponent { 490 public Component getComponent(); 491 public int[] getSelectedIndices(); 492 public void setSelectedIndices(int[] indices); 493 public DefaultListModel getModel(); 494 public ListSelectionModel getSelectionModel(); 495 } 496 497 private static final class JListListComponent implements ListComponent { 498 private JList list; 499 private DefaultListModel model; 500 501 public JListListComponent(JList list) { 502 this.list = list; 503 this.model = model; 504 } 505 506 public Component getComponent() { 507 return list; 508 } 509 510 public int[] getSelectedIndices() { 511 return list.getSelectedIndices(); 512 } 513 514 public void setSelectedIndices(int[] indices) { 515 list.setSelectedIndices(indices); 516 } 517 518 public DefaultListModel getModel() { 519 return (DefaultListModel )list.getModel(); 520 } 521 522 public ListSelectionModel getSelectionModel() { 523 return list.getSelectionModel(); 524 } 525 } 526 527 private static final class JTableListComponent implements ListComponent { 528 private JTable table; 529 private DefaultListModel model; 530 531 public JTableListComponent(JTable table, DefaultListModel model) { 532 this.table = table; 533 this.model = model; 534 } 535 536 public Component getComponent() { 537 return table; 538 } 539 540 public int[] getSelectedIndices() { 541 return table.getSelectedRows(); 542 } 543 544 public void setSelectedIndices(int[] indices) { 545 table.clearSelection(); 546 for (int i = 0; i < indices.length; i++) { 547 table.addRowSelectionInterval(indices[i], indices[i]); 548 } 549 } 550 551 public DefaultListModel getModel() { 552 return model; 553 } 554 555 public ListSelectionModel getSelectionModel() { 556 return table.getSelectionModel(); 557 } 558 } 559 560 public static ListComponent createListComponent(JList list) { 561 return new JListListComponent(list); 562 } 563 564 public static ListComponent createListComponent(JTable table, DefaultListModel model) { 565 return new JTableListComponent(table, model); 566 } 567 } 568 569 private static class SimpleFileFilter extends FileFilter { 570 571 private String description; 572 private Collection extensions; 573 574 575 public SimpleFileFilter (String description, String [] extensions) { 576 this.description = description; 577 this.extensions = Arrays.asList(extensions); 578 } 579 580 public boolean accept(File f) { 581 if (f.isDirectory()) 582 return true; 583 String name = f.getName(); 584 int index = name.lastIndexOf('.'); if (index <= 0 || index==name.length()-1) 586 return false; 587 String extension = name.substring (index+1).toUpperCase(); 588 return this.extensions.contains(extension); 589 } 590 591 public String getDescription() { 592 return this.description; 593 } 594 } 595 596 } 597 | Popular Tags |