1 19 20 package org.netbeans.modules.web.project.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.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import javax.swing.ButtonModel ; 35 import javax.swing.DefaultListSelectionModel ; 36 import javax.swing.Icon ; 37 import javax.swing.ImageIcon ; 38 import javax.swing.JButton ; 39 import javax.swing.JFileChooser ; 40 import javax.swing.JTable ; 41 import javax.swing.ListSelectionModel ; 42 import javax.swing.SwingUtilities ; 43 import javax.swing.event.ListSelectionEvent ; 44 import javax.swing.event.ListSelectionListener ; 45 import javax.swing.event.TableModelEvent ; 46 import javax.swing.event.TableModelListener ; 47 import javax.swing.table.DefaultTableCellRenderer ; 48 49 import org.openide.DialogDescriptor; 50 import org.openide.DialogDisplayer; 51 import org.openide.NotifyDescriptor; 52 import org.openide.filesystems.FileObject; 53 import org.openide.filesystems.FileUtil; 54 import org.openide.filesystems.Repository; 55 import org.openide.loaders.DataFolder; 56 import org.openide.util.NbBundle; 57 import org.openide.util.Utilities; 58 59 import org.netbeans.api.java.project.JavaProjectConstants; 60 import org.netbeans.api.project.Project; 61 import org.netbeans.modules.web.api.webmodule.WebModule; 62 63 import org.netbeans.modules.web.project.classpath.ClassPathSupport; 64 import org.netbeans.modules.web.project.ui.FoldersListSettings; 65 import org.netbeans.modules.web.project.ui.customizer.WarIncludesUiSupport.ClasspathTableModel; 66 67 71 public class WarIncludesUi { 72 73 private WarIncludesUi() {} 74 75 77 public static class EditMediator implements ActionListener , ListSelectionListener , TableModelListener { 78 79 private final Project project; 80 private final JTable list; 81 private final ClasspathTableModel listModel; 82 private final ListSelectionModel selectionModel; 83 private final ButtonModel addJar; 84 private final ButtonModel addLibrary; 85 private final ButtonModel addAntArtifact; 86 private final ButtonModel remove; 87 88 public EditMediator( Project project, 89 JTable list, 90 ButtonModel addJar, 91 ButtonModel addLibrary, 92 ButtonModel addAntArtifact, 93 ButtonModel remove) { 94 95 this.list = list; 96 97 if ( !( list.getModel() instanceof ClasspathTableModel ) ) { 98 throw new IllegalArgumentException ( "The list's model has to be of class DefaultListModel" ); } 100 101 this.listModel = (ClasspathTableModel) list.getModel(); 102 this.selectionModel = list.getSelectionModel(); 103 104 this.addJar = addJar; 105 this.addLibrary = addLibrary; 106 this.addAntArtifact = addAntArtifact; 107 this.remove = remove; 108 109 this.project = project; 110 } 111 112 public static void register(Project project, 113 JTable list, 114 ButtonModel addJar, 115 ButtonModel addLibrary, 116 ButtonModel addAntArtifact, 117 ButtonModel remove) { 118 119 EditMediator em = new EditMediator(project, list, addJar, addLibrary, addAntArtifact, remove); 120 121 addJar.addActionListener( em ); 123 addLibrary.addActionListener( em ); 124 addAntArtifact.addActionListener( em ); 125 remove.addActionListener( em ); 126 em.selectionModel.addListSelectionListener( em ); 128 em.listModel.addTableModelListener(em); 129 em.valueChanged( null ); 131 } 132 133 135 137 public void actionPerformed( ActionEvent e ) { 138 139 Object source = e.getSource(); 140 141 if ( source == addJar ) { 142 JFileChooser chooser = new JFileChooser (); 144 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 145 chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES ); 146 chooser.setMultiSelectionEnabled( true ); 147 chooser.setDialogTitle( NbBundle.getMessage( WarIncludesUi.class, "LBL_AddFile_DialogTitle" ) ); chooser.setAcceptAllFileFilterUsed(true); 149 File curDir = FoldersListSettings.getDefault().getLastUsedClassPathFolder(); 150 chooser.setCurrentDirectory (curDir); 151 int option = chooser.showOpenDialog( SwingUtilities.getWindowAncestor( list ) ); 153 if ( option == JFileChooser.APPROVE_OPTION ) { 154 155 File files[] = chooser.getSelectedFiles(); 156 WarIncludesUiSupport.addJarFiles(files, listModel); 157 curDir = FileUtil.normalizeFile(chooser.getCurrentDirectory()); 158 FoldersListSettings.getDefault().setLastUsedClassPathFolder(curDir); 159 } 160 } 161 else if ( source == addLibrary ) { 162 Set includedLibraries = new HashSet (); 163 Iterator it = WarIncludesUiSupport.getIterator(listModel); 164 while (it.hasNext()) { 165 ClassPathSupport.Item item = (ClassPathSupport.Item) it.next(); 166 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) { 167 includedLibraries.add( item.getLibrary() ); 168 } 169 } 170 Object [] options = new Object [] { 171 new JButton (NbBundle.getMessage (WarIncludesUi.class,"LBL_AddLibrary")), 172 DialogDescriptor.CANCEL_OPTION 173 }; 174 ((JButton )options[0]).setEnabled(false); 175 ((JButton )options[0]).getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (WarIncludesUi.class,"AD_AddLibrary")); 176 177 WebModule wm = WebModule.getWebModule(project.getProjectDirectory()); 178 String j2eeVersion = wm.getJ2eePlatformVersion(); 179 LibrariesChooser panel = new LibrariesChooser ((JButton )options[0], j2eeVersion); 180 DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( WarIncludesUi.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ), 181 true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null); 182 Dialog dlg = DialogDisplayer.getDefault().createDialog(desc); 183 dlg.setVisible(true); 184 if (desc.getValue() == options[0]) { 185 WarIncludesUiSupport.addLibraries(panel.getSelectedLibraries(), includedLibraries, list); 186 } 187 dlg.dispose(); 188 } 189 else if ( source == addAntArtifact ) { 190 AntArtifactChooser.ArtifactItem artifactItems[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, project, list.getParent()); 191 if (artifactItems != null) { 192 WarIncludesUiSupport.addArtifacts(artifactItems, listModel); 193 } 194 } 195 else if ( source == remove ) { 196 WarIncludesUiSupport.remove( list); 197 } 198 } 199 200 201 203 public void valueChanged( ListSelectionEvent e ) { 204 DefaultListSelectionModel sm = (DefaultListSelectionModel ) list.getSelectionModel(); 205 int index = sm.getMinSelectionIndex(); 206 207 boolean canRemove = index != -1; 209 if (canRemove) { 211 ClassPathSupport.Item vcpi = (ClassPathSupport.Item) listModel.getValueAt(index, 0); 212 if (!vcpi.canDelete()) 213 canRemove = false; 214 } 215 216 remove.setEnabled(canRemove); 217 218 } 219 220 public void tableChanged(TableModelEvent e) { 222 if (e.getColumn() == 1) { 223 ClassPathSupport.Item cpItem = (ClassPathSupport.Item) listModel.getValueAt(e.getFirstRow(), 0); 224 String newPathInWar = (String ) listModel.getValueAt(e.getFirstRow(), 1); 225 String message = null; 226 if (cpItem.getType() == ClassPathSupport.Item.TYPE_JAR && newPathInWar.startsWith("WEB-INF")) { if (newPathInWar.equals("WEB-INF\\lib") || newPathInWar.equals("WEB-INF/lib")) { if (((File ) cpItem.getObject()).isDirectory()) { 230 message = NbBundle.getMessage(WarIncludesUi.class, 231 "MSG_NO_FOLDER_IN_WEBINF_LIB", newPathInWar); } else { 233 message = NbBundle.getMessage(WarIncludesUi.class, 234 "MSG_NO_FILE_IN_WEBINF_LIB", newPathInWar); } 236 } else if (newPathInWar.equals("WEB-INF\\classes") || newPathInWar.equals("WEB-INF/classes")) { message = NbBundle.getMessage(WarIncludesUi.class, 238 "MSG_NO_FOLDER_IN_WEBINF_CLASSES", newPathInWar); } 240 } 241 if (message != null) { 242 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (message, NotifyDescriptor.WARNING_MESSAGE)); 243 } 244 cpItem.setPathInWAR((String ) listModel.getValueAt(e.getFirstRow(), 1)); 245 } 246 } 247 } 248 249 static class ClassPathCellRenderer extends DefaultTableCellRenderer { 250 251 private static String RESOURCE_ICON_JAR = "org/netbeans/modules/web/project/ui/resources/jar.gif"; private static String RESOURCE_ICON_LIBRARY = "org/netbeans/modules/web/project/ui/resources/libraries.gif"; private static String RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/web/project/ui/resources/projectDependencies.gif"; private static String RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/web/project/ui/resources/referencedClasspath.gif"; private static String RESOURCE_ICON_BROKEN_BADGE = "org/netbeans/modules/web/project/ui/resources/brokenProjectBadge.gif"; 257 private static ImageIcon ICON_JAR = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_JAR ) ); 258 private static ImageIcon ICON_FOLDER = null; 259 private static ImageIcon ICON_LIBRARY = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) ); 260 private static ImageIcon ICON_ARTIFACT = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) ); 261 private static ImageIcon ICON_CLASSPATH = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) ); 262 private static ImageIcon ICON_BROKEN_BADGE = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_BROKEN_BADGE ) ); 263 264 private static ImageIcon ICON_BROKEN_JAR; 265 private static ImageIcon ICON_BROKEN_LIBRARY; 266 private static ImageIcon ICON_BROKEN_ARTIFACT; 267 268 private static final Map WELL_KNOWN_PATHS_NAMES = new HashMap (); 270 static { 271 WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.JAVAC_CLASSPATH, NbBundle.getMessage( WarIncludesUi.class, "LBL_JavacClasspath_DisplayName" ) ); 272 WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.JAVAC_TEST_CLASSPATH, NbBundle.getMessage( WarIncludesUi.class,"LBL_JavacTestClasspath_DisplayName") ); 273 WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.RUN_TEST_CLASSPATH, NbBundle.getMessage( WarIncludesUi.class, "LBL_RunTestClasspath_DisplayName" ) ); 274 WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.BUILD_CLASSES_DIR, NbBundle.getMessage( WarIncludesUi.class, "LBL_BuildClassesDir_DisplayName" ) ); 275 WELL_KNOWN_PATHS_NAMES.put( WebProjectProperties.BUILD_TEST_CLASSES_DIR, NbBundle.getMessage (WarIncludesUi.class,"LBL_BuildTestClassesDir_DisplayName") ); 276 }; 277 278 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 279 String s = null; 280 if (value instanceof ClassPathSupport.Item) { 281 final ClassPathSupport.Item item = (ClassPathSupport.Item) value; 282 setIcon(getIcon(item)); 283 setToolTipText(getToolTipText(item)); 284 s = getDisplayName(item); 285 } 286 return super.getTableCellRendererComponent(table, s, isSelected, false, row, column); 287 } 288 289 private String getDisplayName( ClassPathSupport.Item item ) { 290 switch ( item.getType() ) { 291 292 case ClassPathSupport.Item.TYPE_LIBRARY: 293 if ( item.isBroken() ) { 294 return NbBundle.getMessage( WarIncludesUi.class, "LBL_MISSING_LIBRARY", getLibraryName( item ) ); 295 } 296 else { 297 return item.getLibrary().getDisplayName(); 298 } 299 case ClassPathSupport.Item.TYPE_CLASSPATH: 300 String name = (String )WELL_KNOWN_PATHS_NAMES.get( WebProjectProperties.getAntPropertyName( item.getReference() ) ); 301 return name == null ? item.getReference() : name; 302 case ClassPathSupport.Item.TYPE_ARTIFACT: 303 if ( item.isBroken() ) { 304 return NbBundle.getMessage( WarIncludesUi.class, "LBL_MISSING_PROJECT", getProjectName( item ) ); 305 } 306 else { 307 return item.getArtifactURI().toString(); 308 } 309 case ClassPathSupport.Item.TYPE_JAR: 310 if ( item.isBroken() ) { 311 return NbBundle.getMessage( WarIncludesUi.class, "LBL_MISSING_FILE", getFileRefName( item ) ); 312 } 313 else { 314 return item.getFile().getPath(); 315 } 316 } 317 318 return item.getReference(); } 320 321 static Icon getIcon( ClassPathSupport.Item item ) { 322 323 switch ( item.getType() ) { 324 325 case ClassPathSupport.Item.TYPE_LIBRARY: 326 if ( item.isBroken() ) { 327 if ( ICON_BROKEN_LIBRARY == null ) { 328 ICON_BROKEN_LIBRARY = new ImageIcon ( Utilities.mergeImages( ICON_LIBRARY.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) ); 329 } 330 return ICON_BROKEN_LIBRARY; 331 } 332 else { 333 return ICON_LIBRARY; 334 } 335 case ClassPathSupport.Item.TYPE_ARTIFACT: 336 if ( item.isBroken() ) { 337 if ( ICON_BROKEN_ARTIFACT == null ) { 338 ICON_BROKEN_ARTIFACT = new ImageIcon ( Utilities.mergeImages( ICON_ARTIFACT.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) ); 339 } 340 return ICON_BROKEN_ARTIFACT; 341 } 342 else { 343 return ICON_ARTIFACT; 344 } 345 case ClassPathSupport.Item.TYPE_JAR: 346 if ( item.isBroken() ) { 347 if ( ICON_BROKEN_JAR == null ) { 348 ICON_BROKEN_JAR = new ImageIcon ( Utilities.mergeImages( ICON_JAR.getImage(), ICON_BROKEN_BADGE.getImage(), 7, 7 ) ); 349 } 350 return ICON_BROKEN_JAR; 351 } 352 else { 353 File file = item.getFile(); 354 return file.isDirectory() ? getFolderIcon() : ICON_JAR; 355 } 356 case ClassPathSupport.Item.TYPE_CLASSPATH: 357 return ICON_CLASSPATH; 358 359 } 360 361 return null; } 363 364 private String getToolTipText( ClassPathSupport.Item item ) { 365 371 return getDisplayName( item ); } 373 374 private static ImageIcon getFolderIcon() { 375 376 if ( ICON_FOLDER == null ) { 377 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 378 DataFolder dataFolder = DataFolder.findFolder( root ); 379 ICON_FOLDER = new ImageIcon ( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) ); 380 } 381 382 return ICON_FOLDER; 383 } 384 385 private String getProjectName( ClassPathSupport.Item item ) { 386 String ID = item.getReference(); 387 return ID.substring(12, ID.indexOf('.', 12)); } 390 391 private String getLibraryName( ClassPathSupport.Item item ) { 392 String ID = item.getReference(); 393 return ID.substring(7, ID.indexOf(".classpath")); } 396 397 private String getFileRefName( ClassPathSupport.Item item ) { 398 String ID = item.getReference(); 399 return ID.substring(17, ID.length()-1); 401 } 402 } 403 } 404 | Popular Tags |