1 19 20 package org.netbeans.modules.j2ee.archive.wizard; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.File ; 25 import java.text.MessageFormat ; 26 import java.util.ResourceBundle ; 27 import javax.swing.JFileChooser ; 28 import javax.swing.JPanel ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.filechooser.FileFilter ; 32 import javax.swing.text.Document ; 33 import org.netbeans.modules.j2ee.archive.project.*; 34 import org.netbeans.modules.j2ee.archive.ui.JavaEePlatformUiSupport; 35 import org.netbeans.modules.j2ee.deployment.devmodules.api.ServerManager; 36 import org.netbeans.spi.project.ui.support.ProjectChooser; 37 import org.openide.WizardDescriptor; 38 import org.openide.WizardValidationException; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.NbBundle; 41 42 class DeployablePanelVisual extends JPanel implements DocumentListener , ActionListener { 43 44 private transient DeployableWizardPanel panel; 45 private static File savedArchiveLoc = null; 46 47 48 public DeployablePanelVisual(DeployableWizardPanel panel) { 49 initComponents(); 50 this.panel = panel; 51 52 projectNameTextField.getDocument().addDocumentListener(this); 54 projectLocationTextField.getDocument().addDocumentListener(this); 55 archiveFileField.getDocument().addDocumentListener(this); 56 serverInstanceComboBox.addActionListener(this); 57 } 58 59 60 public String getProjectName() { 61 return this.projectNameTextField.getText(); 62 } 63 64 private String validFreeProjectName(final File parentFolder, final String formater, final int index) { 65 String name = MessageFormat.format(formater, new Object []{new Integer (index)}); 66 File file = new File (parentFolder, name); 67 return file.exists() ? null : name; 68 } 69 70 75 private void initComponents() { 77 projectNameLabel = new javax.swing.JLabel (); 78 projectNameTextField = new javax.swing.JTextField (); 79 projectLocationLabel = new javax.swing.JLabel (); 80 projectLocationTextField = new javax.swing.JTextField (); 81 projectLocationButton = new javax.swing.JButton (); 82 createdFolderLabel = new javax.swing.JLabel (); 83 createdFolderTextField = new javax.swing.JTextField (); 84 archiveFileLabel = new javax.swing.JLabel (); 85 archiveFileField = new javax.swing.JTextField (); 86 archiveLocationButton = new javax.swing.JButton (); 87 targetServerLabel = new javax.swing.JLabel (); 88 serverInstanceComboBox = new javax.swing.JComboBox (); 89 manageServersButton = new javax.swing.JButton (); 90 91 projectNameLabel.setLabelFor(projectNameTextField); 92 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("projectName_TEXT")); 93 94 projectLocationLabel.setLabelFor(projectLocationTextField); 95 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("projectLocation_TEXT")); 96 97 org.openide.awt.Mnemonics.setLocalizedText(projectLocationButton, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("browse_TEXT")); 98 projectLocationButton.setActionCommand(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("BROWSE")); 99 projectLocationButton.addActionListener(new java.awt.event.ActionListener () { 100 public void actionPerformed(java.awt.event.ActionEvent evt) { 101 projectLocationButtonActionPerformed(evt); 102 } 103 }); 104 105 createdFolderLabel.setLabelFor(createdFolderTextField); 106 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("projectFolder_TEXT")); 107 108 createdFolderTextField.setEditable(false); 109 110 org.openide.awt.Mnemonics.setLocalizedText(archiveFileLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("archiveFileLabel_TEXT")); 111 112 org.openide.awt.Mnemonics.setLocalizedText(archiveLocationButton, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("browse_TEXT")); 113 archiveLocationButton.addActionListener(new java.awt.event.ActionListener () { 114 public void actionPerformed(java.awt.event.ActionEvent evt) { 115 archiveLocationButtonActionPerformed(evt); 116 } 117 }); 118 119 org.openide.awt.Mnemonics.setLocalizedText(targetServerLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("targetServerLabel_TEXT")); 120 121 serverInstanceComboBox.setModel(JavaEePlatformUiSupport.createPlatformComboBoxModel(null)); 122 123 org.openide.awt.Mnemonics.setLocalizedText(manageServersButton, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("LBL_ManageServers")); 124 manageServersButton.addActionListener(new java.awt.event.ActionListener () { 125 public void actionPerformed(java.awt.event.ActionEvent evt) { 126 manageServersButtonActionPerformed(evt); 127 } 128 }); 129 130 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 131 this.setLayout(layout); 132 layout.setHorizontalGroup( 133 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 134 .add(layout.createSequentialGroup() 135 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 136 .add(layout.createSequentialGroup() 137 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 138 .add(projectNameLabel) 139 .add(projectLocationLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE) 140 .add(createdFolderLabel) 141 .add(archiveFileLabel)) 142 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)) 143 .add(layout.createSequentialGroup() 144 .add(targetServerLabel) 145 .add(33, 33, 33))) 146 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 147 .add(serverInstanceComboBox, 0, 195, Short.MAX_VALUE) 148 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE) 149 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE) 150 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE) 151 .add(org.jdesktop.layout.GroupLayout.TRAILING, archiveFileField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)) 152 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 153 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 154 .add(projectLocationButton) 155 .add(archiveLocationButton) 156 .add(manageServersButton)) 157 .addContainerGap()) 158 ); 159 layout.setVerticalGroup( 160 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 161 .add(layout.createSequentialGroup() 162 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 163 .add(projectNameLabel) 164 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 165 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 166 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 167 .add(projectLocationLabel) 168 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 169 .add(projectLocationButton)) 170 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 171 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 172 .add(createdFolderLabel) 173 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 174 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 175 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 176 .add(archiveFileLabel) 177 .add(archiveFileField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 178 .add(archiveLocationButton)) 179 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 180 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 181 .add(targetServerLabel) 182 .add(serverInstanceComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 183 .add(manageServersButton)) 184 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 185 ); 186 } 188 private void manageServersButtonActionPerformed(java.awt.event.ActionEvent evt) { Object serverInstanceWrapper = serverInstanceComboBox.getSelectedItem(); 190 String lastSelectedServerInstanceID = null; 191 if (serverInstanceWrapper != null) { 192 lastSelectedServerInstanceID = JavaEePlatformUiSupport.getServerInstanceID(serverInstanceWrapper); 193 } 194 ServerManager.showCustomizer(lastSelectedServerInstanceID); 195 serverInstanceComboBox.setModel(JavaEePlatformUiSupport.createPlatformComboBoxModel(lastSelectedServerInstanceID)); 196 } 198 private void archiveLocationButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 200 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 201 chooser.setDialogTitle(NbBundle.getMessage(DeployablePanelVisual.class, 202 "DLG_TITLE_ArchiveChooser")); chooser.setFileFilter(new FileFilter () { 204 public boolean accept(File f) { 205 if (f.isDirectory()) { 206 return true; 207 } 208 String path = f.getAbsolutePath(); 209 return acceptableArchive(path); 210 } 211 public String getDescription() { 212 return NbBundle.getMessage(DeployablePanelVisual.class, 213 "ARCHIVE_CHOOSER_DESCRIPTOR"); } 215 }); 216 String path = this.archiveFileField.getText(); 217 if (path.length() > 0) { 218 File f = new File (path); 219 if (f.exists()) { 220 chooser.setSelectedFile(f); 221 } 222 } else if (savedArchiveLoc != null) { 223 chooser.setCurrentDirectory(savedArchiveLoc); 224 } 225 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 226 File projectDir = chooser.getSelectedFile(); 227 archiveFileField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 228 savedArchiveLoc = projectDir.getParentFile(); 229 } 230 panel.fireChangeEvent(); 231 } 233 private void projectLocationButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 235 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 236 chooser.setDialogTitle(NbBundle.getMessage(DeployablePanelVisual.class, 237 "DLG_TITLE_FolderChooser")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 239 String path = this.projectLocationTextField.getText(); 240 if (path.length() > 0) { 241 File f = new File (path); 242 if (f.exists()) { 243 chooser.setSelectedFile(f); 244 } 245 } 246 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 247 File projectDir = chooser.getSelectedFile(); 248 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 249 } 250 panel.fireChangeEvent(); 251 } 253 254 private javax.swing.JTextField archiveFileField; 256 private javax.swing.JLabel archiveFileLabel; 257 private javax.swing.JButton archiveLocationButton; 258 private javax.swing.JLabel createdFolderLabel; 259 private javax.swing.JTextField createdFolderTextField; 260 private javax.swing.JButton manageServersButton; 261 private javax.swing.JButton projectLocationButton; 262 private javax.swing.JLabel projectLocationLabel; 263 private javax.swing.JTextField projectLocationTextField; 264 private javax.swing.JLabel projectNameLabel; 265 private javax.swing.JTextField projectNameTextField; 266 private javax.swing.JComboBox serverInstanceComboBox; 267 private javax.swing.JLabel targetServerLabel; 268 270 public void addNotify() { 271 super.addNotify(); 272 projectNameTextField.requestFocus(); 274 } 275 276 private static final String WP_eM = "WizardPanel_errorMessage"; 278 private static final ResourceBundle bundle = NbBundle.getBundle(DeployablePanelVisual.class); 279 280 boolean valid(WizardDescriptor wizardDescriptor) { 281 if (projectNameTextField.getText().length() == 0) { 282 wizardDescriptor.putProperty(WP_eM, 283 bundle.getString("ERR_valid_folder_name")); return false; } 286 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText()).getAbsoluteFile()); 287 if (!f.isDirectory()) { 288 String message = bundle.getString("ERR_valid_path"); wizardDescriptor.putProperty(WP_eM, message); 290 return false; 291 } 292 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 293 294 File projLoc = destFolder; 295 while (projLoc != null && !projLoc.exists()) { 296 projLoc = projLoc.getParentFile(); 297 } 298 if (projLoc == null || !projLoc.canWrite()) { 299 wizardDescriptor.putProperty(WP_eM, 300 bundle.getString("ERR_cannot_write_folder")); return false; 302 } 303 304 if (FileUtil.toFileObject(projLoc) == null) { 305 String message = bundle.getString("ERR_valid_path"); wizardDescriptor.putProperty(WP_eM, message); 307 return false; 308 } 309 310 File [] kids = destFolder.listFiles(); 311 if (destFolder.exists() && kids != null && kids.length > 0) { 312 wizardDescriptor.putProperty(WP_eM, 314 bundle.getString("ERR_folder_not_empty")); return false; 316 } 317 f = FileUtil.normalizeFile(new File (archiveFileField.getText()).getAbsoluteFile()); 318 if (!f.exists() || !f.isFile() || !f.canRead()) { 319 String message = bundle.getString("ERR_invalid_archive"); wizardDescriptor.putProperty(WP_eM, message); 321 return false; 322 } 323 if (serverInstanceComboBox.getItemCount() == 0) { 324 String message = bundle.getString("ERR_no_possible_target"); wizardDescriptor.putProperty(WP_eM, message); 326 return false; 327 } 328 if (serverInstanceComboBox.getSelectedItem() == null) { 329 String message = bundle.getString("ERR_no_target"); wizardDescriptor.putProperty(WP_eM, message); 331 return false; 332 } 333 334 if (!acceptableArchive(f.getAbsoluteFile().getAbsolutePath(), wizardDescriptor)) { 337 String message = bundle.getString("ERR_unsupported_archive"); wizardDescriptor.putProperty(WP_eM, message); 339 return false; 340 } 341 wizardDescriptor.putProperty(WP_eM, ""); return true; 343 } 344 345 boolean acceptableArchive(String aPath, WizardDescriptor d) { 346 boolean retVal = false; 348 if (aPath.endsWith("war")) { d.putProperty(DeployableWizardIterator.PROJECT_TYPE_PROP, 350 ArchiveProjectProperties.PROJECT_TYPE_VALUE_WAR); 351 retVal = true; 352 } 353 if (aPath.endsWith("car")) { d.putProperty(DeployableWizardIterator.PROJECT_TYPE_PROP, 355 ArchiveProjectProperties.PROJECT_TYPE_VALUE_CAR); 356 retVal = true; 357 } 358 if (aPath.endsWith("rar")) { d.putProperty(DeployableWizardIterator.PROJECT_TYPE_PROP, 360 ArchiveProjectProperties.PROJECT_TYPE_VALUE_RAR); 361 retVal = true; 362 } 363 if (aPath.endsWith("ear")) { d.putProperty(DeployableWizardIterator.PROJECT_TYPE_PROP, 365 ArchiveProjectProperties.PROJECT_TYPE_VALUE_EAR); 366 retVal = true; 367 } 368 if (aPath.endsWith("jar")) { d.putProperty(DeployableWizardIterator.PROJECT_TYPE_PROP, 370 ArchiveProjectProperties.PROJECT_TYPE_VALUE_UNKNOWN); 371 retVal = true; 372 } 373 return retVal; 374 } 375 376 void store(WizardDescriptor d) { 379 String name = projectNameTextField.getText().trim(); 380 String folder = createdFolderTextField.getText().trim(); 381 382 d.putProperty(DeployableWizardIterator.PROJECT_DIR_PROP, new File (folder)); 383 d.putProperty(DeployableWizardIterator.PROJECT_NAME_PROP, name); 384 d.putProperty(DeployableWizardIterator.PROJECT_TARGET_PROP, serverInstanceComboBox.getSelectedItem()); 385 d.putProperty(DeployableWizardIterator.PROJECT_ARCHIVE_PROP, new File (archiveFileField.getText().trim())); 386 } 387 388 void read(WizardDescriptor settings) { 389 File projectLocation = (File ) settings.getProperty(DeployableWizardIterator.PROJECT_DIR_PROP); 390 if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 391 projectLocation = ProjectChooser.getProjectsFolder(); 392 } else { 393 projectLocation = projectLocation.getParentFile(); 394 } 395 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 396 397 String projectName = (String ) settings.getProperty(DeployableWizardIterator.PROJECT_NAME_PROP); 398 if(projectName == null) { 399 int baseCount = 0; 400 String formater = bundle.getString("TXT_DefaultProjectName"); while ((projectName=validFreeProjectName(projectLocation, formater, baseCount))==null) 402 baseCount++; 403 } 404 this.projectNameTextField.setText(projectName); 405 this.projectNameTextField.selectAll(); 406 } 407 408 void validate(WizardDescriptor d) throws WizardValidationException { 409 } 411 412 414 public void changedUpdate(DocumentEvent e) { 415 updateTexts(e); 416 if (this.projectNameTextField.getDocument() == e.getDocument()) { 417 firePropertyChange(DeployableWizardIterator.PROJECT_NAME_PROP,null,this.projectNameTextField.getText()); 418 } 419 } 420 421 public void insertUpdate(DocumentEvent e) { 422 updateTexts(e); 423 if (this.projectNameTextField.getDocument() == e.getDocument()) { 424 firePropertyChange(DeployableWizardIterator.PROJECT_NAME_PROP,null, 425 this.projectNameTextField.getText()); 426 } 427 } 428 429 public void removeUpdate(DocumentEvent e) { 430 updateTexts(e); 431 if (this.projectNameTextField.getDocument() == e.getDocument()) { 432 firePropertyChange(DeployableWizardIterator.PROJECT_NAME_PROP,null, 433 this.projectNameTextField.getText()); 434 } 435 } 436 437 438 private void updateTexts(DocumentEvent e) { 439 440 Document doc = e.getDocument(); 441 442 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 443 445 String projectName = projectNameTextField.getText(); 446 String projectFolder = projectLocationTextField.getText(); 447 448 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 449 } 450 panel.fireChangeEvent(); } 452 453 public void actionPerformed(ActionEvent e) { 454 panel.fireChangeEvent(); 455 } 456 457 private boolean acceptableArchive(String aPath) { 458 boolean retVal = false; 459 if (aPath.endsWith("war")) { retVal = true; 461 } 462 if (aPath.endsWith("car")) { retVal = true; 464 } 465 if (aPath.endsWith("ear")) { retVal = true; 467 } 468 if (aPath.endsWith("jar")) { retVal = true; 470 } 471 if (aPath.endsWith("rar")) { retVal = true; 473 } 474 return retVal; 475 } 476 } 477 | Popular Tags |