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