KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > wizards > checkoutstep > CheckoutStep


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.subversion.ui.wizards.checkoutstep;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JFileChooser JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31 import javax.swing.event.DocumentListener JavaDoc;
32 import javax.swing.filechooser.FileFilter JavaDoc;
33 import org.netbeans.modules.subversion.RepositoryFile;
34 import org.netbeans.modules.subversion.SvnModuleConfig;
35 import org.netbeans.modules.subversion.ui.browser.Browser;
36 import org.netbeans.modules.subversion.ui.wizards.AbstractStep;
37 import org.netbeans.modules.subversion.ui.wizards.CheckoutWizard;
38 import org.netbeans.modules.subversion.ui.browser.RepositoryPaths;
39 import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
40 import org.netbeans.spi.project.ui.support.ProjectChooser;
41 import org.openide.ErrorManager;
42 import org.openide.util.HelpCtx;
43 import org.openide.util.NbBundle;
44 import org.tigris.subversion.svnclientadapter.SVNRevision;
45
46 /**
47  * @author Tomas Stupka
48  */

49 public class CheckoutStep extends AbstractStep implements ActionListener JavaDoc, DocumentListener JavaDoc, FocusListener JavaDoc {
50
51     public static final String JavaDoc CHECKOUT_DIRECTORY = "checkoutStep.checkoutDirectory";
52     
53     private CheckoutPanel workdirPanel;
54     private RepositoryPaths repositoryPaths;
55
56     public HelpCtx getHelp() {
57         return new HelpCtx(CheckoutStep.class);
58     }
59
60     protected JComponent JavaDoc createComponent() {
61         if (workdirPanel == null) {
62             workdirPanel = new CheckoutPanel();
63             workdirPanel.browseWorkdirButton.addActionListener(this);
64             workdirPanel.browseRepositoryButton.addActionListener(this);
65             workdirPanel.scanForProjectsCheckBox.addActionListener(this);
66                     
67             workdirPanel.workdirTextField.setText(defaultWorkingDirectory().getPath());
68             workdirPanel.workdirTextField.getDocument().addDocumentListener(this);
69             workdirPanel.workdirTextField.addFocusListener(this);
70             workdirPanel.repositoryPathTextField.getDocument().addDocumentListener(this);
71             workdirPanel.repositoryPathTextField.addFocusListener(this);
72             workdirPanel.revisionTextField.getDocument().addDocumentListener(this);
73             workdirPanel.revisionTextField.addFocusListener(this);
74         }
75         validateUserInput(true);
76         return workdirPanel;
77     }
78
79     public void setup(RepositoryFile repositoryFile) {
80         if(repositoryPaths == null) {
81             repositoryPaths =
82                 new RepositoryPaths(
83                         repositoryFile,
84                         workdirPanel.repositoryPathTextField,
85                         workdirPanel.browseRepositoryButton,
86                         workdirPanel.revisionTextField,
87                         workdirPanel.searchRevisionButton
88                 );
89             String JavaDoc browserPurposeMessage = org.openide.util.NbBundle.getMessage(CheckoutStep.class, "LBL_BrowserMessage");
90             int browserMode = Browser.BROWSER_SHOW_FILES | Browser.BROWSER_FOLDERS_SELECTION_ONLY;
91             repositoryPaths.setupBrowserBehavior(browserPurposeMessage, browserMode);
92         } else {
93             repositoryPaths.setRepositoryFile(repositoryFile);
94         }
95         workdirPanel.repositoryPathTextField.setText(repositoryFile.getPath());
96         if(!repositoryFile.getRevision().equals(SVNRevision.HEAD)) {
97             workdirPanel.revisionTextField.setText(repositoryFile.getRevision().toString());
98         } else {
99             workdirPanel.revisionTextField.setText("");
100         }
101         workdirPanel.scanForProjectsCheckBox.setSelected(SvnModuleConfig.getDefault().getShowCheckoutCompleted());
102     }
103      
104     protected void validateBeforeNext() {
105         if (validateUserInput(true)) {
106             String JavaDoc text = workdirPanel.workdirTextField.getText();
107             File JavaDoc file = new File JavaDoc(text);
108             if (file.exists() == false) {
109                 boolean done = file.mkdirs();
110                 if (done == false) {
111                     invalid(org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2013") + file.getPath());// NOI18N
112
}
113             }
114         }
115     }
116
117     private boolean validateUserInput(boolean full) {
118         if(repositoryPaths != null) {
119             try {
120                 repositoryPaths.getRepositoryFiles();
121             } catch (NumberFormatException JavaDoc ex) {
122                 invalid(org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2018"));// NOI18N
123
return false;
124             } catch (MalformedURLException JavaDoc ex) {
125                 invalid(org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2015"));// NOI18N
126
return false;
127             }
128         }
129         
130         String JavaDoc text = workdirPanel.workdirTextField.getText();
131         if (text == null || text.length() == 0) {
132             invalid(org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2014"));// NOI18N
133
return false;
134         }
135         
136         String JavaDoc errorMessage = null;
137         if (full) {
138             File JavaDoc file = new File JavaDoc(text);
139             if (file.exists() == false) {
140                 // it's automaticaly create later on, check for permisions here
141
File JavaDoc parent = file.getParentFile();
142                 while (parent != null) {
143                     if (parent.exists()) {
144                         if (parent.canWrite() == false) {
145                             errorMessage = org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2016") + parent.getPath();// NOI18N
146
}
147                         break;
148                     }
149                     parent = parent.getParentFile();
150                 }
151             } else {
152                 if (file.isFile()) {
153                     errorMessage = org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2017");// NOI18N
154
}
155             }
156         }
157
158         if (errorMessage == null) {
159             valid();
160         } else {
161             invalid(errorMessage);
162         }
163
164         return errorMessage == null;
165     }
166     
167     private void onBrowseWorkdir() {
168         File JavaDoc defaultDir = defaultWorkingDirectory();
169         JFileChooser JavaDoc fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(CheckoutStep.class, "ACSD_BrowseFolder"), defaultDir);// NOI18N
170
fileChooser.setDialogTitle(NbBundle.getMessage(CheckoutStep.class, "BK0010"));// NOI18N
171
fileChooser.setMultiSelectionEnabled(false);
172         FileFilter JavaDoc[] old = fileChooser.getChoosableFileFilters();
173         for (int i = 0; i < old.length; i++) {
174             FileFilter JavaDoc fileFilter = old[i];
175             fileChooser.removeChoosableFileFilter(fileFilter);
176
177         }
178         fileChooser.addChoosableFileFilter(new FileFilter JavaDoc() {
179             public boolean accept(File JavaDoc f) {
180                 return f.isDirectory();
181             }
182             public String JavaDoc getDescription() {
183                 return NbBundle.getMessage(CheckoutStep.class, "BK0008");// NOI18N
184
}
185         });
186         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
187         fileChooser.showDialog(workdirPanel, NbBundle.getMessage(CheckoutStep.class, "BK0009"));// NOI18N
188
File JavaDoc f = fileChooser.getSelectedFile();
189         if (f != null) {
190             workdirPanel.workdirTextField.setText(f.getAbsolutePath());
191         }
192     }
193     
194     /**
195      * Returns file to be initaly used.
196      * <ul>
197      * <li>first is takes text in workTextField
198      * <li>then recent project folder
199      * <li>then recent checkout folder
200      * <li>finally <tt>user.home</tt>
201      * <ul>
202      */

203     private File JavaDoc defaultWorkingDirectory() {
204         File JavaDoc defaultDir = null;
205         String JavaDoc current = workdirPanel.workdirTextField.getText();
206         if (current != null && !(current.trim().equals(""))) { // NOI18N
207
File JavaDoc currentFile = new File JavaDoc(current);
208             while (currentFile != null && currentFile.exists() == false) {
209                 currentFile = currentFile.getParentFile();
210             }
211             if (currentFile != null) {
212                 if (currentFile.isFile()) {
213                     defaultDir = currentFile.getParentFile();
214                 } else {
215                     defaultDir = currentFile;
216                 }
217             }
218         }
219
220         if (defaultDir == null) {
221             String JavaDoc coDir = SvnModuleConfig.getDefault().getPreferences().get(CHECKOUT_DIRECTORY, null);
222             if(coDir != null) {
223                 defaultDir = new File JavaDoc(coDir);
224             }
225         }
226
227         if (defaultDir == null) {
228             File JavaDoc projectFolder = ProjectChooser.getProjectsFolder();
229             if (projectFolder.exists() && projectFolder.isDirectory()) {
230                 defaultDir = projectFolder;
231             }
232         }
233
234         if (defaultDir == null) {
235             defaultDir = new File JavaDoc(System.getProperty("user.home")); // NOI18N
236
}
237
238         return defaultDir;
239     }
240
241     public void insertUpdate(DocumentEvent JavaDoc e) {
242         validateUserInput(false);
243         refreshSkipLabel();
244     }
245
246     public void removeUpdate(DocumentEvent JavaDoc e) {
247         validateUserInput(false);
248         refreshSkipLabel();
249     }
250
251     public void changedUpdate(DocumentEvent JavaDoc e) {
252     }
253
254     public void focusGained(FocusEvent JavaDoc e) {
255     }
256
257     public void focusLost(FocusEvent JavaDoc e) {
258         validateUserInput(true);
259         refreshSkipLabel();
260     }
261         
262     public void actionPerformed(ActionEvent JavaDoc e) {
263         if(e.getSource()==workdirPanel.browseWorkdirButton) {
264             onBrowseWorkdir();
265         } else if (e.getSource() == workdirPanel.scanForProjectsCheckBox) {
266             SvnModuleConfig.getDefault().setShowCheckoutCompleted(workdirPanel.scanForProjectsCheckBox.isSelected());
267         }
268     }
269     
270     public File JavaDoc getWorkdir() {
271         return new File JavaDoc(workdirPanel.workdirTextField.getText());
272     }
273
274     public RepositoryFile[] getRepositoryFiles() {
275         try {
276             return repositoryPaths.getRepositoryFiles(".");
277         } catch (MalformedURLException JavaDoc ex) {
278             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
279
}
280         return null;
281     }
282
283     public boolean isAtWorkingDirLevel() {
284         return workdirPanel.atWorkingDirLevelCheckBox.isSelected();
285     }
286
287     private void refreshSkipLabel() {
288         if(workdirPanel.repositoryPathTextField.getText().trim().equals("")) {
289             resetWorkingDirLevelCheckBox();
290             return;
291         }
292         
293         RepositoryFile[] repositoryFiles = null;
294         try {
295             repositoryFiles = repositoryPaths.getRepositoryFiles();
296         } catch (NumberFormatException JavaDoc ex) {
297             // ignore
298
} catch (MalformedURLException JavaDoc ex) {
299             // ignore
300
}
301         
302         if(repositoryFiles == null ||
303            repositoryFiles.length > 1)
304         {
305             resetWorkingDirLevelCheckBox();
306             return;
307         }
308         
309         String JavaDoc repositoryFolder = repositoryFiles[0].getFileUrl().getLastPathSegment().trim();
310         if(repositoryFolder.equals("") || // the skip option doesn't make sense if there is no one,
311
repositoryFolder.equals(".")) // or more as one folder to be checked out
312
{
313             resetWorkingDirLevelCheckBox();
314             return;
315         } else {
316             workdirPanel.atWorkingDirLevelCheckBox.setText (
317                     NbBundle.getMessage(CheckoutStep.class,
318                                         "CTL_Checkout_CheckoutContentFolder",
319                                          new Object JavaDoc[] {repositoryFolder})
320             );
321             workdirPanel.atWorkingDirLevelCheckBox.setEnabled(true);
322         }
323     }
324     
325     private void resetWorkingDirLevelCheckBox() {
326         workdirPanel.atWorkingDirLevelCheckBox.setText(NbBundle.getMessage(CheckoutStep.class, "CTL_Checkout_CheckoutContentEmpty"));
327         workdirPanel.atWorkingDirLevelCheckBox.setEnabled(false);
328     }
329 }
330
331
Popular Tags