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