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