1 19 20 package org.netbeans.modules.projectimport.jbuilder.ui; 21 import java.awt.Color ; 22 import java.io.File ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import javax.swing.JFileChooser ; 26 import javax.swing.JTextField ; 27 import javax.swing.SwingUtilities ; 28 import javax.swing.UIManager ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.filechooser.FileFilter ; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.modules.projectimport.j2seimport.AbstractProject; 34 import org.netbeans.modules.projectimport.j2seimport.ui.BasicWizardIterator; 35 import org.netbeans.modules.projectimport.jbuilder.parsing.ProjectBuilder; 36 import org.netbeans.modules.projectimport.jbuilder.parsing.UserLibrarySupport; 37 import org.openide.WizardDescriptor; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.HelpCtx; 40 import org.openide.util.NbBundle; 41 import org.openide.util.RequestProcessor; 42 43 44 49 public final class JBWizardPanel extends BasicWizardIterator.Panel { 50 private JBWizardData data = null; 51 52 private boolean projectFileValid = false; 53 private Collection allPrjDefs = null; 54 private DocumentListener documentListener; 55 56 57 JBWizardPanel(WizardDescriptor wiz, JBWizardData data) { 58 super(wiz); 59 this.data = data; 60 initComponents(); 61 initAccessibility(); 62 } 63 64 private static String getMessage(String key) { 65 return NbBundle.getMessage(JBWizardPanel.class, key); 66 } 67 68 private void initAccessibility() { 69 prjFileBrowse.getAccessibleContext().setAccessibleDescription(getMessage("ACS_BrowseProjectFile")); 70 prjFileTextField.getAccessibleContext().setAccessibleDescription(getMessage("ACS_Project")); 71 destDirTextField.getAccessibleContext().setAccessibleDescription(getMessage("ACS_ProjectDestination")); 72 userHomeTextField.getAccessibleContext().setAccessibleDescription(getMessage("ACS_UserHome")); 73 userHomeBrowse.getAccessibleContext().setAccessibleDescription(getMessage("ACS_BrowseUserHome")); 74 destDirBrowse.getAccessibleContext().setAccessibleDescription(getMessage("ACS_BrowseDestDir")); 75 jbDirBrowse.getAccessibleContext().setAccessibleDescription(getMessage("ACS_BrowseInstallDir")); 76 jbDirTextField.getAccessibleContext().setAccessibleDescription(getMessage("ACS_InstallDir")); 77 } 78 79 private void addListeners() { 80 if (documentListener == null) { 81 documentListener = new DocumentListener () { 82 public void removeUpdate(DocumentEvent e) { insertUpdate(null); } 83 public void changedUpdate(DocumentEvent e) { insertUpdate(null); } 84 public void insertUpdate(DocumentEvent e) { fieldUpdate(); } 85 }; 86 87 prjFileTextField.getDocument().addDocumentListener(documentListener); 88 destDirTextField.getDocument().addDocumentListener(documentListener); 89 jbDirTextField.getDocument().addDocumentListener(documentListener); 90 userHomeTextField.getDocument().addDocumentListener(documentListener); 91 } 92 } 93 94 private void removeListeners() { 95 if (documentListener != null) { 96 prjFileTextField.getDocument().removeDocumentListener(documentListener); 97 destDirTextField.getDocument().removeDocumentListener(documentListener); 98 jbDirTextField.getDocument().removeDocumentListener(documentListener); 99 userHomeTextField.getDocument().removeDocumentListener(documentListener); 100 } 101 } 102 103 public void addNotify() { 104 super.addNotify(); 105 addListeners(); 106 fieldUpdate(); 107 Color lblBgr = UIManager.getColor("Label.background"); prjFileTextArea.setBackground(lblBgr); 109 jSpanTextArea.setBackground(lblBgr); 110 } 111 112 public void removeNotify() { 113 super.removeNotify(); 114 removeListeners(); 115 } 116 117 private void changedInstallDir() { 118 File libFolder = null; 119 if (UserLibrarySupport.getInstallDirLib() == null) { 120 File installDir = getInstallationDir(); 121 if (installDir.exists()) { 122 if (!installDir.getName().equals("lib")) { libFolder = new File (installDir,"lib"); libFolder = (libFolder.exists()) ? libFolder : null; 125 } 126 } 127 } 128 129 if (libFolder != null) { 130 Iterator it = Utils.getInvalidUserLibraries(allPrjDefs).iterator(); 131 while (it.hasNext()) { 132 AbstractProject.UserLibrary uLib = (AbstractProject.UserLibrary)it.next(); 133 UserLibrarySupport usInstance; 134 135 if (UserLibrarySupport.getInstance(uLib.getName(), libFolder) != null) { 136 UserLibrarySupport.setInstallDirLib(libFolder); 137 if (projectFileValid) { 138 parseProjectFile(getProjectFile()); 139 } 140 break; 141 } 142 } 143 } 144 } 145 146 private void fieldUpdate() { 147 String errorMessage = null; 148 boolean isUserHomeInvalid = false; 149 File destFolder = (isEmpty(destDirTextField)) ? null : getFile(destDirTextField); 150 boolean isDesttDirValid = false; 151 for(File tempFolder = destFolder; tempFolder != null ; tempFolder = tempFolder.getParentFile()) { 152 if (tempFolder == null || tempFolder.isFile()) { 153 break; 154 } 155 if (tempFolder.canWrite()) { 156 isDesttDirValid = true; 157 break; 158 } 159 } 160 if (!isDesttDirValid) { 161 errorMessage = NbBundle.getMessage(JBWizardPanel.class, "MSG_NotRegularDestinationDir",destDirTextField.getText()); } 163 File projectFile = (isEmpty(prjFileTextField)) ? null : getFile(prjFileTextField); 164 boolean unresolvedReferences = Utils.checkNotFoundUserLibraries(allPrjDefs); 165 if (projectFile != null && ProjectBuilder.isProjectFile(projectFile)) { 166 if (!projectFileValid) { 167 projectFileValid = true; 168 parseProjectFile(projectFile); 169 return; 170 } else { 171 if (unresolvedReferences) { 172 File userHomeDir = UserLibrarySupport.getUserHomeLib(); 173 File f = getFile(userHomeTextField); 174 if (isDesttDirValid) { 175 if (!isEmpty(userHomeTextField) && f.exists() && !f.equals(userHomeDir)) { 176 UserLibrarySupport.setUserHomeLib(f); 177 parseProjectFile(projectFile); 178 return; 179 } else if (isEmpty(userHomeTextField) || !f.exists() || f.getName().indexOf("jbuilder") == -1 ) { 180 isUserHomeInvalid = true; 182 } else if (jbDirTextField.isEnabled()) { 183 changedInstallDir(); 184 } 185 } 186 } 187 } 188 enableAdditionalComponents(unresolvedReferences); 189 } else { 190 projectFileValid = false; 191 enableAdditionalComponents(false); 192 errorMessage = NbBundle.getMessage(JBWizardPanel.class, "MSG_NotRegularProject",prjFileTextField.getText()); } 194 195 if (errorMessage == null) { 196 markValid(); 197 if (jbDirTextField.isEnabled() || isUserHomeInvalid) { 198 setWarning(getMessage("LBL_Warning")); 199 } 200 } else { 201 setError(errorMessage); 202 } 203 } 204 205 protected String getPanelName() { 206 return NbBundle.getMessage(JBWizardPanel.class, "CTL_FirstStep"); 207 } 208 209 protected void storeToDataModel() { 210 removeListeners(); 211 fieldUpdate(); 212 213 this.data.setDestinationDir(getFile(destDirTextField)); 214 this.data.setProjectFile(getFile(prjFileTextField)); 215 this.data.setProjectDefinition(this.allPrjDefs); 216 this.data.setIncludeDependencies((Utils.getDependencyErrors(allPrjDefs,getDestinationDir()) == null)); 217 } 218 219 protected void readFromDataModel() { 220 addListeners(); 221 enableAdditionalComponents(false); 222 if (data.getDestinationDir() != null) { 223 destDirTextField.setText(data.getDestinationDir().getAbsolutePath()); 224 } else { 225 destDirTextField.setText(""); } 227 if (data.getProjectFile() != null) { 228 prjFileTextField.setText(data.getProjectFile().getAbsolutePath()); 229 } else { 230 prjFileTextField.setText(""); } 232 File uLib = UserLibrarySupport.getUserHomeLib(); 233 if (uLib != null && uLib.exists()) { 234 userHomeTextField.setText(uLib.getAbsolutePath()); 235 } 236 237 File iLib = UserLibrarySupport.getInstallDirLib(); 238 if (iLib != null && iLib.exists()) { 239 if (iLib.getName().equals("lib") && iLib.getParentFile() != null) { iLib = iLib.getParentFile(); 241 } 242 jbDirTextField.setText(iLib.getAbsolutePath()); 243 } 244 245 this.allPrjDefs = data.getProjectDefinition(); 246 } 247 248 protected HelpCtx getHelp() { 249 return new HelpCtx(JBWizardPanel.class); 250 } 251 252 private void parseProjectFile(final File projectFile) { 253 RequestProcessor.getDefault().post(new Runnable () { 254 public void run() { 255 ProjectManager.mutex().writeAccess(new Runnable () { 256 public void run() { 257 Collection result = ProjectBuilder.buildProjectModels(projectFile); 258 if (result != null && result.size() > 0 && projectFileValid && 259 projectFile.equals(getFile(prjFileTextField))) { 260 allPrjDefs = result; 261 SwingUtilities.invokeLater(new Runnable () { 262 public void run() { 263 fieldUpdate(); 264 } 265 }); 266 } 267 } 268 }); 269 } 270 }); 271 } 272 273 private void enableAdditionalComponents(boolean enable) { 274 jbDirTextField.setEnabled(enable); 275 jbDirBrowse.setEnabled(enable); 276 jbDirLabel.setEnabled(enable); 277 } 278 279 public static File getFile(JTextField textField) { 280 return new File (textField.getText().trim()); 281 } 282 283 public static boolean isEmpty(JTextField textField) { 284 return textField.getText().trim().length() == 0; 285 } 286 287 public File getDestinationDir() { 288 return getFile(destDirTextField); 289 } 290 291 292 public File getProjectFile() { 293 return getFile(prjFileTextField); 294 } 295 296 public File getInstallationDir() { 297 return getFile(jbDirTextField); 298 } 299 300 private void browseActionPerformed(JTextField field, boolean fileHidingEnabled) { 301 String initValue = field.getText().trim(); 302 JFileChooser chooser = new JFileChooser (initValue); 303 FileUtil.preventFileChooserSymlinkTraversal(chooser, chooser.getCurrentDirectory()); 304 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 305 chooser.setFileHidingEnabled(fileHidingEnabled); 306 int ret = chooser.showOpenDialog(this); 307 if (ret == JFileChooser.APPROVE_OPTION) { 308 field.setText(chooser.getSelectedFile().getAbsolutePath()); 309 } 310 } 311 312 317 private void initComponents() { 319 java.awt.GridBagConstraints gridBagConstraints; 320 321 buttonGroup = new javax.swing.ButtonGroup (); 322 prjFileTextField = new javax.swing.JTextField (); 323 prjFileBrowse = new javax.swing.JButton (); 324 prjFileLabel = new javax.swing.JLabel (); 325 destDirLabel = new javax.swing.JLabel (); 326 destDirTextField = new javax.swing.JTextField (); 327 destDirBrowse = new javax.swing.JButton (); 328 prjFileTextArea = new javax.swing.JTextArea (); 329 jbDirTextField = new javax.swing.JTextField (); 330 jbDirBrowse = new javax.swing.JButton (); 331 jSpanTextArea = new javax.swing.JTextArea (); 332 jbDirLabel = new javax.swing.JLabel (); 333 userHomeLabel = new javax.swing.JLabel (); 334 userHomeTextField = new javax.swing.JTextField (); 335 userHomeBrowse = new javax.swing.JButton (); 336 337 setLayout(new java.awt.GridBagLayout ()); 338 339 setPreferredSize(new java.awt.Dimension (500, 380)); 340 gridBagConstraints = new java.awt.GridBagConstraints (); 341 gridBagConstraints.gridx = 1; 342 gridBagConstraints.gridy = 1; 343 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 344 gridBagConstraints.weightx = 1.0; 345 add(prjFileTextField, gridBagConstraints); 346 347 org.openide.awt.Mnemonics.setLocalizedText(prjFileBrowse, java.util.ResourceBundle.getBundle("org/netbeans/modules/projectimport/jbuilder/ui/Bundle").getString("CTL_BrowseProjectFile")); 348 prjFileBrowse.addActionListener(new java.awt.event.ActionListener () { 349 public void actionPerformed(java.awt.event.ActionEvent evt) { 350 prjFileBrowseActionPerformed(evt); 351 } 352 }); 353 354 gridBagConstraints = new java.awt.GridBagConstraints (); 355 gridBagConstraints.gridx = 2; 356 gridBagConstraints.gridy = 1; 357 gridBagConstraints.insets = new java.awt.Insets (0, 11, 0, 10); 358 add(prjFileBrowse, gridBagConstraints); 359 360 prjFileLabel.setLabelFor(prjFileTextField); 361 org.openide.awt.Mnemonics.setLocalizedText(prjFileLabel, org.openide.util.NbBundle.getMessage(JBWizardPanel.class, "LBL_Project")); 362 gridBagConstraints = new java.awt.GridBagConstraints (); 363 gridBagConstraints.gridx = 0; 364 gridBagConstraints.gridy = 1; 365 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 366 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 367 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 368 add(prjFileLabel, gridBagConstraints); 369 370 destDirLabel.setLabelFor(destDirTextField); 371 org.openide.awt.Mnemonics.setLocalizedText(destDirLabel, org.openide.util.NbBundle.getMessage(JBWizardPanel.class, "LBL_ProjectDestination")); 372 gridBagConstraints = new java.awt.GridBagConstraints (); 373 gridBagConstraints.gridx = 0; 374 gridBagConstraints.gridy = 2; 375 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 376 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 377 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 378 add(destDirLabel, gridBagConstraints); 379 380 gridBagConstraints = new java.awt.GridBagConstraints (); 381 gridBagConstraints.gridx = 1; 382 gridBagConstraints.gridy = 2; 383 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 384 gridBagConstraints.weightx = 1.0; 385 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 386 add(destDirTextField, gridBagConstraints); 387 388 org.openide.awt.Mnemonics.setLocalizedText(destDirBrowse, java.util.ResourceBundle.getBundle("org/netbeans/modules/projectimport/jbuilder/ui/Bundle").getString("CTL_BrowseDestDir")); 389 destDirBrowse.addActionListener(new java.awt.event.ActionListener () { 390 public void actionPerformed(java.awt.event.ActionEvent evt) { 391 destDirBrowseActionPerformed(evt); 392 } 393 }); 394 395 gridBagConstraints = new java.awt.GridBagConstraints (); 396 gridBagConstraints.gridx = 2; 397 gridBagConstraints.gridy = 2; 398 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 10); 399 add(destDirBrowse, gridBagConstraints); 400 401 prjFileTextArea.setEditable(false); 402 prjFileTextArea.setLineWrap(true); 403 prjFileTextArea.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/projectimport/jbuilder/ui/Bundle").getString("LBL_SpecifyProjectDescription")); 404 prjFileTextArea.setWrapStyleWord(true); 405 gridBagConstraints = new java.awt.GridBagConstraints (); 406 gridBagConstraints.gridx = 0; 407 gridBagConstraints.gridy = 0; 408 gridBagConstraints.gridwidth = 3; 409 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 410 gridBagConstraints.insets = new java.awt.Insets (0, 0, 24, 0); 411 add(prjFileTextArea, gridBagConstraints); 412 413 gridBagConstraints = new java.awt.GridBagConstraints (); 414 gridBagConstraints.gridx = 1; 415 gridBagConstraints.gridy = 4; 416 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 417 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 418 gridBagConstraints.weightx = 1.0; 419 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 420 add(jbDirTextField, gridBagConstraints); 421 422 org.openide.awt.Mnemonics.setLocalizedText(jbDirBrowse, java.util.ResourceBundle.getBundle("org/netbeans/modules/projectimport/jbuilder/ui/Bundle").getString("CTL_BrowseInstallDir")); 423 jbDirBrowse.addActionListener(new java.awt.event.ActionListener () { 424 public void actionPerformed(java.awt.event.ActionEvent evt) { 425 jbDirBrowseActionPerformed(evt); 426 } 427 }); 428 429 gridBagConstraints = new java.awt.GridBagConstraints (); 430 gridBagConstraints.gridx = 2; 431 gridBagConstraints.gridy = 4; 432 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 10); 433 add(jbDirBrowse, gridBagConstraints); 434 435 jSpanTextArea.setEditable(false); 436 jSpanTextArea.setLineWrap(true); 437 jSpanTextArea.setWrapStyleWord(true); 438 gridBagConstraints = new java.awt.GridBagConstraints (); 439 gridBagConstraints.gridx = 0; 440 gridBagConstraints.gridy = 5; 441 gridBagConstraints.gridwidth = 3; 442 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 443 gridBagConstraints.weighty = 1.0; 444 gridBagConstraints.insets = new java.awt.Insets (24, 0, 10, 0); 445 add(jSpanTextArea, gridBagConstraints); 446 447 jbDirLabel.setLabelFor(jbDirTextField); 448 org.openide.awt.Mnemonics.setLocalizedText(jbDirLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/projectimport/jbuilder/ui/Bundle").getString("LBL_InstallDir")); 449 gridBagConstraints = new java.awt.GridBagConstraints (); 450 gridBagConstraints.gridx = 0; 451 gridBagConstraints.gridy = 4; 452 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 453 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 454 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 455 add(jbDirLabel, gridBagConstraints); 456 457 userHomeLabel.setLabelFor(userHomeTextField); 458 org.openide.awt.Mnemonics.setLocalizedText(userHomeLabel, org.openide.util.NbBundle.getMessage(JBWizardPanel.class, "LBL_UserHome")); 459 gridBagConstraints = new java.awt.GridBagConstraints (); 460 gridBagConstraints.gridx = 0; 461 gridBagConstraints.gridy = 3; 462 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 463 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 464 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 465 add(userHomeLabel, gridBagConstraints); 466 467 gridBagConstraints = new java.awt.GridBagConstraints (); 468 gridBagConstraints.gridx = 1; 469 gridBagConstraints.gridy = 3; 470 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 471 gridBagConstraints.weightx = 1.0; 472 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 473 add(userHomeTextField, gridBagConstraints); 474 475 org.openide.awt.Mnemonics.setLocalizedText(userHomeBrowse, java.util.ResourceBundle.getBundle("org/netbeans/modules/projectimport/jbuilder/ui/Bundle").getString("CTL_BrowseUserHome")); 476 userHomeBrowse.addActionListener(new java.awt.event.ActionListener () { 477 public void actionPerformed(java.awt.event.ActionEvent evt) { 478 userHomeBrowseActionPerformed(evt); 479 } 480 }); 481 482 gridBagConstraints = new java.awt.GridBagConstraints (); 483 gridBagConstraints.gridx = 2; 484 gridBagConstraints.gridy = 3; 485 gridBagConstraints.insets = new java.awt.Insets (5, 11, 0, 10); 486 add(userHomeBrowse, gridBagConstraints); 487 488 } 490 private void userHomeBrowseActionPerformed(java.awt.event.ActionEvent evt) { browseActionPerformed(userHomeTextField, false); 492 } 494 private void jbDirBrowseActionPerformed(java.awt.event.ActionEvent evt) { browseActionPerformed(jbDirTextField, true); 496 } 498 private void destDirBrowseActionPerformed(java.awt.event.ActionEvent evt) { browseActionPerformed(destDirTextField, true); 500 } 502 503 private void prjFileBrowseActionPerformed(java.awt.event.ActionEvent evt) { String initValue = prjFileTextField.getText().trim(); 505 JFileChooser chooser = new JFileChooser (initValue); 506 chooser.setFileFilter(new FileFilter (){ 507 public boolean accept(File f) { 508 return f.isDirectory() || ProjectBuilder.isProjectFile(f); 509 } 510 511 public String getDescription() { 512 return NbBundle.getMessage(JBWizardPanel.class, "ProjectFileDescription"); } 514 515 }); 516 FileUtil.preventFileChooserSymlinkTraversal(chooser, chooser.getCurrentDirectory()); 517 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 518 int ret = chooser.showOpenDialog(this); 519 if (ret == JFileChooser.APPROVE_OPTION) { 520 prjFileTextField.setText(chooser.getSelectedFile().getAbsolutePath()); 521 } 522 } 524 525 526 private javax.swing.ButtonGroup buttonGroup; 528 private javax.swing.JButton destDirBrowse; 529 private javax.swing.JLabel destDirLabel; 530 private javax.swing.JTextField destDirTextField; 531 private javax.swing.JTextArea jSpanTextArea; 532 private javax.swing.JButton jbDirBrowse; 533 private javax.swing.JLabel jbDirLabel; 534 private javax.swing.JTextField jbDirTextField; 535 private javax.swing.JButton prjFileBrowse; 536 private javax.swing.JLabel prjFileLabel; 537 private javax.swing.JTextArea prjFileTextArea; 538 private javax.swing.JTextField prjFileTextField; 539 private javax.swing.JButton userHomeBrowse; 540 private javax.swing.JLabel userHomeLabel; 541 private javax.swing.JTextField userHomeTextField; 542 } 544 | Popular Tags |