1 19 20 package org.netbeans.modules.ruby.rubyproject.ui.wizards; 21 22 import java.awt.Dialog ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.text.MessageFormat ; 26 import java.util.Enumeration ; 27 import javax.swing.JFileChooser ; 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.event.DocumentListener ; 30 import org.openide.WizardDescriptor; 31 import org.openide.WizardValidationException; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.NbBundle; 35 import org.netbeans.api.project.ProjectManager; 36 import org.netbeans.modules.ruby.rubyproject.ui.FoldersListSettings; 37 import org.netbeans.spi.project.ui.support.ProjectChooser; 38 39 40 44 public class PanelProjectLocationExtSrc extends SettingsPanel { 45 46 private PanelConfigureProject firer; 47 private WizardDescriptor wizardDescriptor; 48 private boolean calculatePF; 49 50 51 public PanelProjectLocationExtSrc (PanelConfigureProject panel) { 52 this.firer = panel; 53 initComponents(); 54 this.projectName.getDocument().addDocumentListener (new DocumentListener (){ 55 public void changedUpdate(DocumentEvent e) { 56 calculateProjectFolder (); 57 dataChanged (); 58 } 59 60 public void insertUpdate(DocumentEvent e) { 61 calculateProjectFolder (); 62 dataChanged (); 63 } 64 65 public void removeUpdate(DocumentEvent e) { 66 calculateProjectFolder (); 67 dataChanged (); 68 } 69 }); 70 this.projectLocation.getDocument().addDocumentListener(new DocumentListener () { 71 public void changedUpdate(DocumentEvent e) { 72 setCalculateProjectFolder (false); 73 dataChanged (); 74 } 75 76 public void insertUpdate(DocumentEvent e) { 77 setCalculateProjectFolder (false); 78 dataChanged (); 79 } 80 81 public void removeUpdate(DocumentEvent e) { 82 setCalculateProjectFolder (false); 83 dataChanged (); 84 } 85 }); 86 } 87 88 private synchronized void calculateProjectFolder () { 89 if (this.calculatePF) { 90 File f = ProjectChooser.getProjectsFolder(); 91 this.projectLocation.setText (f.getAbsolutePath() + File.separator + this.projectName.getText()); 92 this.calculatePF = true; 93 } 94 } 95 96 private synchronized void setCalculateProjectFolder (boolean value) { 97 this.calculatePF = value; 98 } 99 100 private void dataChanged () { 101 this.firer.fireChangeEvent(); 102 } 103 104 105 void read (WizardDescriptor settings) { 106 this.wizardDescriptor = settings; 107 String path = null; 108 String projectName = null; 109 File projectLocation = (File ) settings.getProperty ("projdir"); if (projectLocation == null) { 111 projectLocation = ProjectChooser.getProjectsFolder(); 112 int index = FoldersListSettings.getDefault().getNewProjectCount(); 113 String formater = NbBundle.getMessage(PanelSourceFolders.class,"TXT_JavaProject"); 114 File file; 115 do { 116 index++; 117 projectName = MessageFormat.format (formater, new Object []{new Integer (index)}); 118 file = new File (projectLocation, projectName); 119 } while (file.exists()); 120 settings.putProperty (NewRubyProjectWizardIterator.PROP_NAME_INDEX, new Integer (index)); 121 this.projectLocation.setText (projectLocation.getAbsolutePath()); 122 this.setCalculateProjectFolder(true); 123 } 124 else { 125 projectName = (String ) settings.getProperty ("name"); boolean tmpFlag = this.calculatePF; 127 this.projectLocation.setText (projectLocation.getAbsolutePath()); 128 this.setCalculateProjectFolder(tmpFlag); 129 } 130 this.projectName.setText (projectName); 131 this.projectName.selectAll(); 132 } 133 134 void store (WizardDescriptor settings) { 135 settings.putProperty ("name",this.projectName.getText()); File projectsDir = new File (this.projectLocation.getText()); 137 settings.putProperty ("projdir", projectsDir); } 139 140 boolean valid (WizardDescriptor settings) { 141 String result = checkValidity (this.projectName.getText(), this.projectLocation.getText()); 142 if (result == null) { 143 wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); return true; 145 } 146 else { 147 wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); return false; 149 } 150 } 151 152 static String checkValidity (final String projectName, final String projectLocation) { 153 if ( projectName.length() == 0 154 || projectName.indexOf('/') > 0 || projectName.indexOf('\\') > 0 || projectName.indexOf(':') > 0) { return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectName"); 159 } 160 161 File projLoc = new File (projectLocation).getAbsoluteFile(); 162 163 if (PanelProjectLocationVisual.getCanonicalFile(projLoc) == null) { 164 return NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 165 } 166 167 while (projLoc != null && !projLoc.exists()) { 168 projLoc = projLoc.getParentFile(); 169 } 170 if (projLoc == null || !projLoc.canWrite()) { 171 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_ProjectFolderReadOnly"); 172 } 173 174 File destFolder = FileUtil.normalizeFile(new File ( projectLocation )); 175 File [] kids = destFolder.listFiles(); 176 if ( destFolder.exists() && kids != null && kids.length > 0) { 177 String file = null; 178 for (int i=0; i< kids.length; i++) { 179 String childName = kids[i].getName(); 180 if ("nbproject".equals(childName)) { file = NbBundle.getMessage (PanelSourceFolders.class,"TXT_NetBeansProject"); 182 } 183 if (file != null) { 196 String format = NbBundle.getMessage (PanelSourceFolders.class,"MSG_ProjectFolderInvalid"); 197 return MessageFormat.format(format, new Object [] {file}); 198 } 199 } 200 } 201 202 if (destFolder.isDirectory()) { 204 FileObject destFO = FileUtil.toFileObject(destFolder); 205 assert destFO != null : "No FileObject for " + destFolder; 206 boolean clear = false; 207 try { 208 clear = ProjectManager.getDefault().findProject(destFO) == null; 209 } catch (IOException e) { 210 } 212 if (!clear) { 213 return NbBundle.getMessage(PanelSourceFolders.class, "MSG_ProjectFolderHasDeletedProject"); 214 } 215 } 216 return null; 217 } 218 219 void validate(WizardDescriptor settings) throws WizardValidationException { 220 } 221 222 227 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 229 230 jPanel2 = new javax.swing.JPanel (); 231 jLabel4 = new javax.swing.JLabel (); 232 jLabel5 = new javax.swing.JLabel (); 233 projectName = new javax.swing.JTextField (); 234 jLabel6 = new javax.swing.JLabel (); 235 projectLocation = new javax.swing.JTextField (); 236 jButton3 = new javax.swing.JButton (); 237 jPanel1 = new javax.swing.JPanel (); 238 239 setLayout(new java.awt.GridBagLayout ()); 240 241 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_PanelSourceFolders")); 242 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_PanelSourceFolders")); 243 jPanel2.setLayout(new java.awt.GridBagLayout ()); 244 245 jLabel4.setLabelFor(jPanel2); 246 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_ProjectNameAndLocationLabel")); 247 gridBagConstraints = new java.awt.GridBagConstraints (); 248 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 249 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 250 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 251 gridBagConstraints.weightx = 1.0; 252 jPanel2.add(jLabel4, gridBagConstraints); 253 jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jLabel4")); 254 jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jLabel4")); 255 256 jLabel5.setLabelFor(projectName); 257 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_ProjectName_Label")); 258 gridBagConstraints = new java.awt.GridBagConstraints (); 259 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 260 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 261 jPanel2.add(jLabel5, gridBagConstraints); 262 jLabel5.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectNameLabel")); 263 jLabel5.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectNameLabel")); 264 265 gridBagConstraints = new java.awt.GridBagConstraints (); 266 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 267 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 268 gridBagConstraints.weightx = 1.0; 269 gridBagConstraints.insets = new java.awt.Insets (12, 6, 0, 0); 270 jPanel2.add(projectName, gridBagConstraints); 271 272 jLabel6.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(PanelProjectLocationExtSrc.class).getString("LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0)); 273 jLabel6.setLabelFor(projectLocation); 274 org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); 275 gridBagConstraints = new java.awt.GridBagConstraints (); 276 gridBagConstraints.gridx = 0; 277 gridBagConstraints.gridy = 2; 278 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 279 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 280 jPanel2.add(jLabel6, gridBagConstraints); 281 jLabel6.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectLocationLabel")); 282 jLabel6.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectLocationLabel")); 283 284 gridBagConstraints = new java.awt.GridBagConstraints (); 285 gridBagConstraints.gridy = 2; 286 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 287 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 288 gridBagConstraints.weightx = 1.0; 289 gridBagConstraints.insets = new java.awt.Insets (12, 6, 0, 0); 290 jPanel2.add(projectLocation, gridBagConstraints); 291 292 org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_BrowseLocation_Button3")); 293 jButton3.addActionListener(new java.awt.event.ActionListener () { 294 public void actionPerformed(java.awt.event.ActionEvent evt) { 295 browseProjectLocation(evt); 296 } 297 }); 298 299 gridBagConstraints = new java.awt.GridBagConstraints (); 300 gridBagConstraints.gridy = 2; 301 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 302 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 303 gridBagConstraints.insets = new java.awt.Insets (12, 6, 0, 0); 304 jPanel2.add(jButton3, gridBagConstraints); 305 jButton3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_browseButton")); 306 jButton3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_browseButton")); 307 308 gridBagConstraints = new java.awt.GridBagConstraints (); 309 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 310 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 311 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 312 gridBagConstraints.weightx = 1.0; 313 add(jPanel2, gridBagConstraints); 314 315 jPanel1.setLayout(new java.awt.GridBagLayout ()); 316 317 gridBagConstraints = new java.awt.GridBagConstraints (); 318 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 319 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 320 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 321 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 322 gridBagConstraints.weightx = 1.0; 323 gridBagConstraints.weighty = 1.0; 324 add(jPanel1, gridBagConstraints); 325 jPanel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jPanel1")); 326 jPanel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jPanel1")); 327 328 } 330 private void browseProjectLocation(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 333 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 334 chooser.setDialogTitle(NbBundle.getMessage(PanelSourceFolders.class,"LBL_NWP1_SelectProjectLocation")); 335 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 336 String path = this.projectLocation.getText(); 337 if (path.length() > 0) { 338 File f = new File (path); 339 if (f.exists()) { 340 chooser.setSelectedFile (f); 341 } 342 } 343 if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) { 344 File file = chooser.getSelectedFile(); 345 if (file != null) { 346 this.projectLocation.setText (FileUtil.normalizeFile(file).getAbsolutePath()); 347 } 348 } 349 } 351 352 353 private javax.swing.JButton jButton3; 355 private javax.swing.JLabel jLabel4; 356 private javax.swing.JLabel jLabel5; 357 private javax.swing.JLabel jLabel6; 358 private javax.swing.JPanel jPanel1; 359 private javax.swing.JPanel jPanel2; 360 private javax.swing.JTextField projectLocation; 361 private javax.swing.JTextField projectName; 362 364 365 } 366 | Popular Tags |