1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.wizards; 21 22 import org.openide.*; 23 import org.openide.filesystems.FileUtil; 24 import org.openide.util.NbBundle; 25 import org.openide.util.HelpCtx; 26 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig; 27 import org.netbeans.modules.versioning.system.cvss.ui.selectors.ModuleSelector; 28 import org.netbeans.modules.versioning.system.cvss.ui.selectors.BranchSelector; 29 import org.netbeans.lib.cvsclient.CVSRoot; 30 31 import javax.swing.event.ChangeListener ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.DocumentListener ; 34 import javax.swing.event.DocumentEvent ; 35 import javax.swing.*; 36 import javax.swing.filechooser.FileFilter ; 37 import java.util.*; 38 import java.util.List ; 39 import java.awt.event.ActionListener ; 40 import java.awt.event.ActionEvent ; 41 import java.awt.event.FocusListener ; 42 import java.awt.event.FocusEvent ; 43 import java.io.File ; 44 import java.text.MessageFormat ; 45 46 import org.netbeans.modules.versioning.util.AccessibleJFileChooser; 47 import org.netbeans.modules.versioning.util.Utils; 48 import org.netbeans.spi.project.ui.support.ProjectChooser; 49 50 55 public final class CheckoutWizard implements ChangeListener { 56 57 private static final String RECENT_DIRECTORY = "checkout.recentDirectory"; 58 private static final String CHECKOUT_RECENT_MODULE = "checkout.recentModule"; 59 private static final String CHECKOUT_RECENT_TAG = "checkout.recentTag"; 60 61 private WizardDescriptor wizard; 62 63 private String errorMessage; 64 65 private WizardDescriptor.Iterator wizardIterator; 66 67 private ModulePanel modulePanel; 68 69 private RepositoryStep repositoryStep; 70 71 73 private String initialCvsRoot; 74 75 private String initialModule; 76 77 78 public CheckoutWizard() { 79 } 80 81 public CheckoutWizard(String cvsRoot, String module) { 82 initialCvsRoot = cvsRoot; 83 initialModule = module; 84 } 85 86 public boolean show() { 87 wizardIterator = panelIterator(); 88 wizard = new WizardDescriptor(wizardIterator); 89 wizard.putProperty("WizardPanel_contentData", new String [] { 91 NbBundle.getMessage(CheckoutWizard.class, "BK0006"), 92 NbBundle.getMessage(CheckoutWizard.class, "BK2009") 93 } 94 ); 95 wizard.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); wizard.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); wizard.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); wizard.setTitleFormat(new MessageFormat ("{0}")); wizard.setTitle(NbBundle.getMessage(CheckoutWizard.class, "BK0007")); 100 Object result = DialogDisplayer.getDefault().notify(wizard); 101 boolean finished = NotifyDescriptor.OK_OPTION.equals(result); 102 if (finished) { 103 onFinished(); 104 } 105 return finished; 106 } 107 108 109 private void onFinished() { 110 String checkout = modulePanel.workTextField.getText(); 111 CvsModuleConfig.getDefault().getPreferences().put(CHECKOUT_RECENT_MODULE, modulePanel.moduleTextField.getText()); 112 CvsModuleConfig.getDefault().getPreferences().put(CHECKOUT_RECENT_TAG, modulePanel.tagTextField.getText()); 113 Utils.insert(CvsModuleConfig.getDefault().getPreferences(), RECENT_DIRECTORY, checkout, 8); 114 } 115 116 117 String getErrorMessage() { 118 String value; 119 if (wizard != null) { 120 value = (String ) wizard.getProperty("WizardPanel_errorMessage"); } else { 122 value = errorMessage; 123 } 124 if (value == null) value = ""; return value; 126 } 127 128 private void setErrorMessage(String msg) { 129 errorMessage = msg; 130 if (wizard != null) { 131 wizard.putProperty("WizardPanel_errorMessage", msg); } 133 } 134 135 public void stateChanged(ChangeEvent e) { 136 AbstractStep step = (AbstractStep) wizardIterator.current(); 137 setErrorMessage(step.getErrorMessage()); 138 } 139 140 private WizardDescriptor.Iterator panelIterator() { 141 repositoryStep = new RepositoryStep(initialCvsRoot); 142 repositoryStep.addChangeListener(this); 143 WizardDescriptor.Panel modulePanel = new ModuleStep(); 144 modulePanel.addChangeListener(this); 145 146 final WizardDescriptor.Panel[] panels = new WizardDescriptor.Panel[2]; 147 panels[0] = repositoryStep; 148 panels[1] = modulePanel; 149 150 WizardDescriptor.ArrayIterator ret = new WizardDescriptor.ArrayIterator(panels) { 151 public WizardDescriptor.Panel current() { 152 WizardDescriptor.Panel ret = super.current(); 153 for (int i = 0; i<panels.length; i++) { 154 if (panels[i] == ret) { 155 wizard.putProperty("WizardPanel_contentSelectedIndex", new Integer (i)); } 157 } 158 return ret; 159 } 160 }; 161 return ret; 162 } 163 164 public String getModules() { 165 return modulePanel.moduleTextField.getText().trim(); 166 } 167 168 public String getTag() { 169 return modulePanel.tagTextField.getText().trim(); 170 } 171 172 175 public String getWorkingDir() { 176 String path = modulePanel.workTextField.getText(); 177 return FileUtil.normalizeFile(new File (path)).getAbsolutePath(); 178 } 179 180 181 public String getScrambledPassword() { 182 return repositoryStep.getScrambledPassword(); 183 } 184 185 public String getCvsRoot() { 186 return repositoryStep.getCvsRoot(); 187 } 188 189 private class ModuleStep extends AbstractStep implements DocumentListener , FocusListener , ActionListener { 190 191 protected JComponent createComponent() { 192 modulePanel = new ModulePanel(); 193 194 if (initialModule != null) { 195 modulePanel.moduleTextField.setText(initialModule); 196 } 197 198 modulePanel.moduleTextField.setText(CvsModuleConfig.getDefault().getPreferences().get(CHECKOUT_RECENT_MODULE, "")); 199 modulePanel.tagTextField.setText(CvsModuleConfig.getDefault().getPreferences().get(CHECKOUT_RECENT_TAG, "")); 200 201 String path = defaultWorkingDirectory().getPath(); 202 modulePanel.workTextField.setText(path); 203 modulePanel.workTextField.getDocument().addDocumentListener(this); 204 modulePanel.workTextField.addFocusListener(this); 205 modulePanel.workTextField.addActionListener(this); 206 validateUserInput(true); 207 208 modulePanel.moduleButton.addActionListener(this); 209 modulePanel.tagButton.addActionListener(this); 210 modulePanel.workButton.addActionListener(this); 211 return modulePanel; 212 } 213 214 public HelpCtx getHelp() { 215 return new HelpCtx(ModuleStep.class); 216 } 217 218 protected void validateBeforeNext() { 219 if (validateUserInput(true)) { 220 String text = modulePanel.workTextField.getText(); 221 File file = new File (text); 222 if (file.exists() == false) { 223 boolean done = file.mkdirs(); 224 if (done == false) { 225 invalid(org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2013") + file.getPath()); 226 } 227 } 228 } 229 } 230 231 private boolean validateUserInput(boolean full) { 232 String text = modulePanel.workTextField.getText(); 233 if (text == null || text.length() == 0) { 234 invalid(org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2014")); 235 return false; 236 } 237 238 String errorMessage = null; 239 if (full) { 240 File file = new File (text); 241 if (file.exists() == false) { 242 File parent = file.getParentFile(); 244 while (parent != null) { 245 if (parent.exists()) { 246 if (parent.canWrite() == false) { 247 errorMessage = org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2016") + parent.getPath(); 248 } 249 break; 250 } 251 252 parent = parent.getParentFile(); 253 } 254 } else { 255 if (file.isFile()) { 256 errorMessage = org.openide.util.NbBundle.getMessage(CheckoutWizard.class, "BK2017"); 257 } 258 } 259 } 260 261 if (errorMessage == null) { 262 valid(); 263 } else { 264 invalid(errorMessage); 265 } 266 267 return errorMessage == null; 268 } 269 270 public void changedUpdate(DocumentEvent e) { 271 } 272 273 public void insertUpdate(DocumentEvent e) { 274 validateUserInput(false); 275 } 276 277 public void removeUpdate(DocumentEvent e) { 278 validateUserInput(false); 279 } 280 281 public void focusGained(FocusEvent e) { 282 } 283 284 public void focusLost(FocusEvent e) { 285 validateUserInput(true); 286 } 287 288 public void actionPerformed(ActionEvent e) { 289 if (e.getSource() == modulePanel.moduleButton) { 290 ModuleSelector selector = new ModuleSelector(); 291 String rootString = repositoryStep.getCvsRoot(); 292 CVSRoot root = CVSRoot.parse(rootString); 293 Set modules = selector.selectModules(root); 294 StringBuffer buf = new StringBuffer (); 295 String separator = ""; Iterator it = modules.iterator(); 297 while (it.hasNext()) { 298 String module = (String ) it.next(); 299 buf.append(separator).append(module); 300 separator = ","; } 302 modulePanel.moduleTextField.setText(buf.toString()); 303 } else if (e.getSource() == modulePanel.tagButton) { 304 BranchSelector selector = new BranchSelector(); 305 String rootString = repositoryStep.getCvsRoot(); 306 CVSRoot root = CVSRoot.parse(rootString); 307 String s = modulePanel.moduleTextField.getText(); 308 if (s.trim().length() == 0) { 309 s = "."; } 311 String module = new StringTokenizer(s, ", ").nextToken(); String tag = selector.selectTag(root, module); 313 if (tag != null) { 314 modulePanel.tagTextField.setText(tag); 315 } 316 } else if (e.getSource() == modulePanel.workButton) { 317 318 File defaultDir = defaultWorkingDirectory(); 319 JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(CheckoutWizard.class, "ACSD_BrowseFolder"), defaultDir); 320 fileChooser.setDialogTitle(NbBundle.getMessage(CheckoutWizard.class, "BK0010")); 321 fileChooser.setMultiSelectionEnabled(false); 322 FileFilter [] old = fileChooser.getChoosableFileFilters(); 323 for (int i = 0; i < old.length; i++) { 324 FileFilter fileFilter = old[i]; 325 fileChooser.removeChoosableFileFilter(fileFilter); 326 327 } 328 fileChooser.addChoosableFileFilter(new FileFilter () { 329 public boolean accept(File f) { 330 return f.isDirectory(); 331 } 332 public String getDescription() { 333 return NbBundle.getMessage(CheckoutWizard.class, "BK0008"); 334 } 335 }); 336 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 337 fileChooser.showDialog(modulePanel, NbBundle.getMessage(CheckoutWizard.class, "BK0009")); 338 File f = fileChooser.getSelectedFile(); 339 if (f != null) { 340 modulePanel.workTextField.setText(f.getAbsolutePath()); 341 } 342 } else { 343 validateUserInput(true); 344 } 345 } 346 347 356 private File defaultWorkingDirectory() { 357 File defaultDir = null; 358 String current = modulePanel.workTextField.getText(); 359 if (current != null && !(current.trim().equals(""))) { File currentFile = new File (current); 361 while (currentFile != null && currentFile.exists() == false) { 362 currentFile = currentFile.getParentFile(); 363 } 364 if (currentFile != null) { 365 if (currentFile.isFile()) { 366 defaultDir = currentFile.getParentFile(); 367 } else { 368 defaultDir = currentFile; 369 } 370 } 371 } 372 373 if (defaultDir == null) { 374 List recent = Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), RECENT_DIRECTORY); 375 Iterator it = recent.iterator(); 376 377 while (it.hasNext()) { 378 String path = (String ) it.next(); 379 File file = new File (path); 380 File parent = file.getParentFile(); 381 if (parent != null && parent.exists() && parent.isDirectory()) { 382 defaultDir = file; 383 break; 384 } 385 } 386 } 387 388 if (defaultDir == null) { 389 File projectFolder = ProjectChooser.getProjectsFolder(); 390 if (projectFolder.exists() && projectFolder.isDirectory()) { 391 defaultDir = projectFolder; 392 } 393 } 394 395 if (defaultDir == null) { 396 defaultDir = new File (System.getProperty("user.home")); } 398 399 return defaultDir; 400 } 401 } 402 403 } 404 | Popular Tags |