1 19 20 package org.netbeans.modules.ruby.railsprojects.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.railsprojects.ui.FoldersListSettings; 37 import org.netbeans.spi.project.ui.support.ProjectChooser; 38 import org.openide.util.Exceptions; 39 40 41 45 public class PanelProjectLocationExtSrc extends SettingsPanel { 46 47 private PanelConfigureProject firer; 48 private WizardDescriptor wizardDescriptor; 49 private boolean calculateName; 50 51 52 public PanelProjectLocationExtSrc (PanelConfigureProject panel) { 53 this.firer = panel; 54 initComponents(); 55 56 89 90 this.projectName.getDocument().addDocumentListener (new DocumentListener (){ 91 public void changedUpdate(DocumentEvent e) { 92 setCalculateProjectName (false); 93 dataChanged (); 94 } 95 96 public void insertUpdate(DocumentEvent e) { 97 setCalculateProjectName (false); 98 dataChanged (); 99 } 100 101 public void removeUpdate(DocumentEvent e) { 102 setCalculateProjectName (false); 103 dataChanged (); 104 } 105 }); 106 this.projectLocation.getDocument().addDocumentListener(new DocumentListener () { 107 public void changedUpdate(DocumentEvent e) { 108 calculateProjectName (); 109 dataChanged (); 110 } 111 112 public void insertUpdate(DocumentEvent e) { 113 calculateProjectName (); 114 dataChanged (); 115 } 116 117 public void removeUpdate(DocumentEvent e) { 118 calculateProjectName (); 119 dataChanged (); 120 } 121 }); 122 123 } 124 125 private synchronized void calculateProjectName () { 126 if (this.calculateName) { 127 String path = this.projectLocation.getText().trim(); 128 if (path.length() > 0) { 129 File f = new File (path); 130 try { 131 File g = f.getCanonicalFile(); 132 if (g != null) { 133 f = g; 134 } 135 } catch (IOException ioe) { 136 Exceptions.printStackTrace(ioe); 137 } 138 this.projectName.setText(f.getName()); 139 } 140 File f = new File (this.projectLocation.getText()); 141 this.calculateName = true; 142 } 143 } 144 145 private synchronized void setCalculateProjectName (boolean value) { 146 this.calculateName = value; 147 } 148 149 150 private void dataChanged () { 151 this.firer.fireChangeEvent(); 152 } 153 154 155 void read (WizardDescriptor settings) { 156 this.wizardDescriptor = settings; 157 String path = null; 158 String projectName = null; 159 File projectLocation = (File ) settings.getProperty ("projdir"); if (projectLocation == null) { 161 projectLocation = ProjectChooser.getProjectsFolder(); 162 this.projectLocation.setText (projectLocation.getAbsolutePath()); 172 this.setCalculateProjectName(true); 173 this.calculateProjectName(); 174 this.setCalculateProjectName(true); 175 projectName = this.projectName.getText(); 176 } 177 else { 178 projectName = (String ) settings.getProperty ("name"); boolean tmpFlag = this.calculateName; 180 this.projectLocation.setText (projectLocation.getAbsolutePath()); 181 this.setCalculateProjectName(tmpFlag); 182 } 183 this.projectLocation.selectAll(); 186 } 187 188 void store (WizardDescriptor settings) { 189 settings.putProperty ("name",this.projectName.getText()); File projectsDir = new File (this.projectLocation.getText()); 191 settings.putProperty ("projdir", projectsDir); } 193 194 boolean valid (WizardDescriptor settings) { 195 String result = checkValidity (this.projectName.getText(), this.projectLocation.getText()); 196 if (result == null) { 197 wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); return true; 199 } 200 else { 201 wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); return false; 203 } 204 } 205 206 static String checkValidity (final String projectName, final String projectLocation) { 207 if ( projectName.length() == 0 208 || projectName.indexOf('/') > 0 || projectName.indexOf('\\') > 0 || projectName.indexOf(':') > 0) { return NbBundle.getMessage(PanelProjectLocationExtSrc.class,"MSG_IllegalProjectName"); 213 } 214 215 File projLoc = new File (projectLocation).getAbsoluteFile(); 216 217 if (PanelProjectLocationVisual.getCanonicalFile(projLoc) == null) { 218 return NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation"); 219 } 220 221 while (projLoc != null && !projLoc.exists()) { 222 projLoc = projLoc.getParentFile(); 223 } 224 if (projLoc == null || !projLoc.canWrite()) { 225 return NbBundle.getMessage(PanelProjectLocationExtSrc.class,"MSG_ProjectFolderReadOnly"); 226 } 227 228 File destFolder = FileUtil.normalizeFile(new File ( projectLocation )); 229 File [] kids = destFolder.listFiles(); 230 boolean foundRails = false; 231 if ( destFolder.exists() && kids != null && kids.length > 0) { 232 String file = null; 233 for (int i=0; i< kids.length; i++) { 234 String childName = kids[i].getName(); 235 if ("nbproject".equals(childName)) { file = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"TXT_NetBeansProject"); 237 } 238 239 else if ("config".equals(childName)) { 240 foundRails = true; 241 } 242 256 257 258 if (file != null) { 259 String format = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"MSG_ProjectFolderInvalid"); 260 return MessageFormat.format(format, new Object [] {file}); 261 } 262 } 263 } 264 265 if (!foundRails) { 266 return NbBundle.getMessage(PanelProjectLocationExtSrc.class, "TXT_NoRails"); 267 } 268 269 return null; 285 } 286 287 void validate(WizardDescriptor settings) throws WizardValidationException { 288 } 289 290 295 private void initComponents() { 297 java.awt.GridBagConstraints gridBagConstraints; 298 299 jPanel2 = new javax.swing.JPanel (); 300 jLabel4 = new javax.swing.JLabel (); 301 jLabel5 = new javax.swing.JLabel (); 302 projectName = new javax.swing.JTextField (); 303 jLabel6 = new javax.swing.JLabel (); 304 projectLocation = new javax.swing.JTextField (); 305 jButton3 = new javax.swing.JButton (); 306 jPanel1 = new javax.swing.JPanel (); 307 308 setLayout(new java.awt.GridBagLayout ()); 309 310 jLabel4.setLabelFor(jPanel2); 311 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_ProjectNameAndLocationLabel")); 313 jLabel5.setLabelFor(projectName); 314 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_ProjectName_Label")); 316 jLabel6.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(PanelProjectLocationExtSrc.class).getString("LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0)); 317 jLabel6.setLabelFor(projectLocation); 318 org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); 320 org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_BrowseLocation_Button3")); jButton3.addActionListener(new java.awt.event.ActionListener () { 322 public void actionPerformed(java.awt.event.ActionEvent evt) { 323 browseProjectLocation(evt); 324 } 325 }); 326 327 org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2); 328 jPanel2.setLayout(jPanel2Layout); 329 jPanel2Layout.setHorizontalGroup( 330 jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 331 .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 400, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 332 .add(jPanel2Layout.createSequentialGroup() 333 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 334 .add(jLabel6) 335 .add(jLabel5)) 336 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 337 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 338 .add(projectName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE) 339 .add(projectLocation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)) 340 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 341 .add(jButton3)) 342 ); 343 jPanel2Layout.setVerticalGroup( 344 jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 345 .add(jPanel2Layout.createSequentialGroup() 346 .add(jLabel4) 347 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 348 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 349 .add(jLabel6) 350 .add(jButton3) 351 .add(projectLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 352 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 353 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 354 .add(jLabel5) 355 .add(projectName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 356 .addContainerGap(40, Short.MAX_VALUE)) 357 ); 358 359 jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jLabel4")); jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jLabel4")); jLabel5.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectNameLabel")); jLabel5.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectNameLabel")); jLabel6.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectLocationLabel")); jLabel6.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectLocationLabel")); jButton3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_browseButton")); jButton3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_browseButton")); 368 gridBagConstraints = new java.awt.GridBagConstraints (); 369 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 370 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 371 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 372 gridBagConstraints.weightx = 1.0; 373 add(jPanel2, gridBagConstraints); 374 375 jPanel1.setLayout(new java.awt.GridBagLayout ()); 376 gridBagConstraints = new java.awt.GridBagConstraints (); 377 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 378 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 379 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 380 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; 381 gridBagConstraints.weightx = 1.0; 382 gridBagConstraints.weighty = 1.0; 383 add(jPanel1, gridBagConstraints); 384 jPanel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jPanel1")); jPanel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jPanel1")); 387 getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_PanelSourceFolders")); getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_PanelSourceFolders")); } 391 private void browseProjectLocation(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 394 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 395 chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationExtSrc.class,"LBL_NWP1_SelectProjectLocation")); 396 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 397 String path = this.projectLocation.getText(); 398 if (path.length() > 0) { 399 File f = new File (path); 400 if (f.exists()) { 401 chooser.setSelectedFile (f); 402 } 403 } 404 if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) { 405 File file = chooser.getSelectedFile(); 406 if (file != null) { 407 this.projectLocation.setText (FileUtil.normalizeFile(file).getAbsolutePath()); 408 } 409 } 410 } 412 413 414 private javax.swing.JButton jButton3; 416 private javax.swing.JLabel jLabel4; 417 private javax.swing.JLabel jLabel5; 418 private javax.swing.JLabel jLabel6; 419 private javax.swing.JPanel jPanel1; 420 private javax.swing.JPanel jPanel2; 421 private javax.swing.JTextField projectLocation; 422 private javax.swing.JTextField projectName; 423 425 426 } 427 | Popular Tags |