Add missing @Override annotations.
[jSite.git] / src / 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.text.MessageFormat;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Set;
34
35 import javax.swing.AbstractAction;
36 import javax.swing.Action;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JComboBox;
40 import javax.swing.JComponent;
41 import javax.swing.JLabel;
42 import javax.swing.JList;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JScrollPane;
46 import javax.swing.JTextField;
47 import javax.swing.ListSelectionModel;
48 import javax.swing.SwingUtilities;
49 import javax.swing.event.DocumentEvent;
50 import javax.swing.event.DocumentListener;
51 import javax.swing.event.ListSelectionEvent;
52 import javax.swing.event.ListSelectionListener;
53 import javax.swing.text.BadLocationException;
54 import javax.swing.text.Document;
55
56 import de.todesbaum.jsite.application.FileOption;
57 import de.todesbaum.jsite.application.Project;
58 import de.todesbaum.jsite.gui.FileScanner.ScannedFile;
59 import de.todesbaum.jsite.i18n.I18n;
60 import de.todesbaum.jsite.i18n.I18nContainer;
61 import de.todesbaum.util.mime.DefaultMIMETypes;
62 import de.todesbaum.util.swing.TLabel;
63 import de.todesbaum.util.swing.TWizard;
64 import de.todesbaum.util.swing.TWizardPage;
65
66 /**
67  * Wizard page that lets the user manage the files of a project.
68  *
69  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
70  */
71 public class ProjectFilesPage extends TWizardPage implements ActionListener, ListSelectionListener, DocumentListener, FileScannerListener {
72
73         /** The project. */
74         private Project project;
75
76         /** The “scan files” action. */
77         private Action scanAction;
78
79         /** The “ignore hidden files” checkbox. */
80         private JCheckBox ignoreHiddenFilesCheckBox;
81
82         /** The list of project files. */
83         private JList projectFileList;
84
85         /** The “default file” checkbox. */
86         private JCheckBox defaultFileCheckBox;
87
88         /** The “insert” checkbox. */
89         private JCheckBox fileOptionsInsertCheckBox;
90
91         /** The “force insert” checkbox. */
92         private JCheckBox fileOptionsForceInsertCheckBox;
93
94         /** The “insert redirect” checkbox. */
95         private JCheckBox fileOptionsInsertRedirectCheckBox;
96
97         /** The “custom key” textfield. */
98         private JTextField fileOptionsCustomKeyTextField;
99
100         /** The “rename” check box. */
101         private JCheckBox fileOptionsRenameCheckBox;
102
103         /** The “new name” text field. */
104         private JTextField fileOptionsRenameTextField;
105
106         /** The “mime type” combo box. */
107         private JComboBox fileOptionsMIMETypeComboBox;
108
109         /**
110          * Creates a new project file page.
111          *
112          * @param wizard
113          *            The wizard the page belongs to
114          */
115         public ProjectFilesPage(final TWizard wizard) {
116                 super(wizard);
117                 pageInit();
118         }
119
120         /**
121          * Initializes the page and all its actions and components.
122          */
123         private void pageInit() {
124                 createActions();
125                 setLayout(new BorderLayout(12, 12));
126                 add(createProjectFilesPanel(), BorderLayout.CENTER);
127         }
128
129         /**
130          * Creates all actions.
131          */
132         private void createActions() {
133                 scanAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.rescan")) {
134
135                         @Override
136                         @SuppressWarnings("synthetic-access")
137                         public void actionPerformed(ActionEvent actionEvent) {
138                                 actionScan();
139                         }
140                 };
141                 scanAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
142                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
143
144                 I18nContainer.getInstance().registerRunnable(new Runnable() {
145
146                         @Override
147                         @SuppressWarnings("synthetic-access")
148                         public void run() {
149                                 scanAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.rescan"));
150                                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
151                         }
152                 });
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         @Override
159         public void pageAdded(TWizard wizard) {
160                 actionScan();
161                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
162                 this.wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
163                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
164         }
165
166         /**
167          * Creates the panel contains the project file list and options.
168          *
169          * @return The created panel
170          */
171         private JComponent createProjectFilesPanel() {
172                 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
173
174                 projectFileList = new JList();
175                 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
176                 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
177                 projectFileList.addListSelectionListener(this);
178
179                 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
180
181                 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
182                 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
183                 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
184                 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
185
186                 ignoreHiddenFilesCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
187                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.ignore-hidden-files.tooltip"));
188                 ignoreHiddenFilesCheckBox.setName("ignore-hidden-files");
189                 ignoreHiddenFilesCheckBox.addActionListener(this);
190                 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));
191
192                 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));
193
194                 final JLabel fileOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
195                 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));
196
197                 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
198                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
199                 defaultFileCheckBox.setName("default-file");
200                 defaultFileCheckBox.addActionListener(this);
201                 defaultFileCheckBox.setEnabled(false);
202
203                 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));
204
205                 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
206                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
207                 fileOptionsInsertCheckBox.setName("insert");
208                 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
209                 fileOptionsInsertCheckBox.addActionListener(this);
210                 fileOptionsInsertCheckBox.setEnabled(false);
211
212                 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));
213
214                 fileOptionsForceInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.force-insert"));
215                 fileOptionsForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.force-insert.tooltip"));
216                 fileOptionsForceInsertCheckBox.setName("force-insert");
217                 fileOptionsForceInsertCheckBox.setMnemonic(KeyEvent.VK_F);
218                 fileOptionsForceInsertCheckBox.addActionListener(this);
219                 fileOptionsForceInsertCheckBox.setEnabled(false);
220
221                 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));
222
223                 fileOptionsCustomKeyTextField = new JTextField(45);
224                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
225                 fileOptionsCustomKeyTextField.setEnabled(false);
226                 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
227
228                 fileOptionsInsertRedirectCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert-redirect"), false);
229                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
230                 fileOptionsInsertRedirectCheckBox.setName("insert-redirect");
231                 fileOptionsInsertRedirectCheckBox.setMnemonic(KeyEvent.VK_R);
232                 fileOptionsInsertRedirectCheckBox.addActionListener(this);
233                 fileOptionsInsertRedirectCheckBox.setEnabled(false);
234
235                 final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField);
236                 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));
237                 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));
238                 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));
239
240                 fileOptionsRenameCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.rename"), false);
241                 fileOptionsRenameCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.rename.tooltip"));
242                 fileOptionsRenameCheckBox.setName("rename");
243                 fileOptionsRenameCheckBox.setMnemonic(KeyEvent.VK_N);
244                 fileOptionsRenameCheckBox.addActionListener(this);
245                 fileOptionsRenameCheckBox.setEnabled(false);
246
247                 fileOptionsRenameTextField = new JTextField();
248                 fileOptionsRenameTextField.setEnabled(false);
249                 fileOptionsRenameTextField.getDocument().addDocumentListener(new DocumentListener() {
250
251                         @SuppressWarnings("synthetic-access")
252                         private void storeText(DocumentEvent documentEvent) {
253                                 FileOption fileOption = getSelectedFile();
254                                 if (fileOption == null) {
255                                         /* no file selected. */
256                                         return;
257                                 }
258                                 Document document = documentEvent.getDocument();
259                                 int documentLength = document.getLength();
260                                 try {
261                                         fileOption.setChangedName(document.getText(0, documentLength).trim());
262                                 } catch (BadLocationException ble1) {
263                                         /* ignore, it should never happen. */
264                                 }
265                         }
266
267                         @Override
268                         public void changedUpdate(DocumentEvent documentEvent) {
269                                 storeText(documentEvent);
270                         }
271
272                         @Override
273                         public void insertUpdate(DocumentEvent documentEvent) {
274                                 storeText(documentEvent);
275                         }
276
277                         @Override
278                         public void removeUpdate(DocumentEvent documentEvent) {
279                                 storeText(documentEvent);
280                         }
281
282                 });
283
284                 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));
285                 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));
286
287                 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
288                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
289                 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
290                 fileOptionsMIMETypeComboBox.addActionListener(this);
291                 fileOptionsMIMETypeComboBox.setEditable(true);
292                 fileOptionsMIMETypeComboBox.setEnabled(false);
293
294                 final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
295                 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));
296                 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));
297
298                 I18nContainer.getInstance().registerRunnable(new Runnable() {
299
300                         @Override
301                         @SuppressWarnings("synthetic-access")
302                         public void run() {
303                                 ignoreHiddenFilesCheckBox.setText(I18n.getMessage("jsite.project-files.ignore-hidden-files"));
304                                 ignoreHiddenFilesCheckBox.setToolTipText(I18n.getMessage("jsite.projet-files.ignore-hidden-files.tooltip"));
305                                 fileOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
306                                 defaultFileCheckBox.setText(I18n.getMessage("jsite.project-files.default"));
307                                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
308                                 fileOptionsInsertCheckBox.setText(I18n.getMessage("jsite.project-files.insert"));
309                                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
310                                 fileOptionsForceInsertCheckBox.setText(I18n.getMessage("jsite.project-files.force-insert"));
311                                 fileOptionsForceInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.force-insert.tooltip"));
312                                 fileOptionsInsertRedirectCheckBox.setText(I18n.getMessage("jsite.project-files.insert-redirect"));
313                                 fileOptionsInsertRedirectCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert-redirect.tooltip"));
314                                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
315                                 customKeyLabel.setText(I18n.getMessage("jsite.project-files.custom-key") + ":");
316                                 fileOptionsRenameCheckBox.setText("jsite.project-files.rename");
317                                 fileOptionsRenameCheckBox.setToolTipText("jsite.project-files.rename.tooltip");
318                                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
319                                 mimeTypeLabel.setText(I18n.getMessage("jsite.project-files.mime-type") + ":");
320                         }
321                 });
322
323                 return projectFilesPanel;
324         }
325
326         /**
327          * Sets the project whose files to manage.
328          *
329          * @param project
330          *            The project whose files to manage
331          */
332         public void setProject(final Project project) {
333                 this.project = project;
334                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
335                 setDescription(I18n.getMessage("jsite.project-files.description"));
336                 ignoreHiddenFilesCheckBox.setSelected(project.isIgnoreHiddenFiles());
337                 I18nContainer.getInstance().registerRunnable(new Runnable() {
338
339                         @Override
340                         public void run() {
341                                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
342                                 setDescription(I18n.getMessage("jsite.project-files.description"));
343                         }
344                 });
345         }
346
347         //
348         // ACTIONS
349         //
350
351         /**
352          * Rescans the project’s files.
353          */
354         private void actionScan() {
355                 projectFileList.clearSelection();
356                 projectFileList.setListData(new Object[0]);
357
358                 wizard.setNextEnabled(false);
359                 wizard.setPreviousEnabled(false);
360                 wizard.setQuitEnabled(false);
361
362                 FileScanner fileScanner = new FileScanner(project);
363                 fileScanner.addFileScannerListener(this);
364                 new Thread(fileScanner).start();
365         }
366
367         /**
368          * {@inheritDoc}
369          * <p>
370          * Updates the file list.
371          */
372         @Override
373         public void fileScannerFinished(FileScanner fileScanner) {
374                 final boolean error = fileScanner.isError();
375                 if (!error) {
376                         final List<ScannedFile> files = fileScanner.getFiles();
377                         SwingUtilities.invokeLater(new Runnable() {
378
379                                 @Override
380                                 @SuppressWarnings("synthetic-access")
381                                 public void run() {
382                                         projectFileList.setListData(files.toArray());
383                                         projectFileList.clearSelection();
384                                 }
385                         });
386                         Set<String> entriesToRemove = new HashSet<String>();
387                         Iterator<String> filenames = new HashSet<String>(project.getFileOptions().keySet()).iterator();
388                         while (filenames.hasNext()) {
389                                 String filename = filenames.next();
390                                 boolean found = false;
391                                 for (ScannedFile scannedFile : files) {
392                                         if (scannedFile.getFilename().equals(filename)) {
393                                                 found = true;
394                                                 break;
395                                         }
396                                 }
397                                 if (!found) {
398                                         entriesToRemove.add(filename);
399                                 }
400                         }
401                         for (String filename : entriesToRemove) {
402                                 project.setFileOption(filename, null);
403                         }
404                 } else {
405                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
406                 }
407                 SwingUtilities.invokeLater(new Runnable() {
408
409                         @Override
410                         @SuppressWarnings("synthetic-access")
411                         public void run() {
412                                 wizard.setPreviousEnabled(true);
413                                 wizard.setNextEnabled(!error);
414                                 wizard.setQuitEnabled(true);
415                         }
416                 });
417         }
418
419         /**
420          * Returns the {@link FileOption file options} for the currently selected
421          * file.
422          *
423          * @return The {@link FileOption}s for the selected file, or {@code null} if
424          *         no file is selected
425          */
426         private FileOption getSelectedFile() {
427                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
428                 if (scannedFile == null) {
429                         return null;
430                 }
431                 return project.getFileOption(scannedFile.getFilename());
432         }
433
434         //
435         // INTERFACE ActionListener
436         //
437
438         /**
439          * {@inheritDoc}
440          */
441         @Override
442         public void actionPerformed(ActionEvent actionEvent) {
443                 Object source = actionEvent.getSource();
444                 if ((source instanceof JCheckBox) && ("ignore-hidden-files".equals(((JCheckBox) source).getName()))) {
445                         project.setIgnoreHiddenFiles(((JCheckBox) source).isSelected());
446                         actionScan();
447                         return;
448                 }
449                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
450                 if (scannedFile == null) {
451                         return;
452                 }
453                 String filename = scannedFile.getFilename();
454                 FileOption fileOption = project.getFileOption(filename);
455                 if (source instanceof JCheckBox) {
456                         JCheckBox checkBox = (JCheckBox) source;
457                         if ("default-file".equals(checkBox.getName())) {
458                                 if (checkBox.isSelected()) {
459                                         if (filename.indexOf('/') > -1) {
460                                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.invalid-default-file"), null, JOptionPane.ERROR_MESSAGE);
461                                                 checkBox.setSelected(false);
462                                         } else {
463                                                 project.setIndexFile(filename);
464                                         }
465                                 } else {
466                                         if (filename.equals(project.getIndexFile())) {
467                                                 project.setIndexFile(null);
468                                         }
469                                 }
470                         } else if ("insert".equals(checkBox.getName())) {
471                                 boolean isInsert = checkBox.isSelected();
472                                 fileOption.setInsert(isInsert);
473                                 fileOptionsInsertRedirectCheckBox.setEnabled(!isInsert);
474                         } else if ("force-insert".equals(checkBox.getName())) {
475                                 boolean isForceInsert = checkBox.isSelected();
476                                 fileOption.setForceInsert(isForceInsert);
477                         } else if ("insert-redirect".equals(checkBox.getName())) {
478                                 boolean isInsertRedirect = checkBox.isSelected();
479                                 fileOption.setInsertRedirect(isInsertRedirect);
480                                 fileOptionsCustomKeyTextField.setEnabled(isInsertRedirect);
481                         } else if ("rename".equals(checkBox.getName())) {
482                                 boolean isRenamed = checkBox.isSelected();
483                                 fileOptionsRenameTextField.setEnabled(isRenamed);
484                                 fileOption.setChangedName(isRenamed ? fileOptionsRenameTextField.getText() : "");
485                         }
486                 } else if (source instanceof JComboBox) {
487                         JComboBox comboBox = (JComboBox) source;
488                         if ("project-files.mime-type".equals(comboBox.getName())) {
489                                 fileOption.setMimeType((String) comboBox.getSelectedItem());
490                         }
491                 }
492         }
493
494         //
495         // INTERFACE ListSelectionListener
496         //
497
498         /**
499          * {@inheritDoc}
500          */
501         @Override
502         @SuppressWarnings("null")
503         public void valueChanged(ListSelectionEvent e) {
504                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
505                 boolean enabled = scannedFile != null;
506                 String filename = (scannedFile == null) ? null : scannedFile.getFilename();
507                 defaultFileCheckBox.setEnabled(enabled);
508                 fileOptionsInsertCheckBox.setEnabled(enabled);
509                 fileOptionsRenameCheckBox.setEnabled(enabled);
510                 fileOptionsMIMETypeComboBox.setEnabled(enabled);
511                 if (filename != null) {
512                         FileOption fileOption = project.getFileOption(filename);
513                         defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
514                         fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
515                         fileOptionsForceInsertCheckBox.setEnabled(scannedFile.getHash().equals(fileOption.getLastInsertHash()));
516                         fileOptionsForceInsertCheckBox.setSelected(fileOption.isForceInsert());
517                         fileOptionsInsertRedirectCheckBox.setEnabled(!fileOption.isInsert());
518                         fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect());
519                         fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect());
520                         fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
521                         fileOptionsRenameCheckBox.setSelected(fileOption.hasChangedName());
522                         fileOptionsRenameTextField.setEnabled(fileOption.hasChangedName());
523                         fileOptionsRenameTextField.setText(fileOption.getChangedName());
524                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
525                 } else {
526                         defaultFileCheckBox.setSelected(false);
527                         fileOptionsInsertCheckBox.setSelected(true);
528                         fileOptionsForceInsertCheckBox.setEnabled(false);
529                         fileOptionsForceInsertCheckBox.setSelected(false);
530                         fileOptionsInsertRedirectCheckBox.setEnabled(false);
531                         fileOptionsInsertRedirectCheckBox.setSelected(false);
532                         fileOptionsCustomKeyTextField.setEnabled(false);
533                         fileOptionsCustomKeyTextField.setText("CHK@");
534                         fileOptionsRenameCheckBox.setEnabled(false);
535                         fileOptionsRenameCheckBox.setSelected(false);
536                         fileOptionsRenameTextField.setEnabled(false);
537                         fileOptionsRenameTextField.setText("");
538                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
539                 }
540         }
541
542         //
543         // INTERFACE DocumentListener
544         //
545
546         /**
547          * Updates the options of the currently selected file with the changes made
548          * in the “custom key” textfield.
549          *
550          * @param documentEvent
551          *            The document event to process
552          */
553         private void processDocumentUpdate(DocumentEvent documentEvent) {
554                 ScannedFile scannedFile = (ScannedFile) projectFileList.getSelectedValue();
555                 if (scannedFile == null) {
556                         return;
557                 }
558                 FileOption fileOption = project.getFileOption(scannedFile.getFilename());
559                 Document document = documentEvent.getDocument();
560                 try {
561                         String text = document.getText(0, document.getLength());
562                         fileOption.setCustomKey(text);
563                 } catch (BadLocationException ble1) {
564                         /* ignore. */
565                 }
566         }
567
568         /**
569          * {@inheritDoc}
570          */
571         @Override
572         public void changedUpdate(DocumentEvent documentEvent) {
573                 processDocumentUpdate(documentEvent);
574         }
575
576         /**
577          * {@inheritDoc}
578          */
579         @Override
580         public void insertUpdate(DocumentEvent documentEvent) {
581                 processDocumentUpdate(documentEvent);
582         }
583
584         /**
585          * {@inheritDoc}
586          */
587         @Override
588         public void removeUpdate(DocumentEvent documentEvent) {
589                 processDocumentUpdate(documentEvent);
590         }
591
592 }