View Javadoc

1   /**
2    * Copyright 2008 WebPhotos
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.webphotos.netbeans.project.empty;
17  
18  import java.io.File;
19  import javax.swing.JFileChooser;
20  import javax.swing.JPanel;
21  import javax.swing.event.DocumentEvent;
22  import javax.swing.event.DocumentListener;
23  import javax.swing.text.Document;
24  import org.netbeans.spi.project.ui.support.ProjectChooser;
25  import org.openide.WizardDescriptor;
26  import org.openide.WizardValidationException;
27  import org.openide.filesystems.FileUtil;
28  
29  public class EmptyWebPhotosProjectPanelVisual extends JPanel {
30  
31      public static final String PROP_PROJECT_NAME = "projectName";
32      private EmptyWebPhotosProjectWizardPanel panel;
33  
34      public EmptyWebPhotosProjectPanelVisual(EmptyWebPhotosProjectWizardPanel panel) {
35          initComponents();
36          this.panel = panel;
37          // Register listener on the textFields to make the automatic updates
38          EmptyWebPhotosProjectPanelVisualDocumentListener documentListener = new EmptyWebPhotosProjectPanelVisualDocumentListener();
39          projectNameTextField.getDocument().addDocumentListener(documentListener);
40          projectLocationTextField.getDocument().addDocumentListener(documentListener);
41      }
42  
43      public String getProjectName() {
44          return this.projectNameTextField.getText();
45      }
46  
47      /**
48       * This method is called from within the constructor to initialize the form.
49       * WARNING: Do NOT modify this code. The content of this method is always
50       * regenerated by the Form Editor.
51       */
52      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
53      private void initComponents() {
54  
55          projectNameLabel = new javax.swing.JLabel();
56          projectNameTextField = new javax.swing.JTextField();
57          projectLocationLabel = new javax.swing.JLabel();
58          projectLocationTextField = new javax.swing.JTextField();
59          browseButton = new javax.swing.JButton();
60          createdFolderLabel = new javax.swing.JLabel();
61          createdFolderTextField = new javax.swing.JTextField();
62  
63          projectNameLabel.setLabelFor(projectNameTextField);
64          org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:");
65  
66          projectLocationLabel.setLabelFor(projectLocationTextField);
67          org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:");
68  
69          org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse...");
70          browseButton.setActionCommand("BROWSE");
71          browseButton.addActionListener(new java.awt.event.ActionListener() {
72              public void actionPerformed(java.awt.event.ActionEvent evt) {
73                  browseButtonActionPerformed(evt);
74              }
75          });
76  
77          createdFolderLabel.setLabelFor(createdFolderTextField);
78          org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:");
79  
80          createdFolderTextField.setEditable(false);
81  
82          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
83          this.setLayout(layout);
84          layout.setHorizontalGroup(
85              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
86              .addGroup(layout.createSequentialGroup()
87                  .addContainerGap()
88                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
89                      .addComponent(projectNameLabel)
90                      .addComponent(projectLocationLabel)
91                      .addComponent(createdFolderLabel))
92                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
93                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
94                      .addComponent(projectNameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
95                      .addComponent(projectLocationTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
96                      .addComponent(createdFolderTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
97                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
98                  .addComponent(browseButton)
99                  .addContainerGap())
100         );
101         layout.setVerticalGroup(
102             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
103             .addGroup(layout.createSequentialGroup()
104                 .addContainerGap()
105                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
106                     .addComponent(projectNameLabel)
107                     .addComponent(projectNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
108                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
109                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
110                     .addComponent(projectLocationLabel)
111                     .addComponent(projectLocationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
112                     .addComponent(browseButton))
113                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
114                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
115                     .addComponent(createdFolderLabel)
116                     .addComponent(createdFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
117                 .addContainerGap(213, Short.MAX_VALUE))
118         );
119     }// </editor-fold>//GEN-END:initComponents
120 
121     private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
122         String command = evt.getActionCommand();
123         if ("BROWSE".equals(command)) {
124             JFileChooser chooser = new JFileChooser();
125             chooser.setDialogTitle(java.util.ResourceBundle.getBundle("net/sf/webphotos/netbeans/project/empty/Bundle").getString("LBL_CreateProjectStep_DialogTitle"));
126             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
127             String path = this.projectLocationTextField.getText();
128             if (path.length() > 0) {
129                 File f = new File(path);
130                 if (f.exists()) {
131                     chooser.setSelectedFile(f);
132                 }
133             }
134             if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
135                 File projectDir = chooser.getSelectedFile();
136                 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
137             }
138             panel.fireChangeEvent();
139         }
140 
141     }//GEN-LAST:event_browseButtonActionPerformed
142     // Variables declaration - do not modify//GEN-BEGIN:variables
143     private javax.swing.JButton browseButton;
144     private javax.swing.JLabel createdFolderLabel;
145     private javax.swing.JTextField createdFolderTextField;
146     private javax.swing.JLabel projectLocationLabel;
147     private javax.swing.JTextField projectLocationTextField;
148     private javax.swing.JLabel projectNameLabel;
149     private javax.swing.JTextField projectNameTextField;
150     // End of variables declaration//GEN-END:variables
151 
152     @Override
153     public void addNotify() {
154         super.addNotify();
155         //same problem as in 31086, initial focus on Cancel button
156         projectNameTextField.requestFocus();
157     }
158 
159     boolean valid(WizardDescriptor wizardDescriptor) {
160 
161         if (projectNameTextField.getText().length() == 0) {
162             // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
163             wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE,
164                     "Project Name is not a valid folder name.");
165             return false; // Display name not specified
166         }
167         File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
168         if (!f.isDirectory()) {
169             String message = "Project Folder is not a valid path.";
170             wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, message);
171             return false;
172         }
173         final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile());
174 
175         File projLoc = destFolder;
176         while (projLoc != null && !projLoc.exists()) {
177             projLoc = projLoc.getParentFile();
178         }
179         if (projLoc == null || !projLoc.canWrite()) {
180             wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE,
181                     "Project Folder cannot be created.");
182             return false;
183         }
184 
185         if (FileUtil.toFileObject(projLoc) == null) {
186             String message = "Project Folder is not a valid path.";
187             wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, message);
188             return false;
189         }
190 
191         File[] kids = destFolder.listFiles();
192         if (destFolder.exists() && kids != null && kids.length > 0) {
193             // Folder exists and is not empty
194             wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE,
195                     "Project Folder already exists and is not empty.");
196             return false;
197         }
198         wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, "");
199         return true;
200     }
201 
202     void store(WizardDescriptor d) {
203         String name = projectNameTextField.getText().trim();
204         String folder = createdFolderTextField.getText().trim();
205 
206         d.putProperty("projdir", new File(folder));
207         d.putProperty("name", name);
208     }
209 
210     void read(WizardDescriptor settings) {
211         File projectLocation = (File) settings.getProperty("projdir");
212         if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
213             projectLocation = ProjectChooser.getProjectsFolder();
214         } else {
215             projectLocation = projectLocation.getParentFile();
216         }
217         this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
218 
219         String projectName = (String) settings.getProperty("name");
220         if (projectName == null) {
221             projectName = "EmptyWebPhotosProject";
222         }
223         this.projectNameTextField.setText(projectName);
224         this.projectNameTextField.selectAll();
225     }
226 
227     void validate(WizardDescriptor d) throws WizardValidationException {
228         // nothing to validate
229     }
230 
231     private class EmptyWebPhotosProjectPanelVisualDocumentListener implements DocumentListener {
232 
233         // Implementation of DocumentListener --------------------------------------
234         @Override
235         public void changedUpdate(DocumentEvent e) {
236             updateTexts(e);
237             if (projectNameTextField.getDocument() == e.getDocument()) {
238                 firePropertyChange(PROP_PROJECT_NAME, null, projectNameTextField.getText());
239             }
240         }
241 
242         @Override
243         public void insertUpdate(DocumentEvent e) {
244             updateTexts(e);
245             if (projectNameTextField.getDocument() == e.getDocument()) {
246                 firePropertyChange(PROP_PROJECT_NAME, null, projectNameTextField.getText());
247             }
248         }
249 
250         @Override
251         public void removeUpdate(DocumentEvent e) {
252             updateTexts(e);
253             if (projectNameTextField.getDocument() == e.getDocument()) {
254                 firePropertyChange(PROP_PROJECT_NAME, null, projectNameTextField.getText());
255             }
256         }
257 
258         /**
259          * Handles changes in the Project name and project directory,
260          */
261         private void updateTexts(DocumentEvent e) {
262 
263             Document doc = e.getDocument();
264 
265             if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
266                 // Change in the project name
267 
268                 String projectName = projectNameTextField.getText();
269                 String projectFolder = projectLocationTextField.getText();
270 
271                 //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
272                 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
273                 //}
274 
275             }
276             panel.fireChangeEvent(); // Notify that the panel changed
277         }
278     }
279 }