KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > wizards > importstep > ImportStep


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.importstep;
21
22 import java.awt.event.FocusEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import org.netbeans.modules.subversion.FileStatusCache;
31 import org.netbeans.modules.subversion.RepositoryFile;
32 import org.netbeans.modules.subversion.Subversion;
33 import org.netbeans.modules.subversion.client.ExceptionHandler;
34 import org.netbeans.modules.subversion.client.SvnClient;
35 import org.netbeans.modules.subversion.client.WizardStepProgressSupport;
36 import org.netbeans.modules.subversion.ui.browser.Browser;
37 import org.netbeans.modules.subversion.ui.wizards.AbstractStep;
38 import org.netbeans.modules.subversion.ui.browser.BrowserAction;
39 import org.netbeans.modules.subversion.ui.browser.RepositoryPaths;
40 import org.netbeans.modules.subversion.ui.checkout.CheckoutAction;
41 import org.netbeans.modules.subversion.util.FileUtils;
42 import org.netbeans.modules.subversion.util.SvnUtils;
43 import org.openide.ErrorManager;
44 import org.openide.WizardDescriptor;
45 import org.openide.util.HelpCtx;
46 import org.tigris.subversion.svnclientadapter.SVNClientException;
47 import org.tigris.subversion.svnclientadapter.SVNUrl;
48
49 /**
50  * @author Tomas Stupka
51  */

