1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.ui.wizards; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 26 import javax.swing.JFileChooser ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 29 import org.netbeans.modules.j2ee.ejbjarproject.ui.FoldersListSettings; 30 31 import org.netbeans.spi.project.ui.support.ProjectChooser; 32 33 import org.openide.WizardDescriptor; 34 import org.openide.WizardValidationException; 35 import org.openide.util.NbBundle; 36 37 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener { 38 39 private PanelConfigureProject panel; 40 41 42 public PanelProjectLocationVisual(PanelConfigureProject panel) { 43 initComponents(); 44 this.panel = panel; 45 46 projectNameTextField.getDocument().addDocumentListener(this); 48 projectLocationTextField.getDocument().addDocumentListener(this); 49 } 50 51 56 private void initComponents() { 58 java.awt.GridBagConstraints gridBagConstraints; 59 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 Button = new javax.swing.JButton (); 65 createdFolderLabel = new javax.swing.JLabel (); 66 createdFolderTextField = new javax.swing.JTextField (); 67 68 setLayout(new java.awt.GridBagLayout ()); 69 70 projectNameLabel.setLabelFor(projectNameTextField); 71 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_ProjectName_Label")); 72 gridBagConstraints = new java.awt.GridBagConstraints (); 73 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 74 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 75 add(projectNameLabel, gridBagConstraints); 76 77 gridBagConstraints = new java.awt.GridBagConstraints (); 78 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 79 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 80 gridBagConstraints.weightx = 1.0; 81 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 82 add(projectNameTextField, gridBagConstraints); 83 projectNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_ProjectName_A11YDesc")); 84 85 projectLocationLabel.setLabelFor(projectLocationTextField); 86 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_ProjectLocation_Label")); 87 gridBagConstraints = new java.awt.GridBagConstraints (); 88 gridBagConstraints.gridy = 1; 89 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 90 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 91 add(projectLocationLabel, gridBagConstraints); 92 93 gridBagConstraints = new java.awt.GridBagConstraints (); 94 gridBagConstraints.gridy = 1; 95 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 96 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 97 gridBagConstraints.weightx = 1.0; 98 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 99 add(projectLocationTextField, gridBagConstraints); 100 projectLocationTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NPW1_ProjectLocation_A11YDesc")); 101 102 org.openide.awt.Mnemonics.setLocalizedText(Button, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_BrowseLocation_Button")); 103 Button.setActionCommand("BROWSE"); 104 Button.addActionListener(new java.awt.event.ActionListener () { 105 public void actionPerformed(java.awt.event.ActionEvent evt) { 106 browseLocationAction(evt); 107 } 108 }); 109 110 gridBagConstraints = new java.awt.GridBagConstraints (); 111 gridBagConstraints.gridy = 1; 112 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 113 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 114 add(Button, gridBagConstraints); 115 Button.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACS_LBL_NWP1_BrowseLocation_A11YDesc")); 116 117 createdFolderLabel.setLabelFor(createdFolderTextField); 118 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("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 } 137 139 private void browseLocationAction(java.awt.event.ActionEvent evt) { String command = evt.getActionCommand(); 141 142 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 144 chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation")); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 146 String path = projectLocationTextField.getText(); 147 if (path.length() > 0) { 148 File f = new File (path); 149 if (f.exists()) 150 chooser.setSelectedFile(f); 151 } 152 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 153 File projectDir = chooser.getSelectedFile(); 154 projectLocationTextField.setText(projectDir.getAbsolutePath()); 155 } 156 panel.fireChangeEvent(); 157 } 158 } 160 public void addNotify() { 161 super.addNotify(); 162 projectNameTextField.requestFocus(); 164 } 165 166 boolean valid(WizardDescriptor wizardDescriptor) { 167 if (projectNameTextField.getText().length() == 0) { 168 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName")); return false; } 171 172 File destFolder = new File (createdFolderTextField.getText()); 173 File [] children = destFolder.listFiles(); 174 if (destFolder.exists() && children != null && children.length > 0) { 175 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists")); return false; 178 } 179 180 wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); return true; 182 } 183 184 void store(WizardDescriptor d) { 185 String name = projectNameTextField.getText().trim(); 186 187 d.putProperty(WizardProperties.PROJECT_DIR, new File (createdFolderTextField.getText().trim())); 188 d.putProperty(WizardProperties.NAME, name); 189 190 File projectsDir = new File (this.projectLocationTextField.getText()); 191 if (projectsDir.isDirectory()) { 192 ProjectChooser.setProjectsFolder (projectsDir); 193 } 194 } 195 196 void validate (WizardDescriptor d) throws WizardValidationException { 197 } 199 200 void read (WizardDescriptor settings) { 201 File projectLocation = (File ) settings.getProperty(WizardProperties.PROJECT_DIR); 202 if (projectLocation == null) 203 projectLocation = ProjectChooser.getProjectsFolder(); 204 else 205 projectLocation = projectLocation.getParentFile(); 206 207 projectLocationTextField.setText(projectLocation.getAbsolutePath()); 208 209 String projectName = (String ) settings.getProperty(WizardProperties.NAME); 210 if (projectName == null) { 211 int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1; 212 String formater = NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NPW1_DefaultProjectName"); 213 while ((projectName = validFreeProjectName(projectLocation, formater, baseCount)) == null) 214 baseCount++; 215 } 216 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 public void changedUpdate(DocumentEvent e) { 247 updateTexts(e); 248 } 249 250 public void insertUpdate(DocumentEvent e) { 251 updateTexts(e); 252 } 253 254 public void removeUpdate(DocumentEvent e) { 255 updateTexts(e); 256 } 257 259 260 262 private void updateTexts(DocumentEvent e) { 263 createdFolderTextField.setText(getCreatedFolderPath()); 264 265 panel.fireChangeEvent(); } 267 268 private String getCreatedFolderPath() { 269 StringBuffer folder = new StringBuffer (projectLocationTextField.getText().trim()); 270 if (!projectLocationTextField.getText().endsWith(File.separator)) 271 folder.append(File.separatorChar); 272 folder.append(projectNameTextField.getText().trim()); 273 274 return folder.toString(); 275 } 276 277 static File getCanonicalFile(File file) { 278 try { 279 return file.getCanonicalFile(); 280 } catch (IOException e) { 281 return null; 282 } 283 } 284 285 } 286 287 | Popular Tags |