KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > copy > CreateCopy


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 package org.netbeans.modules.subversion.ui.copy;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.FocusEvent JavaDoc;
24 import java.awt.event.FocusListener JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.File JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import javax.swing.event.DocumentEvent JavaDoc;
30 import javax.swing.event.DocumentListener JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import org.netbeans.modules.subversion.RepositoryFile;
33 import org.netbeans.modules.subversion.ui.browser.Browser;
34 import org.netbeans.modules.subversion.ui.browser.BrowserAction;
35 import org.netbeans.modules.subversion.ui.browser.CreateFolderAction;
36 import org.netbeans.modules.subversion.ui.browser.RepositoryPaths;
37 import org.openide.ErrorManager;
38 import org.openide.util.NbBundle;
39
40 /**
41  *
42  * @author Tomas Stupka
43  */

44 public class CreateCopy extends CopyDialog implements DocumentListener JavaDoc, FocusListener JavaDoc, ActionListener JavaDoc, PropertyChangeListener JavaDoc {
45
46     private final RepositoryPaths copyToRepositoryPaths;
47     private final RepositoryPaths copyFromRepositoryPaths;
48     
49     private final File JavaDoc localeFile;
50     private final RepositoryFile repositoryFile;
51     private final boolean localChanges;
52     
53     /** Creates a new instance of CreateCopy */
54     public CreateCopy(RepositoryFile repositoryFile, File JavaDoc localeFile, boolean localChanges) {
55         super(new CreateCopyPanel(), NbBundle.getMessage(CreateCopy.class, "CTL_CopyDialog_Prompt", localeFile.getName()), NbBundle.getMessage(CreateCopy.class, "CTL_CopyDialog_Title")); // NOI18N
56

57         this.localeFile = localeFile;
58         this.repositoryFile = repositoryFile;
59         this.localChanges = localChanges;
60         
61         CreateCopyPanel panel = getCreateCopyPanel();
62                        
63         panel.localRadioButton.addActionListener(this);
64         panel.remoteRadioButton.addActionListener(this);
65         panel.skipCheckBox.addActionListener(this);
66
67         panel.copyFromLocalTextField.setText(localeFile.getAbsolutePath());
68         panel.copyFromRemoteTextField.setText(repositoryFile.getFileUrl().toString());
69         
70         copyFromRepositoryPaths =
71             new RepositoryPaths(
72                 repositoryFile,
73                 panel.copyFromRemoteTextField,
74                 null,
75                 panel.copyFromRevisionTextField,
76                 panel.searchButton
77             );
78         
79         if(localeFile.isFile()) {
80             panel.localRadioButton.setText(org.openide.util.NbBundle.getMessage(CreateCopy.class, "CTL_CopyForm_fromLocalFile")); // NOI18N
81
panel.remoteRadioButton.setText(org.openide.util.NbBundle.getMessage(CreateCopy.class, "CTL_CopyForm_fromRemoteFile")); // NOI18N
82
panel.skipCheckBox.setEnabled(false);
83         } else {
84             panel.localRadioButton.setText(org.openide.util.NbBundle.getMessage(CreateCopy.class, "CTL_CopyForm_fromLocalFolder")); // NOI18N
85
panel.remoteRadioButton.setText(org.openide.util.NbBundle.getMessage(CreateCopy.class, "CTL_CopyForm_fromRemoteFolder")); // NOI18N
86
}
87         
88         panel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CreateCopy.class, "CTL_CopyDialog_Title")); // NOI18N
89

