KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > wizards > CheckoutWizard


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.versioning.system.cvss.ui.wizards;
21
22 import org.openide.*;
23 import org.openide.filesystems.FileUtil;
24 import org.openide.util.NbBundle;
25 import org.openide.util.HelpCtx;
26 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
27 import org.netbeans.modules.versioning.system.cvss.ui.selectors.ModuleSelector;
28 import org.netbeans.modules.versioning.system.cvss.ui.selectors.BranchSelector;
29 import org.netbeans.lib.cvsclient.CVSRoot;
30
31 import javax.swing.event.ChangeListener JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.DocumentListener JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.*;
36 import javax.swing.filechooser.FileFilter JavaDoc;
37 import java.util.*;
38 import java.util.List JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.awt.event.ActionEvent JavaDoc;
41 import java.awt.event.FocusListener JavaDoc;
42 import java.awt.event.FocusEvent JavaDoc;
43 import java.io.File JavaDoc;
44 import java.text.MessageFormat JavaDoc;
45
46 import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
47 import org.netbeans.modules.versioning.util.Utils;
48 import org.netbeans.spi.project.ui.support.ProjectChooser;
49
50 /**
51  * Checkout wizard controller with input validation.
52  *
53  * @author Petr Kuzel
54  */

55 public final class CheckoutWizard implements ChangeListener JavaDoc{
56
57     private static final String JavaDoc RECENT_DIRECTORY = "checkout.recentDirectory";
58     private static final String JavaDoc CHECKOUT_RECENT_MODULE = "checkout.recentModule";
59     private static final String JavaDoc CHECKOUT_RECENT_TAG = "checkout.recentTag";
60
61     private WizardDescriptor wizard;
62
63     private String JavaDoc errorMessage;
64
65     private WizardDescriptor.Iterator wizardIterator;
66
67     private ModulePanel modulePanel;
68
69     private RepositoryStep repositoryStep;
70
71     // output data
72

73     private String JavaDoc initialCvsRoot;
74
75     private String JavaDoc initialModule;
76
77     /** Creates a new instance of CheckoutWizard */
78     public CheckoutWizard() {
79     }
80
81     public CheckoutWizard(String JavaDoc cvsRoot, String JavaDoc module) {
82         initialCvsRoot = cvsRoot;
83         initialModule = module;
84     }
85
86     public boolean show() {
87         wizardIterator = panelIterator();
88         wizard = new WizardDescriptor(wizardIterator);
89         wizard.putProperty("WizardPanel_contentData", // NOI18N
90
new String JavaDoc[] {
91                     NbBundle.getMessage(CheckoutWizard.class, "BK0006"),
92                     NbBundle.getMessage(CheckoutWizard.class, "BK2009")
93                 }
94         );
95         wizard.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
96
wizard.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
97
wizard.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
98
wizard.setTitleFormat(new MessageFormat JavaDoc("{0}")); // NOI18N
99
wizard.setTitle(NbBundle.getMessage(CheckoutWizard.class, "BK0007"));
100         Object JavaDoc result = DialogDisplayer.getDefault().notify(wizard);
101         boolean finished = NotifyDescriptor.OK_OPTION.equals(result);
102         if (finished) {
103             onFinished();
104         }
105         return finished;
106     }
107
108     /** Called on sucessfull finish. */
109     private void onFinished() {
110         String JavaDoc checkout = modulePanel.workTextField.getText();
111         CvsModuleConfig.getDefault().getPreferences().put(CHECKOUT_RECENT_MODULE, modulePanel.moduleTextField.getText());
112         CvsModuleConfig.getDefault().getPreferences().put(CHECKOUT_RECENT_TAG, modulePanel.tagTextField.getText());
113         Utils.insert(CvsModuleConfig.getDefault().getPreferences(), RECENT_DIRECTORY, checkout, 8);
114     }
115
116     /** Tells invalidation reason never <code>null</code>, */
117     String JavaDoc getErrorMessage() {
118         String JavaDoc value;
119         if (wizard != null) {
120             value = (String JavaDoc) wizard.getProperty("WizardPanel_errorMessage"); // NOI18N
121
} else {
122             value = errorMessage;
123         }
124         if (value == null) value = ""; // NOI18N
125
return value;
126     }
127
128     private void setErrorMessage(String JavaDoc msg) {
129         errorMessage = msg;
130         if (wizard != null) {
131             wizard.putProperty("WizardPanel_errorMessage", msg); // NOI18N
132
}
133     }
134
135     public void stateChanged(ChangeEvent JavaDoc e) {
136         AbstractStep step = (AbstractStep) wizardIterator.current();
137         setErrorMessage(step.getErrorMessage());
138     }
139
140     private WizardDescriptor.Iterator panelIterator() {
141         repositoryStep = new RepositoryStep(initialCvsRoot);
142         repositoryStep.addChangeListener(this);
143         WizardDescriptor.Panel modulePanel = new ModuleStep();
144         modulePanel.addChangeListener(this);
145
146         final WizardDescriptor.Panel[] panels = new WizardDescriptor.Panel[2];
147         panels[0] = repositoryStep;
148         panels[1] = modulePanel;
149
150         WizardDescriptor.ArrayIterator ret = new WizardDescriptor.ArrayIterator(panels) {
151             public WizardDescriptor.Panel current() {
152                 WizardDescriptor.Panel ret = super.current();
153                 for (int i = 0; i<panels.length; i++) {
154                     if (panels[i] == ret) {
155                         wizard.putProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
156
}
157                 }
158                 return ret;
159             }
160         };
161         return ret;
162     }
163
164     public String JavaDoc getModules() {
165         return modulePanel.moduleTextField.getText().trim();
166     }
167
168     public String JavaDoc getTag() {
169         return modulePanel.tagTextField.getText().trim();
170     }
171
172     /**
173      * @return normalized file path
174      */

175     public String JavaDoc getWorkingDir() {
176         String JavaDoc path = modulePanel.workTextField.getText();
177         return FileUtil.normalizeFile(new File JavaDoc(path)).getAbsolutePath();
178     }
179
180     /** Password scrambled by standard scramler. */
181     public String JavaDoc getScrambledPassword() {
182         return repositoryStep.getScrambledPassword();
183     }
184
185     public String JavaDoc getCvsRoot() {
186         return repositoryStep.getCvsRoot();
187     }
188
189     private class ModuleStep extends AbstractStep implements DocumentListener JavaDoc, FocusListener JavaDoc, ActionListener JavaDoc {
190
191         protected JComponent createComponent() {
192             modulePanel = new ModulePanel();
193
194             if (initialModule != null) {
195                 modulePanel.moduleTextField.setText(initialModule);
196             }
197
198             modulePanel.moduleTextField.setText(CvsModuleConfig.getDefault().getPreferences().get(CHECKOUT_RECENT_MODULE, ""));
199             modulePanel.tagTextField.setText(CvsModuleConfig.getDefault().getPreferences().get(CHECKOUT_RECENT_TAG, ""));
200
201             String JavaDoc path = defaultWorkingDirectory().getPath();
202             modulePanel.workTextField.setText(path);
203             modulePanel.workTextField.getDocument().addDocumentListener(this);
204             modulePanel.workTextField.addFocusListener(this);
205             modulePanel.workTextField.addActionListener(this);
206             validateUserInput(true);
207
208             modulePanel.moduleButton.addActionListener(this);
209             modulePanel.tagButton.addActionListener(this);
210             modulePanel.workButton.addActionListener(this);
211             return modulePanel;
212         }
213
214         public HelpCtx getHelp() {
215             return new HelpCtx(ModuleStep.class);
216         }
217
218         protected void validateBeforeNext() {
219             if (validateUserInput(true)) {
220                 String JavaDoc text = modulePanel.workTextField.getText();
221                 File JavaDoc file = new File JavaDoc(text);
222                 if (file.exists() == false) {
223                     boolean done = file.mkdirs();
224                     if (done == false) {
225                         invalid(org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2013") + file.getPath());
226                     }
227                 }
228             }
229         }
230
231         private boolean validateUserInput(boolean full) {
232             String JavaDoc text = modulePanel.workTextField.getText();
233             if (text == null || text.length() == 0) {
234                 invalid(org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2014"));
235                 return false;
236             }
237
238             String JavaDoc errorMessage = null;
239             if (full) {
240                 File JavaDoc file = new File JavaDoc(text);
241                 if (file.exists() == false) {
242                     // it's automaticaly create later on, check for permisions here
243
File JavaDoc parent = file.getParentFile();
244                     while (parent != null) {
245                         if (parent.exists()) {
246                             if (parent.canWrite() == false) {
247                                 errorMessage = org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2016") + parent.getPath();
248                             }
249                             break;
250                         }
251
252                         parent = parent.getParentFile();
253                     }
254                 } else {
255                     if (file.isFile()) {
256                         errorMessage = org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2017");
257                     }
258                 }
259             }
260
261             if (errorMessage == null) {
262                 valid();
263             } else {
264                 invalid(errorMessage);
265             }
266
267             return errorMessage == null;
268         }
269
270         public void changedUpdate(DocumentEvent JavaDoc e) {
271         }
272
273         public void insertUpdate(DocumentEvent JavaDoc e) {
274             validateUserInput(false);
275         }
276
277         public void removeUpdate(DocumentEvent JavaDoc e) {
278             validateUserInput(false);
279         }
280
281         public void focusGained(FocusEvent JavaDoc e) {
282         }
283
284         public void focusLost(FocusEvent JavaDoc e) {
285             validateUserInput(true);
286         }
287
288         public void actionPerformed(ActionEvent JavaDoc e) {
289             if (e.getSource() == modulePanel.moduleButton) {
290                 ModuleSelector selector = new ModuleSelector();
291                 String JavaDoc rootString = repositoryStep.getCvsRoot();
292                 CVSRoot root = CVSRoot.parse(rootString);
293                 Set modules = selector.selectModules(root);
294                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
295                 String JavaDoc separator = ""; // NOI18N
296
Iterator it = modules.iterator();
297                 while (it.hasNext()) {
298                     String JavaDoc module = (String JavaDoc) it.next();
299                     buf.append(separator).append(module);
300                     separator = ","; // NOI18N
301
}
302                 modulePanel.moduleTextField.setText(buf.toString());
303             } else if (e.getSource() == modulePanel.tagButton) {
304                 BranchSelector selector = new BranchSelector();
305                 String JavaDoc rootString = repositoryStep.getCvsRoot();
306                 CVSRoot root = CVSRoot.parse(rootString);
307                 String JavaDoc s = modulePanel.moduleTextField.getText();
308                 if (s.trim().length() == 0) {
309                     s = "."; // NOI18N
310
}
311                 String JavaDoc module = new StringTokenizer(s, ", ").nextToken(); // NOI18N
312
String JavaDoc tag = selector.selectTag(root, module);
313                 if (tag != null) {
314                     modulePanel.tagTextField.setText(tag);
315                 }
316             } else if (e.getSource() == modulePanel.workButton) {
317
318                 File JavaDoc defaultDir = defaultWorkingDirectory();
319                 JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(CheckoutWizard.class, "ACSD_BrowseFolder"), defaultDir);
320                 fileChooser.setDialogTitle(NbBundle.getMessage(CheckoutWizard.class, "BK0010"));
321                 fileChooser.setMultiSelectionEnabled(false);
322                 FileFilter JavaDoc[] old = fileChooser.getChoosableFileFilters();
323                 for (int i = 0; i < old.length; i++) {
324                     FileFilter JavaDoc fileFilter = old[i];
325                     fileChooser.removeChoosableFileFilter(fileFilter);
326
327                 }
328                 fileChooser.addChoosableFileFilter(new FileFilter JavaDoc() {
329                     public boolean accept(File JavaDoc f) {
330                         return f.isDirectory();
331                     }
332                     public String JavaDoc getDescription() {
333                         return NbBundle.getMessage(CheckoutWizard.class, "BK0008");
334                     }
335                 });
336                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
337                 fileChooser.showDialog(modulePanel, NbBundle.getMessage(CheckoutWizard.class, "BK0009"));
338                 File JavaDoc f = fileChooser.getSelectedFile();
339                 if (f != null) {
340                     modulePanel.workTextField.setText(f.getAbsolutePath());
341                 }
342             } else {
343                 validateUserInput(true);
344             }
345         }
346
347         /**
348          * Returns file to be initaly used.
349          * <ul>
350          * <li>first is takes text in workTextField
351          * <li>then recent project folder
352          * <li>then recent checkout folder
353          * <li>finally <tt>user.home</tt>
354          * <ul>
355          */

356         private File JavaDoc defaultWorkingDirectory() {
357             File JavaDoc defaultDir = null;
358             String JavaDoc current = modulePanel.workTextField.getText();
359             if (current != null && !(current.trim().equals(""))) { // NOI18N
360
File JavaDoc currentFile = new File JavaDoc(current);
361                 while (currentFile != null && currentFile.exists() == false) {
362                     currentFile = currentFile.getParentFile();
363                 }
364                 if (currentFile != null) {
365                     if (currentFile.isFile()) {
366                         defaultDir = currentFile.getParentFile();
367                     } else {
368                         defaultDir = currentFile;
369                     }
370                 }
371             }
372
373             if (defaultDir == null) {
374                 List JavaDoc recent = Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), RECENT_DIRECTORY);
375                 Iterator it = recent.iterator();
376
377                 while (it.hasNext()) {
378                     String JavaDoc path = (String JavaDoc) it.next();
379                     File JavaDoc file = new File JavaDoc(path);
380                     File JavaDoc parent = file.getParentFile();
381                     if (parent != null && parent.exists() && parent.isDirectory()) {
382                         defaultDir = file;
383                         break;
384                     }
385                 }
386             }
387
388             if (defaultDir == null) {
389                 File JavaDoc projectFolder = ProjectChooser.getProjectsFolder();
390                 if (projectFolder.exists() && projectFolder.isDirectory()) {
391                     defaultDir = projectFolder;
392                 }
393             }
394             
395             if (defaultDir == null) {
396                 defaultDir = new File JavaDoc(System.getProperty("user.home")); // NOI18N
397
}
398
399             return defaultDir;
400         }
401     }
402
403 }
404
Popular Tags