2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.gui;
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.KeyEvent;
31 import java.text.MessageFormat;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
36 import javax.swing.AbstractAction;
37 import javax.swing.Action;
38 import javax.swing.DefaultComboBoxModel;
39 import javax.swing.JButton;
40 import javax.swing.JCheckBox;
41 import javax.swing.JComboBox;
42 import javax.swing.JComponent;
43 import javax.swing.JLabel;
44 import javax.swing.JList;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JSpinner;
49 import javax.swing.JTextField;
50 import javax.swing.ListSelectionModel;
51 import javax.swing.SpinnerNumberModel;
52 import javax.swing.SwingUtilities;
53 import javax.swing.border.EmptyBorder;
54 import javax.swing.event.ChangeEvent;
55 import javax.swing.event.ChangeListener;
56 import javax.swing.event.DocumentEvent;
57 import javax.swing.event.DocumentListener;
58 import javax.swing.event.ListSelectionEvent;
59 import javax.swing.event.ListSelectionListener;
60 import javax.swing.text.BadLocationException;
61 import javax.swing.text.Document;
63 import de.todesbaum.jsite.application.EditionProject;
64 import de.todesbaum.jsite.application.FileOption;
65 import de.todesbaum.jsite.application.Project;
66 import de.todesbaum.jsite.i18n.I18n;
67 import de.todesbaum.util.mime.DefaultMIMETypes;
68 import de.todesbaum.util.swing.TLabel;
69 import de.todesbaum.util.swing.TWizard;
70 import de.todesbaum.util.swing.TWizardPage;
73 * @author David Roden <droden@gmail.com>
74 * @version $Id: ProjectFilesPage.java 404 2006-03-26 02:11:03Z bombe $
76 public class ProjectFilesPage extends TWizardPage implements ActionListener, ListSelectionListener, DocumentListener, FileScannerListener, ChangeListener {
78 protected TWizard wizard;
80 protected Project project;
82 private Action scanAction;
83 private Action editContainerAction;
84 private Action addContainerAction;
85 private Action deleteContainerAction;
87 protected JList projectFileList;
88 private JCheckBox defaultFileCheckBox;
89 private JCheckBox fileOptionsInsertCheckBox;
90 private JTextField fileOptionsCustomKeyTextField;
91 private JComboBox fileOptionsMIMETypeComboBox;
92 protected DefaultComboBoxModel containerComboBoxModel;
93 private JComboBox fileOptionsContainerComboBox;
94 private JSpinner replaceEditionRangeSpinner;
95 private JCheckBox replacementCheckBox;
97 public ProjectFilesPage() {
102 private void pageInit() {
104 setLayout(new BorderLayout(12, 12));
105 add(createProjectFilesPanel(), BorderLayout.CENTER);
108 private void createActions() {
109 scanAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.rescan")) {
111 public void actionPerformed(ActionEvent actionEvent) {
115 scanAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
116 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
118 addContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.add-container")) {
120 public void actionPerformed(ActionEvent actionEvent) {
121 actionAddContainer();
124 addContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.add-container.tooltip"));
125 addContainerAction.setEnabled(false);
127 editContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.edit-container")) {
129 public void actionPerformed(ActionEvent actionEvent) {
130 actionEditContainer();
133 editContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.edit-container.tooltip"));
134 editContainerAction.setEnabled(false);
136 deleteContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.delete-container")) {
138 public void actionPerformed(ActionEvent actionEvent) {
139 actionDeleteContainer();
142 deleteContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.delete-container.tooltip"));
143 deleteContainerAction.setEnabled(false);
146 public void pageAdded(TWizard wizard) {
147 this.wizard = wizard;
151 private JComponent createProjectFilesPanel() {
152 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
154 projectFileList = new JList();
155 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
156 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
157 projectFileList.addListSelectionListener(this);
159 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
161 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
162 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
163 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
164 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
166 fileOptionsPanel.add(new JButton(scanAction), new GridBagConstraints(0, 0, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
168 fileOptionsPanel.add(new JLabel("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>"), new GridBagConstraints(0, 1, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
170 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
171 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
172 defaultFileCheckBox.setName("default-file");
173 defaultFileCheckBox.addActionListener(this);
174 defaultFileCheckBox.setEnabled(false);
176 fileOptionsPanel.add(defaultFileCheckBox, new GridBagConstraints(0, 2, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
178 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
179 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
180 fileOptionsInsertCheckBox.setName("insert");
181 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
182 fileOptionsInsertCheckBox.addActionListener(this);
183 fileOptionsInsertCheckBox.setEnabled(false);
185 fileOptionsPanel.add(fileOptionsInsertCheckBox, new GridBagConstraints(0, 3, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
187 fileOptionsCustomKeyTextField = new JTextField(45);
188 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
189 fileOptionsCustomKeyTextField.setEnabled(false);
190 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
192 fileOptionsPanel.add(new TLabel(I18n.getMessage("jsite.project-files.custom-key"), KeyEvent.VK_K, fileOptionsCustomKeyTextField), new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
193 fileOptionsPanel.add(fileOptionsCustomKeyTextField, new GridBagConstraints(1, 4, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
195 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
196 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
197 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
198 fileOptionsMIMETypeComboBox.addActionListener(this);
199 fileOptionsMIMETypeComboBox.setEnabled(false);
201 fileOptionsPanel.add(new TLabel(I18n.getMessage("jsite.project-files.mime-type"), KeyEvent.VK_M, fileOptionsMIMETypeComboBox), new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
202 fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 5, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
204 containerComboBoxModel = new DefaultComboBoxModel();
205 fileOptionsContainerComboBox = new JComboBox(containerComboBoxModel);
206 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
207 fileOptionsContainerComboBox.setName("project-files.container");
208 fileOptionsContainerComboBox.addActionListener(this);
209 fileOptionsContainerComboBox.setEnabled(false);
211 fileOptionsPanel.add(new TLabel(I18n.getMessage("jsite.project-files.container"), KeyEvent.VK_C, fileOptionsContainerComboBox), new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
212 fileOptionsPanel.add(fileOptionsContainerComboBox, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
213 fileOptionsPanel.add(new JButton(addContainerAction), new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
214 fileOptionsPanel.add(new JButton(editContainerAction), new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
215 fileOptionsPanel.add(new JButton(deleteContainerAction), new GridBagConstraints(4, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
217 JPanel fileOptionsReplacementPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 6, 6));
218 fileOptionsReplacementPanel.setBorder(new EmptyBorder(-6, -6, -6, -6));
220 replacementCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.replacement"));
221 replacementCheckBox.setName("project-files.replace-edition");
222 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
223 replacementCheckBox.addActionListener(this);
224 replacementCheckBox.setEnabled(false);
225 fileOptionsReplacementPanel.add(replacementCheckBox);
227 replaceEditionRangeSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
228 replaceEditionRangeSpinner.setName("project-files.replace-edition-range");
229 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
230 replaceEditionRangeSpinner.addChangeListener(this);
231 replaceEditionRangeSpinner.setEnabled(false);
232 fileOptionsReplacementPanel.add(new JLabel(I18n.getMessage("jsite.project-files.replacement.edition-range")));
233 fileOptionsReplacementPanel.add(replaceEditionRangeSpinner);
235 fileOptionsPanel.add(fileOptionsReplacementPanel, new GridBagConstraints(0, 7, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
237 return projectFilesPanel;
240 public void setProject(Project project) {
241 this.project = project;
242 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
243 setDescription(I18n.getMessage("jsite.project-files.description"));
246 private List<String> getProjectFiles() {
247 List<String> files = new ArrayList<String>();
248 for (int index = 0, size = projectFileList.getModel().getSize(); index < size; index++) {
249 files.add((String) projectFileList.getModel().getElementAt(index));
254 protected void rebuildContainerComboBox() {
255 /* scan files for containers */
256 List<String> files = getProjectFiles();
257 List<String> containers = new ArrayList<String>(); // ComboBoxModel
261 for (String filename: files) {
262 String container = project.getFileOption(filename).getContainer();
263 if (!containers.contains(container)) {
264 containers.add(container);
267 Collections.sort(containers);
268 containerComboBoxModel.removeAllElements();
269 for (String container: containers) {
270 containerComboBoxModel.addElement(container);
278 protected void actionScan() {
279 projectFileList.clearSelection();
280 projectFileList.setListData(new Object[0]);
282 wizard.setNextEnabled(false);
283 wizard.setPreviousEnabled(false);
284 wizard.setQuitEnabled(false);
286 FileScanner fileScanner = new FileScanner(project);
287 fileScanner.addFileScannerListener(this);
288 new Thread(fileScanner).start();
291 protected void actionAddContainer() {
292 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.add-container.message") + ":", null, JOptionPane.INFORMATION_MESSAGE);
293 if (containerName == null) {
296 containerName = containerName.trim();
297 String filename = (String) projectFileList.getSelectedValue();
298 FileOption fileOption = project.getFileOption(filename);
299 fileOption.setContainer(containerName);
300 rebuildContainerComboBox();
301 fileOptionsContainerComboBox.setSelectedItem(containerName);
304 protected void actionEditContainer() {
305 String selectedFilename = (String) projectFileList.getSelectedValue();
306 FileOption fileOption = project.getFileOption(selectedFilename);
307 String oldContainerName = fileOption.getContainer();
308 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.edit-container.message") + ":", oldContainerName);
309 if (containerName == null) {
312 if (containerName.equals("")) {
313 fileOption.setContainer("");
314 fileOptionsContainerComboBox.setSelectedItem("");
317 List<String> files = getProjectFiles();
318 for (String filename: files) {
319 fileOption = project.getFileOption(filename);
320 if (fileOption.getContainer().equals(oldContainerName)) {
321 fileOption.setContainer(containerName);
324 rebuildContainerComboBox();
325 fileOptionsContainerComboBox.setSelectedItem(containerName);
328 protected void actionDeleteContainer() {
329 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.action.delete-container.message"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
330 String containerName = (String) fileOptionsContainerComboBox.getSelectedItem();
331 List<String> files = getProjectFiles();
332 for (String filename: files) {
333 FileOption fileOption = project.getFileOption(filename);
334 if (fileOption.getContainer().equals(containerName)) {
335 fileOption.setContainer("");
338 fileOptionsContainerComboBox.setSelectedItem("");
342 public void fileScannerFinished(FileScanner fileScanner) {
343 final boolean error = fileScanner.isError();
345 final List<String> files = fileScanner.getFiles();
346 SwingUtilities.invokeLater(new Runnable() {
349 projectFileList.setListData(files.toArray(new String[files.size()]));
350 projectFileList.clearSelection();
351 rebuildContainerComboBox();
355 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
357 SwingUtilities.invokeLater(new Runnable() {
360 wizard.setPreviousEnabled(true);
361 wizard.setNextEnabled(!error);
362 wizard.setQuitEnabled(true);
368 // INTERFACE ActionListener
374 public void actionPerformed(ActionEvent actionEvent) {
375 String filename = (String) projectFileList.getSelectedValue();
376 if (filename == null) {
379 FileOption fileOption = project.getFileOption(filename);
380 Object source = actionEvent.getSource();
381 if (source instanceof JCheckBox) {
382 JCheckBox checkBox = (JCheckBox) source;
383 if ("default-file".equals(checkBox.getName())) {
384 if (checkBox.isSelected()) {
385 project.setIndexFile(filename);
387 project.setIndexFile(null);
389 } else if ("insert".equals(checkBox.getName())) {
390 boolean isInsert = checkBox.isSelected();
391 fileOptionsCustomKeyTextField.setEnabled(!isInsert);
392 fileOption.setInsert(isInsert);
394 fileOptionsContainerComboBox.setSelectedItem("");
396 } else if ("project-files.replace-edition".equals(checkBox.getName())) {
397 boolean replaceEdition = checkBox.isSelected();
398 fileOption.setReplaceEdition(replaceEdition);
399 replaceEditionRangeSpinner.setEnabled(replaceEdition);
401 } else if (source instanceof JComboBox) {
402 JComboBox comboBox = (JComboBox) source;
403 if ("project-files.mime-type".equals(comboBox.getName())) {
404 fileOption.setMimeType((String) comboBox.getSelectedItem());
405 } else if ("project-files.container".equals(comboBox.getName())) {
406 String containerName = (String) comboBox.getSelectedItem();
407 fileOption.setContainer(containerName);
408 boolean enabled = !"".equals(containerName);
409 editContainerAction.setEnabled(enabled);
410 deleteContainerAction.setEnabled(enabled);
412 fileOptionsInsertCheckBox.setSelected(true);
419 // INTERFACE ListSelectionListener
425 public void valueChanged(ListSelectionEvent e) {
426 String filename = (String) projectFileList.getSelectedValue();
427 boolean enabled = filename != null;
428 boolean insert = fileOptionsInsertCheckBox.isSelected();
429 defaultFileCheckBox.setEnabled(enabled);
430 fileOptionsInsertCheckBox.setEnabled(enabled);
431 fileOptionsCustomKeyTextField.setEnabled(enabled && !insert);
432 fileOptionsMIMETypeComboBox.setEnabled(enabled);
433 fileOptionsContainerComboBox.setEnabled(enabled);
434 addContainerAction.setEnabled(enabled);
435 editContainerAction.setEnabled(enabled);
436 deleteContainerAction.setEnabled(enabled);
437 replacementCheckBox.setEnabled(enabled && insert && (project instanceof EditionProject));
438 if (filename != null) {
439 FileOption fileOption = project.getFileOption(filename);
440 defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
441 fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
442 fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
443 fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
444 fileOptionsContainerComboBox.setSelectedItem(fileOption.getContainer());
445 replacementCheckBox.setSelected(fileOption.getReplaceEdition());
446 replaceEditionRangeSpinner.setValue(fileOption.getEditionRange());
447 replaceEditionRangeSpinner.setEnabled(fileOption.getReplaceEdition());
449 defaultFileCheckBox.setSelected(false);
450 fileOptionsInsertCheckBox.setSelected(true);
451 fileOptionsCustomKeyTextField.setText("CHK@");
452 fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
453 fileOptionsContainerComboBox.setSelectedItem("");
454 replacementCheckBox.setSelected(false);
455 replaceEditionRangeSpinner.setValue(0);
460 // INTERFACE DocumentListener
463 private void processDocumentUpdate(DocumentEvent documentEvent) {
464 String filename = (String) projectFileList.getSelectedValue();
465 if (filename == null)
467 FileOption fileOption = project.getFileOption(filename);
468 Document document = documentEvent.getDocument();
470 String text = document.getText(0, document.getLength());
471 fileOption.setCustomKey(text);
472 } catch (BadLocationException ble1) {
479 public void changedUpdate(DocumentEvent documentEvent) {
480 processDocumentUpdate(documentEvent);
486 public void insertUpdate(DocumentEvent documentEvent) {
487 processDocumentUpdate(documentEvent);
493 public void removeUpdate(DocumentEvent documentEvent) {
494 processDocumentUpdate(documentEvent);
498 // INTERFACE ChangeListener
504 public void stateChanged(ChangeEvent changeEvent) {
505 String filename = (String) projectFileList.getSelectedValue();
506 if (filename == null)
508 FileOption fileOption = project.getFileOption(filename);
509 Object source = changeEvent.getSource();
510 if (source instanceof JSpinner) {
511 JSpinner spinner = (JSpinner) source;
512 fileOption.setEditionRange((Integer) spinner.getValue());