1 19 package org.netbeans.modules.apisupport.feedreader; 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 FeedReaderPanelVisual extends JPanel implements DocumentListener { 34 35 public static final String PROP_PROJECT_NAME = "projectName"; 37 private FeedReaderWizardPanel panel; 38 39 40 public FeedReaderPanelVisual(FeedReaderWizardPanel panel) { 41 initComponents(); 42 this.panel = panel; 43 projectNameTextField.getDocument().addDocumentListener( this ); 45 projectLocationTextField.getDocument().addDocumentListener( this ); 46 } 47 48 public String getProjectName() { 49 return this.projectNameTextField.getText(); 50 } 51 52 private void initComponents() { 54 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 browseButton = 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.setLabelFor(projectNameTextField); 67 gridBagConstraints = new java.awt.GridBagConstraints (); 68 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 69 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 70 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_ProjectName")); 71 add(projectNameLabel, gridBagConstraints); 72 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 81 projectLocationLabel.setLabelFor(projectLocationTextField); 82 gridBagConstraints = new java.awt.GridBagConstraints (); 83 gridBagConstraints.gridy = 1; 84 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 85 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 86 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_ProjectLocation")); 87 add(projectLocationLabel, gridBagConstraints); 88 89 gridBagConstraints = new java.awt.GridBagConstraints (); 90 gridBagConstraints.gridy = 1; 91 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 92 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 93 gridBagConstraints.weightx = 1.0; 94 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 95 add(projectLocationTextField, gridBagConstraints); 96 97 browseButton.setActionCommand("BROWSE"); 98 browseButton.addActionListener(new java.awt.event.ActionListener () { 99 public void actionPerformed(java.awt.event.ActionEvent evt) { 100 browseLocationAction(evt); 101 } 102 }); 103 104 gridBagConstraints = new java.awt.GridBagConstraints (); 105 gridBagConstraints.gridy = 1; 106 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 107 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 108 org.openide.awt.Mnemonics.setLocalizedText(browseButton, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_Browse")); 109 add(browseButton, gridBagConstraints); 110 111 createdFolderLabel.setLabelFor(createdFolderTextField); 112 gridBagConstraints = new java.awt.GridBagConstraints (); 113 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 114 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 115 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_ProjectFolder")); 116 add(createdFolderLabel, gridBagConstraints); 117 118 createdFolderTextField.setEditable(false); 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 gridBagConstraints.weightx = 1.0; 124 gridBagConstraints.weighty = 1.0; 125 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 126 add(createdFolderTextField, gridBagConstraints); 127 } 128 130 private void browseLocationAction(java.awt.event.ActionEvent evt) { 131 String command = evt.getActionCommand(); 132 if ("BROWSE".equals(command)) { JFileChooser chooser = new JFileChooser (); 134 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 135 chooser.setDialogTitle(NbBundle.getMessage(FeedReaderPanelVisual.class, "LBL_SelectProjectLocation")); 136 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 137 String path = this.projectLocationTextField.getText(); 138 if (path.length() > 0) { 139 File f = new File (path); 140 if (f.exists()) { 141 chooser.setSelectedFile(f); 142 } 143 } 144 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { File projectDir = chooser.getSelectedFile(); 146 projectLocationTextField.setText( FileUtil.normalizeFile(projectDir).getAbsolutePath() ); 147 } 148 panel.fireChangeEvent(); 149 } 150 } 151 152 public void addNotify() { 153 super.addNotify(); 154 projectNameTextField.requestFocus(); 156 } 157 158 boolean valid( WizardDescriptor wizardDescriptor ) { 159 if ( projectNameTextField.getText().length() == 0 ) { 160 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_InvalidProjectFolderName")); 162 return false; } 164 File f = FileUtil.normalizeFile(new File (projectLocationTextField.getText()).getAbsoluteFile()); 165 if (!f.isDirectory()) { 166 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_InvalidProjectFolderPath")); 168 return false; 169 } 170 final File destFolder = FileUtil.normalizeFile(new File (createdFolderTextField.getText()).getAbsoluteFile()); 171 172 File projLoc = destFolder; 173 while (projLoc != null && !projLoc.exists()) { 174 projLoc = projLoc.getParentFile(); 175 } 176 if (projLoc == null || !projLoc.canWrite()) { 177 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_CanNotCreateProjectFolder")); 179 return false; 180 } 181 182 if (FileUtil.toFileObject(projLoc) == null) { 183 wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_InvalidProjectFolderPath")); 185 return false; 186 } 187 188 File [] kids = destFolder.listFiles(); 189 if ( destFolder.exists() && kids != null && kids.length > 0) { 190 wizardDescriptor.putProperty( "WizardPanel_errorMessage", NbBundle.getMessage(FeedReaderPanelVisual.class, "MSG_ProjectFolderAlreadyExists")); 193 return false; 194 } 195 wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); return true; 197 } 198 199 void store( WizardDescriptor d ) { 200 String name = projectNameTextField.getText().trim(); 201 String folder = createdFolderTextField.getText().trim(); 202 203 d.putProperty( "projdir", new File ( folder )); d.putProperty( "name", name ); } 206 207 void read(WizardDescriptor settings) { 208 File projectLocation = (File ) settings.getProperty("projdir"); if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { 210 projectLocation = ProjectChooser.getProjectsFolder(); 211 } else { 212 projectLocation = projectLocation.getParentFile(); 213 } 214 this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); 215 216 String projectName = (String ) settings.getProperty("name"); if (projectName == null) { 218 projectName = "feedreader-suite"; 219 } 220 this.projectNameTextField.setText(projectName); 221 this.projectNameTextField.selectAll(); 222 } 223 224 void validate(WizardDescriptor d) throws WizardValidationException { 225 } 227 228 private javax.swing.JButton browseButton; 230 private javax.swing.JLabel createdFolderLabel; 231 private javax.swing.JTextField createdFolderTextField; 232 private javax.swing.JLabel projectLocationLabel; 233 private javax.swing.JTextField projectLocationTextField; 234 private javax.swing.JLabel projectNameLabel; 235 private javax.swing.JTextField projectNameTextField; 236 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 Document doc = e.getDocument(); 264 265 if ( doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument() ) { 266 268 String projectName = projectNameTextField.getText(); 269 String projectFolder = projectLocationTextField.getText(); 270 271 createdFolderTextField.setText( projectFolder + File.separatorChar + projectName ); 273 275 } 276 panel.fireChangeEvent(); } 278 279 } 280 | Popular Tags |