KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > samples > JavaEESamplesPanelVisual


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.samples;
21
22 import java.io.File JavaDoc;
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import javax.swing.event.DocumentListener JavaDoc;
27 import javax.swing.text.Document JavaDoc;
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 JavaDoc implements DocumentListener JavaDoc {
39     private boolean withDB = false;
40     
41     private JavaEESamplesWizardPanel panel;
42     
43     /** Creates new form PanelProjectLocationVisual */
44     public JavaEESamplesPanelVisual(JavaEESamplesWizardPanel panel, boolean withDB) {
45         initComponents();
46         this.panel = panel;
47         this.withDB = withDB;
48         // Register listener on the textFields to make the automatic updates
49
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 JavaDoc getProjectName() {
64         return this.projectNameTextField.getText();
65     }
66     
67     /** This method is called from within the constructor to
68      * initialize the form.
69      * WARNING: Do NOT modify this code. The content of this method is
70      * always regenerated by the Form Editor.
71      */

72     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
73
private void initComponents() {
74         projectNameLabel = new javax.swing.JLabel JavaDoc();
75         projectNameTextField = new javax.swing.JTextField JavaDoc();
76         projectLocationLabel = new javax.swing.JLabel JavaDoc();
77         projectLocationTextField = new javax.swing.JTextField JavaDoc();
78         browseButton = new javax.swing.JButton JavaDoc();
79         createdFolderLabel = new javax.swing.JLabel JavaDoc();
80         createdFolderTextField = new javax.swing.JTextField JavaDoc();
81         lblDBName = new javax.swing.JLabel JavaDoc();
82         txtDBName = new javax.swing.JTextField JavaDoc();
83         lblDBLocation = new javax.swing.JLabel JavaDoc();
84         txtDBLocation = new javax.swing.JTextField JavaDoc();
85         infoDBLocation = new javax.swing.JTextArea JavaDoc();
86
87         projectNameLabel.setLabelFor(projectNameTextField);
88         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/samples/Bundle"); // NOI18N
89
org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, bundle.getString("LBL_ProjectName")); // NOI18N
90

91         projectNameTextField.setEditable(false);
92         projectNameTextField.setEnabled(false);
93
94         projectLocationLabel.setLabelFor(projectLocationTextField);
95         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, bundle.getString("LBL_ProjectLocation")); // NOI18N
96

97         org.openide.awt.Mnemonics.setLocalizedText(browseButton, bundle.getString("LBL_Browse")); // NOI18N
98
browseButton.setActionCommand("BROWSE");
99         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
100             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
101                 browseButtonActionPerformed(evt);
102             }
103         });
104
105         createdFolderLabel.setLabelFor(createdFolderTextField);
106         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, bundle.getString("LBL_ProjectFolder")); // NOI18N
107

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")); // NOI18N
113

114         lblDBLocation.setLabelFor(txtDBLocation);
115         org.openide.awt.Mnemonics.setLocalizedText(lblDBLocation, org.openide.util.NbBundle.getMessage(JavaEESamplesPanelVisual.class, "LBL_DBLocation")); // NOI18N
116

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")); // NOI18N
125
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     }// </editor-fold>//GEN-END:initComponents
181

182     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
183
String JavaDoc command = evt.getActionCommand();
184         if ("BROWSE".equals(command)) { //NOI18N
185
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
186             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
187             chooser.setDialogTitle(NbBundle.getMessage(JavaEESamplesPanelVisual.class, "LBL_TITLE"));
188             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
189             String JavaDoc path = this.projectLocationTextField.getText();
190             if (path.length() > 0) {
191                 File JavaDoc f = new File JavaDoc(path);
192                 if (f.exists()) {
193                     chooser.setSelectedFile(f);
194                 }
195             }
196             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
197                 File JavaDoc projectDir = chooser.getSelectedFile();
198                 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
199             }
200             panel.fireChangeEvent();
201         }
202         
203     }//GEN-LAST:event_browseButtonActionPerformed
204

