5fc0bcb71f7545121d402f41d366f5d9598bc93e
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectFilesPage.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.jsite.gui;
21
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.HashSet;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Set;
38
39 import javax.swing.AbstractAction;
40 import javax.swing.Action;
41 import javax.swing.DefaultComboBoxModel;
42 import javax.swing.JButton;
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JComponent;
46 import javax.swing.JLabel;
47 import javax.swing.JList;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.JSpinner;
52 import javax.swing.JTextField;
53 import javax.swing.ListSelectionModel;
54 import javax.swing.SpinnerNumberModel;
55 import javax.swing.SwingUtilities;
56 import javax.swing.border.EmptyBorder;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.event.DocumentEvent;
60 import javax.swing.event.DocumentListener;
61 import javax.swing.event.ListSelectionEvent;
62 import javax.swing.event.ListSelectionListener;
63 import javax.swing.text.BadLocationException;
64 import javax.swing.text.Document;
65
66 import de.todesbaum.jsite.application.FileOption;
67 import de.todesbaum.jsite.application.Project;
68 import de.todesbaum.jsite.i18n.I18n;
69 import de.todesbaum.jsite.i18n.I18nContainer;
70 import de.todesbaum.util.mime.DefaultMIMETypes;
71 import de.todesbaum.util.swing.TLabel;
72 import de.todesbaum.util.swing.TWizard;
73 import de.todesbaum.util.swing.TWizardPage;
74
75 /**
76  * Wizard page that lets the user manage the files of a project.
77  *
78  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
79  */
80 public class ProjectFilesPage extends TWizardPage implements ActionListener, ListSelectionListener, DocumentListener, FileScannerListener, ChangeListener {
81
82         /** The project. */
83         private Project project;
84
85         /** The “scan files” action. */
86         private Action scanAction;
87
88         /** The “edit container” action. */
89         private Action editContainerAction;
90
91         /** The “add container” action. */
92         private Action addContainerAction;
93
94         /** The “delete container” action. */
95         protected Action deleteContainerAction;
96
97         /** The “ignore hidden files” checkbox. */
98         private JCheckBox ignoreHiddenFilesCheckBox;
99
100         /** The list of project files. */
101         private JList projectFileList;
102
103         /** The “default file” checkbox. */
104         private JCheckBox defaultFileCheckBox;
105
106         /** The “insert” checkbox. */
107         private JCheckBox fileOptionsInsertCheckBox;
108
109         /** The “insert redirect” checkbox. */
110         private JCheckBox fileOptionsInsertRedirectCheckBox;
111
112         /** The “custom key” textfield. */
113         private JTextField fileOptionsCustomKeyTextField;
114
115         /** The “mime type” combo box. */
116         private JComboBox fileOptionsMIMETypeComboBox;
117
118         /** The “mime type” combo box model. */
119         private DefaultComboBoxModel containerComboBoxModel;
120
121         /** The “container” combo box. */
122         private JComboBox fileOptionsContainerComboBox;
123
124         /** The “edition replacement range” spinner. */
125         private JSpinner replaceEditionRangeSpinner;
126
127         /** The “replacement” check box. */
128         private JCheckBox replacementCheckBox;
129
130         /**
131          * Creates a new project file page.
132          *
133          * @param wizard
134          *            The wizard the page belongs to
135          */
136         public ProjectFilesPage(final TWizard wizard) {
137                 super(wizard);
138                 pageInit();
139         }
140
141         /**
142          * Initializes the page and all its actions and components.
143          */
144         private void pageInit() {
145                 createActions();
146                 setLayout(new BorderLayout(12, 12));
147                 add(createProjectFilesPanel(), BorderLayout.CENTER);
148         }
149
150         /**
151          * Creates all actions.
152          */
153         private void createActions() {
154                 scanAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.rescan")) {
155
156                         @SuppressWarnings("synthetic-access")
157                         public void actionPerformed(ActionEvent actionEvent) {
158                                 actionScan();
159                         }
160                 };
161                 scanAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
162                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
163
164                 addContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.add-container")) {
165
166                         @SuppressWarnings("synthetic-access")
167                         public void actionPerformed(ActionEvent actionEvent) {
168                                 actionAddContainer();
169                         }
170                 };
171                 addContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.add-container.tooltip"));
172                 addContainerAction.setEnabled(false);
173
174                 editContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.edit-container")) {
175
176                         @SuppressWarnings("synthetic-access")
177                         public void actionPerformed(ActionEvent actionEvent) {
178                                 actionEditContainer();
179                         }
180                 };
181                 editContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.edit-container.tooltip"));
182                 editContainerAction.setEnabled(false);
183
184                 deleteContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.delete-container")) {
185
186                         @SuppressWarnings("synthetic-access")
187                         public void actionPerformed(ActionEvent actionEvent) {
188                                 actionDeleteContainer();
189                         }
190                 };
191                 deleteContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.delete-container.tooltip"));
192                 deleteContainerAction.setEnabled(false);
193
194                 I18nContainer.getInstance().registerRunnable(new Runnable() {
195
196                         @SuppressWarnings("synthetic-access")
197                         public void run() {
198                                 scanAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.rescan"));
199                                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
200                                 addContainerAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.add-container"));
201                                 addContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.add-container.tooltip"));
202                                 editContainerAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.edit-container"));
203                                 editContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.edit-container.tooltip"));
204                                 deleteContainerAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.delete-container"));
205                                 deleteContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.delete-container.tooltip"));
206                         }
207                 });
208         }
209
210         /**
211          * {@inheritDoc}
212          */
213         @Override
214         public void pageAdded(TWizard wizard) {
215                 actionScan();
216                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
217                 this.wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
218                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
219         }
220
221         /**
222          * Creates the panel contains the project file list and options.
223          *
224          * @return The created panel
225          */
226         private JComponent createProjectFilesPanel() {
227                 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
228
229                 projectFileList = new JList();
230                 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
231                 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
232                 projectFileList.addListSelectionListener(this);
233
234                 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
235
236                 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
237                 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
238                 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
239                 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
240
241                 ignoreHiddenFilesCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
242                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.ignore-hidden-files.tooltip"));
243                 ignoreHiddenFilesCheckBox.setName("ignore-hidden-files");
244                 ignoreHiddenFilesCheckBox.addActionListener(this);
245                 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));
246
247                 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));
248
249                 final JLabel fileOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
250                 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));
251
252                 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
253                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
254                 defaultFileCheckBox.setName("default-file");
255                 defaultFileCheckBox.addActionListener(this);
256                 defaultFileCheckBox.setEnabled(false);
257
258                 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));
259
260                 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
261                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
262                 fileOptionsInsertCheckBox.setName("insert");
263                 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
264                 fileOptionsInsertCheckBox.addActionListener(this);
265                 fileOptionsInsertCheckBox.setEnabled(false);
266
267                 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));
268
269                 fileOptionsCustomKeyTextField = new JTextField(45);
270                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
271                 fileOptionsCustomKeyTextField.setEnabled(false);
272                 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
273
274                 fileOptionsInsertRedirectCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert-redirect"), false);
275                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
276                 fileOptionsInsertRedirectCheckBox.setName("insert-redirect");
277                 fileOptionsInsertRedirectCheckBox.setMnemonic(KeyEvent.VK_R);
278                 fileOptionsInsertRedirectCheckBox.addActionListener(this);
279
280                 final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField);
281                 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));
282                 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));
283                 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));
284
285                 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
286                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
287                 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
288                 fileOptionsMIMETypeComboBox.addActionListener(this);
289                 fileOptionsMIMETypeComboBox.setEditable(true);
290                 fileOptionsMIMETypeComboBox.setEnabled(false);
291
292                 final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
293                 fileOptionsPanel.add(mimeTypeLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
294                 fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 6, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
295
296                 containerComboBoxModel = new DefaultComboBoxModel();
297                 fileOptionsContainerComboBox = new JComboBox(containerComboBoxModel);
298                 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
299                 fileOptionsContainerComboBox.setName("project-files.container");
300                 fileOptionsContainerComboBox.addActionListener(this);
301                 fileOptionsContainerComboBox.setEnabled(false);
302                 fileOptionsContainerComboBox.setVisible(false);
303
304                 final TLabel containerLabel = new TLabel(I18n.getMessage("jsite.project-files.container") + ":", KeyEvent.VK_C, fileOptionsContainerComboBox);
305                 containerLabel.setVisible(false);
306                 JButton addContainerButton = new JButton(addContainerAction);
307                 addContainerButton.setVisible(false);
308                 JButton editContainerButton = new JButton(editContainerAction);
309                 editContainerButton.setVisible(false);
310                 JButton deleteContainerButton = new JButton(deleteContainerAction);
311                 deleteContainerButton.setVisible(false);
312                 fileOptionsPanel.add(containerLabel, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
313                 fileOptionsPanel.add(fileOptionsContainerComboBox, new GridBagConstraints(1, 7, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
314                 fileOptionsPanel.add(addContainerButton, new GridBagConstraints(2, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
315                 fileOptionsPanel.add(editContainerButton, new GridBagConstraints(3, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
316                 fileOptionsPanel.add(deleteContainerButton, new GridBagConstraints(4, 7, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
317
318                 JPanel fileOptionsReplacementPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 6, 6));
319                 fileOptionsReplacementPanel.setBorder(new EmptyBorder(-6, -6, -6, -6));
320
321                 replacementCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.replacement"));
322                 replacementCheckBox.setName("project-files.replace-edition");
323                 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
324                 replacementCheckBox.addActionListener(this);
325                 replacementCheckBox.setEnabled(false);
326                 replacementCheckBox.setVisible(false);
327                 fileOptionsReplacementPanel.add(replacementCheckBox);
328
329                 replaceEditionRangeSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
330                 replaceEditionRangeSpinner.setName("project-files.replace-edition-range");
331                 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
332                 replaceEditionRangeSpinner.addChangeListener(this);
333                 replaceEditionRangeSpinner.setEnabled(false);
334                 replaceEditionRangeSpinner.setVisible(false);
335                 final JLabel editionRangeLabel = new JLabel(I18n.getMessage("jsite.project-files.replacement.edition-range"));
336                 editionRangeLabel.setVisible(false);
337                 fileOptionsReplacementPanel.add(editionRangeLabel);
338                 fileOptionsReplacementPanel.add(replaceEditionRangeSpinner);
339
340                 fileOptionsPanel.add(fileOptionsReplacementPanel, new GridBagConstraints(0, 8, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
341
342                 I18nContainer.getInstance().registerRunnable(new Runnable() {
343
344                         @SuppressWarnings("synthetic-access")
345                         public void run() {
346                                 ignoreHiddenFilesCheckBox.setText(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
347                                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.projet-files.ignore-hidden-files.tooltip"));
348                                 fileOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
349                                 defaultFileCheckBox.setText(I18n.getMessage("jsite.project-files.default"));
350                                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
351                                 fileOptionsInsertCheckBox.setText(I18n.getMessage("jsite.project-files.insert"));
352                                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
353                                 fileOptionsInsertRedirectCheckBox.setText(I18n.getMessage("jsite.project-files.insert-redirect"));
354                                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
355                                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
356                                 customKeyLabel.setText(I18n.getMessage("jsite.project-files.custom-key") + ":");
357                                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
358                                 mimeTypeLabel.setText(I18n.getMessage("jsite.project-files.mime-type") + ":");
359                                 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
360                                 containerLabel.setText(I18n.getMessage("jsite.project-files.container") + ":");
361                                 replacementCheckBox.setText(I18n.getMessage("jsite.project-files.replacement"));
362                                 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
363                                 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
364                                 editionRangeLabel.setText(I18n.getMessage("jsite.project-files.replacement.edition-range"));
365                         }
366                 });
367
368                 return projectFilesPanel;
369         }
370
371         /**
372          * Sets the project whose files to manage.
373          *
374          * @param project
375          *            The project whose files to manage
376          */
377         public void setProject(final Project project) {
378                 this.project = project;
379                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
380                 setDescription(I18n.getMessage("jsite.project-files.description"));
381                 ignoreHiddenFilesCheckBox.setSelected(project.isIgnoreHiddenFiles());
382                 I18nContainer.getInstance().registerRunnable(new Runnable() {
383
384                         public void run() {
385                                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
386                                 setDescription(I18n.getMessage("jsite.project-files.description"));
387                         }
388                 });
389         }
390
391         /**
392          * Returns a list of all project files.
393          *
394          * @return All project files
395          */
396         private List<String> getProjectFiles() {
397                 List<String> files = new ArrayList<String>();
398                 for (int index = 0, size = projectFileList.getModel().getSize(); index < size; index++) {
399                         files.add((String) projectFileList.getModel().getElementAt(index));
400                 }
401                 return files;
402         }
403
404         /**
405          * Updates the container combo box model.
406          */
407         private void rebuildContainerComboBox() {
408                 /* scan files for containers */
409                 List<String> files = getProjectFiles();
410                 List<String> containers = new ArrayList<String>(); // ComboBoxModel
411                 // sucks. No
412                 // contains()!
413                 containers.add("");
414                 for (String filename : files) {
415                         String container = project.getFileOption(filename).getContainer();
416                         if (!containers.contains(container)) {
417                                 containers.add(container);
418                         }
419                 }
420                 Collections.sort(containers);
421                 containerComboBoxModel.removeAllElements();
422                 for (String container : containers) {
423                         containerComboBoxModel.addElement(container);
424                 }
425         }
426
427         //
428         // ACTIONS
429         //
430
431         /**
432          * Rescans the project’s files.
433          */
434         private void actionScan() {
435                 projectFileList.clearSelection();
436                 projectFileList.setListData(new Object[0]);
437
438                 wizard.setNextEnabled(false);
439                 wizard.setPreviousEnabled(false);
440                 wizard.setQuitEnabled(false);
441
442                 FileScanner fileScanner = new FileScanner(project);
443                 fileScanner.addFileScannerListener(this);
444                 new Thread(fileScanner).start();
445         }
446
447         /**
448          * Adds a container.
449          */
450         private void actionAddContainer() {
451                 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.add-container.message") + ":", null, JOptionPane.INFORMATION_MESSAGE);
452                 if (containerName == null) {
453                         return;
454                 }
455                 containerName = containerName.trim();
456                 String filename = (String) projectFileList.getSelectedValue();
457                 FileOption fileOption = project.getFileOption(filename);
458                 fileOption.setContainer(containerName);
459                 rebuildContainerComboBox();
460                 fileOptionsContainerComboBox.setSelectedItem(containerName);
461         }
462
463         /**
464          * Edits the container.
465          */
466         private void actionEditContainer() {
467                 String selectedFilename = (String) projectFileList.getSelectedValue();
468                 FileOption fileOption = project.getFileOption(selectedFilename);
469                 String oldContainerName = fileOption.getContainer();
470                 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.edit-container.message") + ":", oldContainerName);
471                 if (containerName == null) {
472                         return;
473                 }
474                 if (containerName.equals("")) {
475                         fileOption.setContainer("");
476                         fileOptionsContainerComboBox.setSelectedItem("");
477                         return;
478                 }
479                 List<String> files = getProjectFiles();
480                 for (String filename : files) {
481                         fileOption = project.getFileOption(filename);
482                         if (fileOption.getContainer().equals(oldContainerName)) {
483                                 fileOption.setContainer(containerName);
484                         }
485                 }
486                 rebuildContainerComboBox();
487                 fileOptionsContainerComboBox.setSelectedItem(containerName);
488         }
489
490         /**
491          * Deletes the container.
492          */
493         private void actionDeleteContainer() {
494                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.action.delete-container.message"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
495                         String containerName = (String) fileOptionsContainerComboBox.getSelectedItem();
496                         List<String> files = getProjectFiles();
497                         for (String filename : files) {
498                                 FileOption fileOption = project.getFileOption(filename);
499                                 if (fileOption.getContainer().equals(containerName)) {
500                                         fileOption.setContainer("");
501                                 }
502                         }
503                         fileOptionsContainerComboBox.setSelectedItem("");
504                 }
505         }
506
507         /**
508          * {@inheritDoc}
509          * <p>
510          * Updates the file list.
511          */
512         public void fileScannerFinished(FileScanner fileScanner) {
513                 final boolean error = fileScanner.isError();
514                 if (!error) {
515                         final List<String> files = fileScanner.getFiles();
516                         SwingUtilities.invokeLater(new Runnable() {
517
518                                 @SuppressWarnings("synthetic-access")
519                                 public void run() {
520                                         projectFileList.setListData(files.toArray(new String[files.size()]));
521                                         projectFileList.clearSelection();
522                                         rebuildContainerComboBox();
523                                 }
524                         });
525                         Set<String> entriesToRemove = new HashSet<String>();
526                         Iterator<String> filenames = new HashSet<String>(project.getFileOptions().keySet()).iterator();
527                         while (filenames.hasNext()) {
528                                 String filename = filenames.next();
529                                 if (!files.contains(filename)) {
530                                         entriesToRemove.add(filename);
531                                 }
532                         }
533                         for (String filename : entriesToRemove) {
534                                 project.setFileOption(filename, null);
535                         }
536                 } else {
537                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
538                 }
539                 SwingUtilities.invokeLater(new Runnable() {
540
541                         @SuppressWarnings("synthetic-access")
542                         public void run() {
543                                 wizard.setPreviousEnabled(true);
544                                 wizard.setNextEnabled(!error);
545                                 wizard.setQuitEnabled(true);
546                         }
547                 });
548         }
549
550         //
551         // INTERFACE ActionListener
552         //
553
554         /**
555          * {@inheritDoc}
556          */
557         public void actionPerformed(ActionEvent actionEvent) {
558                 Object source = actionEvent.getSource();
559                 if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) {
560                         project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected());
561                         actionScan();
562                         return;
563                 }
564                 String filename = (String) projectFileList.getSelectedValue();
565                 if (filename == null) {
566                         return;
567                 }
568                 FileOption fileOption = project.getFileOption(filename);
569                 if (source instanceof JCheckBox) {
570                         JCheckBox checkBox = (JCheckBox) source;
571                         if ("default-file".equals(checkBox.getName())) {
572                                 if (checkBox.isSelected()) {
573                                         project.setIndexFile(filename);
574                                 } else {
575                                         project.setIndexFile(null);
576                                 }
577                         } else if ("insert".equals(checkBox.getName())) {
578                                 boolean isInsert = checkBox.isSelected();
579                                 fileOption.setInsert(isInsert);
580                                 if (!isInsert) {
581                                         fileOptionsContainerComboBox.setSelectedItem("");
582                                 }
583                                 fileOptionsInsertRedirectCheckBox.setEnabled(!isInsert);
584                         } else if ("insert-redirect".equals(checkBox.getName())) {
585                                 boolean isInsertRedirect = checkBox.isSelected();
586                                 fileOption.setInsertRedirect(isInsertRedirect);
587                                 fileOptionsCustomKeyTextField.setEnabled(isInsertRedirect);
588                         } else if ("project-files.replace-edition".equals(checkBox.getName())) {
589                                 boolean replaceEdition = checkBox.isSelected();
590                                 fileOption.setReplaceEdition(replaceEdition);
591                                 replaceEditionRangeSpinner.setEnabled(replaceEdition);
592                         }
593                 } else if (source instanceof JComboBox) {
594                         JComboBox comboBox = (JComboBox) source;
595                         if ("project-files.mime-type".equals(comboBox.getName())) {
596                                 fileOption.setMimeType((String) comboBox.getSelectedItem());
597                         } else if ("project-files.container".equals(comboBox.getName())) {
598                                 String containerName = (String) comboBox.getSelectedItem();
599                                 fileOption.setContainer(containerName);
600                                 boolean enabled = !"".equals(containerName);
601                                 editContainerAction.setEnabled(enabled);
602                                 deleteContainerAction.setEnabled(enabled);
603                                 if (enabled) {
604                                         fileOptionsInsertCheckBox.setSelected(true);
605                                 }
606                         }
607                 }
608         }
609
610         //
611         // INTERFACE ListSelectionListener
612         //
613
614         /**
615          * {@inheritDoc}
616          */
617         public void valueChanged(ListSelectionEvent e) {
618                 String filename = (String) projectFileList.getSelectedValue();
619                 boolean enabled = filename != null;
620                 boolean insert = fileOptionsInsertCheckBox.isSelected();
621                 defaultFileCheckBox.setEnabled(enabled);
622                 fileOptionsInsertCheckBox.setEnabled(enabled);
623                 fileOptionsMIMETypeComboBox.setEnabled(enabled);
624                 fileOptionsContainerComboBox.setEnabled(enabled);
625                 addContainerAction.setEnabled(enabled);
626                 editContainerAction.setEnabled(enabled);
627                 deleteContainerAction.setEnabled(enabled);
628                 replacementCheckBox.setEnabled(enabled && insert);
629                 if (filename != null) {
630                         FileOption fileOption = project.getFileOption(filename);
631                         defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
632                         fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
633                         fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert());
634                         fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect());
635                         fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect());
636                         fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
637                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
638                         fileOptionsContainerComboBox.setSelectedItem(fileOption.getContainer());
639                         replacementCheckBox.setSelected(fileOption.getReplaceEdition());
640                         replaceEditionRangeSpinner.setValue(fileOption.getEditionRange());
641                         replaceEditionRangeSpinner.setEnabled(fileOption.getReplaceEdition());
642                 } else {
643                         defaultFileCheckBox.setSelected(false);
644                         fileOptionsInsertCheckBox.setSelected(true);
645                         fileOptionsInsertRedirectCheckBox.setEnabled(false);
646                         fileOptionsInsertRedirectCheckBox.setSelected(false);
647                         fileOptionsCustomKeyTextField.setEnabled(false);
648                         fileOptionsCustomKeyTextField.setText("CHK@");
649                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
650                         fileOptionsContainerComboBox.setSelectedItem("");
651                         replacementCheckBox.setSelected(false);
652                         replaceEditionRangeSpinner.setValue(0);
653                 }
654         }
655
656         //
657         // INTERFACE DocumentListener
658         //
659
660         /**
661          * Updates the options of the currently selected file with the changes made
662          * in the “custom key” textfield.
663          *
664          * @param documentEvent
665          *            The document event to process
666          */
667         private void processDocumentUpdate(DocumentEvent documentEvent) {
668                 String filename = (String) projectFileList.getSelectedValue();
669                 if (filename == null) {
670                         return;
671                 }
672                 FileOption fileOption = project.getFileOption(filename);
673                 Document document = documentEvent.getDocument();
674                 try {
675                         String text = document.getText(0, document.getLength());
676                         fileOption.setCustomKey(text);
677                 } catch (BadLocationException ble1) {
678                         /* ignore. */
679                 }
680         }
681
682         /**
683          * {@inheritDoc}
684          */
685         public void changedUpdate(DocumentEvent documentEvent) {
686                 processDocumentUpdate(documentEvent);
687         }
688
689         /**
690          * {@inheritDoc}
691          */
692         public void insertUpdate(DocumentEvent documentEvent) {
693                 processDocumentUpdate(documentEvent);
694         }
695
696         /**
697          * {@inheritDoc}
698          */
699         public void removeUpdate(DocumentEvent documentEvent) {
700                 processDocumentUpdate(documentEvent);
701         }
702
703         //
704         // INTERFACE ChangeListener
705         //
706
707         /**
708          * {@inheritDoc}
709          */
710         public void stateChanged(ChangeEvent changeEvent) {
711                 String filename = (String) projectFileList.getSelectedValue();
712                 if (filename == null) {
713                         return;
714                 }
715                 FileOption fileOption = project.getFileOption(filename);
716                 Object source = changeEvent.getSource();
717                 if (source instanceof JSpinner) {
718                         JSpinner spinner = (JSpinner) source;
719                         fileOption.setEditionRange((Integer) spinner.getValue());
720                 }
721         }
722
723 }