90         copyToRepositoryPaths =
91             new RepositoryPaths(
92                 repositoryFile,
93                 (JTextComponent JavaDoc) panel.urlComboBox.getEditor().getEditorComponent(),
94                 panel.browseRepositoryButton,
95                 null,
96                 null
97             );
98                 
99         String JavaDoc browserPurposeMessage = "";
100         if(localeFile.isFile()) {
101             browserPurposeMessage = org.openide.util.NbBundle.getMessage(CreateCopy.class, "LBL_BrowserMessageCopyFile"); // NOI18N
102
} else {
103             browserPurposeMessage = org.openide.util.NbBundle.getMessage(CreateCopy.class, "LBL_BrowserMessageCopyFolder"); // NOI18N
104
}
105
106         String JavaDoc defaultFolderName = localeFile.isFile() ? "" : localeFile.getName();
107         int browserMode = Browser.BROWSER_SINGLE_SELECTION_ONLY;
108         copyToRepositoryPaths.setupBrowserBehavior(browserPurposeMessage, browserMode, new BrowserAction[] { new CreateFolderAction(defaultFolderName)} );
109         copyToRepositoryPaths.addPropertyChangeListener(this);
110
111         setupUrlComboBox(panel.urlComboBox, CreateCopy.class.getName());
112         panel.messageTextArea.getDocument().addDocumentListener(this);
113         ((JTextComponent JavaDoc) panel.urlComboBox.getEditor().getEditorComponent()).getDocument().addDocumentListener(this);
114         
115         setFromLocal();
116         validateUserInput();
117     }
118     
119     protected void validateUserInput() {
120         String JavaDoc text = getCreateCopyPanel().messageTextArea.getText();
121         try {
122             RepositoryFile rf[] = copyToRepositoryPaths.getRepositoryFiles();
123             if(rf == null || rf.length == 0) {
124                 
125                 setErrorText(org.openide.util.NbBundle.getMessage(CreateCopy.class, "LBL_MISSING_REPOSITORY_FOLDER"));
126                 
127                 getOKButton().setEnabled(false);
128                 return;
129             }
130         } catch (NumberFormatException JavaDoc ex) {
131             setErrorText(ex.getLocalizedMessage()); // should not happen
132
getOKButton().setEnabled(false);
133             return;
134         } catch (MalformedURLException JavaDoc ex) {
135             setErrorText(ex.getLocalizedMessage()); // should not happen
136
getOKButton().setEnabled(false);
137             return;
138         }
139         if (text == null || text.length() == 0) {
140                         
141             setErrorText(org.openide.util.NbBundle.getMessage(CreateCopy.class, "LBL_MISSING_COPY_MESSAGE"));
142             
143             getOKButton().setEnabled(false);
144             return;
145         }
146         resetErrorText();
147         getOKButton().setEnabled(true);
148     }
149
150     private void setErrorText(String JavaDoc txt) {
151         CreateCopyPanel panel = getCreateCopyPanel();
152         panel.invalidValuesLabel.setVisible(true);
153         panel.invalidValuesLabel.setText(txt);
154     }
155     
156     private void resetErrorText() {
157         CreateCopyPanel panel = getCreateCopyPanel();
158         panel.invalidValuesLabel.setVisible(false);
159         panel.invalidValuesLabel.setText("");
160     }
161     
162     CreateCopyPanel getCreateCopyPanel() {
163         return (CreateCopyPanel) getPanel();
164     }
165     
166     RepositoryFile getToRepositoryFile() {
167         try {
168             return getToRepositoryFileIntern();
169         } catch (MalformedURLException JavaDoc ex) {
170             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
171
} catch (NumberFormatException JavaDoc ex) {
172             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
173
}
174         return null;
175     }
176
177     private RepositoryFile getToRepositoryFileIntern() throws NumberFormatException JavaDoc, MalformedURLException JavaDoc {
178         RepositoryFile[] toRepositoryFiles = copyToRepositoryPaths.getRepositoryFiles();
179         if(toRepositoryFiles.length > 0) {
180             RepositoryFile toRepositoryFile = toRepositoryFiles[0];
181             if(skipContents()) {
182                 return toRepositoryFile;
183             } else {
184                 if(isLocal()) {
185                     return toRepositoryFile.appendPath(localeFile.getName());
186                 } else {
187                     return toRepositoryFile.appendPath(repositoryFile.getFileUrl().getLastPathSegment());
188                 }
189             }
190         } else {
191             return null;
192         }
193     }
194
195     String JavaDoc getMessage() {
196         return getCreateCopyPanel().messageTextArea.getText();
197     }
198
199     boolean isLocal() {
200         return getCreateCopyPanel().localRadioButton.isSelected();
201     }
202     
203     File JavaDoc getLocalFile() {
204         return localeFile;
205     }
206     
207     RepositoryFile getFromRepositoryFile() {
208         try {
209             return copyFromRepositoryPaths.getRepositoryFiles()[0];
210         }
211         catch (MalformedURLException JavaDoc ex) {
212             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
213
} catch (NumberFormatException JavaDoc ex) {
214             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
215
}
216         return null;
217     }
218     
219     boolean switchTo() {
220         return getCreateCopyPanel().switchToCheckBox.isSelected();
221     }
222
223     boolean skipContents() {
224         return getCreateCopyPanel().skipCheckBox.isSelected();
225     }
226     
227     public void insertUpdate(DocumentEvent JavaDoc e) {
228         validateUserInput();
229         setPreview();
230     }
231
232     public void removeUpdate(DocumentEvent JavaDoc e) {
233         validateUserInput();
234         setPreview();
235     }
236
237     public void changedUpdate(DocumentEvent JavaDoc e) {
238         validateUserInput();
239         setPreview();
240     }
241
242     public void focusGained(FocusEvent JavaDoc e) {
243     }
244
245     public void focusLost(FocusEvent JavaDoc e) {
246         validateUserInput();
247         setPreview();
248     }
249
250     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
251         if( evt.getPropertyName().equals(RepositoryPaths.PROP_VALID) ) {
252             boolean valid = ((Boolean JavaDoc)evt.getNewValue()).booleanValue();
253             if(valid) {
254                 // it's a bit more we have to validate
255
validateUserInput();
256             } else {
257                 getOKButton().setEnabled(valid);
258             }
259         }
260     }
261
262     public void actionPerformed(ActionEvent JavaDoc evt) {
263         if(evt.getSource() == getCreateCopyPanel().localRadioButton) {
264             setFromLocal();
265             setPreview();
266         } else if(evt.getSource() == getCreateCopyPanel().remoteRadioButton) {
267             selectFromRemote();
268             setPreview();
269         } else if(evt.getSource() == getCreateCopyPanel().skipCheckBox) {
270             setPreview();
271         }
272     }
273     
274     private void setFromLocal() {
275         CreateCopyPanel panel = getCreateCopyPanel();
276         panel.copyFromLocalTextField.setEnabled(true);
277         panel.copyFromRemoteTextField.setEnabled(false);
278         panel.warningLabel.setVisible(localChanges);
279     }
280
281     private void selectFromRemote() {
282         CreateCopyPanel panel = getCreateCopyPanel();
283         panel.copyFromLocalTextField.setEnabled(false);
284         panel.copyFromRemoteTextField.setEnabled(true);
285         panel.warningLabel.setVisible(false);
286     }
287
288     private void setPreview() {
289         try {
290             RepositoryFile repositoryFile = getToRepositoryFileIntern();
291             if(repositoryFile!=null) {
292                 getCreateCopyPanel().previewTextField.setText(repositoryFile.getFileUrl().toString());
293             } else {
294                 getCreateCopyPanel().previewTextField.setText(""); // NOI18N
295
}
296         }
297         catch (NumberFormatException JavaDoc ex) {
298             // wrong value -> we can't copy anything
299
getCreateCopyPanel().previewTextField.setText(""); // NOI18N
300
} catch (MalformedURLException JavaDoc ex) {
301             // wrong value -> we can't copy anything
302
getCreateCopyPanel().previewTextField.setText(""); // NOI18N
303
};
304     }
305     
306 }
307
Popular Tags