KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > browser > RepositoryPaths


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.browser;
20
21 import java.awt.Dialog JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JTextField JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
36 import javax.swing.event.ListSelectionEvent JavaDoc;
37 import javax.swing.event.ListSelectionListener JavaDoc;
38 import javax.swing.text.JTextComponent JavaDoc;
39 import org.netbeans.modules.subversion.RepositoryFile;
40 import org.netbeans.modules.subversion.ui.search.SvnSearch;
41 import org.netbeans.modules.subversion.util.SvnUtils;
42 import org.openide.DialogDescriptor;
43 import org.openide.DialogDisplayer;
44 import org.openide.ErrorManager;
45 import org.openide.explorer.ExplorerManager;
46 import org.openide.util.HelpCtx;
47 import org.openide.util.NbBundle;
48 import org.tigris.subversion.svnclientadapter.SVNRevision;
49 import org.tigris.subversion.svnclientadapter.SVNUrl;
50
51 /**
52  *
53  * @author Tomas Stupka
54  */

55 public class RepositoryPaths implements ActionListener JavaDoc, DocumentListener JavaDoc {
56     
57     private final static RepositoryFile[] EMPTY_REPOSITORY_FILES = new RepositoryFile[0];
58
59     private RepositoryFile repositoryFile;
60
61     // controled components
62
private JTextComponent JavaDoc repositoryPathTextField;
63     private JTextComponent JavaDoc revisionTextField;
64     
65     private JButton JavaDoc browseButton;
66     private JButton JavaDoc searchRevisionButton;
67
68     // browser
69
private int browserMode;
70     private BrowserAction[] browserActions;
71     private String JavaDoc browserPurpose;
72             
73     private boolean valid = false;
74     public static final String JavaDoc PROP_VALID = "valid"; // NOI18N
75
private List JavaDoc<PropertyChangeListener JavaDoc> listeners;
76     
77     private PropertyChangeSupport JavaDoc propertyChangeSupport;
78     
79     public RepositoryPaths(RepositoryFile repositoryFile,
80                            JTextComponent JavaDoc repositoryPathTextField,
81                            JButton JavaDoc browseButton,
82                            JTextField JavaDoc revisionTextField,
83                            JButton JavaDoc searchRevisionButton)
84     {
85         assert repositoryFile != null;
86         assert (repositoryPathTextField !=null && browseButton != null) ||
87                (revisionTextField != null && searchRevisionButton != null);
88
89         this.repositoryFile = repositoryFile;
90
91         if(repositoryPathTextField!=null) {
92             this.repositoryPathTextField = repositoryPathTextField;
93             repositoryPathTextField.getDocument().addDocumentListener(this);
94             this.browseButton = browseButton;
95             if(browseButton != null) {
96                 browseButton.addActionListener(this);
97             }
98         }
99
100         if(revisionTextField!=null) {
101             this.revisionTextField = revisionTextField;
102             revisionTextField.getDocument().addDocumentListener(this);
103             this.searchRevisionButton = searchRevisionButton;
104             if(searchRevisionButton != null) {
105                 searchRevisionButton.addActionListener(this);
106             }
107         }
108     }
109
110     public void setupBrowserBehavior(String JavaDoc browserPurpose, int browserMode) {
111         this.browserMode = browserMode;
112         this.browserPurpose = browserPurpose;
113     }
114     
115     public void setupBrowserBehavior(String JavaDoc browserPurpose, int browserMode, BrowserAction[] browserActions) {
116         setupBrowserBehavior(browserPurpose, browserMode);
117         this.browserActions = browserActions;
118         this.browserPurpose = browserPurpose;
119     }
120     
121     public RepositoryFile[] getRepositoryFiles() throws MalformedURLException JavaDoc, NumberFormatException JavaDoc {
122         return getRepositoryFiles(null);
123     }
124     
125     public RepositoryFile[] getRepositoryFiles(String JavaDoc defaultPath) throws MalformedURLException JavaDoc, NumberFormatException JavaDoc {
126
127         SVNRevision revision = getRevision();
128         
129         if(repositoryPathTextField==null) {
130             RepositoryFile rf = new RepositoryFile(repositoryFile.getRepositoryUrl(), repositoryFile.getFileUrl(), revision);
131             return new RepositoryFile[] {rf};
132         }
133      
134         if(getRepositoryString().equals("")) { // NOI18N
135
if(defaultPath != null && !defaultPath.trim().equals("") ) {
136                 return new RepositoryFile[] { new RepositoryFile(getRepositoryUrl(), defaultPath, revision) } ;
137             } else {
138                 return EMPTY_REPOSITORY_FILES;
139             }
140         }
141         if(revision == null) {
142             // should not be possible to get here!
143
return EMPTY_REPOSITORY_FILES;
144         }
145         String JavaDoc[] paths = getRepositoryString().split(","); // NOI18N
146
RepositoryFile[] ret = new RepositoryFile[paths.length];
147         SVNUrl repositoryUrl = getRepositoryUrl();
148        
149         for (int i = 0; i < paths.length; i++) {
150             String JavaDoc path = paths[i].trim();
151             String JavaDoc repositoryUrlString = getRepositoryUrl().toString();
152             if(path.startsWith("file://") || // NOI18N
153
path.startsWith("http://") || // NOI18N
154
path.startsWith("https://") || // NOI18N
155
path.startsWith("svn://") || // NOI18N
156
path.startsWith("svn+ssh://")) { // NOI18N // XXX check only for svn+ and concat the remaining part from the protocol
157
// must be a complete URL
158
// so check if it matches with the given repository URL
159
if(path.startsWith(repositoryUrlString)) {
160                     // lets take only the part without the repository base URL
161
ret[i] = new RepositoryFile(repositoryUrl, path.substring(repositoryUrlString.length()), revision);
162                 } else {
163                     throw new MalformedURLException JavaDoc(NbBundle.getMessage(RepositoryPaths.class, "MSG_RepositoryPath_WrongStart", path, repositoryUrlString)); // NOI18N
164
}
165             } else {
166                 ret[i] = new RepositoryFile(repositoryUrl, path, revision);
167             }
168         }
169         return ret;
170     }
171
172     private void browseRepository() {
173         SVNRevision revision = getRevision();
174         RepositoryFile[] repositoryFilesToSelect;
175         try {
176             repositoryFilesToSelect = getRepositoryFiles();
177         } catch (MalformedURLException JavaDoc ex) {
178             ErrorManager.getDefault().notify(ex);
179             return;
180         }
181         
182         final Browser browser =
183             new Browser(
184                 browserPurpose,
185                 browserMode,
186                 new RepositoryFile(getRepositoryUrl(), revision),
187                 repositoryFilesToSelect,
188                 browserActions);
189         
190         final DialogDescriptor dialogDescriptor =
191                 new DialogDescriptor(browser.getBrowserPanel(), java.util.ResourceBundle.getBundle("org/netbeans/modules/subversion/ui/browser/Bundle").getString("CTL_RepositoryPath_BrowseFolders_Title"));
192         dialogDescriptor.setModal(true);
193         dialogDescriptor.setHelpCtx(new HelpCtx(Browser.class));
194         dialogDescriptor.setValid(false);
195         
196         browser.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
197             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
198                 if( ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName()) ) {
199                     dialogDescriptor.setValid(browser.getSelectedNodes().length > 0);
200                 }
201             }
202         });
203         
204         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
205         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(RepositoryPaths.class, "CTL_RepositoryPath_BrowseFolders_Title"));
206         dialog.setVisible(true);
207
208         // handle results
209
if (DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue())) {
210             RepositoryFile[] selectedFiles = browser.getSelectedFiles();
211                         
212             if(selectedFiles.length > 0) {
213                 StringBuffer JavaDoc paths = new StringBuffer JavaDoc();
214                 for (int i = 0; i < selectedFiles.length; i++) {
215                     paths.append(selectedFiles[i].getPath());
216                     if(i < selectedFiles.length - 1) {
217                         paths.append(", "); // NOI18N
218
}
219                 }
220                 setRepositoryTextField(paths.toString());
221             }
222         } else {
223             browser.cancel();
224         }
225     }
226     
227     private void searchRepository() {
228         SVNRevision revision = getRevision();
229         final SvnSearch svnSearch = new SvnSearch(new RepositoryFile(getRepositoryUrl(), revision));
230         
231         final DialogDescriptor dialogDescriptor =
232                 new DialogDescriptor(svnSearch.getSearchPanel(), java.util.ResourceBundle.getBundle("org/netbeans/modules/subversion/ui/browser/Bundle").getString("CTL_RepositoryPath_SearchRevisions"));
233         dialogDescriptor.setModal(true);
234         dialogDescriptor.setHelpCtx(new HelpCtx(Browser.class));
235         dialogDescriptor.setValid(false);
236         
237         svnSearch.addListSelectionListener(new ListSelectionListener JavaDoc() {
238             public void valueChanged(ListSelectionEvent JavaDoc e) {
239                 //if( ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName()) ) {
240
dialogDescriptor.setValid(svnSearch.getSelectedRevision() != null);
241                // }
242
}
243         });
244         
245         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
246         dialog.setVisible(true);
247
248         // handle results
249
if (DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue())) {
250             revision = svnSearch.getSelectedRevision();
251             if(revision != null) {
252                 if(revision.equals(SVNRevision.HEAD) ) {
253                     setRevisionTextField(""); // NOI18N
254
} else {
255                     setRevisionTextField(revision.toString());
256                 }
257             }
258         } else {
259             svnSearch.cancel();
260         }
261     }
262     
263     public SVNRevision getRevision() {
264         
265         if (revisionTextField == null) {
266             return SVNRevision.HEAD;
267         }
268         String JavaDoc revisionString = getRevisionString();
269
270         if (revisionString.equals("")) {
271             return SVNRevision.HEAD;
272         }
273         return SvnUtils.getSVNRevision(revisionString);
274     }
275     
276     public void actionPerformed(ActionEvent JavaDoc e) {
277         if(e.getSource() == browseButton) {
278             browseRepository();
279         } else if (e.getSource() == searchRevisionButton) {
280             searchRepository();
281         }
282     }
283
284     public void insertUpdate(DocumentEvent JavaDoc e) {
285         validateUserInput();
286     }
287
288     public void removeUpdate(DocumentEvent JavaDoc e) {
289         validateUserInput();
290     }
291
292     public void changedUpdate(DocumentEvent JavaDoc e) {
293         validateUserInput();
294     }
295
296     private void validateUserInput() {
297         boolean oldValue = this.valid;
298         boolean valid = true;
299
300         RepositoryFile[] files = null;
301         try {
302             files = getRepositoryFiles();
303         } catch (NumberFormatException JavaDoc ex) {
304             valid = false;
305         } catch (MalformedURLException JavaDoc ex) {
306             valid = false;
307         }
308         
309         if(browseButton != null) {
310             browseButton.setEnabled(valid);
311         }
312         if(searchRevisionButton != null) {
313             searchRevisionButton.setEnabled(valid);
314         }
315         
316         if(valid && !acceptEmptyUrl() && repositoryPathTextField != null && getRepositoryString().equals("")) { // NOI18N
317
valid = false;
318         }
319
320         if(valid && !acceptEmptyRevision() && revisionTextField != null && getRevisionString().equals("")) { // NOI18N
321
valid = false;
322         }
323         
324         this.valid = valid;
325         fireValidPropertyChanged(oldValue, valid);
326
327     }
328
329     private void fireValidPropertyChanged(boolean oldValue, boolean valid) {
330         getChangeSupport().firePropertyChange(new PropertyChangeEvent JavaDoc(this, PROP_VALID, new Boolean JavaDoc(oldValue), new Boolean JavaDoc(valid)));
331     }
332
333     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
334         getChangeSupport().addPropertyChangeListener(l);
335         validateUserInput();
336     }
337
338     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
339         getChangeSupport().removePropertyChangeListener(l);
340     }
341     
342     private PropertyChangeSupport JavaDoc getChangeSupport() {
343         if(propertyChangeSupport == null) {
344             propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
345         }
346         return propertyChangeSupport;
347     }
348
349     protected boolean acceptEmptyUrl() {
350         return false;
351     }
352         
353     protected boolean acceptEmptyRevision() {
354         return true;
355     }
356
357     public SVNUrl getRepositoryUrl() {
358         return repositoryFile.getRepositoryUrl();
359     }
360
361     public void setRepositoryFile(RepositoryFile repositoryFile) {
362         this.repositoryFile = repositoryFile;
363     }
364
365     public void setRepositoryTextField(String JavaDoc url) {
366         repositoryPathTextField.setText(url);
367     }
368     
369     public void setRevisionTextField(String JavaDoc revision) {
370         revisionTextField.setText(revision);
371     }
372     
373     protected String JavaDoc getRepositoryString() {
374         return repositoryPathTextField.getText().trim();
375     }
376     
377     protected String JavaDoc getRevisionString() {
378         return revisionTextField.getText().trim();
379     }
380 }
381
Popular Tags