2 * jSite - ProjectFilesPage.java - Copyright © 2006–2011 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.gui;
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.KeyEvent;
29 import java.text.MessageFormat;
30 import java.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
36 import javax.swing.AbstractAction;
37 import javax.swing.Action;
38 import javax.swing.JButton;
39 import javax.swing.JCheckBox;
40 import javax.swing.JComboBox;
41 import javax.swing.JComponent;
42 import javax.swing.JLabel;
43 import javax.swing.JList;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JTextField;
48 import javax.swing.ListSelectionModel;
49 import javax.swing.SwingUtilities;
50 import javax.swing.event.DocumentEvent;
51 import javax.swing.event.DocumentListener;
52 import javax.swing.event.ListSelectionEvent;
53 import javax.swing.event.ListSelectionListener;
54 import javax.swing.text.BadLocationException;
55 import javax.swing.text.Document;
57 import de.todesbaum.jsite.application.FileOption;
58 import de.todesbaum.jsite.application.Project;
59 import de.todesbaum.jsite.i18n.I18n;
60 import de.todesbaum.jsite.i18n.I18nContainer;
61 import de.todesbaum.util.mime.DefaultMIMETypes;
62 import de.todesbaum.util.swing.TLabel;
63 import de.todesbaum.util.swing.TWizard;
64 import de.todesbaum.util.swing.TWizardPage;
67 * Wizard page that lets the user manage the files of a project.
69 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
71 public class ProjectFilesPage extends TWizardPage implements ActionListener, ListSelectionListener, DocumentListener, FileScannerListener {
74 private Project project;
76 /** The “scan files” action. */
77 private Action scanAction;
79 /** The “ignore hidden files” checkbox. */
80 private JCheckBox ignoreHiddenFilesCheckBox;
82 /** The list of project files. */
83 private JList projectFileList;
85 /** The “default file” checkbox. */
86 private JCheckBox defaultFileCheckBox;
88 /** The “insert” checkbox. */
89 private JCheckBox fileOptionsInsertCheckBox;
91 /** The “insert redirect” checkbox. */
92 private JCheckBox fileOptionsInsertRedirectCheckBox;
94 /** The “custom key” textfield. */
95 private JTextField fileOptionsCustomKeyTextField;
97 /** The “rename” check box. */
98 private JCheckBox fileOptionsRenameCheckBox;
100 /** The “new name” text field. */
101 private JTextField fileOptionsRenameTextField;
103 /** The “mime type” combo box. */
104 private JComboBox fileOptionsMIMETypeComboBox;
107 * Creates a new project file page.
110 * The wizard the page belongs to
112 public ProjectFilesPage(final TWizard wizard) {
118 * Initializes the page and all its actions and components.
120 private void pageInit() {
122 setLayout(new BorderLayout(12, 12));
123 add(createProjectFilesPanel(), BorderLayout.CENTER);
127 * Creates all actions.
129 private void createActions() {
130 scanAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.rescan")) {
132 @SuppressWarnings("synthetic-access")
133 public void actionPerformed(ActionEvent actionEvent) {
137 scanAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
138 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
140 I18nContainer.getInstance().registerRunnable(new Runnable() {
142 @SuppressWarnings("synthetic-access")
144 scanAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.rescan"));
145 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
154 public void pageAdded(TWizard wizard) {
156 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
157 this.wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
158 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
162 * Creates the panel contains the project file list and options.
164 * @return The created panel
166 private JComponent createProjectFilesPanel() {
167 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
169 projectFileList = new JList();
170 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
171 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
172 projectFileList.addListSelectionListener(this);
174 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
176 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
177 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
178 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
179 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
181 ignoreHiddenFilesCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
182 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.ignore-hidden-files.tooltip"));
183 ignoreHiddenFilesCheckBox.setName("ignore-hidden-files");
184 ignoreHiddenFilesCheckBox.addActionListener(this);
185 fileOptionsPanel.add(ignoreHiddenFilesCheckBox, new GridBagConstraints(0, 0, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
187 fileOptionsPanel.add(new JButton(scanAction), new GridBagConstraints(0, 1, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
189 final JLabel fileOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
190 fileOptionsPanel.add(fileOptionsLabel, new GridBagConstraints(0, 2, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
192 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
193 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
194 defaultFileCheckBox.setName("default-file");
195 defaultFileCheckBox.addActionListener(this);
196 defaultFileCheckBox.setEnabled(false);
198 fileOptionsPanel.add(defaultFileCheckBox, new GridBagConstraints(0, 3, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
200 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
201 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
202 fileOptionsInsertCheckBox.setName("insert");
203 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
204 fileOptionsInsertCheckBox.addActionListener(this);
205 fileOptionsInsertCheckBox.setEnabled(false);
207 fileOptionsPanel.add(fileOptionsInsertCheckBox, new GridBagConstraints(0, 4, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
209 fileOptionsCustomKeyTextField = new JTextField(45);
210 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
211 fileOptionsCustomKeyTextField.setEnabled(false);
212 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
214 fileOptionsInsertRedirectCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert-redirect"), false);
215 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
216 fileOptionsInsertRedirectCheckBox.setName("insert-redirect");
217 fileOptionsInsertRedirectCheckBox.setMnemonic(KeyEvent.VK_R);
218 fileOptionsInsertRedirectCheckBox.addActionListener(this);
219 fileOptionsInsertRedirectCheckBox.setEnabled(false);
221 final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField);
222 fileOptionsPanel.add(fileOptionsInsertRedirectCheckBox, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
223 fileOptionsPanel.add(customKeyLabel, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 6, 0, 0), 0, 0));
224 fileOptionsPanel.add(fileOptionsCustomKeyTextField, new GridBagConstraints(2, 5, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
226 fileOptionsRenameCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.rename"), false);
227 fileOptionsRenameCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.rename.tooltip"));
228 fileOptionsRenameCheckBox.setName("rename");
229 fileOptionsRenameCheckBox.setMnemonic(KeyEvent.VK_N);
230 fileOptionsRenameCheckBox.addActionListener(this);
231 fileOptionsRenameCheckBox.setEnabled(false);
233 fileOptionsRenameTextField = new JTextField();
234 fileOptionsRenameTextField.setEnabled(false);
235 fileOptionsRenameTextField.getDocument().addDocumentListener(new DocumentListener() {
237 @SuppressWarnings("synthetic-access")
238 private void storeText(DocumentEvent documentEvent) {
239 FileOption fileOption = getSelectedFile();
240 Document document = documentEvent.getDocument();
241 int documentLength = document.getLength();
243 fileOption.setChangedName(document.getText(0, documentLength).trim());
244 } catch (BadLocationException ble1) {
245 /* ignore, it should never happen. */
249 public void changedUpdate(DocumentEvent documentEvent) {
250 storeText(documentEvent);
253 public void insertUpdate(DocumentEvent documentEvent) {
254 storeText(documentEvent);
257 public void removeUpdate(DocumentEvent documentEvent) {
258 storeText(documentEvent);
263 fileOptionsPanel.add(fileOptionsRenameCheckBox, new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
264 fileOptionsPanel.add(fileOptionsRenameTextField, new GridBagConstraints(2, 6, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
266 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
267 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
268 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
269 fileOptionsMIMETypeComboBox.addActionListener(this);
270 fileOptionsMIMETypeComboBox.setEditable(true);
271 fileOptionsMIMETypeComboBox.setEnabled(false);
273 final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
274 fileOptionsPanel.add(mimeTypeLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
275 fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 7, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
277 I18nContainer.getInstance().registerRunnable(new Runnable() {
279 @SuppressWarnings("synthetic-access")
281 ignoreHiddenFilesCheckBox.setText(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
282 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.projet-files.ignore-hidden-files.tooltip"));
283 fileOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
284 defaultFileCheckBox.setText(I18n.getMessage("jsite.project-files.default"));
285 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
286 fileOptionsInsertCheckBox.setText(I18n.getMessage("jsite.project-files.insert"));
287 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
288 fileOptionsInsertRedirectCheckBox.setText(I18n.getMessage("jsite.project-files.insert-redirect"));
289 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
290 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
291 customKeyLabel.setText(I18n.getMessage("jsite.project-files.custom-key") + ":");
292 fileOptionsRenameCheckBox.setText("jsite.project-files.rename");
293 fileOptionsRenameCheckBox.setToolTipText("jsite.project-files.rename.tooltip");
294 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
295 mimeTypeLabel.setText(I18n.getMessage("jsite.project-files.mime-type") + ":");
299 return projectFilesPanel;
303 * Sets the project whose files to manage.
306 * The project whose files to manage
308 public void setProject(final Project project) {
309 this.project = project;
310 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
311 setDescription(I18n.getMessage("jsite.project-files.description"));
312 ignoreHiddenFilesCheckBox.setSelected(project.isIgnoreHiddenFiles());
313 I18nContainer.getInstance().registerRunnable(new Runnable() {
316 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
317 setDescription(I18n.getMessage("jsite.project-files.description"));
323 * Returns a list of all project files.
325 * @return All project files
327 private List<String> getProjectFiles() {
328 List<String> files = new ArrayList<String>();
329 for (int index = 0, size = projectFileList.getModel().getSize(); index < size; index++) {
330 files.add((String) projectFileList.getModel().getElementAt(index));
340 * Rescans the project’s files.
342 private void actionScan() {
343 projectFileList.clearSelection();
344 projectFileList.setListData(new Object[0]);
346 wizard.setNextEnabled(false);
347 wizard.setPreviousEnabled(false);
348 wizard.setQuitEnabled(false);
350 FileScanner fileScanner = new FileScanner(project);
351 fileScanner.addFileScannerListener(this);
352 new Thread(fileScanner).start();
358 * Updates the file list.
360 public void fileScannerFinished(FileScanner fileScanner) {
361 final boolean error = fileScanner.isError();
363 final List<String> files = fileScanner.getFiles();
364 SwingUtilities.invokeLater(new Runnable() {
366 @SuppressWarnings("synthetic-access")
368 projectFileList.setListData(files.toArray(new String[files.size()]));
369 projectFileList.clearSelection();
372 Set<String> entriesToRemove = new HashSet<String>();
373 Iterator<String> filenames = new HashSet<String>(project.getFileOptions().keySet()).iterator();
374 while (filenames.hasNext()) {
375 String filename = filenames.next();
376 if (!files.contains(filename)) {
377 entriesToRemove.add(filename);
380 for (String filename : entriesToRemove) {
381 project.setFileOption(filename, null);
384 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
386 SwingUtilities.invokeLater(new Runnable() {
388 @SuppressWarnings("synthetic-access")
390 wizard.setPreviousEnabled(true);
391 wizard.setNextEnabled(!error);
392 wizard.setQuitEnabled(true);
398 * Returns the {@link FileOption file options} for the currently selected
401 * @return The {@link FileOption}s for the selected file, or {@code null} if
402 * no file is selected
404 private FileOption getSelectedFile() {
405 String filename = (String) projectFileList.getSelectedValue();
406 if (filename == null) {
409 return project.getFileOption(filename);
413 // INTERFACE ActionListener
419 public void actionPerformed(ActionEvent actionEvent) {
420 Object source = actionEvent.getSource();
421 if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) {
422 project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected());
426 String filename = (String) projectFileList.getSelectedValue();
427 if (filename == null) {
430 FileOption fileOption = project.getFileOption(filename);
431 if (source instanceof JCheckBox) {
432 JCheckBox checkBox = (JCheckBox) source;
433 if ("default-file".equals(checkBox.getName())) {
434 if (checkBox.isSelected()) {
435 if (filename.indexOf('/') > -1) {
436 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.invalid-default-file"), null, JOptionPane.ERROR_MESSAGE);
437 checkBox.setSelected(false);
439 project.setIndexFile(filename);
442 if (filename.equals(project.getIndexFile())) {
443 project.setIndexFile(null);
446 } else if ("insert".equals(checkBox.getName())) {
447 boolean isInsert = checkBox.isSelected();
448 fileOption.setInsert(isInsert);
449 fileOptionsInsertRedirectCheckBox.setEnabled(!isInsert);
450 } else if ("insert-redirect".equals(checkBox.getName())) {
451 boolean isInsertRedirect = checkBox.isSelected();
452 fileOption.setInsertRedirect(isInsertRedirect);
453 fileOptionsCustomKeyTextField.setEnabled(isInsertRedirect);
454 } else if ("rename".equals(checkBox.getName())) {
455 boolean isRenamed = checkBox.isSelected();
456 fileOptionsRenameTextField.setEnabled(isRenamed);
457 fileOption.setChangedName(isRenamed ? fileOptionsRenameTextField.getText() : "");
459 } else if (source instanceof JComboBox) {
460 JComboBox comboBox = (JComboBox) source;
461 if ("project-files.mime-type".equals(comboBox.getName())) {
462 fileOption.setMimeType((String) comboBox.getSelectedItem());
468 // INTERFACE ListSelectionListener
474 public void valueChanged(ListSelectionEvent e) {
475 String filename = (String) projectFileList.getSelectedValue();
476 boolean enabled = filename != null;
477 boolean insert = fileOptionsInsertCheckBox.isSelected();
478 defaultFileCheckBox.setEnabled(enabled);
479 fileOptionsInsertCheckBox.setEnabled(enabled);
480 fileOptionsRenameCheckBox.setEnabled(enabled);
481 fileOptionsMIMETypeComboBox.setEnabled(enabled);
482 if (filename != null) {
483 FileOption fileOption = project.getFileOption(filename);
484 defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
485 fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
486 fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert());
487 fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect());
488 fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect());
489 fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
490 fileOptionsRenameCheckBox.setSelected(fileOption.hasChangedName());
491 fileOptionsRenameTextField.setEnabled(fileOption.hasChangedName());
492 fileOptionsRenameTextField.setText(fileOption.getChangedName());
493 fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
495 defaultFileCheckBox.setSelected(false);
496 fileOptionsInsertCheckBox.setSelected(true);
497 fileOptionsInsertRedirectCheckBox.setEnabled(false);
498 fileOptionsInsertRedirectCheckBox.setSelected(false);
499 fileOptionsCustomKeyTextField.setEnabled(false);
500 fileOptionsCustomKeyTextField.setText("CHK@");
501 fileOptionsRenameCheckBox.setEnabled(false);
502 fileOptionsRenameCheckBox.setSelected(false);
503 fileOptionsRenameTextField.setEnabled(false);
504 fileOptionsRenameTextField.setText("");
505 fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
510 // INTERFACE DocumentListener
514 * Updates the options of the currently selected file with the changes made
515 * in the “custom key” textfield.
517 * @param documentEvent
518 * The document event to process
520 private void processDocumentUpdate(DocumentEvent documentEvent) {
521 String filename = (String) projectFileList.getSelectedValue();
522 if (filename == null) {
525 FileOption fileOption = project.getFileOption(filename);
526 Document document = documentEvent.getDocument();
528 String text = document.getText(0, document.getLength());
529 fileOption.setCustomKey(text);
530 } catch (BadLocationException ble1) {
538 public void changedUpdate(DocumentEvent documentEvent) {
539 processDocumentUpdate(documentEvent);
545 public void insertUpdate(DocumentEvent documentEvent) {
546 processDocumentUpdate(documentEvent);
552 public void removeUpdate(DocumentEvent documentEvent) {
553 processDocumentUpdate(documentEvent);