1 19 package org.netbeans.modules.subversion.ui.browser; 20 21 import java.awt.Dialog ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import java.net.MalformedURLException ; 28 import java.text.ParseException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import javax.swing.JButton ; 33 import javax.swing.JTextField ; 34 import javax.swing.event.DocumentEvent ; 35 import javax.swing.event.DocumentListener ; 36 import javax.swing.event.ListSelectionEvent ; 37 import javax.swing.event.ListSelectionListener ; 38 import javax.swing.text.JTextComponent ; 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 55 public class RepositoryPaths implements ActionListener , DocumentListener { 56 57 private final static RepositoryFile[] EMPTY_REPOSITORY_FILES = new RepositoryFile[0]; 58 59 private RepositoryFile repositoryFile; 60 61 private JTextComponent repositoryPathTextField; 63 private JTextComponent revisionTextField; 64 65 private JButton browseButton; 66 private JButton searchRevisionButton; 67 68 private int browserMode; 70 private BrowserAction[] browserActions; 71 private String browserPurpose; 72 73 private boolean valid = false; 74 public static final String PROP_VALID = "valid"; private List <PropertyChangeListener > listeners; 76 77 private PropertyChangeSupport propertyChangeSupport; 78 79 public RepositoryPaths(RepositoryFile repositoryFile, 80 JTextComponent repositoryPathTextField, 81 JButton browseButton, 82 JTextField revisionTextField, 83 JButton 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 browserPurpose, int browserMode) { 111 this.browserMode = browserMode; 112 this.browserPurpose = browserPurpose; 113 } 114 115 public void setupBrowserBehavior(String 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 , NumberFormatException { 122 return getRepositoryFiles(null); 123 } 124 125 public RepositoryFile[] getRepositoryFiles(String defaultPath) throws MalformedURLException , NumberFormatException { 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("")) { 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 return EMPTY_REPOSITORY_FILES; 144 } 145 String [] paths = getRepositoryString().split(","); RepositoryFile[] ret = new RepositoryFile[paths.length]; 147 SVNUrl repositoryUrl = getRepositoryUrl(); 148 149 for (int i = 0; i < paths.length; i++) { 150 String path = paths[i].trim(); 151 String repositoryUrlString = getRepositoryUrl().toString(); 152 if(path.startsWith("file://") || path.startsWith("http://") || path.startsWith("https://") || path.startsWith("svn://") || path.startsWith("svn+ssh://")) { if(path.startsWith(repositoryUrlString)) { 160 ret[i] = new RepositoryFile(repositoryUrl, path.substring(repositoryUrlString.length()), revision); 162 } else { 163 throw new MalformedURLException (NbBundle.getMessage(RepositoryPaths.class, "MSG_RepositoryPath_WrongStart", path, repositoryUrlString)); } 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 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 () { 197 public void propertyChange(PropertyChangeEvent evt) { 198 if( ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName()) ) { 199 dialogDescriptor.setValid(browser.getSelectedNodes().length > 0); 200 } 201 } 202 }); 203 204 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 205 dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(RepositoryPaths.class, "CTL_RepositoryPath_BrowseFolders_Title")); 206 dialog.setVisible(true); 207 208 if (DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue())) { 210 RepositoryFile[] selectedFiles = browser.getSelectedFiles(); 211 212 if(selectedFiles.length > 0) { 213 StringBuffer paths = new StringBuffer (); 214 for (int i = 0; i < selectedFiles.length; i++) { 215 paths.append(selectedFiles[i].getPath()); 216 if(i < selectedFiles.length - 1) { 217 paths.append(", "); } 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 () { 238 public void valueChanged(ListSelectionEvent e) { 239 dialogDescriptor.setValid(svnSearch.getSelectedRevision() != null); 241 } 243 }); 244 245 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 246 dialog.setVisible(true); 247 248 if (DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue())) { 250 revision = svnSearch.getSelectedRevision(); 251 if(revision != null) { 252 if(revision.equals(SVNRevision.HEAD) ) { 253 setRevisionTextField(""); } 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 revisionString = getRevisionString(); 269 270 if (revisionString.equals("")) { 271 return SVNRevision.HEAD; 272 } 273 return SvnUtils.getSVNRevision(revisionString); 274 } 275 276 public void actionPerformed(ActionEvent e) { 277 if(e.getSource() == browseButton) { 278 browseRepository(); 279 } else if (e.getSource() == searchRevisionButton) { 280 searchRepository(); 281 } 282 } 283 284 public void insertUpdate(DocumentEvent e) { 285 validateUserInput(); 286 } 287 288 public void removeUpdate(DocumentEvent e) { 289 validateUserInput(); 290 } 291 292 public void changedUpdate(DocumentEvent 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 ex) { 304 valid = false; 305 } catch (MalformedURLException 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("")) { valid = false; 318 } 319 320 if(valid && !acceptEmptyRevision() && revisionTextField != null && getRevisionString().equals("")) { 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 (this, PROP_VALID, new Boolean (oldValue), new Boolean (valid))); 331 } 332 333 public void addPropertyChangeListener(PropertyChangeListener l) { 334 getChangeSupport().addPropertyChangeListener(l); 335 validateUserInput(); 336 } 337 338 public void removePropertyChangeListener(PropertyChangeListener l) { 339 getChangeSupport().removePropertyChangeListener(l); 340 } 341 342 private PropertyChangeSupport getChangeSupport() { 343 if(propertyChangeSupport == null) { 344 propertyChangeSupport = new PropertyChangeSupport (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 url) { 366 repositoryPathTextField.setText(url); 367 } 368 369 public void setRevisionTextField(String revision) { 370 revisionTextField.setText(revision); 371 } 372 373 protected String getRepositoryString() { 374 return repositoryPathTextField.getText().trim(); 375 } 376 377 protected String getRevisionString() { 378 return revisionTextField.getText().trim(); 379 } 380 } 381 | Popular Tags |