Disable “insert redirect” checkbox on dialog creation.
[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                 fileOptionsInsertRedirectCheckBox.setEnabled(false);
280
281                 final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField);
282                 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));
283                 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));
284                 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));
285
286                 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
287                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
288                 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
289                 fileOptionsMIMETypeComboBox.addActionListener(this);
290                 fileOptionsMIMETypeComboBox.setEditable(true);
291                 fileOptionsMIMETypeComboBox.setEnabled(false);
292
293                 final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
294                 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));
295                 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));
296
297                 containerComboBoxModel = new DefaultComboBoxModel();
298                 fileOptionsContainerComboBox = new JComboBox(containerComboBoxModel);
299                 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
300                 fileOptionsContainerComboBox.setName("project-files.container");
301                 fileOptionsContainerComboBox.addActionListener(this);
302                 fileOptionsContainerComboBox.setEnabled(false);
303                 fileOptionsContainerComboBox.setVisible(false);
304
305                 final TLabel containerLabel = new TLabel(I18n.getMessage("jsite.project-files.container") + ":", KeyEvent.VK_C, fileOptionsContainerComboBox);
306                 containerLabel.setVisible(false);
307                 JButton addContainerButton = new JButton(addContainerAction);
308                 addContainerButton.setVisible(false);
309                 JButton editContainerButton = new JButton(editContainerAction);
310                 editContainerButton.setVisible(false);
311                 JButton deleteContainerButton = new JButton(deleteContainerAction);
312                 deleteContainerButton.setVisible(false);
313                 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));
314                 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));
315                 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));
316                 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));
317                 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));
318
319                 JPanel fileOptionsReplacementPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 6, 6));
320                 fileOptionsReplacementPanel.setBorder(new EmptyBorder(-6, -6, -6, -6));
321
322                 replacementCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.replacement"));
323                 replacementCheckBox.setName("project-files.replace-edition");
324                 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
325                 replacementCheckBox.addActionListener(this);
326                 replacementCheckBox.setEnabled(false);
327                 replacementCheckBox.setVisible(false);
328                 fileOptionsReplacementPanel.add(replacementCheckBox);
329
330                 replaceEditionRangeSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
331                 replaceEditionRangeSpinner.setName("project-files.replace-edition-range");
332                 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
333                 replaceEditionRangeSpinner.addChangeListener(this);
334                 replaceEditionRangeSpinner.setEnabled(false);
335                 replaceEditionRangeSpinner.setVisible(false);
336                 final JLabel editionRangeLabel = new JLabel(I18n.getMessage("jsite.project-files.replacement.edition-range"));
337                 editionRangeLabel.setVisible(false);
338                 fileOptionsReplacementPanel.add(editionRangeLabel);
339                 fileOptionsReplacementPanel.add(replaceEditionRangeSpinner);
340
341                 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));
342
343                 I18nContainer.getInstance().registerRunnable(new Runnable() {
344
345                         @SuppressWarnings("synthetic-access")
346                         public void run() {
347                                 ignoreHiddenFilesCheckBox.setText(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
348                                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.projet-files.ignore-hidden-files.tooltip"));
349                                 fileOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
350                                 defaultFileCheckBox.setText(I18n.getMessage("jsite.project-files.default"));
351                                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
352                                 fileOptionsInsertCheckBox.setText(I18n.getMessage("jsite.project-files.insert"));
353                                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
354                                 fileOptionsInsertRedirectCheckBox.setText(I18n.getMessage("jsite.project-files.insert-redirect"));
355                                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
356                                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
357                                 customKeyLabel.setText(I18n.getMessage("jsite.project-files.custom-key") + ":");
358                                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
359                                 mimeTypeLabel.setText(I18n.getMessage("jsite.project-files.mime-type") + ":");
360                                 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
361                                 containerLabel.setText(I18n.getMessage("jsite.project-files.container") + ":");
362                                 replacementCheckBox.setText(I18n.getMessage("jsite.project-files.replacement"));
363                                 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
364                                 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
365                                 editionRangeLabel.setText(I18n.getMessage("jsite.project-files.replacement.edition-range"));
366                         }
367                 });
368
369                 return projectFilesPanel;
370         }
371
372         /**
373          * Sets the project whose files to manage.
374          *
375          * @param project
376          *            The project whose files to manage
377          */
378         public void setProject(final Project project) {
379                 this.project = project;
380                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
381                 setDescription(I18n.getMessage("jsite.project-files.description"));
382                 ignoreHiddenFilesCheckBox.setSelected(project.isIgnoreHiddenFiles());
383                 I18nContainer.getInstance().registerRunnable(new Runnable() {
384
385                         public void run() {
386                                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
387                                 setDescription(I18n.getMessage("jsite.project-files.description"));
388                         }
389                 });
390         }
391
392         /**
393          * Returns a list of all project files.
394          *
395          * @return All project files
396          */
397         private List<String> getProjectFiles() {
398                 List<String> files = new ArrayList<String>();
399                 for (int index = 0, size = projectFileList.getModel().getSize(); index < size; index++) {
400                         files.add((String) projectFileList.getModel().getElementAt(index));
401                 }
402                 return files;
403         }
404
405         /**
406          * Updates the container combo box model.
407          */
408         private void rebuildContainerComboBox() {
409                 /* scan files for containers */
410                 List<String> files = getProjectFiles();
411                 List<String> containers = new ArrayList<String>(); // ComboBoxModel
412                 // sucks. No
413                 // contains()!
414                 containers.add("");
415                 for (String filename : files) {
416                         String container = project.getFileOption(filename).getContainer();
417                         if (!containers.contains(container)) {
418                                 containers.add(container);
419                         }
420                 }
421                 Collections.sort(containers);
422                 containerComboBoxModel.removeAllElements();
423                 for (String container : containers) {
424                         containerComboBoxModel.addElement(container);
425                 }
426         }
427
428         //
429         // ACTIONS
430         //
431
432         /**
433          * Rescans the project’s files.
434          */
435         private void actionScan() {
436                 projectFileList.clearSelection();
437                 projectFileList.setListData(new Object[0]);
438
439                 wizard.setNextEnabled(false);
440                 wizard.setPreviousEnabled(false);
441                 wizard.setQuitEnabled(false);
442
443                 FileScanner fileScanner = new FileScanner(project);
444                 fileScanner.addFileScannerListener(this);
445                 new Thread(fileScanner).start();
446         }
447
448         /**
449          * Adds a container.
450          */
451         private void actionAddContainer() {
452                 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.add-container.message") + ":", null, JOptionPane.INFORMATION_MESSAGE);
453                 if (containerName == null) {
454                         return;
455                 }
456                 containerName = containerName.trim();
457                 String filename = (String) projectFileList.getSelectedValue();
458                 FileOption fileOption = project.getFileOption(filename);
459                 fileOption.setContainer(containerName);
460                 rebuildContainerComboBox();
461                 fileOptionsContainerComboBox.setSelectedItem(containerName);
462         }
463
464         /**
465          * Edits the container.
466          */
467         private void actionEditContainer() {
468                 String selectedFilename = (String) projectFileList.getSelectedValue();
469                 FileOption fileOption = project.getFileOption(selectedFilename);
470                 String oldContainerName = fileOption.getContainer();
471                 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.edit-container.message") + ":", oldContainerName);
472                 if (containerName == null) {
473                         return;
474                 }
475                 if (containerName.equals("")) {
476                         fileOption.setContainer("");
477                         fileOptionsContainerComboBox.setSelectedItem("");
478                         return;
479                 }
480                 List<String> files = getProjectFiles();
481                 for (String filename : files) {
482                         fileOption = project.getFileOption(filename);
483                         if (fileOption.getContainer().equals(oldContainerName)) {
484                                 fileOption.setContainer(containerName);
485                         }
486                 }
487                 rebuildContainerComboBox();
488                 fileOptionsContainerComboBox.setSelectedItem(containerName);
489         }
490
491         /**
492          * Deletes the container.
493          */
494         private void actionDeleteContainer() {
495                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.action.delete-container.message"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
496                         String containerName = (String) fileOptionsContainerComboBox.getSelectedItem();
497                         List<String> files = getProjectFiles();
498                         for (String filename : files) {
499                                 FileOption fileOption = project.getFileOption(filename);
500                                 if (fileOption.getContainer().equals(containerName)) {
501                                         fileOption.setContainer("");
502                                 }
503                         }
504                         fileOptionsContainerComboBox.setSelectedItem("");
505                 }
506         }
507
508         /**
509          * {@inheritDoc}
510          * <p>
511          * Updates the file list.
512          */
513         public void fileScannerFinished(FileScanner fileScanner) {
514                 final boolean error = fileScanner.isError();
515                 if (!error) {
516                         final List<String> files = fileScanner.getFiles();
517                         SwingUtilities.invokeLater(new Runnable() {
518
519                                 @SuppressWarnings("synthetic-access")
520                                 public void run() {
521                                         projectFileList.setListData(files.toArray(new String[files.size()]));
522                                         projectFileList.clearSelection();
523                                         rebuildContainerComboBox();
524                                 }
525                         });
526                         Set<String> entriesToRemove = new HashSet<String>();
527                         Iterator<String> filenames = new HashSet<String>(project.getFileOptions().keySet()).iterator();
528                         while (filenames.hasNext()) {
529                                 String filename = filenames.next();
530                                 if (!files.contains(filename)) {
531                                         entriesToRemove.add(filename);
532                                 }
533                         }
534                         for (String filename : entriesToRemove) {
535                                 project.setFileOption(filename, null);
536                         }
537                 } else {
538                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
539                 }
540                 SwingUtilities.invokeLater(new Runnable() {
541
542                         @SuppressWarnings("synthetic-access")
543                         public void run() {
544                                 wizard.setPreviousEnabled(true);
545                                 wizard.setNextEnabled(!error);
546                                 wizard.setQuitEnabled(true);
547                         }
548                 });
549         }
550
551         //
552         // INTERFACE ActionListener
553         //
554
555         /**
556          * {@inheritDoc}
557          */
558         public void actionPerformed(ActionEvent actionEvent) {
559                 Object source = actionEvent.getSource();
560                 if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) {
561                         project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected());
562                         actionScan();
563                         return;
564                 }
565                 String filename = (String) projectFileList.getSelectedValue();
566                 if (filename == null) {
567                         return;
568                 }
569                 FileOption fileOption = project.getFileOption(filename);
570                 if (source instanceof JCheckBox) {
571                         JCheckBox checkBox = (JCheckBox) source;
572                         if ("default-file".equals(checkBox.getName())) {
573                                 if (checkBox.isSelected()) {
574                                         project.setIndexFile(filename);
575                                 } else {
576                                         project.setIndexFile(null);
577                                 }
578                         } else if ("insert".equals(checkBox.getName())) {
579                                 boolean isInsert = checkBox.isSelected();
580                                 fileOption.setInsert(isInsert);
581                                 if (!isInsert) {
582                                         fileOptionsContainerComboBox.setSelectedItem("");
583                                 }
584                                 fileOptionsInsertRedirectCheckBox.setEnabled(!isInsert);
585                         } else if ("insert-redirect".equals(checkBox.getName())) {
586                                 boolean isInsertRedirect = checkBox.isSelected();
587                                 fileOption.setInsertRedirect(isInsertRedirect);
588                                 fileOptionsCustomKeyTextField.setEnabled(isInsertRedirect);
589                         } else if ("project-files.replace-edition".equals(checkBox.getName())) {
590                                 boolean replaceEdition = checkBox.isSelected();
591                                 fileOption.setReplaceEdition(replaceEdition);
592                                 replaceEditionRangeSpinner.setEnabled(replaceEdition);
593                         }
594                 } else if (source instanceof JComboBox) {
595                         JComboBox comboBox = (JComboBox) source;
596                         if ("project-files.mime-type".equals(comboBox.getName())) {
597                                 fileOption.setMimeType((String) comboBox.getSelectedItem());
598                         } else if ("project-files.container".equals(comboBox.getName())) {
599                                 String containerName = (String) comboBox.getSelectedItem();
600                                 fileOption.setContainer(containerName);
601                                 boolean enabled = !"".equals(containerName);
602                                 editContainerAction.setEnabled(enabled);
603                                 deleteContainerAction.setEnabled(enabled);
604                                 if (enabled) {
605                                         fileOptionsInsertCheckBox.setSelected(true);
606                                 }
607                         }
608                 }
609         }
610
611         //
612         // INTERFACE ListSelectionListener
613         //
614
615         /**
616          * {@inheritDoc}
617          */
618         public void valueChanged(ListSelectionEvent e) {
619                 String filename = (String) projectFileList.getSelectedValue();
620                 boolean enabled = filename != null;
621                 boolean insert = fileOptionsInsertCheckBox.isSelected();
622                 defaultFileCheckBox.setEnabled(enabled);
623                 fileOptionsInsertCheckBox.setEnabled(enabled);
624                 fileOptionsMIMETypeComboBox.setEnabled(enabled);
625                 fileOptionsContainerComboBox.setEnabled(enabled);
626                 addContainerAction.setEnabled(enabled);
627                 editContainerAction.setEnabled(enabled);
628                 deleteContainerAction.setEnabled(enabled);
629                 replacementCheckBox.setEnabled(enabled && insert);
630                 if (filename != null) {
631                         FileOption fileOption = project.getFileOption(filename);
632                         defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
633                         fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
634                         fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert());
635                         fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect());
636                         fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect());
637                         fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
638                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
639                         fileOptionsContainerComboBox.setSelectedItem(fileOption.getContainer());
640                         replacementCheckBox.setSelected(fileOption.getReplaceEdition());
641                         replaceEditionRangeSpinner.setValue(fileOption.getEditionRange());
642                         replaceEditionRangeSpinner.setEnabled(fileOption.getReplaceEdition());
643                 } else {
644                         defaultFileCheckBox.setSelected(false);
645                         fileOptionsInsertCheckBox.setSelected(true);
646                         fileOptionsInsertRedirectCheckBox.setEnabled(false);
647                         fileOptionsInsertRedirectCheckBox.setSelected(false);
648                         fileOptionsCustomKeyTextField.setEnabled(false);
649                         fileOptionsCustomKeyTextField.setText("CHK@");
650                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
651                         fileOptionsContainerComboBox.setSelectedItem("");
652                         replacementCheckBox.setSelected(false);
653                         replaceEditionRangeSpinner.setValue(0);
654                 }
655         }
656
657         //
658         // INTERFACE DocumentListener
659         //
660
661         /**
662          * Updates the options of the currently selected file with the changes made
663          * in the “custom key” textfield.
664          *
665          * @param documentEvent
666          *            The document event to process
667          */
668         private void processDocumentUpdate(DocumentEvent documentEvent) {
669                 String filename = (String) projectFileList.getSelectedValue();
670                 if (filename == null) {
671                         return;
672                 }
673                 FileOption fileOption = project.getFileOption(filename);
674                 Document document = documentEvent.getDocument();
675                 try {
676                         String text = document.getText(0, document.getLength());
677                         fileOption.setCustomKey(text);
678                 } catch (BadLocationException ble1) {
679                         /* ignore. */
680                 }
681         }
682
683         /**
684          * {@inheritDoc}
685          */
686         public void changedUpdate(DocumentEvent documentEvent) {
687                 processDocumentUpdate(documentEvent);
688         }
689
690         /**
691          * {@inheritDoc}
692          */
693         public void insertUpdate(DocumentEvent documentEvent) {
694                 processDocumentUpdate(documentEvent);
695         }
696
697         /**
698          * {@inheritDoc}
699          */
700         public void removeUpdate(DocumentEvent documentEvent) {
701                 processDocumentUpdate(documentEvent);
702         }
703
704         //
705         // INTERFACE ChangeListener
706         //
707
708         /**
709          * {@inheritDoc}
710          */
711         public void stateChanged(ChangeEvent changeEvent) {
712                 String filename = (String) projectFileList.getSelectedValue();
713                 if (filename == null) {
714                         return;
715                 }
716                 FileOption fileOption = project.getFileOption(filename);
717                 Object source = changeEvent.getSource();
718                 if (source instanceof JSpinner) {
719                         JSpinner spinner = (JSpinner) source;
720                         fileOption.setEditionRange((Integer) spinner.getValue());
721                 }
722         }
723
724 }