1 19 package org.netbeans.bluej.wizard; 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 import org.openide.util.NbBundle; 32 33 public class EmptyBluejPanelVisual extends JPanel implements DocumentListener { 34 35 public static final String PROP_PROJECT_NAME = "projectName"; private static final String WIZ_PANEL_ERROR = "WizardPanel_errorMessage"; private EmptyBluejWizardPanel panel; 38 39 40 public EmptyBluejPanelVisual(EmptyBluejWizardPanel panel) { 41 initComponents(); 42 this.panel = panel; 43 projectNameTextField.getDocument().addDocumentListener(this); 45 projectLocationTextField.getDocument().addDocumentListener(this); 46 } 47 48 49 public String getProjectName() { 50 return this.projectNameTextField.getText(); 51 } 52 53 58 private void initComponents() { 60 projectNameLabel = new javax.swing.JLabel (); 61 projectNameTextField = new javax.swing.JTextField (); 62 projectLocationLabel = new javax.swing.JLabel (); 63 projectLocationTextField = new javax.swing.JTextField (); 64 browseButton = new javax.swing.JButton (); 65 createdFolderLabel = new javax.swing.JLabel (); 66 createdFolderTextField = new javax.swing.JTextField (); 67 68 projectNameLabel.setLabelFor(projectNameTextField); 69 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "projectNameLabel")); 70 71 projectLocationLabel.setLabelFor(projectLocationTextField); 72 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "projectLocationLabel")); 73 74 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(EmptyBluejPanelVisual.class, "browseButton")); 75 browseButton.setActionCommand("BROWSE"); 76 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(EmptyBluejPanelVisual.class, "createdFolderLabel")); 84 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) { String command = evt.getActionCommand(); 128 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 130 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 131 chooser.setDialogTitle(NbBundle.getMessage(EmptyBluejPanelVisual.class, "browse_dialog_title")); 132 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 133 String path = this.projectLocationTextField.getText(); 134 if (path.length() > 0) { 135 File f = new File (path); 136 if (f.exists()) { 137 chooser.setSelectedFile(f); 138 } 139 } 140 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 141 File projectDir = chooser.getSelectedFile(); 142 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); 143 } 144 panel.fireChangeEvent(); 145 } 146 147 } 149 150 private javax.swing.JButton browseButton; 152 private javax.swing.JLabel createdFolderLabel; 153 private javax.swing.JTextField createdFolderTextField; 154 private javax.swing.JLabel projectLocationLabel; 155 private javax.swing.JTextField projectLocationTextField; 156 private javax.swing.JLabel projectNameLabel; 157 private javax.swing.JTextField projectNameTextField; 158 160 public void addNotify() { 161 super.addNotify(); 162 projectNameTextField.requestFocus(); 164 } 165 166 167 void store(WizardDescriptor d) { 168 String name = projectNameTextField.getText().trim(); 169 String folder = createdFolderTextField.getText().trim(); 170 171 d.putProperty("projdir", new File (folder)); d.putProperty("name", name); } 174 175 void read(WizardDescriptor settings) { 176 File projectLocation = (File ) settings.getProperty("projdir"); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 178 projectLocation = ProjectChooser.getProjectsFolder(); 179 } else { 180 projectLocation = projectLocation.getParentFile(); 181 } 182 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 183 184 String projectName = (String ) settings.getProperty("name"); if(projectName == null) { 186 projectName = "EmptyBluej"; } 188 this.projectNameTextField.setText(projectName); 189 this.projectNameTextField.selectAll(); 190 } 191 192 void validate(WizardDescriptor d) throws WizardValidationException { 193 } 195 196 public boolean valid(WizardDescriptor wizardDescriptor) { 197 198 if (projectNameTextField.getText().length() == 0) { 199 wizardDescriptor.putProperty(WIZ_PANEL_ERROR, 200 NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_wrongName")); 201 return false; } 203 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText()).getAbsoluteFile()); 204 if (!f.isDirectory()) { 205 String message = NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_wrongFolder"); 206 wizardDescriptor.putProperty(WIZ_PANEL_ERROR, message); 207 return false; 208 } 209 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 210 211 File projLoc = destFolder; 212 while (projLoc != null && !projLoc.exists()) { 213 projLoc = projLoc.getParentFile(); 214 } 215 if (projLoc == null || !projLoc.canWrite()) { 216 wizardDescriptor.putProperty(WIZ_PANEL_ERROR, 217 NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_cannot_create_folder")); 218 return false; 219 } 220 221 if (FileUtil.toFileObject(projLoc) == null) { 222 String message = NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_wrong_path"); 223 wizardDescriptor.putProperty(WIZ_PANEL_ERROR, message); 224 return false; 225 } 226 227 File [] kids = destFolder.listFiles(); 228 if (destFolder.exists() && kids != null && kids.length > 0) { 229 wizardDescriptor.putProperty(WIZ_PANEL_ERROR, 231 NbBundle.getMessage(EmptyBluejPanelVisual.class, "ERROR_exists")); 232 return false; 233 } 234 wizardDescriptor.putProperty(WIZ_PANEL_ERROR, ""); 235 return true; 236 } 237 238 240 public void changedUpdate(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 insertUpdate(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 public void removeUpdate(DocumentEvent e) { 255 updateTexts(e); 256 if (this.projectNameTextField.getDocument() == e.getDocument()) { 257 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText()); 258 } 259 } 260 261 262 private void updateTexts(DocumentEvent e) { 263 264 Document doc = e.getDocument(); 265 266 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { 267 269 String projectName = projectNameTextField.getText(); 270 String projectFolder = projectLocationTextField.getText(); 271 272 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); 274 276 } 277 panel.fireChangeEvent(); } 279 280 } 281 | Popular Tags |