1 19 20 package org.netbeans.modules.ant.freeform.samples; 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.spi.project.ui.support.ProjectChooser; 29 import org.openide.WizardDescriptor; 30 import org.openide.WizardValidationException; 31 import org.openide.filesystems.FileUtil; 32 33 public class FreeformPanelVisual extends JPanel implements DocumentListener { 34 35 public static final String PROP_PROJECT_NAME = "projectName"; 36 37 private FreeformWizardPanel panel; 38 private int type; 39 40 public FreeformPanelVisual(FreeformWizardPanel panel) { 41 initComponents(); 42 this.panel = panel; 43 this.type = type; 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 private void initComponents() { 56 projectNameLabel = new javax.swing.JLabel (); 57 projectNameTextField = new javax.swing.JTextField (); 58 projectLocationLabel = new javax.swing.JLabel (); 59 projectLocationTextField = new javax.swing.JTextField (); 60 browseButton = new javax.swing.JButton (); 61 createdFolderLabel = new javax.swing.JLabel (); 62 createdFolderTextField = new javax.swing.JTextField (); 63 64 projectNameLabel.setLabelFor(projectNameTextField); 65 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:"); 66 67 projectLocationLabel.setLabelFor(projectLocationTextField); 68 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:"); 69 70 org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse..."); 71 browseButton.setActionCommand("BROWSE"); 72 browseButton.addActionListener(new java.awt.event.ActionListener () { 73 public void actionPerformed(java.awt.event.ActionEvent evt) { 74 browseButtonActionPerformed(evt); 75 } 76 }); 77 78 createdFolderLabel.setLabelFor(createdFolderTextField); 79 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:"); 80 81 createdFolderTextField.setEditable(false); 82 83 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 84 this.setLayout(layout); 85 layout.setHorizontalGroup( 86 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 87 .add(layout.createSequentialGroup() 88 .addContainerGap() 89 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 90 .add(projectNameLabel) 91 .add(projectLocationLabel) 92 .add(createdFolderLabel)) 93 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 94 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 95 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) 96 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) 97 .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)) 98 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 99 .add(browseButton) 100 .addContainerGap()) 101 ); 102 layout.setVerticalGroup( 103 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 104 .add(layout.createSequentialGroup() 105 .addContainerGap() 106 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 107 .add(projectNameLabel) 108 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 109 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 110 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 111 .add(projectLocationLabel) 112 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 113 .add(browseButton)) 114 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 115 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 116 .add(createdFolderLabel) 117 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 118 .addContainerGap(213, Short.MAX_VALUE)) 119 ); 120 } 122 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 124 JFileChooser chooser = new JFileChooser (); 125 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 126 chooser.setDialogTitle("Select Project Location"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 128 String path = this.projectLocationTextField.getText(); 129 if (path.length() > 0) { 130 File f = new File (path); 131 if (f.exists()) { 132 chooser.setSelectedFile(f); 133 } 134 } 135 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 136 File projectDir = chooser.getSelectedFile(); 137 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 138 } 139 panel.fireChangeEvent(); 140 } 142 143 private javax.swing.JButton browseButton; 145 private javax.swing.JLabel createdFolderLabel; 146 private javax.swing.JTextField createdFolderTextField; 147 private javax.swing.JLabel projectLocationLabel; 148 private javax.swing.JTextField projectLocationTextField; 149 private javax.swing.JLabel projectNameLabel; 150 private javax.swing.JTextField projectNameTextField; 151 153 public void addNotify() { 154 super.addNotify(); 155 projectNameTextField.requestFocus(); 157 } 158 159 boolean valid(WizardDescriptor wizardDescriptor) { 160 162 if (projectNameTextField.getText().length() == 0) { 163 wizardDescriptor.putProperty("WizardPanel_errorMessage", 164 "Project Name is not a valid folder name."); 165 return false; } 167 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText()).getAbsoluteFile()); 168 if (!f.isDirectory()) { 169 String message = "Project Folder is not a valid path."; 170 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 171 return false; 172 } 173 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 174 175 File projLoc = destFolder; 176 while (projLoc != null && !projLoc.exists()) { 177 projLoc = projLoc.getParentFile(); 178 } 179 if (projLoc == null || !projLoc.canWrite()) { 180 wizardDescriptor.putProperty("WizardPanel_errorMessage", 181 "Project Folder cannot be created."); 182 return false; 183 } 184 185 if (FileUtil.toFileObject(projLoc) == null) { 186 String message = "Project Folder is not a valid path."; 187 wizardDescriptor.putProperty("WizardPanel_errorMessage", message); 188 return false; 189 } 190 191 File [] kids = destFolder.listFiles(); 192 if (destFolder.exists() && kids != null && kids.length > 0) { 193 wizardDescriptor.putProperty("WizardPanel_errorMessage", 195 "Project Folder already exists and is not empty."); 196 return false; 197 } 198 wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); 199 return true; 200 } 201 202 void store(WizardDescriptor d) { 203 String name = projectNameTextField.getText().trim(); 204 String folder = createdFolderTextField.getText().trim(); 205 206 d.putProperty("projdir", new File (folder)); 207 d.putProperty("name", name); 208 } 209 210 void read(WizardDescriptor settings) { 211 File projectLocation = (File ) settings.getProperty("projdir"); 212 if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 213 projectLocation = ProjectChooser.getProjectsFolder(); 214 } else { 215 projectLocation = projectLocation.getParentFile(); 216 } 217 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 218 219 String projectName = (String ) settings.getProperty("name"); 220 if (projectName == null) { 221 projectName = "freeform"; 222 } 223 this.projectNameTextField.setText(projectName); 224 this.projectNameTextField.selectAll(); 225 } 226 227 void validate(WizardDescriptor d) throws WizardValidationException { 228 } 230 231 233 public void changedUpdate(DocumentEvent e) { 234 updateTexts(e); 235 if (this.projectNameTextField.getDocument() == e.getDocument()) { 236 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 237 } 238 } 239 240 public void insertUpdate(DocumentEvent e) { 241 updateTexts(e); 242 if (this.projectNameTextField.getDocument() == e.getDocument()) { 243 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 244 } 245 } 246 247 public void removeUpdate(DocumentEvent e) { 248 updateTexts(e); 249 if (this.projectNameTextField.getDocument() == e.getDocument()) { 250 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 251 } 252 } 253 254 255 private void updateTexts(DocumentEvent e) { 256 257 Document doc = e.getDocument(); 258 259 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 260 262 String projectName = projectNameTextField.getText(); 263 String projectFolder = projectLocationTextField.getText(); 264 265 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 267 269 } 270 panel.fireChangeEvent(); } 272 273 } 274 | Popular Tags |