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.Iterator;
35 import java.util.List;
37 import javax.swing.AbstractAction;
38 import javax.swing.Action;
39 import javax.swing.DefaultComboBoxModel;
40 import javax.swing.JButton;
41 import javax.swing.JCheckBox;
42 import javax.swing.JComboBox;
43 import javax.swing.JComponent;
44 import javax.swing.JLabel;
45 import javax.swing.JList;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JSpinner;
50 import javax.swing.JTextField;
51 import javax.swing.ListSelectionModel;
52 import javax.swing.SpinnerNumberModel;
53 import javax.swing.SwingUtilities;
54 import javax.swing.border.EmptyBorder;
55 import javax.swing.event.ChangeEvent;
56 import javax.swing.event.ChangeListener;
57 import javax.swing.event.DocumentEvent;
58 import javax.swing.event.DocumentListener;
59 import javax.swing.event.ListSelectionEvent;
60 import javax.swing.event.ListSelectionListener;
61 import javax.swing.text.BadLocationException;
62 import javax.swing.text.Document;
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>
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);
147 public void pageAdded(TWizard wizard) {
148 this.wizard = wizard;
152 private JComponent createProjectFilesPanel() {
153 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
155 projectFileList = new JList();
156 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
157 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
158 projectFileList.addListSelectionListener(this);
160 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
162 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
163 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
164 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
165 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
167 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));
169 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));
171 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
172 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
173 defaultFileCheckBox.setName("default-file");
174 defaultFileCheckBox.addActionListener(this);
175 defaultFileCheckBox.setEnabled(false);
177 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));
179 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
180 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
181 fileOptionsInsertCheckBox.setName("insert");
182 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
183 fileOptionsInsertCheckBox.addActionListener(this);
184 fileOptionsInsertCheckBox.setEnabled(false);
186 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));
188 fileOptionsCustomKeyTextField = new JTextField(45);
189 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
190 fileOptionsCustomKeyTextField.setEnabled(false);
191 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
193 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));
194 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));
196 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
197 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
198 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
199 fileOptionsMIMETypeComboBox.addActionListener(this);
200 fileOptionsMIMETypeComboBox.setEnabled(false);
202 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));
203 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));
205 containerComboBoxModel = new DefaultComboBoxModel();
206 fileOptionsContainerComboBox = new JComboBox(containerComboBoxModel);
207 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
208 fileOptionsContainerComboBox.setName("project-files.container");
209 fileOptionsContainerComboBox.addActionListener(this);
210 fileOptionsContainerComboBox.setEnabled(false);
212 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));
213 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));
214 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));
215 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));
216 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));
218 JPanel fileOptionsReplacementPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 6, 6));
219 fileOptionsReplacementPanel.setBorder(new EmptyBorder(-6, -6, -6, -6));
221 replacementCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.replacement"));
222 replacementCheckBox.setName("project-files.replace-edition");
223 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
224 replacementCheckBox.addActionListener(this);
225 replacementCheckBox.setEnabled(false);
226 fileOptionsReplacementPanel.add(replacementCheckBox);
228 replaceEditionRangeSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
229 replaceEditionRangeSpinner.setName("project-files.replace-edition-range");
230 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
231 replaceEditionRangeSpinner.addChangeListener(this);
232 replaceEditionRangeSpinner.setEnabled(false);
233 fileOptionsReplacementPanel.add(new JLabel(I18n.getMessage("jsite.project-files.replacement.edition-range")));
234 fileOptionsReplacementPanel.add(replaceEditionRangeSpinner);
236 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));
238 return projectFilesPanel;
241 public void setProject(Project project) {
242 this.project = project;
243 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
244 setDescription(I18n.getMessage("jsite.project-files.description"));
247 private List<String> getProjectFiles() {
248 List<String> files = new ArrayList<String>();
249 for (int index = 0, size = projectFileList.getModel().getSize(); index < size; index++) {
250 files.add((String) projectFileList.getModel().getElementAt(index));
255 protected void rebuildContainerComboBox() {
256 /* scan files for containers */
257 List<String> files = getProjectFiles();
258 List<String> containers = new ArrayList<String>(); // ComboBoxModel
262 for (String filename: files) {
263 String container = project.getFileOption(filename).getContainer();
264 if (!containers.contains(container)) {
265 containers.add(container);
268 Collections.sort(containers);
269 containerComboBoxModel.removeAllElements();
270 for (String container: containers) {
271 containerComboBoxModel.addElement(container);
279 protected void actionScan() {
280 projectFileList.clearSelection();
281 projectFileList.setListData(new Object[0]);
283 wizard.setNextEnabled(false);
284 wizard.setPreviousEnabled(false);
285 wizard.setQuitEnabled(false);
287 FileScanner fileScanner = new FileScanner(project);
288 fileScanner.addFileScannerListener(this);
289 new Thread(fileScanner).start();
292 protected void actionAddContainer() {
293 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.add-container.message") + ":", null, JOptionPane.INFORMATION_MESSAGE);
294 if (containerName == null) {
297 containerName = containerName.trim();
298 String filename = (String) projectFileList.getSelectedValue();
299 FileOption fileOption = project.getFileOption(filename);
300 fileOption.setContainer(containerName);
301 rebuildContainerComboBox();
302 fileOptionsContainerComboBox.setSelectedItem(containerName);
305 protected void actionEditContainer() {
306 String selectedFilename = (String) projectFileList.getSelectedValue();
307 FileOption fileOption = project.getFileOption(selectedFilename);
308 String oldContainerName = fileOption.getContainer();
309 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.edit-container.message") + ":", oldContainerName);
310 if (containerName == null) {
313 if (containerName.equals("")) {
314 fileOption.setContainer("");
315 fileOptionsContainerComboBox.setSelectedItem("");
318 List<String> files = getProjectFiles();
319 for (String filename: files) {
320 fileOption = project.getFileOption(filename);
321 if (fileOption.getContainer().equals(oldContainerName)) {
322 fileOption.setContainer(containerName);
325 rebuildContainerComboBox();
326 fileOptionsContainerComboBox.setSelectedItem(containerName);
329 protected void actionDeleteContainer() {
330 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.action.delete-container.message"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
331 String containerName = (String) fileOptionsContainerComboBox.getSelectedItem();
332 List<String> files = getProjectFiles();
333 for (String filename: files) {
334 FileOption fileOption = project.getFileOption(filename);
335 if (fileOption.getContainer().equals(containerName)) {
336 fileOption.setContainer("");
339 fileOptionsContainerComboBox.setSelectedItem("");
343 public void fileScannerFinished(FileScanner fileScanner) {
344 final boolean error = fileScanner.isError();
346 final List<String> files = fileScanner.getFiles();
347 SwingUtilities.invokeLater(new Runnable() {
350 projectFileList.setListData(files.toArray(new String[files.size()]));
351 projectFileList.clearSelection();
352 rebuildContainerComboBox();
355 Iterator<String> filenames = project.getFileOptions().keySet().iterator();
356 while (filenames.hasNext()) {
357 String filename = filenames.next();
358 if (!files.contains(filename)) {
359 project.setFileOption(filename, null);
363 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
365 SwingUtilities.invokeLater(new Runnable() {
368 wizard.setPreviousEnabled(true);
369 wizard.setNextEnabled(!error);
370 wizard.setQuitEnabled(true);
376 // INTERFACE ActionListener
382 public void actionPerformed(ActionEvent actionEvent) {
383 String filename = (String) projectFileList.getSelectedValue();
384 if (filename == null) {
387 FileOption fileOption = project.getFileOption(filename);
388 Object source = actionEvent.getSource();
389 if (source instanceof JCheckBox) {
390 JCheckBox checkBox = (JCheckBox) source;
391 if ("default-file".equals(checkBox.getName())) {
392 if (checkBox.isSelected()) {
393 project.setIndexFile(filename);
395 project.setIndexFile(null);
397 } else if ("insert".equals(checkBox.getName())) {
398 boolean isInsert = checkBox.isSelected();
399 fileOptionsCustomKeyTextField.setEnabled(!isInsert);
400 fileOption.setInsert(isInsert);
402 fileOptionsContainerComboBox.setSelectedItem("");
404 } else if ("project-files.replace-edition".equals(checkBox.getName())) {
405 boolean replaceEdition = checkBox.isSelected();
406 fileOption.setReplaceEdition(replaceEdition);
407 replaceEditionRangeSpinner.setEnabled(replaceEdition);
409 } else if (source instanceof JComboBox) {
410 JComboBox comboBox = (JComboBox) source;
411 if ("project-files.mime-type".equals(comboBox.getName())) {
412 fileOption.setMimeType((String) comboBox.getSelectedItem());
413 } else if ("project-files.container".equals(comboBox.getName())) {
414 String containerName = (String) comboBox.getSelectedItem();
415 fileOption.setContainer(containerName);
416 boolean enabled = !"".equals(containerName);
417 editContainerAction.setEnabled(enabled);
418 deleteContainerAction.setEnabled(enabled);
420 fileOptionsInsertCheckBox.setSelected(true);
427 // INTERFACE ListSelectionListener
433 public void valueChanged(ListSelectionEvent e) {
434 String filename = (String) projectFileList.getSelectedValue();
435 boolean enabled = filename != null;
436 boolean insert = fileOptionsInsertCheckBox.isSelected();
437 defaultFileCheckBox.setEnabled(enabled);
438 fileOptionsInsertCheckBox.setEnabled(enabled);
439 fileOptionsCustomKeyTextField.setEnabled(enabled && !insert);
440 fileOptionsMIMETypeComboBox.setEnabled(enabled);
441 fileOptionsContainerComboBox.setEnabled(enabled);
442 addContainerAction.setEnabled(enabled);
443 editContainerAction.setEnabled(enabled);
444 deleteContainerAction.setEnabled(enabled);
445 replacementCheckBox.setEnabled(enabled && insert);
446 if (filename != null) {
447 FileOption fileOption = project.getFileOption(filename);
448 defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
449 fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
450 fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
451 fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
452 fileOptionsContainerComboBox.setSelectedItem(fileOption.getContainer());
453 replacementCheckBox.setSelected(fileOption.getReplaceEdition());
454 replaceEditionRangeSpinner.setValue(fileOption.getEditionRange());
455 replaceEditionRangeSpinner.setEnabled(fileOption.getReplaceEdition());
457 defaultFileCheckBox.setSelected(false);
458 fileOptionsInsertCheckBox.setSelected(true);
459 fileOptionsCustomKeyTextField.setText("CHK@");
460 fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
461 fileOptionsContainerComboBox.setSelectedItem("");
462 replacementCheckBox.setSelected(false);
463 replaceEditionRangeSpinner.setValue(0);
468 // INTERFACE DocumentListener
471 private void processDocumentUpdate(DocumentEvent documentEvent) {
472 String filename = (String) projectFileList.getSelectedValue();
473 if (filename == null) {
476 FileOption fileOption = project.getFileOption(filename);
477 Document document = documentEvent.getDocument();
479 String text = document.getText(0, document.getLength());
480 fileOption.setCustomKey(text);
481 } catch (BadLocationException ble1) {
488 public void changedUpdate(DocumentEvent documentEvent) {
489 processDocumentUpdate(documentEvent);
495 public void insertUpdate(DocumentEvent documentEvent) {
496 processDocumentUpdate(documentEvent);
502 public void removeUpdate(DocumentEvent documentEvent) {
503 processDocumentUpdate(documentEvent);
507 // INTERFACE ChangeListener
513 public void stateChanged(ChangeEvent changeEvent) {
514 String filename = (String) projectFileList.getSelectedValue();
515 if (filename == null) {
518 FileOption fileOption = project.getFileOption(filename);
519 Object source = changeEvent.getSource();
520 if (source instanceof JSpinner) {
521 JSpinner spinner = (JSpinner) source;
522 fileOption.setEditionRange((Integer) spinner.getValue());