1 19 20 package org.netbeans.modules.j2ee.earproject.ui.customizer; 21 22 import java.awt.Dimension ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import javax.swing.DefaultListModel ; 28 import javax.swing.JFileChooser ; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.api.project.ProjectUtils; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.NbBundle; 35 import org.netbeans.api.project.ant.AntArtifact; 36 import org.netbeans.api.project.ant.AntArtifactQuery; 37 import org.netbeans.spi.project.ui.support.ProjectChooser; 38 import org.openide.DialogDisplayer; 39 import org.openide.NotifyDescriptor; 40 41 46 public class AntArtifactChooser extends javax.swing.JPanel implements PropertyChangeListener { 47 48 private String [] artifactType; 50 51 52 public AntArtifactChooser( String [] artifactType, JFileChooser chooser ) { 53 this.artifactType = artifactType; 54 55 initComponents(); 56 jListArtifacts.setModel( new DefaultListModel () ); 57 chooser.addPropertyChangeListener( this ); 58 } 59 60 65 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 67 68 jLabelName = new javax.swing.JLabel (); 69 jTextFieldName = new javax.swing.JTextField (); 70 jLabelJarFiles = new javax.swing.JLabel (); 71 jScrollPane1 = new javax.swing.JScrollPane (); 72 jListArtifacts = new javax.swing.JList (); 73 74 setLayout(new java.awt.GridBagLayout ()); 75 76 jLabelName.setText(NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel")); 77 gridBagConstraints = new java.awt.GridBagConstraints (); 78 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 79 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 80 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 81 add(jLabelName, gridBagConstraints); 82 83 jTextFieldName.setEditable(false); 84 gridBagConstraints = new java.awt.GridBagConstraints (); 85 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 86 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 87 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 88 gridBagConstraints.weightx = 1.0; 89 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 90 add(jTextFieldName, gridBagConstraints); 91 92 jLabelJarFiles.setText(NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel")); 93 gridBagConstraints = new java.awt.GridBagConstraints (); 94 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 95 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 96 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 97 add(jLabelJarFiles, gridBagConstraints); 98 99 jScrollPane1.setViewportView(jListArtifacts); 100 101 gridBagConstraints = new java.awt.GridBagConstraints (); 102 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 103 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 104 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 105 gridBagConstraints.weightx = 1.0; 106 gridBagConstraints.weighty = 1.0; 107 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 108 add(jScrollPane1, gridBagConstraints); 109 110 } 112 113 public void propertyChange( PropertyChangeEvent e ) { 114 115 if ( JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals( e.getPropertyName() ) ) { 116 JFileChooser chooser = (JFileChooser )e.getSource(); 118 File dir = chooser.getSelectedFile(); 119 Project project = dir == null ? null : getProject(FileUtil.normalizeFile(dir)); 120 populateAccessory( project ); 121 } 122 } 123 124 private Project getProject( File projectDir ) { 125 126 try { 127 projectDir = FileUtil.normalizeFile (projectDir); 128 FileObject fo = FileUtil.toFileObject(projectDir); 129 130 if (fo != null) { 131 Project project = ProjectManager.getDefault().findProject(fo); 132 return project; 133 } 134 } 135 catch ( IOException e ) { 136 } 138 139 return null; 140 } 141 142 143 private void populateAccessory( Project project ) { 144 145 DefaultListModel model = (DefaultListModel )jListArtifacts.getModel(); 146 model.clear(); 147 jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); 149 if ( project != null ) { 150 151 for (int j = 0; j < artifactType.length; j++) { 152 153 AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType[j] ); 154 155 for( int i = 0; i < artifacts.length; i++ ) { 156 model.addElement( new ArtifactItem( artifacts[i])); 157 } 158 if (artifacts.length > 0) 159 return; 160 } 161 } 162 163 } 164 165 166 private javax.swing.JLabel jLabelJarFiles; 168 private javax.swing.JLabel jLabelName; 169 private javax.swing.JList jListArtifacts; 170 private javax.swing.JScrollPane jScrollPane1; 171 private javax.swing.JTextField jTextFieldName; 172 174 175 178 public static AntArtifact[] showDialog( String artifactType, Project master ) { 179 return showDialog(master, new String [] { artifactType }); 180 } 181 182 public static AntArtifact[] showDialog(Project master, String [] artifactTypes) { 183 JFileChooser chooser = ProjectChooser.projectChooser(); 184 chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); 187 AntArtifactChooser accessory = new AntArtifactChooser( artifactTypes, chooser ); 188 chooser.setAccessory( accessory ); 189 190 chooser.setPreferredSize( new Dimension ( 650, 360 ) ); 191 192 int option = chooser.showOpenDialog( null ); 194 if ( option == JFileChooser.APPROVE_OPTION ) { 195 196 File dir = chooser.getSelectedFile(); 197 dir = FileUtil.normalizeFile (dir); 198 Project selectedProject = accessory.getProject( dir ); 199 200 if ( selectedProject == null ) { 201 return null; 202 } 203 204 if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) { 205 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 206 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ), 207 NotifyDescriptor.INFORMATION_MESSAGE ) ); 208 return null; 209 } 210 211 if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) { 212 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 213 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ), 214 NotifyDescriptor.INFORMATION_MESSAGE ) ); 215 return null; 216 } 217 218 DefaultListModel model = (DefaultListModel )accessory.jListArtifacts.getModel(); 219 220 AntArtifact artifacts[] = new AntArtifact[ model.size() ]; 221 222 for( int i = 0; i < artifacts.length; i++ ) { 224 artifacts[i] = ((ArtifactItem)model.getElementAt( i )).getArtifact(); 225 } 226 227 return artifacts; 228 229 } 230 else { 231 return null; 232 } 233 234 } 235 236 private static class ArtifactItem { 237 238 private AntArtifact artifact; 239 240 ArtifactItem( AntArtifact artifact ) { 241 this.artifact = artifact; 242 } 243 244 AntArtifact getArtifact() { 245 return artifact; 246 } 247 248 public String toString() { 249 return artifact.getArtifactLocations()[0].toString(); 250 } 251 252 } 253 } 254 | Popular Tags |