1 19 20 package org.netbeans.modules.scripting.php.webproject.wizard; 21 22 import java.io.File ; 23 import javax.swing.JFileChooser ; 24 import javax.swing.JPanel ; 25 import javax.swing.event.DocumentEvent ; 26 import javax.swing.event.DocumentListener ; 27 import javax.swing.text.Document ; 28 import org.netbeans.modules.scripting.php.webproject.PhpProjectProperties; 29 import org.netbeans.spi.project.ui.support.ProjectChooser; 30 import org.openide.WizardDescriptor; 31 import org.openide.WizardValidationException; 32 import org.openide.filesystems.FileUtil; 33 34 public class PHPWebProjectPanelVisual extends JPanel implements DocumentListener { 35 36 public static final String PROP_PROJECT_NAME = "projectName"; 38 private static final String DEFAULT_HTTP_PREFIX = "http://localhost/"; 40 private PHPWebProjectWizardPanel panel; 41 private String oldProjectName; 42 43 public PHPWebProjectPanelVisual(PHPWebProjectWizardPanel panel) { 44 initComponents(); 45 this.panel = panel; 46 projectNameTextField.getDocument().addDocumentListener(this); 48 projectLocationTextField.getDocument().addDocumentListener(this); 49 serverUsesSourceCheckBox.setSelected(true); 50 targetDirTextField.setEnabled(!serverUsesSourceCheckBox.isSelected()); 51 welcomePageTextField.setText(PhpProjectProperties.DEFAULT_START_PAGE); 52 } 53 54 public String getProjectName() { 55 return this.projectNameTextField.getText(); 56 } 57 58 63 private void initComponents() { 65 66 projectNameLabel = new javax.swing.JLabel (); 67 projectNameTextField = new javax.swing.JTextField (); 68 projectLocationLabel = new javax.swing.JLabel (); 69 projectLocationTextField = new javax.swing.JTextField (); 70 browseButton = new javax.swing.JButton (); 71 createdFolderLabel = new javax.swing.JLabel (); 72 createdFolderTextField = new javax.swing.JTextField (); 73 welcomePageLabel = new javax.swing.JLabel (); 74 welcomePageTextField = new javax.swing.JTextField (); 75 serverUsesSourceCheckBox = new javax.swing.JCheckBox (); 76 targetDirLabel = new javax.swing.JLabel (); 77 targetDirTextField = new javax.swing.JTextField (); 78 urlLabel = new javax.swing.JLabel (); 79 urlTextField = new javax.swing.JTextField (); 80 81 projectNameLabel.setLabelFor(projectNameTextField); 82 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectName")); 84 projectNameTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_ProjectName")); 86 projectLocationLabel.setLabelFor(projectLocationTextField); 87 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectLocation")); 89 projectLocationTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_ProjectLocation")); 91 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectLocationBrowse")); browseButton.setActionCommand("BROWSE"); 93 browseButton.addActionListener(new java.awt.event.ActionListener () { 94 public void actionPerformed(java.awt.event.ActionEvent evt) { 95 browseButtonActionPerformed(evt); 96 } 97 }); 98 99 createdFolderLabel.setLabelFor(createdFolderTextField); 100 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectSrcFolder")); 102 createdFolderTextField.setEditable(false); 103 104 welcomePageLabel.setLabelFor(welcomePageTextField); 105 org.openide.awt.Mnemonics.setLocalizedText(welcomePageLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_welcomePage")); 107 welcomePageTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_welcomePage")); 109 org.openide.awt.Mnemonics.setLocalizedText(serverUsesSourceCheckBox, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_serverUsesSourceDir")); serverUsesSourceCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 111 serverUsesSourceCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 112 serverUsesSourceCheckBox.addActionListener(new java.awt.event.ActionListener () { 113 public void actionPerformed(java.awt.event.ActionEvent evt) { 114 serverUsesSourceCheckBoxActionPerformed(evt); 115 } 116 }); 117 118 targetDirLabel.setLabelFor(targetDirTextField); 119 org.openide.awt.Mnemonics.setLocalizedText(targetDirLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_targetDir")); 121 targetDirTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_targetDir")); 123 urlLabel.setLabelFor(urlTextField); 124 org.openide.awt.Mnemonics.setLocalizedText(urlLabel, org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_url")); 126 urlTextField.setToolTipText(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "TT_url")); 128 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 129 this.setLayout(layout); 130 layout.setHorizontalGroup( 131 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 132 .add(layout.createSequentialGroup() 133 .addContainerGap() 134 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 135 .add(layout.createSequentialGroup() 136 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 137 .add(projectNameLabel) 138 .add(projectLocationLabel) 139 .add(createdFolderLabel) 140 .add(welcomePageLabel) 141 .add(targetDirLabel) 142 .add(urlLabel)) 143 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 144 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 145 .add(targetDirTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE) 146 .add(org.jdesktop.layout.GroupLayout.LEADING, welcomePageTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE) 147 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE) 148 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE) 149 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE) 150 .add(urlTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)) 151 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 152 .add(browseButton)) 153 .add(serverUsesSourceCheckBox)) 154 .addContainerGap()) 155 ); 156 layout.setVerticalGroup( 157 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 158 .add(layout.createSequentialGroup() 159 .addContainerGap() 160 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 161 .add(projectNameLabel) 162 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 163 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 164 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 165 .add(projectLocationLabel) 166 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 167 .add(browseButton)) 168 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 169 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 170 .add(createdFolderLabel) 171 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 172 .add(10, 10, 10) 173 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 174 .add(welcomePageLabel) 175 .add(welcomePageTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 176 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 177 .add(serverUsesSourceCheckBox) 178 .add(15, 15, 15) 179 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 180 .add(targetDirLabel) 181 .add(targetDirTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 182 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 183 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 184 .add(urlLabel) 185 .add(urlTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 186 .addContainerGap(104, Short.MAX_VALUE)) 187 ); 188 189 browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PHPWebProjectPanelVisual.class, "LBL_ProjectLocationBrowse")); } 192 private void serverUsesSourceCheckBoxActionPerformed(java.awt.event.ActionEvent evt) { targetDirTextField.setEnabled(!serverUsesSourceCheckBox.isSelected()); 194 } 196 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 198 if ("BROWSE".equals(command)) { 199 JFileChooser chooser = new JFileChooser (); 200 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 201 chooser.setDialogTitle("Select Project Location"); 202 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 203 String path = this.projectLocationTextField.getText(); 204 if (path.length() > 0) { 205 File f = new File (path); 206 if (f.exists()) { 207 chooser.setSelectedFile(f); 208 } 209 } 210 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 211 File projectDir = chooser.getSelectedFile(); 212 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 213 } 214 panel.fireChangeEvent(); 215 } 216 217 } 219 220 private javax.swing.JButton browseButton; 222 private javax.swing.JLabel createdFolderLabel; 223 private javax.swing.JTextField createdFolderTextField; 224 private javax.swing.JLabel projectLocationLabel; 225 private javax.swing.JTextField projectLocationTextField; 226 private javax.swing.JLabel projectNameLabel; 227 private javax.swing.JTextField projectNameTextField; 228 private javax.swing.JCheckBox serverUsesSourceCheckBox; 229 private javax.swing.JLabel targetDirLabel; 230 private javax.swing.JTextField targetDirTextField; 231 private javax.swing.JLabel urlLabel; 232 private javax.swing.JTextField urlTextField; 233 private javax.swing.JLabel welcomePageLabel; 234 private javax.swing.JTextField welcomePageTextField; 235 237 public void addNotify() { 238 super.addNotify(); 239 projectNameTextField.requestFocus(); 241 } 242 243 boolean valid(WizardDescriptor d) { 244 245 if (projectNameTextField.getText().length() == 0) { 246 d.putProperty("WizardPanel_errorMessage", 247 "Project Name is not a valid folder name."); 248 return false; } 250 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText()).getAbsoluteFile()); 251 if (!f.isDirectory()) { 252 String message = "Project Folder is not a valid path."; 253 d.putProperty("WizardPanel_errorMessage", message); 254 return false; 255 } 256 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 257 258 File projLoc = destFolder; 259 while (projLoc != null && !projLoc.exists()) { 260 projLoc = projLoc.getParentFile(); 261 } 262 if (projLoc == null || !projLoc.canWrite()) { 263 d.putProperty("WizardPanel_errorMessage", 264 "Project Folder cannot be created."); 265 return false; 266 } 267 268 if (FileUtil.toFileObject(projLoc) == null) { 269 String message = "Project Folder is not a valid path."; 270 d.putProperty("WizardPanel_errorMessage", message); 271 return false; 272 } 273 274 File [] kids = destFolder.listFiles(); 275 if (destFolder.exists() && kids != null && kids.length > 0) { 276 d.putProperty("WizardPanel_errorMessage", 278 "Project Folder already exists and is not empty."); 279 return false; 280 } 281 d.putProperty("WizardPanel_errorMessage", ""); 282 283 return true; 284 } 285 286 void store(WizardDescriptor d) { 287 String name = projectNameTextField.getText().trim(); 288 String folder = createdFolderTextField.getText().trim(); 289 290 d.putProperty(PhpProjectProperties.PROJECT_DIR, new File (folder)); 291 d.putProperty(PhpProjectProperties.NAME, name); 292 d.putProperty(PhpProjectProperties.WELCOME_PAGE, welcomePageTextField.getText().trim()); 293 d.putProperty(PhpProjectProperties.USE_SOURCE_DIR, Boolean.valueOf(serverUsesSourceCheckBox.isSelected())); 294 d.putProperty(PhpProjectProperties.TARGET_DIR, targetDirTextField.getText().trim()); 295 d.putProperty(PhpProjectProperties.URL, urlTextField.getText().trim()); 296 } 297 298 void read(WizardDescriptor settings) { 299 File projectLocation = (File ) settings.getProperty(PhpProjectProperties.PROJECT_DIR); 300 if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 301 projectLocation = ProjectChooser.getProjectsFolder(); 302 } else { 303 projectLocation = projectLocation.getParentFile(); 304 } 305 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 306 307 String projectName = (String ) settings.getProperty(PhpProjectProperties.NAME); 308 if(projectName == null) { 309 projectName = "PHPWebProject"; 310 } 311 this.projectNameTextField.setText(projectName); 312 this.projectNameTextField.selectAll(); 313 } 314 315 void validate(WizardDescriptor d) throws WizardValidationException { 316 } 318 319 321 public void changedUpdate(DocumentEvent e) { 322 updateTexts(e); 323 if (this.projectNameTextField.getDocument() == e.getDocument()) { 324 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 325 } 326 } 327 328 public void insertUpdate(DocumentEvent e) { 329 updateTexts(e); 330 if (this.projectNameTextField.getDocument() == e.getDocument()) { 331 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 332 } 333 } 334 335 public void removeUpdate(DocumentEvent e) { 336 updateTexts(e); 337 if (this.projectNameTextField.getDocument() == e.getDocument()) { 338 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 339 } 340 } 341 342 343 private void updateTexts(DocumentEvent e) { 344 345 Document doc = e.getDocument(); 346 347 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 348 350 String projectName = projectNameTextField.getText(); 351 String projectFolder = projectLocationTextField.getText(); 352 353 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 354 355 if (oldProjectName == null || oldProjectName.length() == 0) { 356 urlTextField.setText(DEFAULT_HTTP_PREFIX + projectName); 357 targetDirTextField.setText(File.separator + projectName); 358 } 359 else if (!oldProjectName.equals(projectName)) { 360 urlTextField.setText(urlTextField.getText().replaceAll(oldProjectName, projectName)); 361 targetDirTextField.setText(targetDirTextField.getText().replaceAll(oldProjectName, projectName)); 362 } 363 364 oldProjectName = projectName; 365 } 366 panel.fireChangeEvent(); } 368 369 } 370 | Popular Tags |