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