1 19 20 package org.netbeans.modules.ant.freeform.ui; 21 22 import java.io.File ; 23 import java.text.MessageFormat ; 24 import javax.swing.JFileChooser ; 25 import javax.swing.event.ChangeListener ; 26 import javax.swing.event.DocumentEvent ; 27 import javax.swing.event.DocumentListener ; 28 import org.netbeans.api.project.FileOwnerQuery; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectInformation; 31 import org.netbeans.modules.ant.freeform.Util; 32 import org.netbeans.spi.project.ui.support.ProjectChooser; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.NbBundle; 37 38 41 public class BasicProjectInfoPanel extends javax.swing.JPanel implements HelpCtx.Provider{ 42 43 private DocumentListener documentListener; 44 private ChangeListener listener; 45 46 private boolean antScriptTouched = false; 47 48 private boolean projectFolderTouched = false; 49 50 private boolean projectNameTouched = false; 51 52 private boolean antScriptValidityChecked; 53 54 public BasicProjectInfoPanel(String projectLocation, String antScript, String projectName, String projectFolder, 55 ChangeListener listener) { 56 initComponents(); 57 this.projectLocation.setText(projectLocation); 58 this.antScript.setText(antScript); 59 this.projectName.setText(projectName); 60 this.projectFolder.setText(projectFolder); 61 this.listener = listener; 62 documentListener = new DocumentListener () { 63 public void insertUpdate(DocumentEvent e) { 64 update(e); 65 } 66 67 public void removeUpdate(DocumentEvent e) { 68 update(e); 69 } 70 71 public void changedUpdate(DocumentEvent e) { 72 update(e); 73 } 74 }; 75 this.projectLocation.getDocument().addDocumentListener(documentListener); 76 this.antScript.getDocument().addDocumentListener(documentListener); 77 this.projectName.getDocument().addDocumentListener(documentListener); 78 this.projectFolder.getDocument().addDocumentListener(documentListener); 79 } 80 81 public HelpCtx getHelpCtx() { 82 return new HelpCtx(BasicProjectInfoPanel.class); 83 } 84 85 public File getProjectLocation() { 86 return getAsFile(projectLocation.getText()); 87 } 88 89 public File getAntScript() { 90 return getAsFile(antScript.getText()); 91 } 92 93 public String getProjectName() { 94 return projectName.getText(); 95 } 96 97 public File getProjectFolder() { 98 return getAsFile(projectFolder.getText()); 99 } 100 101 public Boolean getMainProject() { 102 return Boolean.valueOf(mainProject.isSelected()); 103 } 104 105 public String getError() { 106 if (projectLocation.getText().length() == 0) { 107 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_1"); 108 } 109 if (!getProjectLocation().exists()) { 110 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_2"); 111 } 112 if (antScript.getText().length() == 0) { 113 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_3"); 114 } 115 if (!getAntScript().exists()) { 116 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_4"); 117 } 118 if (!antScriptValidityChecked) { 119 FileObject fo = FileUtil.toFileObject(getAntScript()); 120 if (fo != null && Util.getAntScriptTargetNames(fo) != null) { 121 antScriptValidityChecked = true; 122 } else { 123 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_5"); 124 } 125 } 126 if (getProjectName().length() == 0) { 127 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_6"); 128 } 129 if (projectFolder.getText().length() == 0) { 130 return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_7"); 131 } 132 if (getAsFile(projectFolder.getText() + File.separatorChar + "nbproject").exists()){ return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_8"); 134 } 135 136 Project p; 137 File projectFolder = getProjectFolder(); 138 139 assert projectFolder != null; 140 141 if ((p = FileOwnerQuery.getOwner(projectFolder.toURI())) != null && projectFolder.equals(FileUtil.toFile(p.getProjectDirectory()))) { 142 ProjectInformation pi = p.getLookup().lookup(ProjectInformation.class); 143 String displayName = (pi == null ? "" : pi.getDisplayName()); return MessageFormat.format(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_9"), 145 new Object [] {displayName}); 146 } 147 148 File projectLocation = getProjectLocation(); 149 150 assert projectLocation != null; 151 152 if ((p = FileOwnerQuery.getOwner(projectLocation.toURI())) != null && projectLocation.equals(FileUtil.toFile(p.getProjectDirectory()))) { 153 ProjectInformation pi = p.getLookup().lookup(ProjectInformation.class); 154 String displayName = (pi == null ? "" : pi.getDisplayName()); return MessageFormat.format(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_10"), 156 new Object [] {displayName}); 157 } 158 return null; 159 } 160 161 private File getAsFile(String filename) { 162 return FileUtil.normalizeFile(new File (filename)); 163 } 164 165 private boolean ignoreEvent = false; 166 167 private void update(DocumentEvent e) { 168 if (ignoreEvent) { 169 return; 171 } 172 173 ignoreEvent = true; 175 176 if (projectLocation.getDocument() == e.getDocument()) { 177 antScriptValidityChecked = false; 178 updateAntScriptLocation(); 179 updateProjectName(); 180 updateProjectFolder(); 181 } 182 if (antScript.getDocument() == e.getDocument()) { 183 antScriptValidityChecked = false; 184 updateProjectName(); 185 } 186 187 ignoreEvent = false; 189 190 if (projectFolder.getDocument() == e.getDocument()) { 191 projectFolderTouched = !"".equals(projectFolder.getText()); 192 } 193 if (antScript.getDocument() == e.getDocument()) { 194 antScriptTouched = !"".equals(antScript.getText()); 195 } 196 if (projectName.getDocument() == e.getDocument()) { 197 projectNameTouched = !"".equals(projectName.getText()); 198 } 199 200 listener.stateChanged(null); 201 } 202 203 private boolean isValidProjectLocation() { 204 return (getProjectLocation().exists() && getProjectLocation().isDirectory() && 205 projectLocation.getText().length() > 0 && (!projectLocation.getText().endsWith(":"))); } 207 208 private void updateAntScriptLocation() { 209 if (antScriptTouched) { 210 return; 211 } 212 if (isValidProjectLocation()) { 213 File as = new File (getProjectLocation().getAbsolutePath() + File.separatorChar + "build.xml"); if (as.exists()) { 215 antScript.setText(as.getAbsolutePath()); 216 return; 217 } 218 } 219 antScript.setText(""); } 221 222 private void updateProjectName() { 223 if (projectNameTouched) { 224 return; 225 } 226 if (getAntScript().exists()) { 227 File as = new File (getAntScript().getAbsolutePath()); 228 if (as.exists()) { 229 FileObject fo = FileUtil.toFileObject(as); 230 assert fo != null : as; 231 String name = Util.getAntScriptName(fo); 232 if (name != null) { 233 projectName.setText(name); 234 return; 235 } 236 } 237 } 238 projectName.setText(""); } 240 241 private void updateProjectFolder() { 242 if (projectFolderTouched) { 243 return; 244 } 245 if (isValidProjectLocation()) { 246 projectFolder.setText(getProjectLocation().getAbsolutePath()); 247 } else { 248 projectFolder.setText(""); } 250 } 251 252 258 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 260 261 jLabel1 = new javax.swing.JLabel (); 262 jLabel2 = new javax.swing.JLabel (); 263 jLabel3 = new javax.swing.JLabel (); 264 jLabel4 = new javax.swing.JLabel (); 265 jLabel5 = new javax.swing.JLabel (); 266 antScript = new javax.swing.JTextField (); 267 projectName = new javax.swing.JTextField (); 268 projectFolder = new javax.swing.JTextField (); 269 browseAntScript = new javax.swing.JButton (); 270 browseProjectFolder = new javax.swing.JButton (); 271 projectLocation = new javax.swing.JTextField (); 272 jLabel6 = new javax.swing.JLabel (); 273 browseProjectLocation = new javax.swing.JButton (); 274 jSeparator1 = new javax.swing.JSeparator (); 275 mainProject = new javax.swing.JCheckBox (); 276 277 FormListener formListener = new FormListener(); 278 279 setLayout(new java.awt.GridBagLayout ()); 280 281 setPreferredSize(new java.awt.Dimension (323, 223)); 282 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_jLabel1")); 283 gridBagConstraints = new java.awt.GridBagConstraints (); 284 gridBagConstraints.gridwidth = 3; 285 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 286 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 287 add(jLabel1, gridBagConstraints); 288 jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_jLabel1")); 289 290 jLabel2.setLabelFor(antScript); 291 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_jLabel2")); 292 gridBagConstraints = new java.awt.GridBagConstraints (); 293 gridBagConstraints.gridx = 0; 294 gridBagConstraints.gridy = 2; 295 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 296 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 297 add(jLabel2, gridBagConstraints); 298 jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_jLabel2")); 299 300 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_jLabel3")); 301 gridBagConstraints = new java.awt.GridBagConstraints (); 302 gridBagConstraints.gridx = 0; 303 gridBagConstraints.gridy = 3; 304 gridBagConstraints.gridwidth = 3; 305 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 306 gridBagConstraints.insets = new java.awt.Insets (24, 0, 12, 0); 307 add(jLabel3, gridBagConstraints); 308 jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_jLabel3")); 309 310 jLabel4.setLabelFor(projectName); 311 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_jLabel4")); 312 gridBagConstraints = new java.awt.GridBagConstraints (); 313 gridBagConstraints.gridx = 0; 314 gridBagConstraints.gridy = 4; 315 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 316 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 317 add(jLabel4, gridBagConstraints); 318 jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_jLabel4")); 319 320 jLabel5.setLabelFor(projectFolder); 321 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_jLabel5")); 322 gridBagConstraints = new java.awt.GridBagConstraints (); 323 gridBagConstraints.gridx = 0; 324 gridBagConstraints.gridy = 5; 325 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 326 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 327 add(jLabel5, gridBagConstraints); 328 jLabel5.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_jLabel5")); 329 330 gridBagConstraints = new java.awt.GridBagConstraints (); 331 gridBagConstraints.gridx = 1; 332 gridBagConstraints.gridy = 2; 333 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 334 gridBagConstraints.weightx = 1.0; 335 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 336 add(antScript, gridBagConstraints); 337 338 gridBagConstraints = new java.awt.GridBagConstraints (); 339 gridBagConstraints.gridx = 1; 340 gridBagConstraints.gridy = 4; 341 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 342 gridBagConstraints.weightx = 1.0; 343 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 344 add(projectName, gridBagConstraints); 345 346 gridBagConstraints = new java.awt.GridBagConstraints (); 347 gridBagConstraints.gridx = 1; 348 gridBagConstraints.gridy = 5; 349 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 350 gridBagConstraints.weightx = 1.0; 351 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 352 add(projectFolder, gridBagConstraints); 353 354 org.openide.awt.Mnemonics.setLocalizedText(browseAntScript, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "BTN_BasicProjectInfoPanel_browseAntScript")); 355 browseAntScript.addActionListener(formListener); 356 357 gridBagConstraints = new java.awt.GridBagConstraints (); 358 gridBagConstraints.gridx = 2; 359 gridBagConstraints.gridy = 2; 360 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 361 add(browseAntScript, gridBagConstraints); 362 browseAntScript.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_browseAntScript")); 363 364 org.openide.awt.Mnemonics.setLocalizedText(browseProjectFolder, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "BTN_BasicProjectInfoPanel_browseProjectFolder")); 365 browseProjectFolder.addActionListener(formListener); 366 367 gridBagConstraints = new java.awt.GridBagConstraints (); 368 gridBagConstraints.gridx = 2; 369 gridBagConstraints.gridy = 5; 370 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 371 add(browseProjectFolder, gridBagConstraints); 372 browseProjectFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_browseProjectFolder")); 373 374 gridBagConstraints = new java.awt.GridBagConstraints (); 375 gridBagConstraints.gridx = 1; 376 gridBagConstraints.gridy = 1; 377 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 378 gridBagConstraints.weightx = 1.0; 379 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 380 add(projectLocation, gridBagConstraints); 381 382 jLabel6.setLabelFor(projectLocation); 383 org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_jLabel6")); 384 gridBagConstraints = new java.awt.GridBagConstraints (); 385 gridBagConstraints.gridx = 0; 386 gridBagConstraints.gridy = 1; 387 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 388 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 389 add(jLabel6, gridBagConstraints); 390 jLabel6.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_jLabel6")); 391 392 org.openide.awt.Mnemonics.setLocalizedText(browseProjectLocation, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "BTN_BasicProjectInfoPanel_browseProjectLocation")); 393 browseProjectLocation.addActionListener(formListener); 394 395 gridBagConstraints = new java.awt.GridBagConstraints (); 396 gridBagConstraints.gridx = 2; 397 gridBagConstraints.gridy = 1; 398 add(browseProjectLocation, gridBagConstraints); 399 browseProjectLocation.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_browseProjectLocation")); 400 401 gridBagConstraints = new java.awt.GridBagConstraints (); 402 gridBagConstraints.gridx = 0; 403 gridBagConstraints.gridy = 6; 404 gridBagConstraints.gridwidth = 3; 405 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 406 gridBagConstraints.insets = new java.awt.Insets (12, 0, 12, 0); 407 add(jSeparator1, gridBagConstraints); 408 409 mainProject.setSelected(true); 410 org.openide.awt.Mnemonics.setLocalizedText(mainProject, org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_mainProject")); 411 gridBagConstraints = new java.awt.GridBagConstraints (); 412 gridBagConstraints.gridx = 0; 413 gridBagConstraints.gridy = 7; 414 gridBagConstraints.gridwidth = 3; 415 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 416 gridBagConstraints.weighty = 1.0; 417 add(mainProject, gridBagConstraints); 418 mainProject.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "ACSD_BasicProjectInfoPanel_mainProject")); 419 420 } 421 422 424 private class FormListener implements java.awt.event.ActionListener { 425 public void actionPerformed(java.awt.event.ActionEvent evt) { 426 if (evt.getSource() == browseAntScript) { 427 BasicProjectInfoPanel.this.browseAntScriptActionPerformed(evt); 428 } 429 else if (evt.getSource() == browseProjectFolder) { 430 BasicProjectInfoPanel.this.browseProjectFolderActionPerformed(evt); 431 } 432 else if (evt.getSource() == browseProjectLocation) { 433 BasicProjectInfoPanel.this.browseProjectLocationActionPerformed(evt); 434 } 435 } 436 } 438 private void browseProjectLocationActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 440 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 441 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 442 if (projectLocation.getText().length() > 0 && getProjectLocation().exists()) { 443 chooser.setSelectedFile(getProjectLocation()); 444 } else { 445 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 446 } 447 chooser.setDialogTitle(NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_Browse_Location")); 448 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 449 File projectLoc = FileUtil.normalizeFile(chooser.getSelectedFile()); 450 projectLocation.setText(projectLoc.getAbsolutePath()); 451 } 452 } 454 private void browseProjectFolderActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 456 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 457 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 458 if (projectFolder.getText().length() > 0 && getProjectFolder().exists()) { 459 chooser.setSelectedFile(getProjectFolder()); 460 } else if (projectLocation.getText().length() > 0 && getProjectLocation().exists()) { 461 chooser.setSelectedFile(getProjectLocation()); 462 } else { 463 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 464 } 465 chooser.setDialogTitle(NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_Browse_Project_Folder")); 466 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 467 File projectDir = FileUtil.normalizeFile(chooser.getSelectedFile()); 468 projectFolder.setText(projectDir.getAbsolutePath()); 469 } 470 } 472 private void browseAntScriptActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 474 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 475 chooser.setFileSelectionMode (JFileChooser.FILES_ONLY); 476 if (antScript.getText().length() > 0 && getAntScript().exists()) { 477 chooser.setSelectedFile(getAntScript()); 478 } else if (projectLocation.getText().length() > 0 && getProjectLocation().exists()) { 479 chooser.setSelectedFile(getProjectLocation()); 480 } else { 481 chooser.setSelectedFile(ProjectChooser.getProjectsFolder()); 482 } 483 chooser.setDialogTitle(NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_Browse_Build_Script")); 484 if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 485 File script = FileUtil.normalizeFile(chooser.getSelectedFile()); 486 antScript.setText(script.getAbsolutePath()); 487 } 488 } 490 491 private javax.swing.JTextField antScript; 493 private javax.swing.JButton browseAntScript; 494 private javax.swing.JButton browseProjectFolder; 495 private javax.swing.JButton browseProjectLocation; 496 private javax.swing.JLabel jLabel1; 497 private javax.swing.JLabel jLabel2; 498 private javax.swing.JLabel jLabel3; 499 private javax.swing.JLabel jLabel4; 500 private javax.swing.JLabel jLabel5; 501 private javax.swing.JLabel jLabel6; 502 private javax.swing.JSeparator jSeparator1; 503 private javax.swing.JCheckBox mainProject; 504 private javax.swing.JTextField projectFolder; 505 private javax.swing.JTextField projectLocation; 506 private javax.swing.JTextField projectName; 507 509 } 510 | Popular Tags |