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