1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.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 javax.swing.DefaultListModel ; 30 import javax.swing.JFileChooser ; 31 import javax.swing.JPanel ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.api.project.ProjectUtils; 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.netbeans.modules.j2ee.ejbjarproject.ui.FoldersListSettings; 39 import org.openide.DialogDisplayer; 40 import org.openide.ErrorManager; 41 import org.openide.NotifyDescriptor; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.util.NbBundle; 45 46 51 public class AntArtifactChooser extends JPanel implements PropertyChangeListener { 52 53 private String artifactType; 55 56 57 public AntArtifactChooser( String artifactType, JFileChooser chooser ) { 58 this.artifactType = artifactType; 59 60 initComponents(); 61 jListArtifacts.setModel( new DefaultListModel () ); 62 chooser.addPropertyChangeListener( this ); 63 } 64 65 70 private void initComponents() { 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.setText(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectName_JLabel")); 82 gridBagConstraints = new java.awt.GridBagConstraints (); 83 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 84 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 85 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 86 add(jLabelName, gridBagConstraints); 87 88 jTextFieldName.setEditable(false); 89 gridBagConstraints = new java.awt.GridBagConstraints (); 90 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 91 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 92 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 93 gridBagConstraints.weightx = 1.0; 94 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 95 add(jTextFieldName, gridBagConstraints); 96 97 jLabelJarFiles.setText(org.openide.util.NbBundle.getMessage(AntArtifactChooser.class, "LBL_AACH_ProjectJarFiles_JLabel")); 98 gridBagConstraints = new java.awt.GridBagConstraints (); 99 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 100 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 101 gridBagConstraints.insets = new java.awt.Insets (0, 12, 2, 0); 102 add(jLabelJarFiles, gridBagConstraints); 103 104 jScrollPane1.setViewportView(jListArtifacts); 105 106 gridBagConstraints = new java.awt.GridBagConstraints (); 107 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 108 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 109 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 110 gridBagConstraints.weightx = 1.0; 111 gridBagConstraints.weighty = 1.0; 112 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 113 add(jScrollPane1, gridBagConstraints); 114 115 } 117 118 public void propertyChange(PropertyChangeEvent e) { 119 if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) { 120 JFileChooser chooser = (JFileChooser ) e.getSource(); 122 File dir = chooser.getSelectedFile(); Project project = getProject(dir); populateAccessory(project); 125 } 126 } 127 128 private Project getProject( File projectDir ) { 129 130 if (projectDir == null) { return null; 132 } 133 134 try { 135 File normProjectDir = FileUtil.normalizeFile(projectDir); 136 FileObject fo = FileUtil.toFileObject(normProjectDir); 137 if (fo != null) { 138 return ProjectManager.getDefault().findProject(fo); 139 } 140 } catch (IOException e) { 141 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 142 } 144 145 return null; 146 } 147 148 152 private void populateAccessory( Project project ) { 153 154 DefaultListModel model = (DefaultListModel )jListArtifacts.getModel(); 155 model.clear(); 156 jTextFieldName.setText(project == null ? "" : ProjectUtils.getInformation(project).getDisplayName()); 158 if ( project != null ) { 159 160 AntArtifact artifacts[] = AntArtifactQuery.findArtifactsByType( project, artifactType ); 161 162 for( int i = 0; i < artifacts.length; i++ ) { 163 URI uris[] = artifacts[i].getArtifactLocations(); 164 for( int y = 0; y < uris.length; y++ ) { 165 model.addElement( new ArtifactItem(artifacts[i], uris[y])); 166 } 167 } 168 jListArtifacts.setSelectionInterval(0, model.size()); 169 } 170 171 } 172 173 174 private javax.swing.JLabel jLabelJarFiles; 176 private javax.swing.JLabel jLabelName; 177 private javax.swing.JList jListArtifacts; 178 private javax.swing.JScrollPane jScrollPane1; 179 private javax.swing.JTextField jTextFieldName; 180 182 183 186 public static ArtifactItem[] showDialog( String artifactType, Project master, Component parent ) { 187 188 JFileChooser chooser = ProjectChooser.projectChooser(); 189 chooser.setDialogTitle( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_Title" ) ); chooser.setApproveButtonText( NbBundle.getMessage( AntArtifactChooser.class, "LBL_AACH_SelectProject" ) ); 192 AntArtifactChooser accessory = new AntArtifactChooser( artifactType, chooser ); 193 chooser.setAccessory( accessory ); 194 chooser.setPreferredSize( new Dimension ( 650, 380 ) ); 195 chooser.setCurrentDirectory (FoldersListSettings.getDefault().getLastUsedArtifactFolder()); 196 197 int option = chooser.showOpenDialog( parent ); 199 if ( option == JFileChooser.APPROVE_OPTION ) { 200 201 File dir = chooser.getSelectedFile(); 202 dir = FileUtil.normalizeFile (dir); 203 Project selectedProject = accessory.getProject( dir ); 204 205 if ( selectedProject == null ) { 206 return null; 207 } 208 209 if ( selectedProject.getProjectDirectory().equals( master.getProjectDirectory() ) ) { 210 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 211 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_RefToItself" ), 212 NotifyDescriptor.INFORMATION_MESSAGE ) ); 213 return null; 214 } 215 216 if ( ProjectUtils.hasSubprojectCycles( master, selectedProject ) ) { 217 DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message( 218 NbBundle.getMessage( AntArtifactChooser.class, "MSG_AACH_Cycles" ), 219 NotifyDescriptor.INFORMATION_MESSAGE ) ); 220 return null; 221 } 222 223 FoldersListSettings.getDefault().setLastUsedArtifactFolder (FileUtil.normalizeFile(chooser.getCurrentDirectory())); 224 225 Object [] tmp = new Object [accessory.jListArtifacts.getModel().getSize()]; 226 int count = 0; 227 for(int i = 0; i < tmp.length; i++) { 228 if (accessory.jListArtifacts.isSelectedIndex(i)) { 229 tmp[count] = accessory.jListArtifacts.getModel().getElementAt(i); 230 count++; 231 } 232 } 233 ArtifactItem artifactItems[] = new ArtifactItem[count]; 234 System.arraycopy(tmp, 0, artifactItems, 0, count); 235 return artifactItems; 236 } 237 else { 238 return null; 239 } 240 241 } 242 243 246 public static class ArtifactItem { 247 248 private AntArtifact artifact; 249 private URI artifactURI; 250 251 public ArtifactItem(AntArtifact artifact, URI artifactURI) { 252 this.artifact = artifact; 253 this.artifactURI = artifactURI; 254 } 255 256 public AntArtifact getArtifact() { 257 return artifact; 258 } 259 260 public URI getArtifactURI() { 261 return artifactURI; 262 } 263 264 public String toString() { 265 return artifactURI.toString(); 266 } 267 268 } 269 } 270 | Popular Tags |