205     
206     // Variables declaration - do not modify//GEN-BEGIN:variables
207
private javax.swing.JButton JavaDoc browseButton;
208     private javax.swing.JLabel JavaDoc createdFolderLabel;
209     private javax.swing.JTextField JavaDoc createdFolderTextField;
210     private javax.swing.JTextArea JavaDoc infoDBLocation;
211     private javax.swing.JLabel JavaDoc lblDBLocation;
212     private javax.swing.JLabel JavaDoc lblDBName;
213     private javax.swing.JLabel JavaDoc projectLocationLabel;
214     private javax.swing.JTextField JavaDoc projectLocationTextField;
215     private javax.swing.JLabel JavaDoc projectNameLabel;
216     private javax.swing.JTextField JavaDoc projectNameTextField;
217     private javax.swing.JTextField JavaDoc txtDBLocation;
218     private javax.swing.JTextField JavaDoc txtDBName;
219     // End of variables declaration//GEN-END:variables
220

221     public void addNotify() {
222         super.addNotify();
223         //same problem as in 31086, initial focus on Cancel button
224
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; // Display name not specified
242
}
243         String JavaDoc projectLocation = projectLocationTextField.getText();
244         File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(projectLocation).getAbsoluteFile());
245         if (!f.isDirectory() || projectLocation.length() == 0) {
246             String JavaDoc message = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_InvalidPath");
247             wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, message);
248             return false;
249         }
250         final File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc(createdFolderTextField.getText()).getAbsoluteFile());
251         
252         File JavaDoc 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 JavaDoc message = NbBundle.getMessage(JavaEESamplesPanelVisual.class, "MSG_InvalidPath");
265             wizardDescriptor.putProperty(WizardProperties.WIZARD_ERROR_MSG, message);
266             return false;
267         }
268         
269         File JavaDoc[] kids = destFolder.listFiles();
270         if (destFolder.exists() && kids != null && kids.length > 0) {
271             // Folder exists and is not empty
272
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 JavaDoc dbName = txtDBName.getText();
285         String JavaDoc 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 JavaDoc name = projectNameTextField.getText().trim();
306         String JavaDoc folder = createdFolderTextField.getText().trim();
307         
308         d.putProperty(WizardProperties.PROJ_DIR, new File JavaDoc(folder));
309         d.putProperty(WizardProperties.NAME, name);
310         
311         if (withDB){
312             String JavaDoc dbName = txtDBName.getText().trim();
313             d.putProperty(WizardProperties.DB_NAME, dbName);
314         }
315     }
316     
317     void read(WizardDescriptor settings) {
318         File JavaDoc projectLocation = (File JavaDoc) 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 JavaDoc projectName = (String JavaDoc) settings.getProperty(WizardProperties.NAME);
327         if(projectName == null) {
328             projectName = "sample"; //NOI18N
329
}
330         this.projectNameTextField.setText(projectName);
331         this.projectNameTextField.selectAll();
332         
333         if (withDB){
334             String JavaDoc 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         // nothing to validate
343
}
344     
345     // Implementation of DocumentListener --------------------------------------
346

347     public void changedUpdate(DocumentEvent JavaDoc e) {
348         update(e);
349     }
350     
351     public void insertUpdate(DocumentEvent JavaDoc e) {
352         update(e);
353     }
354     
355     public void removeUpdate(DocumentEvent JavaDoc e) {
356         update(e);
357     }
358     
359     /** Handles changes in the Project name and project directory, */
360     private void update(DocumentEvent JavaDoc 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 JavaDoc doc = e.getDocument();
371         
372         if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
373             // Change in the project name
374

375             String JavaDoc projectName = projectNameTextField.getText();
376             String JavaDoc projectFolder = projectLocationTextField.getText();
377             
378             //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
379
createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
380             //}
381

382         }
383         else if (doc == txtDBName.getDocument()){
384             String JavaDoc dbName = txtDBName.getText();
385             firePropertyChange(WizardProperties.DB_NAME, null, dbName);
386             updateDBPath(dbName);
387         }
388         
389         panel.fireChangeEvent(); // Notify that the panel changed
390
}
391     
392     private void updateDBPath(String JavaDoc dbName){
393         String JavaDoc dbPath = new File JavaDoc(DerbyDatabases.getSystemHome(), dbName).getPath();
394         txtDBLocation.setText(dbPath);
395     }
396
397     private boolean isJavaEECapableServerRegistered() {
398         for (String JavaDoc 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