1 19 20 package org.netbeans.modules.web.examples; 21 22 import java.io.File ; 23 import java.text.MessageFormat ; 24 25 import javax.swing.JFileChooser ; 26 import javax.swing.event.DocumentEvent ; 27 import javax.swing.event.DocumentListener ; 28 29 import org.netbeans.spi.project.ui.support.ProjectChooser; 30 31 import org.openide.WizardDescriptor; 32 import org.openide.util.NbBundle; 33 34 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener { 35 36 private PanelConfigureProject panel; 37 38 39 public PanelProjectLocationVisual(PanelConfigureProject panel) { 40 initComponents(); 41 this.panel = panel; 42 44 projectNameTextField.getDocument().addDocumentListener(this); 46 projectLocationTextField.getDocument().addDocumentListener(this); 47 } 48 49 54 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 56 57 projectNameLabel = new javax.swing.JLabel (); 58 projectNameTextField = new javax.swing.JTextField (); 59 projectLocationLabel = new javax.swing.JLabel (); 60 projectLocationTextField = new javax.swing.JTextField (); 61 Button = new javax.swing.JButton (); 62 createdFolderLabel = new javax.swing.JLabel (); 63 createdFolderTextField = new javax.swing.JTextField (); 64 65 setLayout(new java.awt.GridBagLayout ()); 66 67 projectNameLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_LabelMnemonic").charAt(0)); 68 projectNameLabel.setLabelFor(projectNameTextField); 69 projectNameLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label")); 70 gridBagConstraints = new java.awt.GridBagConstraints (); 71 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 72 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 73 add(projectNameLabel, gridBagConstraints); 74 75 gridBagConstraints = new java.awt.GridBagConstraints (); 76 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 77 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 78 gridBagConstraints.weightx = 1.0; 79 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 80 add(projectNameTextField, gridBagConstraints); 81 projectNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_ProjectName_A11YDesc")); 82 83 projectLocationLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_LabelMnemonic").charAt(0)); 84 projectLocationLabel.setLabelFor(projectLocationTextField); 85 projectLocationLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label")); 86 gridBagConstraints = new java.awt.GridBagConstraints (); 87 gridBagConstraints.gridy = 1; 88 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 89 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 90 add(projectLocationLabel, gridBagConstraints); 91 92 gridBagConstraints = new java.awt.GridBagConstraints (); 93 gridBagConstraints.gridy = 1; 94 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 95 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 96 gridBagConstraints.weightx = 1.0; 97 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 98 add(projectLocationTextField, gridBagConstraints); 99 projectLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NPW1_ProjectLocation_A11YDesc")); 100 101 Button.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); 102 Button.setActionCommand("BROWSE"); 103 Button.addActionListener(new java.awt.event.ActionListener () { 104 public void actionPerformed(java.awt.event.ActionEvent evt) { 105 browseLocationAction(evt); 106 } 107 }); 108 109 gridBagConstraints = new java.awt.GridBagConstraints (); 110 gridBagConstraints.gridy = 1; 111 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 112 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 113 add(Button, gridBagConstraints); 114 Button.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_BrowseLocation_A11YDesc")); 115 116 createdFolderLabel.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0)); 117 createdFolderLabel.setLabelFor(createdFolderTextField); 118 createdFolderLabel.setText(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); 119 gridBagConstraints = new java.awt.GridBagConstraints (); 120 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 121 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 122 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 123 add(createdFolderLabel, gridBagConstraints); 124 125 createdFolderTextField.setEditable(false); 126 gridBagConstraints = new java.awt.GridBagConstraints (); 127 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 128 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 129 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 130 gridBagConstraints.weightx = 1.0; 131 gridBagConstraints.weighty = 1.0; 132 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 133 add(createdFolderTextField, gridBagConstraints); 134 createdFolderTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_CreatedProjectFolder_A11YDesc")); 135 136 } 138 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 140 141 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 143 chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 145 String path = projectLocationTextField.getText(); 146 if (path.length() > 0) { 147 File f = new File (path); 148 if (f.exists()) 149 chooser.setSelectedFile(f); 150 } 151 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 152 File projectDir = chooser.getSelectedFile(); 153 projectLocationTextField.setText(projectDir.getAbsolutePath()); 154 } 155 panel.fireChangeEvent(); 156 } 157 } 159 public void addNotify() { 160 super.addNotify(); 161 projectLocationTextField.requestFocus(); 163 } 164 165 boolean valid(WizardDescriptor wizardDescriptor) { 166 if (projectNameTextField.getText().length() == 0) { 167 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); return false; } 170 171 File destFolder = new File (createdFolderTextField.getText()); 172 File [] children = destFolder.listFiles(); 173 if (destFolder.exists() && children != null && children.length > 0) { 174 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); return false; 177 } 178 179 wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); return true; 181 } 182 183 void store(WizardDescriptor d) { 184 String name = projectNameTextField.getText().trim(); 185 186 d.putProperty(WizardProperties.PROJECT_DIR, new File (createdFolderTextField.getText().trim())); 187 d.putProperty(WizardProperties.NAME, name); 188 189 File projectsDir = new File (this.projectLocationTextField.getText()); 190 if (projectsDir.isDirectory()) { 191 ProjectChooser.setProjectsFolder (projectsDir); 192 } 193 } 194 195 void read (WizardDescriptor settings) { 196 File projectLocation = (File ) settings.getProperty(WizardProperties.PROJECT_DIR); 197 if (projectLocation == null) 198 projectLocation = ProjectChooser.getProjectsFolder(); 199 else 200 projectLocation = projectLocation.getParentFile(); 201 202 projectLocationTextField.setText(projectLocation.getAbsolutePath()); 203 projectLocationTextField.selectAll(); 204 205 String formater = null; 206 String projectName = (String ) settings.getProperty(WizardProperties.NAME); 207 208 if (projectName == null) { 209 formater = NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NPW1_DefaultProjectName"); } else { 211 formater = projectName + "{0}"; } 213 if ((projectName == null) || (validFreeProjectName(projectLocation, projectName) == null)) { 214 int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1; 215 while ((projectName = validFreeProjectName(projectLocation, formater, baseCount)) == null) 216 baseCount++; 217 } 219 projectNameTextField.setText(projectName); 220 projectNameTextField.selectAll(); 221 } 222 223 private javax.swing.JButton Button; 225 private javax.swing.JLabel createdFolderLabel; 226 private javax.swing.JTextField createdFolderTextField; 227 private javax.swing.JLabel projectLocationLabel; 228 private javax.swing.JTextField projectLocationTextField; 229 private javax.swing.JLabel projectNameLabel; 230 protected javax.swing.JTextField projectNameTextField; 231 233 private static JFileChooser createChooser() { 234 JFileChooser chooser = new JFileChooser (); 235 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 236 chooser.setAcceptAllFileFilterUsed(false); 237 238 return chooser; 239 } 240 241 private String validFreeProjectName(final File parentFolder, final String formater, final int index) { 242 String name = MessageFormat.format(formater, new Object [] {new Integer (index)}); 243 File file = new File (parentFolder, name); 244 return file.exists() ? null : name; 245 } 246 247 private String validFreeProjectName(final File parentFolder, final String name) { 248 File file = new File (parentFolder, name); 249 return file.exists() ? null : name; 250 } 251 252 public void changedUpdate(DocumentEvent e) { 254 updateTexts(e); 255 } 256 257 public void insertUpdate(DocumentEvent e) { 258 updateTexts(e); 259 } 260 261 public void removeUpdate(DocumentEvent e) { 262 updateTexts(e); 263 } 264 266 267 269 private void updateTexts(DocumentEvent e) { 270 createdFolderTextField.setText(getCreatedFolderPath()); 271 272 panel.fireChangeEvent(); } 274 275 private String getCreatedFolderPath() { 276 StringBuffer folder = new StringBuffer (projectLocationTextField.getText().trim()); 277 if (!projectLocationTextField.getText().endsWith(File.separator)) 278 folder.append(File.separatorChar); 279 folder.append(projectNameTextField.getText().trim()); 280 281 return folder.toString(); 282 } 283 284 } 285 286 | Popular Tags |