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