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.util.ArrayList ; 26 import java.util.List ; 27 import javax.swing.JFileChooser ; 28 import org.netbeans.api.queries.CollocationQuery; 29 import org.netbeans.modules.ant.freeform.spi.support.Util; 30 import org.netbeans.modules.web.freeform.WebProjectGenerator; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.HelpCtx; 33 import org.openide.util.NbBundle; 34 import org.netbeans.spi.project.AuxiliaryConfiguration; 35 import org.netbeans.spi.project.support.ant.AntProjectHelper; 36 import org.netbeans.spi.project.support.ant.PropertyUtils; 37 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 38 import org.netbeans.spi.project.ui.support.ProjectChooser; 39 import org.netbeans.spi.project.ui.templates.support.Templates; 40 import org.openide.WizardDescriptor; 41 import org.openide.filesystems.FileObject; 42 43 44 48 public class WebLocationsPanel extends javax.swing.JPanel implements HelpCtx.Provider { 49 50 private static String J2EE_SPEC_5 = "1.5"; private static String J2EE_SPEC_1_4 = "1.4"; private static String J2EE_SPEC_1_3 = "1.3"; 54 55 private File baseFolder; 56 57 private File nbProjectFolder; 58 59 private AntProjectHelper projectHelper; 60 61 private File srcPackagesLocation; 62 private String classpath; 63 64 private WizardDescriptor wizardDescriptor; 65 66 67 public WebLocationsPanel(WizardDescriptor wizardDescriptor) { 68 initComponents(); 69 this.wizardDescriptor = wizardDescriptor; 70 jComboBoxJ2eeLevel.addItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_5")); jComboBoxJ2eeLevel.addItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_0")); jComboBoxJ2eeLevel.addItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_1")); jComboBoxJ2eeLevel.setSelectedIndex(0); 74 } 75 76 public WebLocationsPanel(AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) { 77 this(null); 78 this.projectHelper = projectHelper; 79 setFolders(Util.getProjectLocation(projectHelper, projectEvaluator), FileUtil.toFile(projectHelper.getProjectDirectory())); 80 81 List l = WebProjectGenerator.getWebmodules(projectHelper, aux); 82 if (l != null) { 83 WebProjectGenerator.WebModule wm = (WebProjectGenerator.WebModule)l.get(0); 84 String docroot = getLocationDisplayName(projectEvaluator, nbProjectFolder, wm.docRoot); 85 classpath = wm.classpath; 86 jTextFieldWeb.setText(docroot); 87 88 jTextFieldContextPath.setText(wm.contextPath); 89 if (wm.j2eeSpecLevel.equals(J2EE_SPEC_5)) 90 jComboBoxJ2eeLevel.setSelectedItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_5")); 91 else if (wm.j2eeSpecLevel.equals(J2EE_SPEC_1_4)) 92 jComboBoxJ2eeLevel.setSelectedItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_0")); 93 else 94 jComboBoxJ2eeLevel.setSelectedItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_1")); 95 } 96 } 97 98 102 public static String getLocationDisplayName(PropertyEvaluator evaluator, File base, String val) { 104 File f = Util.resolveFile(evaluator, base, val); 105 if (f == null) { 106 return val; 107 } 108 String location = f.getAbsolutePath(); 109 if (CollocationQuery.areCollocated(base, f)) { 110 location = PropertyUtils.relativizeFile(base, f).replace('/', File.separatorChar); } 112 return location; 113 } 114 115 public HelpCtx getHelpCtx() { 116 return new HelpCtx( WebLocationsPanel.class ); 117 } 118 119 124 private void initComponents() { 126 java.awt.GridBagConstraints gridBagConstraints; 127 128 jLabel1 = new javax.swing.JLabel (); 129 jLabel2 = new javax.swing.JLabel (); 130 jTextFieldWeb = new javax.swing.JTextField (); 131 jButtonWeb = new javax.swing.JButton (); 132 jLabel4 = new javax.swing.JLabel (); 133 jTextFieldContextPath = new javax.swing.JTextField (); 134 jLabel5 = new javax.swing.JLabel (); 135 jComboBoxJ2eeLevel = new javax.swing.JComboBox (); 136 137 setLayout(new java.awt.GridBagLayout ()); 138 139 jLabel1.setText(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_Description")); 140 gridBagConstraints = new java.awt.GridBagConstraints (); 141 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 142 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 143 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 144 add(jLabel1, gridBagConstraints); 145 146 jLabel2.setLabelFor(jTextFieldWeb); 147 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_WebPagesLocation_Label")); 148 gridBagConstraints = new java.awt.GridBagConstraints (); 149 gridBagConstraints.gridx = 0; 150 gridBagConstraints.gridy = 1; 151 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 152 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 11); 153 add(jLabel2, gridBagConstraints); 154 155 gridBagConstraints = new java.awt.GridBagConstraints (); 156 gridBagConstraints.gridx = 1; 157 gridBagConstraints.gridy = 1; 158 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 159 gridBagConstraints.weightx = 1.0; 160 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 11); 161 add(jTextFieldWeb, gridBagConstraints); 162 jTextFieldWeb.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_WebPagesLocation_A11YDesc")); 163 164 org.openide.awt.Mnemonics.setLocalizedText(jButtonWeb, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "BTN_BasicProjectInfoPanel_browseAntScript")); 165 jButtonWeb.addActionListener(new java.awt.event.ActionListener () { 166 public void actionPerformed(java.awt.event.ActionEvent evt) { 167 jButtonWebActionPerformed(evt); 168 } 169 }); 170 171 gridBagConstraints = new java.awt.GridBagConstraints (); 172 gridBagConstraints.gridx = 2; 173 gridBagConstraints.gridy = 1; 174 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 175 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 176 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 177 add(jButtonWeb, gridBagConstraints); 178 jButtonWeb.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_WebPagesLocationBrowse_A11YDesc")); 179 180 jLabel4.setLabelFor(jTextFieldContextPath); 181 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_ContextPath_Label")); 182 gridBagConstraints = new java.awt.GridBagConstraints (); 183 gridBagConstraints.gridx = 0; 184 gridBagConstraints.gridy = 3; 185 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 186 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 11); 187 add(jLabel4, gridBagConstraints); 188 189 gridBagConstraints = new java.awt.GridBagConstraints (); 190 gridBagConstraints.gridx = 1; 191 gridBagConstraints.gridy = 3; 192 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 193 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 194 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 195 add(jTextFieldContextPath, gridBagConstraints); 196 jTextFieldContextPath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_ContextPath_A11YDesc")); 197 198 jLabel5.setLabelFor(jComboBoxJ2eeLevel); 199 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_J2EESpecLevel_Label")); 200 gridBagConstraints = new java.awt.GridBagConstraints (); 201 gridBagConstraints.gridx = 0; 202 gridBagConstraints.gridy = 4; 203 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 204 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 205 gridBagConstraints.insets = new java.awt.Insets (3, 0, 0, 11); 206 add(jLabel5, gridBagConstraints); 207 208 gridBagConstraints = new java.awt.GridBagConstraints (); 209 gridBagConstraints.gridx = 1; 210 gridBagConstraints.gridy = 4; 211 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 212 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 213 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 214 gridBagConstraints.weighty = 1.0; 215 add(jComboBoxJ2eeLevel, gridBagConstraints); 216 jComboBoxJ2eeLevel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_J2EESpecLevel_A11YDesc")); 217 218 } 220 private void jButtonWebActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = createChooser(getWebPagesLocation(), wizardDescriptor); 222 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 223 setWebPages(chooser.getSelectedFile()); 224 } 225 } 227 private javax.swing.JButton jButtonWeb; 229 private javax.swing.JComboBox jComboBoxJ2eeLevel; 230 private javax.swing.JLabel jLabel1; 231 private javax.swing.JLabel jLabel2; 232 private javax.swing.JLabel jLabel4; 233 private javax.swing.JLabel jLabel5; 234 private javax.swing.JTextField jTextFieldContextPath; 235 private javax.swing.JTextField jTextFieldWeb; 236 238 private static JFileChooser createChooser(File webPagesLoc, WizardDescriptor wizardDescriptor) { 239 String path = webPagesLoc.getAbsolutePath(); 240 JFileChooser chooser = new JFileChooser (); 241 FileUtil.preventFileChooserSymlinkTraversal(chooser, new File (path)); 242 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 243 chooser.setAcceptAllFileFilterUsed(false); 244 245 if (path.length() > 0 && webPagesLoc.exists()) { 246 chooser.setSelectedFile(webPagesLoc); 247 } else { 248 if (wizardDescriptor != null) { 249 File currentDirectory = null; 251 FileObject existingSourcesFO = Templates.getExistingSourcesFolder(wizardDescriptor); 252 if (existingSourcesFO != null) { 253 File existingSourcesFile = FileUtil.toFile(existingSourcesFO); 254 if (existingSourcesFile != null && existingSourcesFile.isDirectory()) { 255 currentDirectory = existingSourcesFile; 256 } 257 } 258 if (currentDirectory != null) { 259 chooser.setCurrentDirectory(currentDirectory); 260 } else { 261 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 262 } 263 } else { 264 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 265 } 266 } 267 268 return chooser; 269 } 270 271 protected List getWebModules() { 272 ArrayList l = new ArrayList (); 273 274 WebProjectGenerator.WebModule wm = new WebProjectGenerator.WebModule (); 275 wm.docRoot = getRelativeLocation(getWebPagesLocation()); 276 wm.contextPath = jTextFieldContextPath.getText().trim(); 277 278 String j2eeLevel = (String ) jComboBoxJ2eeLevel.getSelectedItem(); 279 if (j2eeLevel.equals(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_5"))) 280 wm.j2eeSpecLevel = J2EE_SPEC_5; 281 else if (j2eeLevel.equals(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_0"))) 282 wm.j2eeSpecLevel = J2EE_SPEC_1_4; 283 else 284 wm.j2eeSpecLevel = J2EE_SPEC_1_3; 285 286 wm.classpath = classpath; 287 l.add (wm); 288 return l; 289 } 290 291 protected List getJavaSrcFolder() { 292 ArrayList l = new ArrayList (); 293 File sourceLoc = getSrcPackagesLocation(); 294 l.add(getRelativeLocation(sourceLoc)); 295 l.add(sourceLoc.getName()); 296 return l; 297 } 298 299 302 protected List getWebSrcFolder() { 303 ArrayList l = new ArrayList (); 304 final File webLocation = getWebPagesLocation(); 305 l.add(getRelativeLocation(webLocation)); 306 l.add(webLocation.getName()); 307 return l; 308 } 309 310 private File getAsFile(String filename) { 311 return PropertyUtils.resolveFile(nbProjectFolder, filename); 312 } 313 314 319 public void setFolders(File baseFolder, File nbProjectFolder) { 320 this.baseFolder = baseFolder; 321 this.nbProjectFolder = nbProjectFolder; 322 } 323 324 protected void setWebPages(String path) { 325 jTextFieldWeb.setText(path); 326 } 327 328 protected void setSrcPackages(String path) { 329 setSrcPackages(getAsFile(path)); 330 } 331 332 private void setWebPages(final File file) { 333 setWebPages(relativizeFile(file)); 334 } 335 336 protected File getWebPagesLocation() { 337 return getAsFile(jTextFieldWeb.getText()).getAbsoluteFile(); 338 339 } 340 341 private void setSrcPackages(final File file) { 342 srcPackagesLocation = file; 343 } 344 345 protected File getSrcPackagesLocation() { 346 return srcPackagesLocation; 347 } 348 349 private String relativizeFile(final File file) { 350 File normalizedFile = FileUtil.normalizeFile(file); 351 if (CollocationQuery.areCollocated(nbProjectFolder, file)) { 352 return PropertyUtils.relativizeFile(nbProjectFolder, normalizedFile); 353 } else { 354 return normalizedFile.getAbsolutePath(); 355 } 356 } 357 358 private String getRelativeLocation(final File location) { 359 final File normalizedLocation = FileUtil.normalizeFile(location); 360 return Util.relativizeLocation(baseFolder, nbProjectFolder, normalizedLocation); 361 } 362 363 ActionListener getCustomizerOkListener() { 364 return new ActionListener () { 365 public void actionPerformed(ActionEvent arg0) { 366 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(projectHelper); 367 WebProjectGenerator.putWebSourceFolder(projectHelper, getWebSrcFolder()); 368 WebProjectGenerator.putWebModules(projectHelper, aux, getWebModules()); 369 } 370 }; 371 } 372 373 } 374 | Popular Tags |