1 19 20 package org.netbeans.modules.j2ee.clientproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.net.URI ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.List ; 32 import javax.swing.DefaultListModel ; 33 import javax.swing.JFileChooser ; 34 import javax.swing.JPanel ; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.api.project.ProjectUtils; 38 import org.netbeans.api.project.ant.AntArtifact; 39 import org.netbeans.api.project.ant.AntArtifactQuery; 40 import org.netbeans.spi.project.ui.support.ProjectChooser; 41 import org.netbeans.modules.j2ee.clientproject.ui.FoldersListSettings; 42 import org.openide.DialogDisplayer; 43 import org.openide.ErrorManager; 44 import org.openide.NotifyDescriptor; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.NbBundle; 48 49 54 public class AntArtifactChooser extends JPanel implements PropertyChangeListener { 55 56 private String [] artifactTypes; 57 58 59 public AntArtifactChooser( String [] artifactTypes, JFileChooser chooser ) { 60 this.artifactTypes = artifactTypes; 61 62 initComponents(); 63 jListArtifacts.setModel( new DefaultListModel () ); 64 chooser.addPropertyChangeListener( this ); 65 } 66 67 public AntArtifactChooser( String artifactType, JFileChooser chooser ) { 68 this(new String [] {artifactType}, chooser); 69 } 70 71 72 77 private void initComponents() { 79 java.awt.GridBagConstraints gridBagConstraints; 80 81 jLabelName = new javax.swing.JLabel (); 82 jTextFieldName = new javax.swing.JTextField (); 83 jLabelJarFiles = new javax.swing.JLabel (); 84 jScrollPane1 = new javax.swing.JScrollPane (); 85 jListArtifacts = new javax.swing.JList (); 86 87 setLayout(new java.awt.GridBagLayout ()); 88 89 jLabelName.setLabelFor(jTextFieldName); 90 org.openide.awt.Mnemonics.setLocalizedText(jLabelName, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel")); 91 gridBagConstraints = new java.awt.GridBagConstraints (); 92 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 93 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 94 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 95 add(jLabelName, gridBagConstraints); 96 97 jTextFieldName.setEditable(false); 98 gridBagConstraints = new java.awt.GridBagConstraints (); 99 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 100 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 101 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 102 gridBagConstraints.weightx = 1.0; 103 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 104 add(jTextFieldName, gridBagConstraints); 105 jTextFieldName.getAccessibleContext().setAccessibleDescription(null); 106 107 jLabelJarFiles.setLabelFor(jListArtifacts); 108 org.openide.awt.Mnemonics.setLocalizedText(jLabelJarFiles, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel")); 109 gridBagConstraints = new java.awt.GridBagConstraints (); 110 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 111 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 112 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 113 add(jLabelJarFiles, gridBagConstraints); 114 115 jScrollPane1.setViewportView(jListArtifacts); 116 jListArtifacts.getAccessibleContext().setAccessibleDescription(null); 117 118 gridBagConstraints = new java.awt.GridBagConstraints (); 119 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 120 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 121 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 122 gridBagConstraints.weightx = 1.0; 123 gridBagConstraints.weighty = 1.0; 124 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 125 add(jScrollPane1, gridBagConstraints); 126 127 } 129 130 public void propertyChange(PropertyChangeEvent e) { 131 if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) { 132 JFileChooser chooser = (JFileChooser ) e.getSource(); 134 File dir = chooser.getSelectedFile(); Project project = getProject(dir); populateAccessory(project); 137 } 138 } 139 140 private Project getProject( File projectDir ) { 141 142 if (projectDir == null) { return null; 144 } 145 146 try { 147 File normProjectDir = FileUtil.normalizeFile(projectDir); 148 FileObject fo = FileUtil.toFileObject(normProjectDir); 149 if (fo != null) { 150 return ProjectManager.getDefault().findProject(fo); 151 } 152 } catch (IOException e) { 153 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 154 } 156 157 return null; 158 } 159 160 164 private void populateAccessory( Project project ) { 165 166 DefaultListModel model = (DefaultListModel )jListArtifacts.getModel(); 167 model.clear(); 168 jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); 170 if ( project != null ) { 171 172 List <AntArtifact> artifacts = new ArrayList <AntArtifact>(); 173 for (int i=0; i<artifactTypes.length; i++) { 174 artifacts.addAll(Arrays.asList(AntArtifactQuery.findArtifactsByType(project, artifactTypes[i]))); 175 } 176 177 for (AntArtifact artifact : artifacts) { 178 URI uris[] = artifact.getArtifactLocations(); 179 for( int y = 0; y < uris.length; y++ ) { 180 model.addElement( new ArtifactItem(artifact, uris[y])); 181 } 182 } 183 jListArtifacts.setSelectionInterval(0, model.size()); 184 } 185 186 } 187 188 189 private javax.swing.JLabel jLabelJarFiles; 191 private javax.swing.JLabel jLabelName; 192 private javax.swing.JList jListArtifacts; 193 private javax.swing.JScrollPane jScrollPane1; 194 private javax.swing.JTextField jTextFieldName; 195 197 198 201 public static ArtifactItem[] showDialog( String [] artifactTypes, Project master, Component parent ) { 202 203 JFileChooser chooser = ProjectChooser.projectChooser(); 204 chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); chooser.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AntArtifactChooser.class,"AD_AACH_SelectProject")); 207 AntArtifactChooser accessory = new AntArtifactChooser( artifactTypes, chooser ); 208 chooser.setAccessory( accessory ); 209 chooser.setPreferredSize( new Dimension ( 650, 380 ) ); 210 chooser.setCurrentDirectory(FoldersListSettings.getDefault().getLastUsedArtifactFolder()); 211 212 int option = chooser.showOpenDialog( parent ); 214 if ( option == JFileChooser.APPROVE_OPTION ) { 215 216 File dir = chooser.getSelectedFile(); 217 dir = FileUtil.normalizeFile(dir); 218 Project selectedProject = accessory.getProject( dir ); 219 220 if ( selectedProject == null ) { 221 return null; 222 } 223 224 if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) { 225 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 226 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ), 227 NotifyDescriptor.INFORMATION_MESSAGE ) ); 228 return null; 229 } 230 231 if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) { 232 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 233 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ), 234 NotifyDescriptor.INFORMATION_MESSAGE ) ); 235 return null; 236 } 237 238 FoldersListSettings.getDefault().setLastUsedArtifactFolder(FileUtil.normalizeFile(chooser.getCurrentDirectory())); 239 240 Object [] tmp = new Object [accessory.jListArtifacts.getModel().getSize()]; 241 int count = 0; 242 for(int i = 0; i < tmp.length; i++) { 243 if (accessory.jListArtifacts.isSelectedIndex(i)) { 244 tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i); 245 count++; 246 } 247 } 248 ArtifactItem artifactItems[] = new ArtifactItem[count]; 249 System.arraycopy(tmp, 0, artifactItems, 0, count); 250 return artifactItems; 251 } else { 252 return null; 253 } 254 255 } 256 257 260 public static ArtifactItem[] showDialog( String artifactType, Project master, Component parent ) { 261 262 JFileChooser chooser = ProjectChooser.projectChooser(); 263 chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); 266 AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser ); 267 chooser.setAccessory( accessory ); 268 chooser.setPreferredSize( new Dimension ( 650, 380 ) ); 269 chooser.setCurrentDirectory(FoldersListSettings.getDefault().getLastUsedArtifactFolder()); 270 271 int option = chooser.showOpenDialog( parent ); 273 if ( option == JFileChooser.APPROVE_OPTION ) { 274 275 File dir = chooser.getSelectedFile(); 276 dir = FileUtil.normalizeFile(dir); 277 Project selectedProject = accessory.getProject( dir ); 278 279 if ( selectedProject == null ) { 280 return null; 281 } 282 283 if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) { 284 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 285 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ), 286 NotifyDescriptor.INFORMATION_MESSAGE ) ); 287 return null; 288 } 289 290 if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) { 291 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 292 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ), 293 NotifyDescriptor.INFORMATION_MESSAGE ) ); 294 return null; 295 } 296 297 FoldersListSettings.getDefault().setLastUsedArtifactFolder(FileUtil.normalizeFile(chooser.getCurrentDirectory())); 298 299 Object [] tmp = new Object [accessory.jListArtifacts.getModel().getSize()]; 300 int count = 0; 301 for(int i = 0; i < tmp.length; i++) { 302 if (accessory.jListArtifacts.isSelectedIndex(i)) { 303 tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i); 304 count++; 305 } 306 } 307 ArtifactItem artifactItems[] = new ArtifactItem[count]; 308 System.arraycopy(tmp, 0, artifactItems, 0, count); 309 return artifactItems; 310 } else { 311 return null; 312 } 313 314 } 315 316 319 public static class ArtifactItem { 320 321 private AntArtifact artifact; 322 private URI artifactURI; 323 324 public ArtifactItem(AntArtifact artifact, URI artifactURI) { 325 this.artifact = artifact; 326 this.artifactURI = artifactURI; 327 } 328 329 public AntArtifact getArtifact() { 330 return artifact; 331 } 332 333 public URI getArtifactURI() { 334 return artifactURI; 335 } 336 337 public String toString() { 338 return artifactURI.toString(); 339 } 340 341 } 342 } 343 | Popular Tags |