1 19 package org.netbeans.modules.apisupport.paintapp; 20 21 import java.io.File ; 22 import javax.swing.JFileChooser ; 23 import javax.swing.JPanel ; 24 import javax.swing.event.DocumentEvent ; 25 import javax.swing.event.DocumentListener ; 26 import javax.swing.text.Document ; 27 import org.netbeans.spi.project.ui.support.ProjectChooser; 28 import org.openide.WizardDescriptor; 29 import org.openide.WizardValidationException; 30 import org.openide.filesystems.FileUtil; 31 32 public class PaintAppPanelVisual extends JPanel implements DocumentListener { 33 34 private static final String WIZARD_PANEL_ERROR_MESSAGE = "WizardPanel_errorMessage"; 36 public static final String PROP_PROJECT_NAME = "projectName"; 38 private PaintAppWizardPanel panel; 39 40 41 public PaintAppPanelVisual(PaintAppWizardPanel panel) { 42 initComponents(); 43 this.panel = panel; 44 projectNameTextField.getDocument().addDocumentListener(this); 46 projectLocationTextField.getDocument().addDocumentListener(this); 47 } 48 49 50 public String getProjectName() { 51 return this.projectNameTextField.getText(); 52 } 53 54 59 private void initComponents() { 61 projectNameLabel = new javax.swing.JLabel (); 62 projectNameTextField = new javax.swing.JTextField (); 63 projectLocationLabel = new javax.swing.JLabel (); 64 projectLocationTextField = new javax.swing.JTextField (); 65 browseButton = new javax.swing.JButton (); 66 createdFolderLabel = new javax.swing.JLabel (); 67 createdFolderTextField = new javax.swing.JTextField (); 68 69 projectNameLabel.setLabelFor(projectNameTextField); 70 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.project_name")); 72 projectLocationLabel.setLabelFor(projectLocationTextField); 73 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.project_location")); 75 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.browse")); browseButton.addActionListener(new java.awt.event.ActionListener () { 77 public void actionPerformed(java.awt.event.ActionEvent evt) { 78 browseButtonActionPerformed(evt); 79 } 80 }); 81 82 createdFolderLabel.setLabelFor(createdFolderTextField); 83 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.project_folder")); 85 createdFolderTextField.setEditable(false); 86 87 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 88 this.setLayout(layout); 89 layout.setHorizontalGroup( 90 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 91 .add(layout.createSequentialGroup() 92 .addContainerGap() 93 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 94 .add(projectNameLabel) 95 .add(projectLocationLabel) 96 .add(createdFolderLabel)) 97 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 98 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 99 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) 100 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) 101 .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)) 102 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 103 .add(browseButton) 104 .addContainerGap()) 105 ); 106 layout.setVerticalGroup( 107 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 108 .add(layout.createSequentialGroup() 109 .addContainerGap() 110 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 111 .add(projectNameLabel) 112 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 113 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 114 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 115 .add(projectLocationLabel) 116 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 117 .add(browseButton)) 118 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 119 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 120 .add(createdFolderLabel) 121 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 122 .addContainerGap(213, Short.MAX_VALUE)) 123 ); 124 } 126 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 128 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 129 chooser.setDialogTitle(org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.select_project_location")); 130 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 131 String path = this.projectLocationTextField.getText(); 132 if (path.length() > 0) { 133 File f = new File (path); 134 if (f.exists()) { 135 chooser.setSelectedFile(f); 136 } 137 } 138 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 139 File projectDir = chooser.getSelectedFile(); 140 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 141 } 142 panel.fireChangeEvent(); 143 144 } 146 147 private javax.swing.JButton browseButton; 149 private javax.swing.JLabel createdFolderLabel; 150 private javax.swing.JTextField createdFolderTextField; 151 private javax.swing.JLabel projectLocationLabel; 152 private javax.swing.JTextField projectLocationTextField; 153 private javax.swing.JLabel projectNameLabel; 154 private javax.swing.JTextField projectNameTextField; 155 157 public void addNotify() { 158 super.addNotify(); 159 projectNameTextField.requestFocus(); 161 } 162 163 boolean valid(WizardDescriptor wizardDescriptor) { 164 165 if (projectNameTextField.getText().length() == 0) { 166 wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, 167 org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.invalid_folder_name")); 168 return false; } 170 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText()).getAbsoluteFile()); 171 if (!f.isDirectory()) { 172 String message = org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.invalid_path"); 173 wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, message); 174 return false; 175 } 176 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 177 178 File projLoc = destFolder; 179 while (projLoc != null && !projLoc.exists()) { 180 projLoc = projLoc.getParentFile(); 181 } 182 if (projLoc == null || !projLoc.canWrite()) { 183 wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, 184 org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.folder_creation_error")); 185 return false; 186 } 187 188 if (FileUtil.toFileObject(projLoc) == null) { 189 String message = org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.invalid_path"); 190 wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, message); 191 return false; 192 } 193 194 File [] kids = destFolder.listFiles(); 195 if (destFolder.exists() && kids != null && kids.length > 0) { 196 wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, 198 org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.folder_exists")); 199 return false; 200 } 201 wizardDescriptor.putProperty(WIZARD_PANEL_ERROR_MESSAGE, ""); 202 return true; 203 } 204 205 void store(WizardDescriptor d) { 206 String name = projectNameTextField.getText().trim(); 207 String folder = createdFolderTextField.getText().trim(); 208 209 d.putProperty("projdir", new File (folder)); d.putProperty("name", name); } 212 213 void read(WizardDescriptor settings) { 214 File projectLocation = (File ) settings.getProperty("projdir"); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 216 projectLocation = ProjectChooser.getProjectsFolder(); 217 } else { 218 projectLocation = projectLocation.getParentFile(); 219 } 220 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 221 222 String projectName = (String ) settings.getProperty("name"); if(projectName == null) { 224 projectName = org.openide.util.NbBundle.getMessage(PaintAppPanelVisual.class, "PaintAppPanelVisual.default_prj_name"); 225 } 226 this.projectNameTextField.setText(projectName); 227 this.projectNameTextField.selectAll(); 228 } 229 230 void validate(WizardDescriptor d) throws WizardValidationException { 231 } 233 234 236 public void changedUpdate(DocumentEvent e) { 237 updateTexts(e); 238 if (this.projectNameTextField.getDocument() == e.getDocument()) { 239 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 240 } 241 } 242 243 public void insertUpdate(DocumentEvent e) { 244 updateTexts(e); 245 if (this.projectNameTextField.getDocument() == e.getDocument()) { 246 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 247 } 248 } 249 250 public void removeUpdate(DocumentEvent e) { 251 updateTexts(e); 252 if (this.projectNameTextField.getDocument() == e.getDocument()) { 253 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 254 } 255 } 256 257 258 private void updateTexts(DocumentEvent e) { 259 260 Document doc = e.getDocument(); 261 262 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 263 265 String projectName = projectNameTextField.getText(); 266 String projectFolder = projectLocationTextField.getText(); 267 268 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 270 272 } 273 panel.fireChangeEvent(); } 275 276 } 277 | Popular Tags |