67287b76cf5ccf4f8325e5345f4d70e99674e893
[jSite.git] / src / main / java / de / todesbaum / jsite / gui / ProjectFilesPage.java
1 /*
2  * jSite - ProjectFilesPage.java - Copyright © 2006–2012 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.gui;
20
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.WindowAdapter;
30 import java.awt.event.WindowEvent;
31 import java.text.MessageFormat;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Set;
36
37 import javax.swing.AbstractAction;
38 import javax.swing.Action;
39 import javax.swing.BorderFactory;
40 import javax.swing.JButton;
41 import javax.swing.JCheckBox;
42 import javax.swing.JComboBox;
43 import javax.swing.JComponent;
44 import javax.swing.JDialog;
45 import javax.swing.JLabel;
46 import javax.swing.JList;
47 import javax.swing.JOptionPane;
48 import javax.swing.JPanel;
49 import javax.swing.JProgressBar;
50 import javax.swing.JScrollPane;
51 import javax.swing.JTextField;
52 import javax.swing.ListSelectionModel;
53 import javax.swing.SwingConstants;
54 import javax.swing.SwingUtilities;
55 import javax.swing.WindowConstants;
56 import javax.swing.event.DocumentEvent;
57 import javax.swing.event.DocumentListener;
58 import javax.swing.event.ListSelectionEvent;
59 import javax.swing.event.ListSelectionListener;
60 import javax.swing.text.BadLocationException;
61 import javax.swing.text.Document;
62
63 import net.pterodactylus.util.io.MimeTypes;
64 import net.pterodactylus.util.swing.SwingUtils;
65 import net.pterodactylus.util.thread.StoppableDelay;
66 import de.todesbaum.jsite.application.FileOption;
67 import de.todesbaum.jsite.application.Project;
68 import de.todesbaum.jsite.gui.FileScanner.ScannedFile;
69 import de.todesbaum.jsite.i18n.I18n;
70 import de.todesbaum.jsite.i18n.I18nContainer;
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 {
81
82         /** The project. */
83         private Project project;
84
85         /** The “scan files” action. */
86         private Action scanAction;
87
88         /** The “ignore hidden files” checkbox. */
89         private JCheckBox ignoreHiddenFilesCheckBox;
90
91         /** The list of project files. */
92         private JList projectFileList;
93
94         /** The “default file” checkbox. */
95         private JCheckBox defaultFileCheckBox;
96
97         /** The “insert” checkbox. */
98         private JCheckBox fileOptionsInsertCheckBox;
99
100         /** The “force insert” checkbox. */
101         private JCheckBox fileOptionsForceInsertCheckBox;
102
103         /** The “insert redirect” checkbox. */
104         private JCheckBox fileOptionsInsertRedirectCheckBox;
105
106         /** The “custom key” textfield. */
107         private JTextField fileOptionsCustomKeyTextField;
108
109         /** The “rename” check box. */
110         private JCheckBox fileOptionsRenameCheckBox;
111
112         /** The “new name” text field. */
113         private JTextField fileOptionsRenameTextField;
114
115         /** The “mime type” combo box. */
116         private JComboBox fileOptionsMIMETypeComboBox;
117
118         /** Delayed notification for file scanning. */
119         private StoppableDelay delayedNotification;
120
121         /** Dialog to display while scanning. */
122         private JDialog scanningFilesDialog;
123
124         /** The file scanner. */
125         private FileScanner fileScanner;
126
127         /** The progress bar. */
128         private JProgressBar progressBar;
129
130         /**
131          * Creates a new project file page.
132          *
133          * @param wizard
134          *            The wizard the page belongs to
135          */
136         public ProjectFilesPage(final TWizard wizard) {
137                 super(wizard);
138                 pageInit();
139         }
140
141         /**
142          * Initializes the page and all its actions and components.
143          */
144         private void pageInit() {
145                 createActions();
146                 setLayout(new BorderLayout(12, 12));
147                 add(createProjectFilesPanel(), BorderLayout.CENTER);
148         }
149
150         /**
151          * Creates all actions.
152          */
153         private void createActions() {
154                 scanAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.rescan")) {
155
156                         @Override
157                         @SuppressWarnings("synthetic-access")
158                         public void actionPerformed(ActionEvent actionEvent) {
159                                 actionScan();
160                         }
161                 };
162                 scanAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
163                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
164
165                 I18nContainer.getInstance().registerRunnable(new Runnable() {
166
167                         @Override
168                         @SuppressWarnings("synthetic-access")
169                         public void run() {
170                                 scanAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.rescan"));
171                                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
172                         }
173                 });
174         }
175
176         /**
177          * {@inheritDoc}
178          */
179         @Override
180         public void pageAdded(TWizard wizard) {
181                 /* create file scanner. */
182                 fileScanner = new FileScanner(project);
183                 fileScanner.addFileScannerListener(this);
184
185                 actionScan();
186                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
187                 this.wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
188                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
189         }
190
191         /**
192          * Creates the panel contains the project file list and options.
193          *
194          * @return The created panel
195          */
196         private JComponent createProjectFilesPanel() {
197                 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
198
199                 projectFileList = new JList();
200                 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
201                 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
202                 projectFileList.addListSelectionListener(this);
203
204                 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
205
206                 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
207                 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
208                 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
209                 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
210
211                 ignoreHiddenFilesCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
212                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.ignore-hidden-files.tooltip"));
213                 ignoreHiddenFilesCheckBox.setName("ignore-hidden-files");
214                 ignoreHiddenFilesCheckBox.addActionListener(this);
215                 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));
216
217                 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));
218
219                 final JLabel fileOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
220                 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));
221
222                 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
223                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
224                 defaultFileCheckBox.setName("default-file");
225                 defaultFileCheckBox.addActionListener(this);
226                 defaultFileCheckBox.setEnabled(false);
227
228                 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));
229
230                 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
231                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
232                 fileOptionsInsertCheckBox.setName("insert");
233                 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
234                 fileOptionsInsertCheckBox.addActionListener(this);
235                 fileOptionsInsertCheckBox.setEnabled(false);
236
237                 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));
238
239                 fileOptionsForceInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.force-insert"));
240                 fileOptionsForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.force-insert.tooltip"));
241                 fileOptionsForceInsertCheckBox.setName("force-insert");
242                 fileOptionsForceInsertCheckBox.setMnemonic(KeyEvent.VK_F);
243                 fileOptionsForceInsertCheckBox.addActionListener(this);
244                 fileOptionsForceInsertCheckBox.setEnabled(false);
245
246                 fileOptionsPanel.add(fileOptionsForceInsertCheckBox, new GridBagConstraints(0, 5, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
247
248                 fileOptionsCustomKeyTextField = new JTextField(45);
249                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
250                 fileOptionsCustomKeyTextField.setEnabled(false);
251                 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
252
253                 fileOptionsInsertRedirectCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert-redirect"), false);
254                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
255                 fileOptionsInsertRedirectCheckBox.setName("insert-redirect");
256                 fileOptionsInsertRedirectCheckBox.setMnemonic(KeyEvent.VK_R);
257                 fileOptionsInsertRedirectCheckBox.addActionListener(this);
258                 fileOptionsInsertRedirectCheckBox.setEnabled(false);
259
260                 final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField);
261                 fileOptionsPanel.add(fileOptionsInsertRedirectCheckBox, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
262                 fileOptionsPanel.add(customKeyLabel, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 6, 0, 0), 0, 0));
263                 fileOptionsPanel.add(fileOptionsCustomKeyTextField, new GridBagConstraints(2, 6, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
264
265                 fileOptionsRenameCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.rename"), false);
266                 fileOptionsRenameCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.rename.tooltip"));
267                 fileOptionsRenameCheckBox.setName("rename");
268                 fileOptionsRenameCheckBox.setMnemonic(KeyEvent.VK_N);
269                 fileOptionsRenameCheckBox.addActionListener(this);
270                 fileOptionsRenameCheckBox.setEnabled(false);
271
272                 fileOptionsRenameTextField = new JTextField();
273                 fileOptionsRenameTextField.setEnabled(false);
274                 fileOptionsRenameTextField.getDocument().addDocumentListener(new DocumentListener() {
275
276                         @SuppressWarnings("synthetic-access")
277                         private void storeText(DocumentEvent documentEvent) {
278                                 FileOption fileOption = getSelectedFile();
279                                 if (fileOption == null) {
280                                         /* no file selected. */
281                                         return;
282                                 }
283                                 Document document = documentEvent.getDocument();
284                                 int documentLength = document.getLength();
285                                 try {
286                                         fileOption.setChangedName(document.getText(0, documentLength).trim());
287                                 } catch (BadLocationException ble1) {
288                                         /* ignore, it should never happen. */
289                                 }
290                         }
291
292                         @Override
293                         public void changedUpdate(DocumentEvent documentEvent) {
294                                 storeText(documentEvent);
295                         }
296
297                         @Override
298                         public void insertUpdate(DocumentEvent documentEvent) {
299                                 storeText(documentEvent);
300                         }
301
302                         @Override
303                         public void removeUpdate(DocumentEvent documentEvent) {
304                                 storeText(documentEvent);
305                         }
306
307                 });
308
309                 fileOptionsPanel.add(fileOptionsRenameCheckBox, new GridBagConstraints(0, 7, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
310                 fileOptionsPanel.add(fileOptionsRenameTextField, new GridBagConstraints(2, 7, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
311
312                 fileOptionsMIMETypeComboBox = new JComboBox(MimeTypes.getAllMimeTypes().toArray());
313                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
314                 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
315                 fileOptionsMIMETypeComboBox.addActionListener(this);
316                 fileOptionsMIMETypeComboBox.setEditable(true);
317                 fileOptionsMIMETypeComboBox.setEnabled(false);
318
319                 final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
320                 fileOptionsPanel.add(mimeTypeLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
321                 fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 8, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
322
323                 /* create dialog to show while scanning. */
324                 scanningFilesDialog = new JDialog(wizard);
325                 scanningFilesDialog.setModal(true);
326                 scanningFilesDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
327
328                 JPanel progressPanel = new JPanel(new BorderLayout(12, 12));
329                 scanningFilesDialog.getContentPane().add(progressPanel, BorderLayout.CENTER);
330                 progressPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
331
332                 final TLabel scanningLabel = new TLabel(I18n.getMessage("jsite.project-files.scanning"), SwingConstants.CENTER);
333                 progressPanel.add(scanningLabel, BorderLayout.NORTH);
334                 progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
335                 progressPanel.add(progressBar, BorderLayout.SOUTH);
336                 progressBar.setIndeterminate(true);
337                 progressBar.setStringPainted(true);
338                 progressBar.setPreferredSize(new Dimension(progressBar.getPreferredSize().width * 2, progressBar.getPreferredSize().height));
339
340                 scanningFilesDialog.pack();
341                 scanningFilesDialog.addWindowListener(new WindowAdapter() {
342
343                         /**
344                          * {@inheritDoc}
345                          */
346                         @Override
347                         @SuppressWarnings("synthetic-access")
348                         public void windowOpened(WindowEvent e) {
349                                 SwingUtils.center(scanningFilesDialog, wizard);
350                         }
351                 });
352
353                 I18nContainer.getInstance().registerRunnable(new Runnable() {
354
355                         @Override
356                         @SuppressWarnings("synthetic-access")
357                         public void run() {
358                                 ignoreHiddenFilesCheckBox.setText(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
359                                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.projet-files.ignore-hidden-files.tooltip"));
360                                 fileOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
361                                 defaultFileCheckBox.setText(I18n.getMessage("jsite.project-files.default"));
362                                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
363                                 fileOptionsInsertCheckBox.setText(I18n.getMessage("jsite.project-files.insert"));
364                                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
365                                 fileOptionsForceInsertCheckBox.setText(I18n.getMessage("jsite.project-files.force-insert"));
366                                 fileOptionsForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.force-insert.tooltip"));
367                                 fileOptionsInsertRedirectCheckBox.setText(I18n.getMessage("jsite.project-files.insert-redirect"));
368                                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
369                                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
370                                 customKeyLabel.setText(I18n.getMessage("jsite.project-files.custom-key") + ":");
371                                 fileOptionsRenameCheckBox.setText("jsite.project-files.rename");
372                                 fileOptionsRenameCheckBox.setToolTipText("jsite.project-files.rename.tooltip");
373                                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
374                                 mimeTypeLabel.setText(I18n.getMessage("jsite.project-files.mime-type") + ":");
375                                 scanningLabel.setText(I18n.getMessage("jsite.project-files.scanning"));
376                         }
377                 });
378
379                 return projectFilesPanel;
380         }
381
382         /**
383          * Sets the project whose files to manage.
384          *
385          * @param project
386          *            The project whose files to manage
387          */
388         public void setProject(final Project project) {
389                 this.project = project;
390                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
391                 setDescription(I18n.getMessage("jsite.project-files.description"));
392                 ignoreHiddenFilesCheckBox.setSelected(project.isIgnoreHiddenFiles());
393                 I18nContainer.getInstance().registerRunnable(new Runnable() {
394
395                         @Override
396                         public void run() {
397                                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
398                                 setDescription(I18n.getMessage("jsite.project-files.description"));
399                         }
400                 });
401         }
402
403         //
404         // ACTIONS
405         //
406
407         /**
408          * Rescans the project’s files.
409          */
410         private void actionScan() {
411                 projectFileList.clearSelection();
412                 projectFileList.setListData(new Object[0]);
413
414                 wizard.setNextEnabled(false);
415                 wizard.setPreviousEnabled(false);
416                 wizard.setQuitEnabled(false);
417
418                 ignoreHiddenFilesCheckBox.setEnabled(false);
419                 scanAction.setEnabled(false);
420
421                 delayedNotification = new StoppableDelay(new Runnable() {
422
423                         @Override
424                         @SuppressWarnings("synthetic-access")
425                         public void run() {
426                                 scanningFilesDialog.setVisible(true);
427                         }
428                 }, new Runnable() {
429
430                         @Override
431                         @SuppressWarnings("synthetic-access")
432                         public void run() {
433                                 scanningFilesDialog.setVisible(false);
434                         }
435                 }, 2000);
436                 new Thread(fileScanner).start();
437                 new Thread(delayedNotification).start();
438                 new Thread(new Runnable() {
439
440                         @Override
441                         @SuppressWarnings("synthetic-access")
442                         public void run() {
443                                 while (!delayedNotification.isFinished()) {
444                                         try {
445                                                 Thread.sleep(250);
446                                         } catch (InterruptedException ie1) {
447                                                 /* ignore. */
448                                         }
449                                         progressBar.setString(fileScanner.getLastFilename());
450                                 }
451                         }
452                 }).start();
453         }
454
455         /**
456          * {@inheritDoc}
457          * <p>
458          * Updates the file list.
459          */
460         @Override
461         public void fileScannerFinished(FileScanner fileScanner) {
462                 delayedNotification.finish();
463                 final boolean error = fileScanner.isError();
464                 if (!error) {
465                         final List<ScannedFile> files = fileScanner.getFiles();
466                         SwingUtilities.invokeLater(new Runnable() {
467
468                                 @Override
469                                 @SuppressWarnings("synthetic-access")
470                                 public void run() {
471                                         projectFileList.setListData(files.toArray());
472                                         projectFileList.clearSelection();
473                                 }
474                         });
475                         Set<String> entriesToRemove = new HashSet<String>();
476                         Iterator<String> filenames = new HashSet<String>(project.getFileOptions().keySet()).iterator();
477                         while (filenames.hasNext()) {
478                                 String filename = filenames.next();
479                                 boolean found = false;
480                                 for (ScannedFile scannedFile : files) {
481                                         if (scannedFile.getFilename().equals(filename)) {
482                                                 found = true;
483                                                 break;
484                                         }
485                                 }
486                                 if (!found) {
487                                         entriesToRemove.add(filename);
488                                 }
489                         }
490                         for (String filename : entriesToRemove) {
491                                 project.setFileOption(filename, null);
492                         }
493                 } else {
494                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
495                 }
496                 SwingUtilities.invokeLater(new Runnable() {
497
498                         @Override
499                         @SuppressWarnings("synthetic-access")
500                         public void run() {
501                                 wizard.setPreviousEnabled(true);
502                                 wizard.setNextEnabled(!error);
503                                 wizard.setQuitEnabled(true);
504                                 ignoreHiddenFilesCheckBox.setEnabled(true);
505                                 scanAction.setEnabled(true);
506                         }
507                 });
508         }
509
510         /**
511          * Returns the {@link FileOption file options} for the currently selected
512          * file.
513          *
514          * @return The {@link FileOption}s for the selected file, or {@code null} if
515          *         no file is selected
516          */
517         private FileOption getSelectedFile() {
518                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
519                 if (scannedFile == null) {
520                         return null;
521                 }
522                 return project.getFileOption(scannedFile.getFilename());
523         }
524
525         //
526         // INTERFACE ActionListener
527         //
528
529         /**
530          * {@inheritDoc}
531          */
532         @Override
533         public void actionPerformed(ActionEvent actionEvent) {
534                 Object source = actionEvent.getSource();
535                 if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) {
536                         project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected());
537                         actionScan();
538                         return;
539                 }
540                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
541                 if (scannedFile == null) {
542                         return;
543                 }
544                 String filename = scannedFile.getFilename();
545                 FileOption fileOption = project.getFileOption(filename);
546                 if (source instanceof JCheckBox) {
547                         JCheckBox checkBox = (JCheckBox) source;
548                         if ("default-file".equals(checkBox.getName())) {
549                                 if (checkBox.isSelected()) {
550                                         if (filename.indexOf('/') > -1) {
551                                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.invalid-default-file"), null, JOptionPane.ERROR_MESSAGE);
552                                                 checkBox.setSelected(false);
553                                         } else {
554                                                 project.setIndexFile(filename);
555                                         }
556                                 } else {
557                                         if (filename.equals(project.getIndexFile())) {
558                                                 project.setIndexFile(null);
559                                         }
560                                 }
561                         } else if ("insert".equals(checkBox.getName())) {
562                                 boolean isInsert = checkBox.isSelected();
563                                 fileOption.setInsert(isInsert);
564                                 fileOptionsInsertRedirectCheckBox.setEnabled(!isInsert);
565                         } else if ("force-insert".equals(checkBox.getName())) {
566                                 boolean isForceInsert = checkBox.isSelected();
567                                 fileOption.setForceInsert(isForceInsert);
568                         } else if ("insert-redirect".equals(checkBox.getName())) {
569                                 boolean isInsertRedirect = checkBox.isSelected();
570                                 fileOption.setInsertRedirect(isInsertRedirect);
571                                 fileOptionsCustomKeyTextField.setEnabled(isInsertRedirect);
572                         } else if ("rename".equals(checkBox.getName())) {
573                                 boolean isRenamed = checkBox.isSelected();
574                                 fileOptionsRenameTextField.setEnabled(isRenamed);
575                                 fileOption.setChangedName(isRenamed ? fileOptionsRenameTextField.getText() : "");
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                         }
582                 }
583         }
584
585         //
586         // INTERFACE ListSelectionListener
587         //
588
589         /**
590          * {@inheritDoc}
591          */
592         @Override
593         @SuppressWarnings("null")
594         public void valueChanged(ListSelectionEvent e) {
595                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
596                 boolean enabled = scannedFile != null;
597                 String filename = (scannedFile == null) ? null : scannedFile.getFilename();
598                 defaultFileCheckBox.setEnabled(enabled);
599                 fileOptionsInsertCheckBox.setEnabled(enabled);
600                 fileOptionsRenameCheckBox.setEnabled(enabled);
601                 fileOptionsMIMETypeComboBox.setEnabled(enabled);
602                 if (filename != null) {
603                         FileOption fileOption = project.getFileOption(filename);
604                         defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
605                         fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
606                         fileOptionsForceInsertCheckBox.setEnabled(scannedFile.getHash().equals(fileOption.getLastInsertHash()));
607                         fileOptionsForceInsertCheckBox.setSelected(fileOption.isForceInsert());
608                         fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert());
609                         fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect());
610                         fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect());
611                         fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
612                         fileOptionsRenameCheckBox.setSelected(fileOption.hasChangedName());
613                         fileOptionsRenameTextField.setEnabled(fileOption.hasChangedName());
614                         fileOptionsRenameTextField.setText(fileOption.getChangedName());
615                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
616                 } else {
617                         defaultFileCheckBox.setSelected(false);
618                         fileOptionsInsertCheckBox.setSelected(true);
619                         fileOptionsForceInsertCheckBox.setEnabled(false);
620                         fileOptionsForceInsertCheckBox.setSelected(false);
621                         fileOptionsInsertRedirectCheckBox.setEnabled(false);
622                         fileOptionsInsertRedirectCheckBox.setSelected(false);
623                         fileOptionsCustomKeyTextField.setEnabled(false);
624                         fileOptionsCustomKeyTextField.setText("CHK@");
625                         fileOptionsRenameCheckBox.setEnabled(false);
626                         fileOptionsRenameCheckBox.setSelected(false);
627                         fileOptionsRenameTextField.setEnabled(false);
628                         fileOptionsRenameTextField.setText("");
629                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(MimeTypes.DEFAULT_CONTENT_TYPE);
630                 }
631         }
632
633         //
634         // INTERFACE DocumentListener
635         //
636
637         /**
638          * Updates the options of the currently selected file with the changes made
639          * in the “custom key” textfield.
640          *
641          * @param documentEvent
642          *            The document event to process
643          */
644         private void processDocumentUpdate(DocumentEvent documentEvent) {
645                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
646                 if (scannedFile == null) {
647                         return;
648                 }
649                 FileOption fileOption = project.getFileOption(scannedFile.getFilename());
650                 Document document = documentEvent.getDocument();
651                 try {
652                         String text = document.getText(0, document.getLength());
653                         fileOption.setCustomKey(text);
654                 } catch (BadLocationException ble1) {
655                         /* ignore. */
656                 }
657         }
658
659         /**
660          * {@inheritDoc}
661          */
662         @Override
663         public void changedUpdate(DocumentEvent documentEvent) {
664                 processDocumentUpdate(documentEvent);
665         }
666
667         /**
668          * {@inheritDoc}
669          */
670         @Override
671         public void insertUpdate(DocumentEvent documentEvent) {
672                 processDocumentUpdate(documentEvent);
673         }
674
675         /**
676          * {@inheritDoc}
677          */
678         @Override
679         public void removeUpdate(DocumentEvent documentEvent) {
680                 processDocumentUpdate(documentEvent);
681         }
682
683 }