Show progress dialog if scanning takes more than two seconds.
[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.DelayedNotification;
65 import net.pterodactylus.util.swing.SwingUtils;
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 DelayedNotification 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                 delayedNotification = new DelayedNotification(scanningFilesDialog, 2000);
419                 new Thread(fileScanner).start();
420                 new Thread(delayedNotification).start();
421                 new Thread(new Runnable() {
422
423                         @Override
424                         @SuppressWarnings("synthetic-access")
425                         public void run() {
426                                 while (!delayedNotification.isFinished()) {
427                                         try {
428                                                 Thread.sleep(250);
429                                         } catch (InterruptedException ie1) {
430                                                 /* ignore. */
431                                         }
432                                         progressBar.setString(fileScanner.getLastFilename());
433                                 }
434                         }
435                 }).start();
436         }
437
438         /**
439          * {@inheritDoc}
440          * <p>
441          * Updates the file list.
442          */
443         @Override
444         public void fileScannerFinished(FileScanner fileScanner) {
445                 delayedNotification.finish();
446                 final boolean error = fileScanner.isError();
447                 if (!error) {
448                         final List<ScannedFile> files = fileScanner.getFiles();
449                         SwingUtilities.invokeLater(new Runnable() {
450
451                                 @Override
452                                 @SuppressWarnings("synthetic-access")
453                                 public void run() {
454                                         projectFileList.setListData(files.toArray());
455                                         projectFileList.clearSelection();
456                                 }
457                         });
458                         Set<String> entriesToRemove = new HashSet<String>();
459                         Iterator<String> filenames = new HashSet<String>(project.getFileOptions().keySet()).iterator();
460                         while (filenames.hasNext()) {
461                                 String filename = filenames.next();
462                                 boolean found = false;
463                                 for (ScannedFile scannedFile : files) {
464                                         if (scannedFile.getFilename().equals(filename)) {
465                                                 found = true;
466                                                 break;
467                                         }
468                                 }
469                                 if (!found) {
470                                         entriesToRemove.add(filename);
471                                 }
472                         }
473                         for (String filename : entriesToRemove) {
474                                 project.setFileOption(filename, null);
475                         }
476                 } else {
477                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
478                 }
479                 SwingUtilities.invokeLater(new Runnable() {
480
481                         @Override
482                         @SuppressWarnings("synthetic-access")
483                         public void run() {
484                                 wizard.setPreviousEnabled(true);
485                                 wizard.setNextEnabled(!error);
486                                 wizard.setQuitEnabled(true);
487                         }
488                 });
489         }
490
491         /**
492          * Returns the {@link FileOption file options} for the currently selected
493          * file.
494          *
495          * @return The {@link FileOption}s for the selected file, or {@code null} if
496          *         no file is selected
497          */
498         private FileOption getSelectedFile() {
499                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
500                 if (scannedFile == null) {
501                         return null;
502                 }
503                 return project.getFileOption(scannedFile.getFilename());
504         }
505
506         //
507         // INTERFACE ActionListener
508         //
509
510         /**
511          * {@inheritDoc}
512          */
513         @Override
514         public void actionPerformed(ActionEvent actionEvent) {
515                 Object source = actionEvent.getSource();
516                 if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) {
517                         project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected());
518                         actionScan();
519                         return;
520                 }
521                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
522                 if (scannedFile == null) {
523                         return;
524                 }
525                 String filename = scannedFile.getFilename();
526                 FileOption fileOption = project.getFileOption(filename);
527                 if (source instanceof JCheckBox) {
528                         JCheckBox checkBox = (JCheckBox) source;
529                         if ("default-file".equals(checkBox.getName())) {
530                                 if (checkBox.isSelected()) {
531                                         if (filename.indexOf('/') > -1) {
532                                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.invalid-default-file"), null, JOptionPane.ERROR_MESSAGE);
533                                                 checkBox.setSelected(false);
534                                         } else {
535                                                 project.setIndexFile(filename);
536                                         }
537                                 } else {
538                                         if (filename.equals(project.getIndexFile())) {
539                                                 project.setIndexFile(null);
540                                         }
541                                 }
542                         } else if ("insert".equals(checkBox.getName())) {
543                                 boolean isInsert = checkBox.isSelected();
544                                 fileOption.setInsert(isInsert);
545                                 fileOptionsInsertRedirectCheckBox.setEnabled(!isInsert);
546                         } else if ("force-insert".equals(checkBox.getName())) {
547                                 boolean isForceInsert = checkBox.isSelected();
548                                 fileOption.setForceInsert(isForceInsert);
549                         } else if ("insert-redirect".equals(checkBox.getName())) {
550                                 boolean isInsertRedirect = checkBox.isSelected();
551                                 fileOption.setInsertRedirect(isInsertRedirect);
552                                 fileOptionsCustomKeyTextField.setEnabled(isInsertRedirect);
553                         } else if ("rename".equals(checkBox.getName())) {
554                                 boolean isRenamed = checkBox.isSelected();
555                                 fileOptionsRenameTextField.setEnabled(isRenamed);
556                                 fileOption.setChangedName(isRenamed ? fileOptionsRenameTextField.getText() : "");
557                         }
558                 } else if (source instanceof JComboBox) {
559                         JComboBox comboBox = (JComboBox) source;
560                         if ("project-files.mime-type".equals(comboBox.getName())) {
561                                 fileOption.setMimeType((String) comboBox.getSelectedItem());
562                         }
563                 }
564         }
565
566         //
567         // INTERFACE ListSelectionListener
568         //
569
570         /**
571          * {@inheritDoc}
572          */
573         @Override
574         @SuppressWarnings("null")
575         public void valueChanged(ListSelectionEvent e) {
576                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
577                 boolean enabled = scannedFile != null;
578                 String filename = (scannedFile == null) ? null : scannedFile.getFilename();
579                 defaultFileCheckBox.setEnabled(enabled);
580                 fileOptionsInsertCheckBox.setEnabled(enabled);
581                 fileOptionsRenameCheckBox.setEnabled(enabled);
582                 fileOptionsMIMETypeComboBox.setEnabled(enabled);
583                 if (filename != null) {
584                         FileOption fileOption = project.getFileOption(filename);
585                         defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
586                         fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
587                         fileOptionsForceInsertCheckBox.setEnabled(scannedFile.getHash().equals(fileOption.getLastInsertHash()));
588                         fileOptionsForceInsertCheckBox.setSelected(fileOption.isForceInsert());
589                         fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert());
590                         fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect());
591                         fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect());
592                         fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
593                         fileOptionsRenameCheckBox.setSelected(fileOption.hasChangedName());
594                         fileOptionsRenameTextField.setEnabled(fileOption.hasChangedName());
595                         fileOptionsRenameTextField.setText(fileOption.getChangedName());
596                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
597                 } else {
598                         defaultFileCheckBox.setSelected(false);
599                         fileOptionsInsertCheckBox.setSelected(true);
600                         fileOptionsForceInsertCheckBox.setEnabled(false);
601                         fileOptionsForceInsertCheckBox.setSelected(false);
602                         fileOptionsInsertRedirectCheckBox.setEnabled(false);
603                         fileOptionsInsertRedirectCheckBox.setSelected(false);
604                         fileOptionsCustomKeyTextField.setEnabled(false);
605                         fileOptionsCustomKeyTextField.setText("CHK@");
606                         fileOptionsRenameCheckBox.setEnabled(false);
607                         fileOptionsRenameCheckBox.setSelected(false);
608                         fileOptionsRenameTextField.setEnabled(false);
609                         fileOptionsRenameTextField.setText("");
610                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(MimeTypes.DEFAULT_CONTENT_TYPE);
611                 }
612         }
613
614         //
615         // INTERFACE DocumentListener
616         //
617
618         /**
619          * Updates the options of the currently selected file with the changes made
620          * in the “custom key” textfield.
621          *
622          * @param documentEvent
623          *            The document event to process
624          */
625         private void processDocumentUpdate(DocumentEvent documentEvent) {
626                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
627                 if (scannedFile == null) {
628                         return;
629                 }
630                 FileOption fileOption = project.getFileOption(scannedFile.getFilename());
631                 Document document = documentEvent.getDocument();
632                 try {
633                         String text = document.getText(0, document.getLength());
634                         fileOption.setCustomKey(text);
635                 } catch (BadLocationException ble1) {
636                         /* ignore. */
637                 }
638         }
639
640         /**
641          * {@inheritDoc}
642          */
643         @Override
644         public void changedUpdate(DocumentEvent documentEvent) {
645                 processDocumentUpdate(documentEvent);
646         }
647
648         /**
649          * {@inheritDoc}
650          */
651         @Override
652         public void insertUpdate(DocumentEvent documentEvent) {
653                 processDocumentUpdate(documentEvent);
654         }
655
656         /**
657          * {@inheritDoc}
658          */
659         @Override
660         public void removeUpdate(DocumentEvent documentEvent) {
661                 processDocumentUpdate(documentEvent);
662         }
663
664 }