1 19 20 package org.netbeans.modules.web.project.ui.customizer; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.net.URI ; 28 import javax.swing.DefaultListModel ; 29 import javax.swing.JFileChooser ; 30 import javax.swing.JPanel ; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.api.project.ant.AntArtifact; 35 import org.netbeans.api.project.ant.AntArtifactQuery; 36 import org.netbeans.spi.project.ui.support.ProjectChooser; 37 import org.netbeans.modules.web.project.ui.FoldersListSettings; 38 import org.openide.DialogDisplayer; 39 import org.openide.ErrorManager; 40 import org.openide.NotifyDescriptor; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.util.NbBundle; 44 45 50 public class AntArtifactChooser extends JPanel implements PropertyChangeListener { 51 52 private String artifactType; 54 55 56 public AntArtifactChooser( String artifactType, JFileChooser chooser ) { 57 this.artifactType = artifactType; 58 59 initComponents(); 60 jListArtifacts.setModel( new DefaultListModel () ); 61 chooser.addPropertyChangeListener( this ); 62 } 63 64 69 private void initComponents() { 71 java.awt.GridBagConstraints gridBagConstraints; 72 73 jLabelName = new javax.swing.JLabel (); 74 jTextFieldName = new javax.swing.JTextField (); 75 jLabelJarFiles = new javax.swing.JLabel (); 76 jScrollPane1 = new javax.swing.JScrollPane (); 77 jListArtifacts = new javax.swing.JList (); 78 79 setLayout(new java.awt.GridBagLayout ()); 80 81 jLabelName.setLabelFor(jTextFieldName); 82 org.openide.awt.Mnemonics.setLocalizedText(jLabelName, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel")); 83 gridBagConstraints = new java.awt.GridBagConstraints (); 84 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 85 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 86 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 87 add(jLabelName, gridBagConstraints); 88 89 jTextFieldName.setEditable(false); 90 gridBagConstraints = new java.awt.GridBagConstraints (); 91 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 92 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 93 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 94 gridBagConstraints.weightx = 1.0; 95 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 96 add(jTextFieldName, gridBagConstraints); 97 jTextFieldName.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel")); 98 99 jLabelJarFiles.setLabelFor(jListArtifacts); 100 org.openide.awt.Mnemonics.setLocalizedText(jLabelJarFiles, org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel")); 101 gridBagConstraints = new java.awt.GridBagConstraints (); 102 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 103 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 104 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 105 add(jLabelJarFiles, gridBagConstraints); 106 107 jScrollPane1.setViewportView(jListArtifacts); 108 jListArtifacts.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel")); 109 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 112 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 113 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 114 gridBagConstraints.weightx = 1.0; 115 gridBagConstraints.weighty = 1.0; 116 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 117 add(jScrollPane1, gridBagConstraints); 118 119 } 120 122 123 public void propertyChange(PropertyChangeEvent e) { 124 if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) { 125 JFileChooser chooser = (JFileChooser ) e.getSource(); 127 File dir = chooser.getSelectedFile(); Project project = getProject(dir); populateAccessory(project); 130 } 131 } 132 133 private Project getProject( File projectDir ) { 134 135 if (projectDir == null) { return null; 137 } 138 139 try { 140 File normProjectDir = FileUtil.normalizeFile(projectDir); 141 FileObject fo = FileUtil.toFileObject(normProjectDir); 142 if (fo != null) { 143 return ProjectManager.getDefault().findProject(fo); 144 } 145 } catch (IOException e) { 146 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 147 } 149 150 return null; 151 } 152 153 157 private void populateAccessory( Project project ) { 158 159 DefaultListModel model = (DefaultListModel )jListArtifacts.getModel(); 160 model.clear(); 161 jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); 163 if ( project != null ) { 164 165 AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType ); 166 167 for( int i = 0; i < artifacts.length; i++ ) { 168 URI uris[] = artifacts[i].getArtifactLocations(); 169 for( int y = 0; y < uris.length; y++ ) { 170 model.addElement( new ArtifactItem(artifacts[i], uris[y])); 171 } 172 } 173 jListArtifacts.setSelectionInterval(0, model.size()); 174 } 175 176 } 177 178 179 private javax.swing.JLabel jLabelJarFiles; 181 private javax.swing.JLabel jLabelName; 182 private javax.swing.JList jListArtifacts; 183 private javax.swing.JScrollPane jScrollPane1; 184 private javax.swing.JTextField jTextFieldName; 185 187 188 191 public static ArtifactItem[] showDialog( String artifactType, Project master, Component parent ) { 192 193 JFileChooser chooser = ProjectChooser.projectChooser(); 194 chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); 197 AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser ); 198 chooser.setAccessory( accessory ); 199 chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder()); 200 201 int option = chooser.showOpenDialog( parent ); 203 if ( option == JFileChooser.APPROVE_OPTION ) { 204 205 File dir = chooser.getSelectedFile(); 206 dir = FileUtil.normalizeFile (dir); 207 Project selectedProject = accessory.getProject( dir ); 208 209 if ( selectedProject == null ) { 210 return null; 211 } 212 213 if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) { 214 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 215 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ), 216 NotifyDescriptor.INFORMATION_MESSAGE ) ); 217 return null; 218 } 219 220 if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) { 221 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 222 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ), 223 NotifyDescriptor.INFORMATION_MESSAGE ) ); 224 return null; 225 } 226 227 FoldersListSettings.getDefault().setLastUsedArtifactFolder (FileUtil.normalizeFile(chooser.getCurrentDirectory())); 228 229 Object [] tmp = new Object [accessory.jListArtifacts.getModel().getSize()]; 230 int count = 0; 231 for(int i = 0; i < tmp.length; i++) { 232 if (accessory.jListArtifacts.isSelectedIndex(i)) { 233 tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i); 234 count++; 235 } 236 } 237 ArtifactItem artifactItems[] = new ArtifactItem[count]; 238 System.arraycopy(tmp, 0, artifactItems, 0, count); 239 return artifactItems; 240 } 241 else { 242 return null; 243 } 244 245 } 246 247 250 public static class ArtifactItem { 251 252 private AntArtifact artifact; 253 private URI artifactURI; 254 255 public ArtifactItem(AntArtifact artifact, URI artifactURI) { 256 this.artifact = artifact; 257 this.artifactURI = artifactURI; 258 } 259 260 public AntArtifact getArtifact() { 261 return artifact; 262 } 263 264 public URI getArtifactURI() { 265 return artifactURI; 266 } 267 268 public String toString() { 269 return artifactURI.toString(); 270 } 271 272 } 273 } 274 | Popular Tags |