1 19 20 package org.netbeans.modules.java.j2seproject.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.Iterator ; 32 import java.util.List ; 33 import javax.swing.DefaultListModel ; 34 import javax.swing.JFileChooser ; 35 import javax.swing.JPanel ; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.api.project.ProjectUtils; 39 import org.netbeans.api.project.ant.AntArtifact; 40 import org.netbeans.api.project.ant.AntArtifactQuery; 41 import org.netbeans.spi.project.ui.support.ProjectChooser; 42 import org.netbeans.modules.java.j2seproject.ui.FoldersListSettings; 43 import org.openide.DialogDisplayer; 44 import org.openide.ErrorManager; 45 import org.openide.NotifyDescriptor; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.util.NbBundle; 49 50 55 public class AntArtifactChooser extends JPanel implements PropertyChangeListener { 56 57 58 private String [] artifactTypes; 59 60 61 public AntArtifactChooser( String [] artifactTypes, JFileChooser chooser ) { 62 this.artifactTypes = artifactTypes; 63 64 initComponents(); 65 jListArtifacts.setModel( new DefaultListModel () ); 66 chooser.addPropertyChangeListener( this ); 67 } 68 69 74 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 76 77 jLabelName = new javax.swing.JLabel (); 78 jTextFieldName = new javax.swing.JTextField (); 79 jLabelJarFiles = new javax.swing.JLabel (); 80 jScrollPane1 = new javax.swing.JScrollPane (); 81 jListArtifacts = new javax.swing.JList (); 82 83 setLayout(new java.awt.GridBagLayout ()); 84 85 jLabelName.setLabelFor(jTextFieldName); 86 org.openide.awt.Mnemonics.setLocalizedText(jLabelName, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel")); 87 gridBagConstraints = new java.awt.GridBagConstraints (); 88 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 89 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 90 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 91 add(jLabelName, gridBagConstraints); 92 93 jTextFieldName.setEditable(false); 94 gridBagConstraints = new java.awt.GridBagConstraints (); 95 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 96 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 97 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 98 gridBagConstraints.weightx = 1.0; 99 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 100 add(jTextFieldName, gridBagConstraints); 101 jTextFieldName.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/j2seproject/ui/customizer/Bundle").getString("AD_AACH_ProjectName_JLabel")); 102 103 jLabelJarFiles.setLabelFor(jListArtifacts); 104 org.openide.awt.Mnemonics.setLocalizedText(jLabelJarFiles, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel")); 105 gridBagConstraints = new java.awt.GridBagConstraints (); 106 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 107 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 108 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 109 add(jLabelJarFiles, gridBagConstraints); 110 111 jScrollPane1.setViewportView(jListArtifacts); 112 jListArtifacts.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/j2seproject/ui/customizer/Bundle").getString("AD_AACH_ProjectJarFiles_JLabel")); 113 114 gridBagConstraints = new java.awt.GridBagConstraints (); 115 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 116 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 117 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 118 gridBagConstraints.weightx = 1.0; 119 gridBagConstraints.weighty = 1.0; 120 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 121 add(jScrollPane1, gridBagConstraints); 122 123 } 125 126 public void propertyChange(PropertyChangeEvent e) { 127 if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) { 128 JFileChooser chooser = (JFileChooser ) e.getSource(); 130 File dir = chooser.getSelectedFile(); Project project = getProject(dir); populateAccessory(project); 133 } 134 } 135 136 private Project getProject( File projectDir ) { 137 138 if (projectDir == null) { return null; 140 } 141 142 try { 143 File normProjectDir = FileUtil.normalizeFile(projectDir); 144 FileObject fo = FileUtil.toFileObject(normProjectDir); 145 if (fo != null) { 146 return ProjectManager.getDefault().findProject(fo); 147 } 148 } catch (IOException e) { 149 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 150 } 152 153 return null; 154 } 155 156 160 private void populateAccessory( Project project ) { 161 162 DefaultListModel model = (DefaultListModel )jListArtifacts.getModel(); 163 model.clear(); 164 jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); 166 if ( project != null ) { 167 168 List artifacts = new ArrayList (); 169 for (int i=0; i<artifactTypes.length; i++) { 170 artifacts.addAll (Arrays.asList(AntArtifactQuery.findArtifactsByType(project, artifactTypes[i]))); 171 } 172 173 for( Iterator it = artifacts.iterator(); it.hasNext();) { 174 AntArtifact artifact = (AntArtifact) it.next(); 175 URI uris[] = artifact.getArtifactLocations(); 176 for( int y = 0; y < uris.length; y++ ) { 177 model.addElement( new ArtifactItem(artifact, uris[y])); 178 } 179 } 180 jListArtifacts.setSelectionInterval(0, model.size()); 181 } 182 183 } 184 185 186 private javax.swing.JLabel jLabelJarFiles; 188 private javax.swing.JLabel jLabelName; 189 private javax.swing.JList jListArtifacts; 190 private javax.swing.JScrollPane jScrollPane1; 191 private javax.swing.JTextField jTextFieldName; 192 194 195 198 public static ArtifactItem[] showDialog( String [] artifactTypes, Project master, Component parent ) { 199 200 JFileChooser chooser = ProjectChooser.projectChooser(); 201 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")); 204 AntArtifactChooser accessory = new AntArtifactChooser( artifactTypes, chooser ); 205 chooser.setAccessory( accessory ); 206 chooser.setPreferredSize( new Dimension ( 650, 380 ) ); 207 chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder()); 208 209 int option = chooser.showOpenDialog( parent ); 211 if ( option == JFileChooser.APPROVE_OPTION ) { 212 213 File dir = chooser.getSelectedFile(); 214 dir = FileUtil.normalizeFile (dir); 215 Project selectedProject = accessory.getProject( dir ); 216 217 if ( selectedProject == null ) { 218 return null; 219 } 220 221 if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) { 222 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 223 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ), 224 NotifyDescriptor.INFORMATION_MESSAGE ) ); 225 return null; 226 } 227 228 if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) { 229 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 230 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ), 231 NotifyDescriptor.INFORMATION_MESSAGE ) ); 232 return null; 233 } 234 235 FoldersListSettings.getDefault().setLastUsedArtifactFolder (FileUtil.normalizeFile(chooser.getCurrentDirectory())); 236 237 Object [] tmp = new Object [accessory.jListArtifacts.getModel().getSize()]; 238 int count = 0; 239 for(int i = 0; i < tmp.length; i++) { 240 if (accessory.jListArtifacts.isSelectedIndex(i)) { 241 tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i); 242 count++; 243 } 244 } 245 ArtifactItem artifactItems[] = new ArtifactItem[count]; 246 System.arraycopy(tmp, 0, artifactItems, 0, count); 247 return artifactItems; 248 } 249 else { 250 return null; 251 } 252 253 } 254 255 258 public static class ArtifactItem { 259 260 private AntArtifact artifact; 261 private URI artifactURI; 262 263 public ArtifactItem(AntArtifact artifact, URI artifactURI) { 264 this.artifact = artifact; 265 this.artifactURI = artifactURI; 266 } 267 268 public AntArtifact getArtifact() { 269 return artifact; 270 } 271 272 public URI getArtifactURI() { 273 return artifactURI; 274 } 275 276 public String toString() { 277 return artifactURI.toString(); 278 } 279 280 } 281 } 282 | Popular Tags |