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