1 19 20 package org.netbeans.modules.subversion.ui.wizards.checkoutstep; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.FocusEvent ; 25 import java.awt.event.FocusListener ; 26 import java.io.File ; 27 import java.net.MalformedURLException ; 28 import javax.swing.JComponent ; 29 import javax.swing.JFileChooser ; 30 import javax.swing.event.DocumentEvent ; 31 import javax.swing.event.DocumentListener ; 32 import javax.swing.filechooser.FileFilter ; 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 49 public class CheckoutStep extends AbstractStep implements ActionListener , DocumentListener , FocusListener { 50 51 public static final String 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 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 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 text = workdirPanel.workdirTextField.getText(); 107 File file = new File (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()); } 113 } 114 } 115 } 116 117 private boolean validateUserInput(boolean full) { 118 if(repositoryPaths != null) { 119 try { 120 repositoryPaths.getRepositoryFiles(); 121 } catch (NumberFormatException ex) { 122 invalid(org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2018")); return false; 124 } catch (MalformedURLException ex) { 125 invalid(org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2015")); return false; 127 } 128 } 129 130 String text = workdirPanel.workdirTextField.getText(); 131 if (text == null || text.length() == 0) { 132 invalid(org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2014")); return false; 134 } 135 136 String errorMessage = null; 137 if (full) { 138 File file = new File (text); 139 if (file.exists() == false) { 140 File 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(); } 147 break; 148 } 149 parent = parent.getParentFile(); 150 } 151 } else { 152 if (file.isFile()) { 153 errorMessage = org.openide.util.NbBundle.getMessage(CheckoutStep.class, "BK2017"); } 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 defaultDir = defaultWorkingDirectory(); 169 JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(CheckoutStep.class, "ACSD_BrowseFolder"), defaultDir); fileChooser.setDialogTitle(NbBundle.getMessage(CheckoutStep.class, "BK0010")); fileChooser.setMultiSelectionEnabled(false); 172 FileFilter [] old = fileChooser.getChoosableFileFilters(); 173 for (int i = 0; i < old.length; i++) { 174 FileFilter fileFilter = old[i]; 175 fileChooser.removeChoosableFileFilter(fileFilter); 176 177 } 178 fileChooser.addChoosableFileFilter(new FileFilter () { 179 public boolean accept(File f) { 180 return f.isDirectory(); 181 } 182 public String getDescription() { 183 return NbBundle.getMessage(CheckoutStep.class, "BK0008"); } 185 }); 186 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 187 fileChooser.showDialog(workdirPanel, NbBundle.getMessage(CheckoutStep.class, "BK0009")); File f = fileChooser.getSelectedFile(); 189 if (f != null) { 190 workdirPanel.workdirTextField.setText(f.getAbsolutePath()); 191 } 192 } 193 194 203 private File defaultWorkingDirectory() { 204 File defaultDir = null; 205 String current = workdirPanel.workdirTextField.getText(); 206 if (current != null && !(current.trim().equals(""))) { File currentFile = new File (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 coDir = SvnModuleConfig.getDefault().getPreferences().get(CHECKOUT_DIRECTORY, null); 222 if(coDir != null) { 223 defaultDir = new File (coDir); 224 } 225 } 226 227 if (defaultDir == null) { 228 File projectFolder = ProjectChooser.getProjectsFolder(); 229 if (projectFolder.exists() && projectFolder.isDirectory()) { 230 defaultDir = projectFolder; 231 } 232 } 233 234 if (defaultDir == null) { 235 defaultDir = new File (System.getProperty("user.home")); } 237 238 return defaultDir; 239 } 240 241 public void insertUpdate(DocumentEvent e) { 242 validateUserInput(false); 243 refreshSkipLabel(); 244 } 245 246 public void removeUpdate(DocumentEvent e) { 247 validateUserInput(false); 248 refreshSkipLabel(); 249 } 250 251 public void changedUpdate(DocumentEvent e) { 252 } 253 254 public void focusGained(FocusEvent e) { 255 } 256 257 public void focusLost(FocusEvent e) { 258 validateUserInput(true); 259 refreshSkipLabel(); 260 } 261 262 public void actionPerformed(ActionEvent 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 getWorkdir() { 271 return new File (workdirPanel.workdirTextField.getText()); 272 } 273 274 public RepositoryFile[] getRepositoryFiles() { 275 try { 276 return repositoryPaths.getRepositoryFiles("."); 277 } catch (MalformedURLException ex) { 278 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); } 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 ex) { 297 } catch (MalformedURLException ex) { 299 } 301 302 if(repositoryFiles == null || 303 repositoryFiles.length > 1) 304 { 305 resetWorkingDirLevelCheckBox(); 306 return; 307 } 308 309 String repositoryFolder = repositoryFiles[0].getFileUrl().getLastPathSegment().trim(); 310 if(repositoryFolder.equals("") || repositoryFolder.equals(".")) { 313 resetWorkingDirLevelCheckBox(); 314 return; 315 } else { 316 workdirPanel.atWorkingDirLevelCheckBox.setText ( 317 NbBundle.getMessage(CheckoutStep.class, 318 "CTL_Checkout_CheckoutContentFolder", 319 new Object [] {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 |