1 19 20 package org.netbeans.modules.web.freeform.ui; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.util.List ; 27 import javax.swing.DefaultListModel ; 28 import javax.swing.JFileChooser ; 29 import javax.swing.filechooser.FileFilter ; 30 import org.netbeans.modules.ant.freeform.spi.support.Util; 31 import org.netbeans.modules.web.freeform.WebProjectGenerator; 32 import org.netbeans.spi.project.AuxiliaryConfiguration; 33 import org.netbeans.spi.project.support.ant.AntProjectHelper; 34 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 35 import org.netbeans.spi.project.support.ant.PropertyUtils; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 41 45 public class WebClasspathPanel extends javax.swing.JPanel implements HelpCtx.Provider { 46 47 private DefaultListModel listModel; 48 49 private File projectFolder = null; 50 51 private File nbProjectFolder; 52 private File lastChosenFile = null; 53 private boolean isSeparateClasspath = true; 54 private boolean ignoreEvent; 55 private static String JAVA_SOURCES_CLASSPATH 56 = org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_JAVA_SOURCE_CLASSPATH"); 57 59 60 public WebClasspathPanel() { 61 this(true); 62 } 63 64 public WebClasspathPanel(boolean isWizard) { 65 initComponents(); 66 jTextArea1.setBackground(getBackground()); 67 listModel = new DefaultListModel (); 68 listModel.add(0,JAVA_SOURCES_CLASSPATH); 69 classpath.setModel(listModel); 70 if (!isWizard) { 71 jTextArea1.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_Explanation")); 72 } 73 74 } 75 76 public HelpCtx getHelpCtx() { 77 return new HelpCtx( WebClasspathPanel.class ); 78 } 79 80 81 86 private void initComponents() { 88 java.awt.GridBagConstraints gridBagConstraints; 89 90 jLabel3 = new javax.swing.JLabel (); 91 addClasspath = new javax.swing.JButton (); 92 removeClasspath = new javax.swing.JButton (); 93 jScrollPane1 = new javax.swing.JScrollPane (); 94 classpath = new javax.swing.JList (); 95 jPanel1 = new javax.swing.JPanel (); 96 moveUp = new javax.swing.JButton (); 97 moveDown = new javax.swing.JButton (); 98 jTextArea1 = new javax.swing.JTextArea (); 99 100 setLayout(new java.awt.GridBagLayout ()); 101 102 jLabel3.setLabelFor(classpath); 103 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_jLabel3")); 104 gridBagConstraints = new java.awt.GridBagConstraints (); 105 gridBagConstraints.gridx = 0; 106 gridBagConstraints.gridy = 3; 107 gridBagConstraints.gridwidth = 2; 108 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 109 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 0); 110 add(jLabel3, gridBagConstraints); 111 jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_jLabel3")); 112 113 org.openide.awt.Mnemonics.setLocalizedText(addClasspath, org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "BTN_ClasspathPanel_addClasspath")); 114 addClasspath.addActionListener(new java.awt.event.ActionListener () { 115 public void actionPerformed(java.awt.event.ActionEvent evt) { 116 addClasspathActionPerformed(evt); 117 } 118 }); 119 120 gridBagConstraints = new java.awt.GridBagConstraints (); 121 gridBagConstraints.gridx = 2; 122 gridBagConstraints.gridy = 4; 123 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 124 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 125 add(addClasspath, gridBagConstraints); 126 addClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_addClasspath")); 127 128 org.openide.awt.Mnemonics.setLocalizedText(removeClasspath, org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "BTN_ClasspathPanel_removeClasspath")); 129 removeClasspath.addActionListener(new java.awt.event.ActionListener () { 130 public void actionPerformed(java.awt.event.ActionEvent evt) { 131 removeClasspathActionPerformed(evt); 132 } 133 }); 134 135 gridBagConstraints = new java.awt.GridBagConstraints (); 136 gridBagConstraints.gridx = 2; 137 gridBagConstraints.gridy = 5; 138 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 139 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 140 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 141 add(removeClasspath, gridBagConstraints); 142 removeClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_removeClasspath")); 143 144 classpath.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 145 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 146 classpathValueChanged(evt); 147 } 148 }); 149 150 jScrollPane1.setViewportView(classpath); 151 classpath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_classpath")); 152 153 gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 0; 155 gridBagConstraints.gridy = 4; 156 gridBagConstraints.gridwidth = 2; 157 gridBagConstraints.gridheight = 4; 158 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 159 gridBagConstraints.weightx = 1.0; 160 gridBagConstraints.weighty = 1.0; 161 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 162 add(jScrollPane1, gridBagConstraints); 163 164 jPanel1.setLayout(new java.awt.GridBagLayout ()); 165 166 gridBagConstraints = new java.awt.GridBagConstraints (); 167 gridBagConstraints.gridx = 1; 168 gridBagConstraints.gridy = 2; 169 gridBagConstraints.gridwidth = 2; 170 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 171 add(jPanel1, gridBagConstraints); 172 173 moveUp.setMnemonic(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_MoveUp_MNE").charAt(0)); 174 moveUp.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_Move_Up")); 175 moveUp.addActionListener(new java.awt.event.ActionListener () { 176 public void actionPerformed(java.awt.event.ActionEvent evt) { 177 moveUpActionPerformed(evt); 178 } 179 }); 180 181 gridBagConstraints = new java.awt.GridBagConstraints (); 182 gridBagConstraints.gridx = 2; 183 gridBagConstraints.gridy = 6; 184 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 185 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 186 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 187 add(moveUp, gridBagConstraints); 188 moveUp.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/web/freeform/ui/Bundle").getString("AD_ClasspathPanel_noveUp")); 189 190 moveDown.setMnemonic(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_MoveDown_MNE").charAt(0)); 191 moveDown.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "LBL_ClasspathPanel_Move_Down")); 192 moveDown.addActionListener(new java.awt.event.ActionListener () { 193 public void actionPerformed(java.awt.event.ActionEvent evt) { 194 moveDownActionPerformed(evt); 195 } 196 }); 197 198 gridBagConstraints = new java.awt.GridBagConstraints (); 199 gridBagConstraints.gridx = 2; 200 gridBagConstraints.gridy = 7; 201 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 202 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 203 add(moveDown, gridBagConstraints); 204 moveDown.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/web/freeform/ui/Bundle").getString("AD_ClasspathPanel_moveDown")); 205 206 jTextArea1.setEditable(false); 207 jTextArea1.setForeground(new java.awt.Color (0, 0, 0)); 208 jTextArea1.setLineWrap(true); 209 jTextArea1.setText(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "MSG_ClasspathPanel_jTextArea")); 210 jTextArea1.setWrapStyleWord(true); 211 jTextArea1.setDisabledTextColor(java.awt.Color.black); 212 jTextArea1.setEnabled(false); 213 jTextArea1.setSelectedTextColor(new java.awt.Color (0, 0, 0)); 214 gridBagConstraints = new java.awt.GridBagConstraints (); 215 gridBagConstraints.gridwidth = 3; 216 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 217 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 218 add(jTextArea1, gridBagConstraints); 219 jTextArea1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSN_ClasspathPanel_jTextArea")); 220 jTextArea1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebClasspathPanel.class, "ACSD_ClasspathPanel_jTextArea")); 221 222 } 224 private void classpathValueChanged(javax.swing.event.ListSelectionEvent evt) { updateButtons(); 226 } 228 private void moveDownActionPerformed(java.awt.event.ActionEvent evt) { int indices[] = classpath.getSelectedIndices(); 230 if (indices.length == 0 || 231 indices[indices.length - 1] == listModel.getSize() - 1) { 232 return; 233 } 234 for (int i = 0; i < indices.length; i++) { 235 int index = indices[i]; 236 Object o = listModel.remove(index); 237 index++; 238 listModel.add(index, o); 239 indices[i] = index; 240 } 241 classpath.setSelectedIndices(indices); 242 updateButtons(); 243 } 245 private void moveUpActionPerformed(java.awt.event.ActionEvent evt) { int indices[] = classpath.getSelectedIndices(); 247 if (indices.length == 0 || indices[0] == 0) { 248 return; 249 } 250 for (int i = 0; i < indices.length; i++) { 251 int index = indices[i]; 252 Object o = listModel.remove(index); 253 index--; 254 listModel.add(index, o); 255 indices[i] = index; 256 } 257 classpath.setSelectedIndices(indices); 258 updateButtons(); 259 } 261 void updateButtons() { 262 int indices[] = classpath.getSelectedIndices(); 263 removeClasspath.setEnabled(listModel.getSize() > 0 && indices.length != 0 && indices[0] != 0); 264 moveUp.setEnabled(indices.length > 0 && indices[0] > 1); 265 moveDown.setEnabled(indices.length > 0 && indices[indices.length - 1] != listModel.getSize() - 1 && indices[0] != 0); 266 } 267 268 private void removeClasspathActionPerformed(java.awt.event.ActionEvent evt) { int entries[] = classpath.getSelectedIndices(); 270 for (int i = 0; i < entries.length; i++) { 271 listModel.remove(entries[i] - i); 272 } 273 updateButtons(); 274 } 276 private void addClasspathActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 278 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 279 chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES); 280 chooser.setMultiSelectionEnabled(true); 281 if (lastChosenFile != null) { 282 chooser.setSelectedFile(lastChosenFile); 283 } 284 else { 285 if (projectFolder!= null) 286 chooser.setSelectedFile(projectFolder); 287 } 288 chooser.setDialogTitle(NbBundle.getMessage(WebClasspathPanel.class, "LBL_Browse_Classpath")); 289 290 FileFilter fileFilter = new SimpleFileFilter(NbBundle.getMessage(WebClasspathPanel.class, "LBL_ZipJarFolderFilter")); chooser.setFileFilter(fileFilter); 293 chooser.setAcceptAllFileFilterUsed( false ); 294 295 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 296 File files[] = chooser.getSelectedFiles(); 297 for (int i=0; i<files.length; i++) { 298 File file = FileUtil.normalizeFile(files[i]); 299 300 if (!fileFilter.accept(file)) { 303 continue; 304 } 305 306 listModel.addElement(file.getAbsolutePath()); 307 lastChosenFile = file; 308 } 309 updateButtons(); 310 } 311 } 313 318 public void setProjectFolders(File baseFolder, File nbProjectFolder) { 319 this.projectFolder = baseFolder; 320 this.nbProjectFolder = nbProjectFolder; 321 } 322 323 public String getClasspath(){ 324 StringBuffer sf = new StringBuffer (); 325 for (int i = 1; i < listModel.getSize(); i++){ 326 File f = new File ((String )listModel.get(i)); 327 String path = org.netbeans.modules.ant.freeform.spi.support.Util 328 .relativizeLocation(projectFolder, nbProjectFolder, f); 329 sf.append(path); 330 if (i < listModel.getSize()-1) 331 sf.append(File.pathSeparatorChar); 332 } 333 return sf.toString(); 334 } 335 336 void setClasspath(String classpath, PropertyEvaluator evaluator){ 337 if (classpath == null) 338 return; 339 listModel.clear(); 340 listModel.addElement(JAVA_SOURCES_CLASSPATH); 341 342 String [] cpa = PropertyUtils.tokenizePath(evaluator.evaluate(classpath)); 343 for (int i=0; i<cpa.length; i++) { 344 String path = cpa[i]; 345 path = PropertyUtils.resolveFile(nbProjectFolder, path).getAbsolutePath(); 346 if (path != null) { 347 listModel.addElement(path); 348 } 349 } 350 } 351 352 ActionListener getCustomizerOkListener(final AntProjectHelper projectHelper) { 353 return new ActionListener () { 354 public void actionPerformed(ActionEvent arg0) { 355 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(projectHelper); 356 List l = WebProjectGenerator.getWebmodules(projectHelper, aux); 357 if (l != null){ 358 WebProjectGenerator.WebModule wm = (WebProjectGenerator.WebModule)l.get(0); 359 wm.classpath = getClasspath(); 360 WebProjectGenerator.putWebModules(projectHelper, aux, l); 361 } 362 updateButtons(); 364 } 365 }; 366 } 367 368 369 370 371 private javax.swing.JButton addClasspath; 373 private javax.swing.JList classpath; 374 private javax.swing.JLabel jLabel3; 375 private javax.swing.JPanel jPanel1; 376 private javax.swing.JScrollPane jScrollPane1; 377 private javax.swing.JTextArea jTextArea1; 378 private javax.swing.JButton moveDown; 379 private javax.swing.JButton moveUp; 380 private javax.swing.JButton removeClasspath; 381 383 private static class SimpleFileFilter extends FileFilter { 384 385 private String description; 386 387 public SimpleFileFilter (String description) { 388 this.description = description; 389 } 390 391 public boolean accept(File f) { 392 if (f.isDirectory()) 393 return true; 394 try { 395 return FileUtil.isArchiveFile(f.toURI().toURL()); 396 } catch (MalformedURLException mue) { 397 ErrorManager.getDefault().notify(mue); 398 return false; 399 } 400 } 401 402 public String getDescription() { 403 return this.description; 404 } 405 } 406 407 } 408 | Popular Tags |