52 public class ImportStep extends AbstractStep implements DocumentListener JavaDoc, WizardDescriptor.AsynchronousValidatingPanel, WizardDescriptor.FinishablePanel {
53     
54     private ImportPanel importPanel;
55
56     private RepositoryPaths repositoryPaths;
57     private BrowserAction[] actions;
58     private File JavaDoc importDirectory;
59     private WizardStepProgressSupport support;
60     
61     public ImportStep(BrowserAction[] actions, File JavaDoc importDirectory) {
62         this.actions = actions;
63         this.importDirectory = importDirectory;
64     }
65     
66     public HelpCtx getHelp() {
67         return new HelpCtx(ImportStep.class);
68     }
69
70     protected JComponent JavaDoc createComponent() {
71         if (importPanel == null) {
72             importPanel = new ImportPanel();
73             importPanel.messageTextArea.getDocument().addDocumentListener(this);
74             importPanel.repositoryPathTextField.getDocument().addDocumentListener(this);
75         }
76         return importPanel;
77     }
78
79     protected void validateBeforeNext() {
80         try {
81             if(support != null) {
82                 support.performInCurrentThread(org.openide.util.NbBundle.getMessage(ImportStep.class, "CTL_Import_Progress")); // NOI18N
83
}
84         } finally {
85             support = null;
86         }
87     }
88
89     public void prepareValidation() {
90         support = new ImportProgressSupport(importPanel.progressPanel, importPanel.progressLabel);
91         support.startProgress();
92     }
93
94     public boolean validateUserInput() {
95         invalid(null);
96         
97         String JavaDoc text = importPanel.repositoryPathTextField.getText().trim();
98         if (text.length() == 0) {
99             invalid(org.openide.util.NbBundle.getMessage(ImportStep.class, "BK2014")); // NOI18N
100
return false;
101         }
102         
103         text = importPanel.messageTextArea.getText().trim();
104         boolean valid = text.length() > 0;
105         if(valid) {
106             valid();
107         } else {
108             invalid(org.openide.util.NbBundle.getMessage(ImportStep.class, "CTL_Import_MessageRequired")); // NOI18N
109
}
110
111         return valid;
112     }
113     
114     public void insertUpdate(DocumentEvent JavaDoc e) {
115         validateUserInput();
116     }
117
118     public void removeUpdate(DocumentEvent JavaDoc e) {
119         validateUserInput();
120     }
121
122     public void changedUpdate(DocumentEvent JavaDoc e) {
123     }
124
125     public void focusGained(FocusEvent JavaDoc e) {
126         
127     }
128
129     public void focusLost(FocusEvent JavaDoc e) {
130         validateUserInput();
131     }
132
133     public String JavaDoc getImportMessage() {
134         return importPanel.messageTextArea.getText();
135     }
136
137     public void setup(RepositoryFile repositoryFile) {
138         if(importPanel.repositoryPathTextField.getText().trim().equals("")) { // NOI18N
139
// no value set yet ...
140
if(repositoryPaths == null) {
141                 repositoryPaths =
142                     new RepositoryPaths (
143                         repositoryFile,
144                         importPanel.repositoryPathTextField,
145                         importPanel.browseRepositoryButton,
146                         null,
147                         null
148                     );
149                 String JavaDoc browserPurposeMessage = org.openide.util.NbBundle.getMessage(ImportStep.class, "LBL_BrowserMessage");
150                 int browserMode = Browser.BROWSER_SINGLE_SELECTION_ONLY;
151                 repositoryPaths.setupBrowserBehavior(browserPurposeMessage, browserMode, actions);
152             } else {
153                 repositoryPaths.setRepositoryFile(repositoryFile);
154             }
155         }
156         importPanel.repositoryPathTextField.setText(repositoryFile.getPath());
157         validateUserInput();
158     }
159
160     public RepositoryFile getRepositoryFile() {
161         try {
162             return repositoryPaths.getRepositoryFiles()[0]; // more files doesn't make sence
163
} catch (MalformedURLException JavaDoc ex) {
164             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
165         }
166         return null;
167     }
168
169     public SVNUrl getRepositoryFolderUrl() {
170         return getRepositoryFile().getFileUrl();
171     }
172
173     public void stop() {
174         if(support != null) {
175             support.cancel();
176         }
177     }
178
179     public boolean isFinishPanel() {
180         return true;
181     }
182     
183     private class ImportProgressSupport extends WizardStepProgressSupport {
184         public ImportProgressSupport(JPanel JavaDoc panel, JLabel JavaDoc label) {
185             super(panel);
186         }
187         public void perform() {
188             String JavaDoc invalidMsg = null;
189             try {
190                 if(!validateUserInput()) {
191                     return;
192                 }
193
194                 invalid(null);
195
196                 SvnClient client;
197                 try {
198                     client = Subversion.getInstance().getClient(repositoryPaths.getRepositoryUrl(), this);
199                 } catch (SVNClientException ex) {
200                     ErrorManager.getDefault().notify(ex);
201                     valid(ex.getLocalizedMessage());
202                     return;
203                 }
204
205                 try {
206                     RepositoryFile repositoryFile = getRepositoryFile();
207                     SVNUrl repositoryUrl = repositoryFile.getRepositoryUrl();
208                     try {
209                         // if the user came back from the last step and changed the repository folder name,
210
// then this could be already a working copy ...
211
FileUtils.deleteRecursively(new File JavaDoc(importDirectory.getAbsoluteFile() + "/" + ".svn")); // NOI18N
212
FileUtils.deleteRecursively(new File JavaDoc(importDirectory.getAbsoluteFile() + "/" + "_svn")); // NOI18N
213
File JavaDoc importDummyFolder = new File JavaDoc(System.getProperty("java.io.tmpdir") + "/svn_dummy/" + importDirectory.getName()); // NOI18N
214
importDummyFolder.mkdirs();
215                         importDummyFolder.deleteOnExit();
216                         client.doImport(importDummyFolder, repositoryFile.getFileUrl(), getImportMessage(), false);
217                     } catch (SVNClientException ex) {
218                         if(ExceptionHandler.isFileAlreadyExists(ex.getMessage()) ) {
219                             // ignore
220
} else {
221                             throw ex;
222                         }
223                     }
224                     if(isCanceled()) {
225                         return;
226                     }
227
228                     RepositoryFile[] repositoryFiles = new RepositoryFile[] { repositoryFile };
229                     CheckoutAction.checkout(client, repositoryUrl, repositoryFiles, importDirectory, true, this);
230                     Subversion.getInstance().versionedFilesChanged();
231                     SvnUtils.refreshRecursively(importDirectory);
232                     // XXX this is ugly and expensive! the client should notify (onNotify()) the cache. find out why it doesn't work...
233
forceStatusRefresh(importDirectory); // XXX the same for another implementations like this in the code.... (see SvnUtils.refreshRecursively() )
234
if(isCanceled()) {
235                         FileUtils.deleteRecursively(new File JavaDoc(importDirectory.getAbsoluteFile() + "/" + ".svn")); // NOI18N
236
FileUtils.deleteRecursively(new File JavaDoc(importDirectory.getAbsoluteFile() + "/" + "_svn")); // NOI18N
237
return;
238                     }
239                 } catch (SVNClientException ex) {
240                     annotate(ex);
241                     invalidMsg = ExceptionHandler.parseExceptionMessage(ex);
242                 }
243
244             } finally {
245                 if(isCanceled()) {
246                     valid(org.openide.util.NbBundle.getMessage(ImportStep.class, "MSG_Import_ActionCanceled")); // NOI18N
247
} else if(invalidMsg != null) {
248                     valid(invalidMsg);
249                 } else {
250                     valid();
251                 }
252             }
253         }
254
255         public void setEditable(boolean editable) {
256             importPanel.browseRepositoryButton.setEnabled(editable);
257             importPanel.messageTextArea.setEditable(editable);
258             importPanel.repositoryPathTextField.setEditable(editable);
259         }
260
261         private void deleteDirectory(File JavaDoc file) {
262              File JavaDoc[] files = file.listFiles();
263              if(files !=null || files.length > 0) {
264                  for (int i = 0; i < files.length; i++) {
265                      if(files[i].isDirectory()) {
266                          deleteDirectory(files[i]);
267                      } else {
268                         files[i].delete();
269                      }
270                  }
271              }
272              file.delete();
273         }
274     };
275
276     private static void forceStatusRefresh(File JavaDoc file) {
277         Subversion.getInstance().getStatusCache().refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
278         if(!file.isFile()) {
279             File JavaDoc[] files = file.listFiles();
280             for (int i = 0; i < files.length; i++) {
281                 forceStatusRefresh(files[i]);
282             }
283         }
284     }
285     
286 }
287
288
Popular Tags