1 19 20 package org.netbeans.modules.j2ee.samples; 21 22 import java.io.File ; 23 import javax.swing.JFileChooser ; 24 import javax.swing.JPanel ; 25 import javax.swing.event.DocumentEvent ; 26 import javax.swing.event.DocumentListener ; 27 import javax.swing.text.Document ; 28 import org.netbeans.modules.derby.api.DerbyDatabases; 29 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 30 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 31 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 32 import org.netbeans.spi.project.ui.support.ProjectChooser; 33 import org.openide.WizardDescriptor; 34 import org.openide.WizardValidationException; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.NbBundle; 37 38 public class JavaEESamplesPanelVisual extends JPanel implements DocumentListener { 39 private boolean withDB = false; 40 41 private JavaEESamplesWizardPanel panel; 42 43 44 public JavaEESamplesPanelVisual(JavaEESamplesWizardPanel panel, boolean withDB) { 45 initComponents(); 46 this.panel = panel; 47 this.withDB = withDB; 48 projectNameTextField.getDocument().addDocumentListener(this); 50 projectLocationTextField.getDocument().addDocumentListener(this); 51 txtDBName.getDocument().addDocumentListener(this); 52 53 if (!withDB){ 54 txtDBName.setVisible(false); 55 lblDBName.setVisible(false); 56 txtDBLocation.setVisible(false); 57 lblDBLocation.setVisible(false); 58 infoDBLocation.setVisible(false); 59 } 60 } 61 62 63 public String getProjectName() { 64 return this.projectNameTextField.getText(); 65 } 66 67 72 private void initComponents() { 74 projectNameLabel = new javax.swing.JLabel (); 75 projectNameTextField = new javax.swing.JTextField (); 76 projectLocationLabel = new javax.swing.JLabel (); 77 projectLocationTextField = new javax.swing.JTextField (); 78 browseButton = new javax.swing.JButton (); 79 createdFolderLabel = new javax.swing.JLabel (); 80 createdFolderTextField = new javax.swing.JTextField (); 81 lblDBName = new javax.swing.JLabel (); 82 txtDBName = new javax.swing.JTextField (); 83 lblDBLocation = new javax.swing.JLabel (); 84 txtDBLocation = new javax.swing.JTextField (); 85 infoDBLocation = new javax.swing.JTextArea (); 86 87 projectNameLabel.setLabelFor(projectNameTextField); 88 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/samples/Bundle"); org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, bundle.getString("LBL_ProjectName")); 91 projectNameTextField.setEditable(false); 92 projectNameTextField.setEnabled(false); 93 94 projectLocationLabel.setLabelFor(projectLocationTextField); 95 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, bundle.getString("LBL_ProjectLocation")); 97 org.openide.awt.Mnemonics.setLocalizedText(browseButton, bundle.getString("LBL_Browse")); browseButton.setActionCommand("BROWSE"); 99 browseButton.addActionListener(new java.awt.event.ActionListener () { 100 public void actionPerformed(java.awt.event.ActionEvent evt) { 101 browseButtonActionPerformed(evt); 102 } 103 }); 104 105 createdFolderLabel.setLabelFor(createdFolderTextField); 106 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, bundle.getString("LBL_ProjectFolder")); 108 createdFolderTextField.setEditable(false); 109 createdFolderTextField.setEnabled(false); 110 111 lblDBName.setLabelFor(txtDBName); 112 org.openide.awt.Mnemonics.setLocalizedText(lblDBName, org.openide.util.NbBundle.getMessage(JavaEESamplesPanelVisual.class, "LBL_DBName")); 114 lblDBLocation.setLabelFor(txtDBLocation); 115 org.openide.awt.Mnemonics.setLocalizedText(lblDBLocation, org.openide.util.NbBundle.getMessage(JavaEESamplesPanelVisual.class, "LBL_DBLocation")); 117 txtDBLocation.setEditable(false); 118 txtDBLocation.setEnabled(false); 119 120 infoDBLocation.setColumns(20); 121 infoDBLocation.setEditable(false); 122 infoDBLocation.setLineWrap(true); 123 infoDBLocation.setRows(5); 124 infoDBLocation.setText(org.openide.util.NbBundle.getMessage(JavaEESamplesPanelVisual.class, "CreateDatabasePanelVisual.infoTextArea.text")); infoDBLocation.setWrapStyleWord(true); 126 infoDBLocation.setFocusable(false); 127 128 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 129 this.setLayout(layout); 130 layout.setHorizontalGroup( 131 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 132 .add(layout.createSequentialGroup() 133 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 134 .add(org.jdesktop.layout.GroupLayout.LEADING, infoDBLocation) 135 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 136 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 137 .add(projectNameLabel) 138 .add(projectLocationLabel) 139 .add(createdFolderLabel) 140 .add(lblDBName) 141 .add(lblDBLocation)) 142 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 143 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 144 .add(txtDBName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) 145 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) 146 .add(txtDBLocation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) 147 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) 148 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)))) 149 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 150 .add(browseButton)) 151 ); 152 layout.setVerticalGroup( 153 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 154 .add(layout.createSequentialGroup() 155 .addContainerGap() 156 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 157 .add(projectNameLabel) 158 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 159 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 160 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 161 .add(projectLocationLabel) 162 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 163 .add(browseButton)) 164 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 165 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 166 .add(createdFolderLabel) 167 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 168 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 169 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 170 .add(lblDBName) 171 .add(txtDBName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 172 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 173 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 174 .add(lblDBLocation) 175 .add(txtDBLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 176 .add(24, 24, 24) 177 .add(infoDBLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 178 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 179 ); 180 } 182 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 184 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 186 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 187 chooser.setDialogTitle(NbBundle.getMessage(JavaEESamplesPanelVisual.class, "LBL_TITLE")); 188 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 189 String path = this.projectLocationTextField.getText(); 190 if (path.length() > 0) { 191 File f = new File (path); 192 if (f.exists()) { 193 chooser.setSelectedFile(f); 194 } 195 } 196 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 197 File projectDir = chooser.getSelectedFile(); 198 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 199 } 200 panel.fireChangeEvent(); 201 } 202 203 } 205 206 private javax.swing.JButton browseButton; 208 private javax.swing.JLabel createdFolderLabel; 209 private javax.swing.JTextField createdFolderTextField; 210 private javax.swing.JTextArea infoDBLocation; 211 private javax.swing.JLabel lblDBLocation; 212 private javax.swing.JLabel lblDBName; 213 private javax.swing.JLabel projectLocationLabel; 214 private javax.swing.JTextField projectLocationTextField; 215 private javax.swing.JLabel projectNameLabel; 216 private javax.swing.JTextField projectNameTextField; 217 private javax.swing.JTextField txtDBLocation; 218 private javax.swing.JTextField txtDBName; 219 221 public void addNotify() { 222 super.addNotify(); 223 projectNameTextField.requestFocus(); 225 } 226 227 boolean valid(WizardDescriptor wizardDescriptor) { 228 229 if (!isJavaEECapableServerRegistered()){ 230 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, 231 NbBundle.getMessage(JavaEESamplesPanelVisual.class, "ERR_MissingJavaEE5AppServer")); 232 233 return false; 234 } 235 236 if (projectNameTextField.getText().length() == 0) { 237 238 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, 239 NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_InvalidProjectName")); 240 241 return false; } 243 String projectLocation = projectLocationTextField.getText(); 244 File f = FileUtil.normalizeFile(new File (projectLocation).getAbsoluteFile()); 245 if (!f.isDirectory() || projectLocation.length() == 0) { 246 String message = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_InvalidPath"); 247 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, message); 248 return false; 249 } 250 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 251 252 File projLoc = destFolder; 253 while (projLoc != null && !projLoc.exists()) { 254 projLoc = projLoc.getParentFile(); 255 } 256 if (projLoc == null || !projLoc.canWrite()) { 257 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, 258 NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_FolderCannotBeCreated")); 259 260 return false; 261 } 262 263 if (FileUtil.toFileObject(projLoc) == null) { 264 String message = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_InvalidPath"); 265 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, message); 266 return false; 267 } 268 269 File [] kids = destFolder.listFiles(); 270 if (destFolder.exists() && kids != null && kids.length > 0) { 271 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, 273 NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_FolderAlreadyExists")); 274 275 return false; 276 } 277 278 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, null); 279 280 return withDB ? validDBData(wizardDescriptor) : true; 281 } 282 283 boolean validDBData(WizardDescriptor wizardDescriptor){ 284 String dbName = txtDBName.getText(); 285 String errorMsg = null; 286 287 if (DerbyDatabases.isDerbyRegistered()){ 288 int illegalChar = DerbyDatabases.getFirstIllegalCharacter(dbName); 289 290 if (illegalChar > -1){ 291 errorMsg = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "ERR_DatabaseNameIllegalChar", (char)illegalChar); 292 } else if (DerbyDatabases.databaseExists(dbName)){ 293 errorMsg = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "ERR_DatabaseDirectoryExists", dbName); 294 } else if (dbName.length() == 0){ 295 errorMsg = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "ERR_DatabaseNameEmpty"); 296 } 297 } else{ 298 errorMsg = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "ERR_JavaDBNotRegistered"); 299 } 300 wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, errorMsg); 301 return errorMsg == null; 302 } 303 304 void store(WizardDescriptor d) { 305 String name = projectNameTextField.getText().trim(); 306 String folder = createdFolderTextField.getText().trim(); 307 308 d.putProperty(WizardProperties.PROJ_DIR, new File (folder)); 309 d.putProperty(WizardProperties.NAME, name); 310 311 if (withDB){ 312 String dbName = txtDBName.getText().trim(); 313 d.putProperty(WizardProperties.DB_NAME, dbName); 314 } 315 } 316 317 void read(WizardDescriptor settings) { 318 File projectLocation = (File ) settings.getProperty(WizardProperties.PROJ_DIR); 319 if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 320 projectLocation = ProjectChooser.getProjectsFolder(); 321 } else { 322 projectLocation = projectLocation.getParentFile(); 323 } 324 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 325 326 String projectName = (String ) settings.getProperty(WizardProperties.NAME); 327 if(projectName == null) { 328 projectName = "sample"; } 330 this.projectNameTextField.setText(projectName); 331 this.projectNameTextField.selectAll(); 332 333 if (withDB){ 334 String dbName = DerbyDatabases.getFirstFreeDatabaseName(projectName); 335 txtDBName.setText(dbName); 336 txtDBName.selectAll(); 337 updateDBPath(dbName); 338 } 339 } 340 341 void validate(WizardDescriptor d) throws WizardValidationException { 342 } 344 345 347 public void changedUpdate(DocumentEvent e) { 348 update(e); 349 } 350 351 public void insertUpdate(DocumentEvent e) { 352 update(e); 353 } 354 355 public void removeUpdate(DocumentEvent e) { 356 update(e); 357 } 358 359 360 private void update(DocumentEvent e) { 361 362 if (projectNameTextField.getDocument() == e.getDocument()) { 363 firePropertyChange(WizardProperties.NAME, null, projectNameTextField.getText()); 364 } 365 366 if (projectLocationTextField.getDocument() == e.getDocument()){ 367 firePropertyChange(WizardProperties.PROJ_DIR, null, projectLocationTextField.getText()); 368 } 369 370 Document doc = e.getDocument(); 371 372 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 373 375 String projectName = projectNameTextField.getText(); 376 String projectFolder = projectLocationTextField.getText(); 377 378 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 380 382 } 383 else if (doc == txtDBName.getDocument()){ 384 String dbName = txtDBName.getText(); 385 firePropertyChange(WizardProperties.DB_NAME, null, dbName); 386 updateDBPath(dbName); 387 } 388 389 panel.fireChangeEvent(); } 391 392 private void updateDBPath(String dbName){ 393 String dbPath = new File (DerbyDatabases.getSystemHome(), dbName).getPath(); 394 txtDBLocation.setText(dbPath); 395 } 396 397 private boolean isJavaEECapableServerRegistered() { 398 for (String serverInstanceID : Deployment.getDefault().getServerInstanceIDs()){ 399 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceID); 400 401 if (j2eePlatform.getSupportedSpecVersions().contains(J2eeModule.JAVA_EE_5)){ 402 return true; 403 } 404 } 405 406 return false; 407 } 408 } 409 | Popular Tags |