| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos.netbeans.project.empty; |
| 17 | |
|
| 18 | |
import java.io.File; |
| 19 | |
import javax.swing.JFileChooser; |
| 20 | |
import javax.swing.JPanel; |
| 21 | |
import javax.swing.event.DocumentEvent; |
| 22 | |
import javax.swing.event.DocumentListener; |
| 23 | |
import javax.swing.text.Document; |
| 24 | |
import org.netbeans.spi.project.ui.support.ProjectChooser; |
| 25 | |
import org.openide.WizardDescriptor; |
| 26 | |
import org.openide.WizardValidationException; |
| 27 | |
import org.openide.filesystems.FileUtil; |
| 28 | |
|
| 29 | 0 | public class EmptyWebPhotosProjectPanelVisual extends JPanel { |
| 30 | |
|
| 31 | |
public static final String PROP_PROJECT_NAME = "projectName"; |
| 32 | |
private EmptyWebPhotosProjectWizardPanel panel; |
| 33 | |
|
| 34 | 0 | public EmptyWebPhotosProjectPanelVisual(EmptyWebPhotosProjectWizardPanel panel) { |
| 35 | 0 | initComponents(); |
| 36 | 0 | this.panel = panel; |
| 37 | |
|
| 38 | 0 | EmptyWebPhotosProjectPanelVisualDocumentListener documentListener = new EmptyWebPhotosProjectPanelVisualDocumentListener(); |
| 39 | 0 | projectNameTextField.getDocument().addDocumentListener(documentListener); |
| 40 | 0 | projectLocationTextField.getDocument().addDocumentListener(documentListener); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public String getProjectName() { |
| 44 | 0 | return this.projectNameTextField.getText(); |
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
private void initComponents() { |
| 54 | |
|
| 55 | 0 | projectNameLabel = new javax.swing.JLabel(); |
| 56 | 0 | projectNameTextField = new javax.swing.JTextField(); |
| 57 | 0 | projectLocationLabel = new javax.swing.JLabel(); |
| 58 | 0 | projectLocationTextField = new javax.swing.JTextField(); |
| 59 | 0 | browseButton = new javax.swing.JButton(); |
| 60 | 0 | createdFolderLabel = new javax.swing.JLabel(); |
| 61 | 0 | createdFolderTextField = new javax.swing.JTextField(); |
| 62 | |
|
| 63 | 0 | projectNameLabel.setLabelFor(projectNameTextField); |
| 64 | 0 | org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:"); |
| 65 | |
|
| 66 | 0 | projectLocationLabel.setLabelFor(projectLocationTextField); |
| 67 | 0 | org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:"); |
| 68 | |
|
| 69 | 0 | org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse..."); |
| 70 | 0 | browseButton.setActionCommand("BROWSE"); |
| 71 | 0 | browseButton.addActionListener(new java.awt.event.ActionListener() { |
| 72 | |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
| 73 | 0 | browseButtonActionPerformed(evt); |
| 74 | 0 | } |
| 75 | |
}); |
| 76 | |
|
| 77 | 0 | createdFolderLabel.setLabelFor(createdFolderTextField); |
| 78 | 0 | org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:"); |
| 79 | |
|
| 80 | 0 | createdFolderTextField.setEditable(false); |
| 81 | |
|
| 82 | 0 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
| 83 | 0 | this.setLayout(layout); |
| 84 | 0 | layout.setHorizontalGroup( |
| 85 | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 86 | |
.addGroup(layout.createSequentialGroup() |
| 87 | |
.addContainerGap() |
| 88 | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 89 | |
.addComponent(projectNameLabel) |
| 90 | |
.addComponent(projectLocationLabel) |
| 91 | |
.addComponent(createdFolderLabel)) |
| 92 | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
| 93 | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 94 | |
.addComponent(projectNameTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) |
| 95 | |
.addComponent(projectLocationTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE) |
| 96 | |
.addComponent(createdFolderTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)) |
| 97 | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
| 98 | |
.addComponent(browseButton) |
| 99 | |
.addContainerGap()) |
| 100 | |
); |
| 101 | 0 | layout.setVerticalGroup( |
| 102 | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
| 103 | |
.addGroup(layout.createSequentialGroup() |
| 104 | |
.addContainerGap() |
| 105 | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 106 | |
.addComponent(projectNameLabel) |
| 107 | |
.addComponent(projectNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
| 108 | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
| 109 | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 110 | |
.addComponent(projectLocationLabel) |
| 111 | |
.addComponent(projectLocationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
| 112 | |
.addComponent(browseButton)) |
| 113 | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
| 114 | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
| 115 | |
.addComponent(createdFolderLabel) |
| 116 | |
.addComponent(createdFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) |
| 117 | |
.addContainerGap(213, Short.MAX_VALUE)) |
| 118 | |
); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { |
| 122 | 0 | String command = evt.getActionCommand(); |
| 123 | 0 | if ("BROWSE".equals(command)) { |
| 124 | 0 | JFileChooser chooser = new JFileChooser(); |
| 125 | 0 | chooser.setDialogTitle(java.util.ResourceBundle.getBundle("net/sf/webphotos/netbeans/project/empty/Bundle").getString("LBL_CreateProjectStep_DialogTitle")); |
| 126 | 0 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| 127 | 0 | String path = this.projectLocationTextField.getText(); |
| 128 | 0 | if (path.length() > 0) { |
| 129 | 0 | File f = new File(path); |
| 130 | 0 | if (f.exists()) { |
| 131 | 0 | chooser.setSelectedFile(f); |
| 132 | |
} |
| 133 | |
} |
| 134 | 0 | if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { |
| 135 | 0 | File projectDir = chooser.getSelectedFile(); |
| 136 | 0 | projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath()); |
| 137 | |
} |
| 138 | 0 | panel.fireChangeEvent(); |
| 139 | |
} |
| 140 | |
|
| 141 | 0 | } |
| 142 | |
|
| 143 | |
private javax.swing.JButton browseButton; |
| 144 | |
private javax.swing.JLabel createdFolderLabel; |
| 145 | |
private javax.swing.JTextField createdFolderTextField; |
| 146 | |
private javax.swing.JLabel projectLocationLabel; |
| 147 | |
private javax.swing.JTextField projectLocationTextField; |
| 148 | |
private javax.swing.JLabel projectNameLabel; |
| 149 | |
private javax.swing.JTextField projectNameTextField; |
| 150 | |
|
| 151 | |
|
| 152 | |
@Override |
| 153 | |
public void addNotify() { |
| 154 | 0 | super.addNotify(); |
| 155 | |
|
| 156 | 0 | projectNameTextField.requestFocus(); |
| 157 | 0 | } |
| 158 | |
|
| 159 | |
boolean valid(WizardDescriptor wizardDescriptor) { |
| 160 | |
|
| 161 | 0 | if (projectNameTextField.getText().length() == 0) { |
| 162 | |
|
| 163 | 0 | wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, |
| 164 | |
"Project Name is not a valid folder name."); |
| 165 | 0 | return false; |
| 166 | |
} |
| 167 | 0 | File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile()); |
| 168 | 0 | if (!f.isDirectory()) { |
| 169 | 0 | String message = "Project Folder is not a valid path."; |
| 170 | 0 | wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, message); |
| 171 | 0 | return false; |
| 172 | |
} |
| 173 | 0 | final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile()); |
| 174 | |
|
| 175 | 0 | File projLoc = destFolder; |
| 176 | 0 | while (projLoc != null && !projLoc.exists()) { |
| 177 | 0 | projLoc = projLoc.getParentFile(); |
| 178 | |
} |
| 179 | 0 | if (projLoc == null || !projLoc.canWrite()) { |
| 180 | 0 | wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, |
| 181 | |
"Project Folder cannot be created."); |
| 182 | 0 | return false; |
| 183 | |
} |
| 184 | |
|
| 185 | 0 | if (FileUtil.toFileObject(projLoc) == null) { |
| 186 | 0 | String message = "Project Folder is not a valid path."; |
| 187 | 0 | wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, message); |
| 188 | 0 | return false; |
| 189 | |
} |
| 190 | |
|
| 191 | 0 | File[] kids = destFolder.listFiles(); |
| 192 | 0 | if (destFolder.exists() && kids != null && kids.length > 0) { |
| 193 | |
|
| 194 | 0 | wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, |
| 195 | |
"Project Folder already exists and is not empty."); |
| 196 | 0 | return false; |
| 197 | |
} |
| 198 | 0 | wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, ""); |
| 199 | 0 | return true; |
| 200 | |
} |
| 201 | |
|
| 202 | |
void store(WizardDescriptor d) { |
| 203 | 0 | String name = projectNameTextField.getText().trim(); |
| 204 | 0 | String folder = createdFolderTextField.getText().trim(); |
| 205 | |
|
| 206 | 0 | d.putProperty("projdir", new File(folder)); |
| 207 | 0 | d.putProperty("name", name); |
| 208 | 0 | } |
| 209 | |
|
| 210 | |
void read(WizardDescriptor settings) { |
| 211 | 0 | File projectLocation = (File) settings.getProperty("projdir"); |
| 212 | 0 | if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) { |
| 213 | 0 | projectLocation = ProjectChooser.getProjectsFolder(); |
| 214 | |
} else { |
| 215 | 0 | projectLocation = projectLocation.getParentFile(); |
| 216 | |
} |
| 217 | 0 | this.projectLocationTextField.setText(projectLocation.getAbsolutePath()); |
| 218 | |
|
| 219 | 0 | String projectName = (String) settings.getProperty("name"); |
| 220 | 0 | if (projectName == null) { |
| 221 | 0 | projectName = "EmptyWebPhotosProject"; |
| 222 | |
} |
| 223 | 0 | this.projectNameTextField.setText(projectName); |
| 224 | 0 | this.projectNameTextField.selectAll(); |
| 225 | 0 | } |
| 226 | |
|
| 227 | |
void validate(WizardDescriptor d) throws WizardValidationException { |
| 228 | |
|
| 229 | 0 | } |
| 230 | |
|
| 231 | 0 | private class EmptyWebPhotosProjectPanelVisualDocumentListener implements DocumentListener { |
| 232 | |
|
| 233 | |
|
| 234 | |
@Override |
| 235 | |
public void changedUpdate(DocumentEvent e) { |
| 236 | 0 | updateTexts(e); |
| 237 | 0 | if (projectNameTextField.getDocument() == e.getDocument()) { |
| 238 | 0 | firePropertyChange(PROP_PROJECT_NAME, null, projectNameTextField.getText()); |
| 239 | |
} |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
@Override |
| 243 | |
public void insertUpdate(DocumentEvent e) { |
| 244 | 0 | updateTexts(e); |
| 245 | 0 | if (projectNameTextField.getDocument() == e.getDocument()) { |
| 246 | 0 | firePropertyChange(PROP_PROJECT_NAME, null, projectNameTextField.getText()); |
| 247 | |
} |
| 248 | 0 | } |
| 249 | |
|
| 250 | |
@Override |
| 251 | |
public void removeUpdate(DocumentEvent e) { |
| 252 | 0 | updateTexts(e); |
| 253 | 0 | if (projectNameTextField.getDocument() == e.getDocument()) { |
| 254 | 0 | firePropertyChange(PROP_PROJECT_NAME, null, projectNameTextField.getText()); |
| 255 | |
} |
| 256 | 0 | } |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
private void updateTexts(DocumentEvent e) { |
| 262 | |
|
| 263 | 0 | Document doc = e.getDocument(); |
| 264 | |
|
| 265 | 0 | if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) { |
| 266 | |
|
| 267 | |
|
| 268 | 0 | String projectName = projectNameTextField.getText(); |
| 269 | 0 | String projectFolder = projectLocationTextField.getText(); |
| 270 | |
|
| 271 | |
|
| 272 | 0 | createdFolderTextField.setText(projectFolder + File.separatorChar + projectName); |
| 273 | |
|
| 274 | |
|
| 275 | |
} |
| 276 | 0 | panel.fireChangeEvent(); |
| 277 | 0 | } |
| 278 | |
} |
| 279 | |
} |