3789ebddba22976a431caa43118f9b3d48958c6b
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectFilesPage.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.KeyEvent;
31 import java.text.MessageFormat;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Set;
38
39 import javax.swing.AbstractAction;
40 import javax.swing.Action;
41 import javax.swing.DefaultComboBoxModel;
42 import javax.swing.JButton;
43 import javax.swing.JCheckBox;
44 import javax.swing.JComboBox;
45 import javax.swing.JComponent;
46 import javax.swing.JLabel;
47 import javax.swing.JList;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPanel;
50 import javax.swing.JScrollPane;
51 import javax.swing.JSpinner;
52 import javax.swing.JTextField;
53 import javax.swing.ListSelectionModel;
54 import javax.swing.SpinnerNumberModel;
55 import javax.swing.SwingUtilities;
56 import javax.swing.border.EmptyBorder;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.event.DocumentEvent;
60 import javax.swing.event.DocumentListener;
61 import javax.swing.event.ListSelectionEvent;
62 import javax.swing.event.ListSelectionListener;
63 import javax.swing.text.BadLocationException;
64 import javax.swing.text.Document;
65
66 import de.todesbaum.jsite.application.FileOption;
67 import de.todesbaum.jsite.application.Project;
68 import de.todesbaum.jsite.i18n.I18n;
69 import de.todesbaum.jsite.i18n.I18nContainer;
70 import de.todesbaum.util.mime.DefaultMIMETypes;
71 import de.todesbaum.util.swing.TLabel;
72 import de.todesbaum.util.swing.TWizard;
73 import de.todesbaum.util.swing.TWizardPage;
74
75 /**
76  * @author David Roden <droden@gmail.com>
77  * @version $Id$
78  */
79 public class ProjectFilesPage extends TWizardPage implements ActionListener, ListSelectionListener, DocumentListener, FileScannerListener, ChangeListener {
80
81         private Project project;
82
83         private Action scanAction;
84         private Action editContainerAction;
85         private Action addContainerAction;
86         private Action deleteContainerAction;
87
88         private JList projectFileList;
89         private JCheckBox defaultFileCheckBox;
90         private JCheckBox fileOptionsInsertCheckBox;
91         private JTextField fileOptionsCustomKeyTextField;
92         private JComboBox fileOptionsMIMETypeComboBox;
93         private DefaultComboBoxModel containerComboBoxModel;
94         private JComboBox fileOptionsContainerComboBox;
95         private JSpinner replaceEditionRangeSpinner;
96         private JCheckBox replacementCheckBox;
97
98         public ProjectFilesPage(final TWizard wizard) {
99                 super(wizard);
100                 pageInit();
101         }
102
103         private void pageInit() {
104                 createActions();
105                 setLayout(new BorderLayout(12, 12));
106                 add(createProjectFilesPanel(), BorderLayout.CENTER);
107         }
108
109         private void createActions() {
110                 scanAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.rescan")) {
111
112                         @SuppressWarnings("synthetic-access")
113                         public void actionPerformed(ActionEvent actionEvent) {
114                                 actionScan();
115                         }
116                 };
117                 scanAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
118                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
119
120                 addContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.add-container")) {
121
122                         @SuppressWarnings("synthetic-access")
123                         public void actionPerformed(ActionEvent actionEvent) {
124                                 actionAddContainer();
125                         }
126                 };
127                 addContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.add-container.tooltip"));
128                 addContainerAction.setEnabled(false);
129
130                 editContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.edit-container")) {
131
132                         @SuppressWarnings("synthetic-access")
133                         public void actionPerformed(ActionEvent actionEvent) {
134                                 actionEditContainer();
135                         }
136                 };
137                 editContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.edit-container.tooltip"));
138                 editContainerAction.setEnabled(false);
139
140                 deleteContainerAction = new AbstractAction(I18n.getMessage("jsite.project-files.action.delete-container")) {
141
142                         @SuppressWarnings("synthetic-access")
143                         public void actionPerformed(ActionEvent actionEvent) {
144                                 actionDeleteContainer();
145                         }
146                 };
147                 deleteContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.delete-container.tooltip"));
148                 deleteContainerAction.setEnabled(false);
149
150                 I18nContainer.getInstance().registerRunnable(new Runnable() {
151
152                         @SuppressWarnings("synthetic-access")
153                         public void run() {
154                                 scanAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.rescan"));
155                                 scanAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.rescan.tooltip"));
156                                 addContainerAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.add-container"));
157                                 addContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.add-container.tooltip"));
158                                 editContainerAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.edit-container"));
159                                 editContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.edit-container.tooltip"));
160                                 deleteContainerAction.putValue(Action.NAME, I18n.getMessage("jsite.project-files.action.delete-container"));
161                                 deleteContainerAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project-files.action.delete-container.tooltip"));
162                         }
163                 });
164         }
165
166         @Override
167         public void pageAdded(TWizard wizard) {
168                 actionScan();
169                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
170                 this.wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
171                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
172         }
173
174         private JComponent createProjectFilesPanel() {
175                 JPanel projectFilesPanel = new JPanel(new BorderLayout(12, 12));
176
177                 projectFileList = new JList();
178                 projectFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
179                 projectFileList.setMinimumSize(new Dimension(250, projectFileList.getPreferredSize().height));
180                 projectFileList.addListSelectionListener(this);
181
182                 projectFilesPanel.add(new JScrollPane(projectFileList), BorderLayout.CENTER);
183
184                 JPanel fileOptionsAlignmentPanel = new JPanel(new BorderLayout(12, 12));
185                 projectFilesPanel.add(fileOptionsAlignmentPanel, BorderLayout.PAGE_END);
186                 JPanel fileOptionsPanel = new JPanel(new GridBagLayout());
187                 fileOptionsAlignmentPanel.add(fileOptionsPanel, BorderLayout.PAGE_START);
188
189                 fileOptionsPanel.add(new JButton(scanAction), new GridBagConstraints(0, 0, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
190
191                 final JLabel fileOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
192                 fileOptionsPanel.add(fileOptionsLabel, 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                 defaultFileCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.default"));
195                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
196                 defaultFileCheckBox.setName("default-file");
197                 defaultFileCheckBox.addActionListener(this);
198                 defaultFileCheckBox.setEnabled(false);
199
200                 fileOptionsPanel.add(defaultFileCheckBox, new GridBagConstraints(0, 2, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
201
202                 fileOptionsInsertCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.insert"), true);
203                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
204                 fileOptionsInsertCheckBox.setName("insert");
205                 fileOptionsInsertCheckBox.setMnemonic(KeyEvent.VK_I);
206                 fileOptionsInsertCheckBox.addActionListener(this);
207                 fileOptionsInsertCheckBox.setEnabled(false);
208
209                 fileOptionsPanel.add(fileOptionsInsertCheckBox, new GridBagConstraints(0, 3, 5, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
210
211                 fileOptionsCustomKeyTextField = new JTextField(45);
212                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
213                 fileOptionsCustomKeyTextField.setEnabled(false);
214                 fileOptionsCustomKeyTextField.getDocument().addDocumentListener(this);
215
216                 final TLabel customKeyLabel = new TLabel(I18n.getMessage("jsite.project-files.custom-key") + ":", KeyEvent.VK_K, fileOptionsCustomKeyTextField);
217                 fileOptionsPanel.add(customKeyLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
218                 fileOptionsPanel.add(fileOptionsCustomKeyTextField, new GridBagConstraints(1, 4, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
219
220                 fileOptionsMIMETypeComboBox = new JComboBox(DefaultMIMETypes.getAllMIMETypes());
221                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
222                 fileOptionsMIMETypeComboBox.setName("project-files.mime-type");
223                 fileOptionsMIMETypeComboBox.addActionListener(this);
224                 fileOptionsMIMETypeComboBox.setEnabled(false);
225
226                 final TLabel mimeTypeLabel = new TLabel(I18n.getMessage("jsite.project-files.mime-type") + ":", KeyEvent.VK_M, fileOptionsMIMETypeComboBox);
227                 fileOptionsPanel.add(mimeTypeLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
228                 fileOptionsPanel.add(fileOptionsMIMETypeComboBox, new GridBagConstraints(1, 5, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
229
230                 containerComboBoxModel = new DefaultComboBoxModel();
231                 fileOptionsContainerComboBox = new JComboBox(containerComboBoxModel);
232                 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
233                 fileOptionsContainerComboBox.setName("project-files.container");
234                 fileOptionsContainerComboBox.addActionListener(this);
235                 fileOptionsContainerComboBox.setEnabled(false);
236
237                 final TLabel containerLabel = new TLabel(I18n.getMessage("jsite.project-files.container") + ":", KeyEvent.VK_C, fileOptionsContainerComboBox);
238                 fileOptionsPanel.add(containerLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
239                 fileOptionsPanel.add(fileOptionsContainerComboBox, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
240                 fileOptionsPanel.add(new JButton(addContainerAction), new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
241                 fileOptionsPanel.add(new JButton(editContainerAction), new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
242                 fileOptionsPanel.add(new JButton(deleteContainerAction), new GridBagConstraints(4, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
243
244                 JPanel fileOptionsReplacementPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 6, 6));
245                 fileOptionsReplacementPanel.setBorder(new EmptyBorder(-6, -6, -6, -6));
246
247                 replacementCheckBox = new JCheckBox(I18n.getMessage("jsite.project-files.replacement"));
248                 replacementCheckBox.setName("project-files.replace-edition");
249                 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
250                 replacementCheckBox.addActionListener(this);
251                 replacementCheckBox.setEnabled(false);
252                 fileOptionsReplacementPanel.add(replacementCheckBox);
253
254                 replaceEditionRangeSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
255                 replaceEditionRangeSpinner.setName("project-files.replace-edition-range");
256                 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
257                 replaceEditionRangeSpinner.addChangeListener(this);
258                 replaceEditionRangeSpinner.setEnabled(false);
259                 final JLabel editionRangeLabel = new JLabel(I18n.getMessage("jsite.project-files.replacement.edition-range"));
260                 fileOptionsReplacementPanel.add(editionRangeLabel);
261                 fileOptionsReplacementPanel.add(replaceEditionRangeSpinner);
262
263                 fileOptionsPanel.add(fileOptionsReplacementPanel, new GridBagConstraints(0, 7, 5, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
264
265                 I18nContainer.getInstance().registerRunnable(new Runnable() {
266
267                         @SuppressWarnings("synthetic-access")
268                         public void run() {
269                                 fileOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.project-files.file-options") + "</b></html>");
270                                 defaultFileCheckBox.setText(I18n.getMessage("jsite.project-files.default"));
271                                 defaultFileCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.default.tooltip"));
272                                 fileOptionsInsertCheckBox.setText(I18n.getMessage("jsite.project-files.insert"));
273                                 fileOptionsInsertCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.insert.tooltip"));
274                                 fileOptionsCustomKeyTextField.setToolTipText(I18n.getMessage("jsite.project-files.custom-key.tooltip"));
275                                 customKeyLabel.setText(I18n.getMessage("jsite.project-files.custom-key") + ":");
276                                 fileOptionsMIMETypeComboBox.setToolTipText(I18n.getMessage("jsite.project-files.mime-type.tooltip"));
277                                 mimeTypeLabel.setText(I18n.getMessage("jsite.project-files.mime-type") + ":");
278                                 fileOptionsContainerComboBox.setToolTipText(I18n.getMessage("jsite.project-files.container.tooltip"));
279                                 containerLabel.setText(I18n.getMessage("jsite.project-files.container") + ":");
280                                 replacementCheckBox.setText(I18n.getMessage("jsite.project-files.replacement"));
281                                 replacementCheckBox.setToolTipText(I18n.getMessage("jsite.project-files.replacement.tooltip"));
282                                 replaceEditionRangeSpinner.setToolTipText(I18n.getMessage("jsite.project-files.replacement.edition-range.tooltip"));
283                                 editionRangeLabel.setText(I18n.getMessage("jsite.project-files.replacement.edition-range"));
284                         }
285                 });
286
287                 return projectFilesPanel;
288         }
289
290         public void setProject(final Project project) {
291                 this.project = project;
292                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
293                 setDescription(I18n.getMessage("jsite.project-files.description"));
294                 I18nContainer.getInstance().registerRunnable(new Runnable() {
295
296                         public void run() {
297                                 setHeading(MessageFormat.format(I18n.getMessage("jsite.project-files.heading"), project.getName()));
298                                 setDescription(I18n.getMessage("jsite.project-files.description"));
299                         }
300                 });
301         }
302
303         private List<String> getProjectFiles() {
304                 List<String> files = new ArrayList<String>();
305                 for (int index = 0, size = projectFileList.getModel().getSize(); index < size; index++) {
306                         files.add((String) projectFileList.getModel().getElementAt(index));
307                 }
308                 return files;
309         }
310
311         private void rebuildContainerComboBox() {
312                 /* scan files for containers */
313                 List<String> files = getProjectFiles();
314                 List<String> containers = new ArrayList<String>(); // ComboBoxModel
315                 // sucks. No
316                 // contains()!
317                 containers.add("");
318                 for (String filename : files) {
319                         String container = project.getFileOption(filename).getContainer();
320                         if (!containers.contains(container)) {
321                                 containers.add(container);
322                         }
323                 }
324                 Collections.sort(containers);
325                 containerComboBoxModel.removeAllElements();
326                 for (String container : containers) {
327                         containerComboBoxModel.addElement(container);
328                 }
329         }
330
331         //
332         // ACTIONS
333         //
334
335         private void actionScan() {
336                 projectFileList.clearSelection();
337                 projectFileList.setListData(new Object[0]);
338
339                 wizard.setNextEnabled(false);
340                 wizard.setPreviousEnabled(false);
341                 wizard.setQuitEnabled(false);
342
343                 FileScanner fileScanner = new FileScanner(project);
344                 fileScanner.addFileScannerListener(this);
345                 new Thread(fileScanner).start();
346         }
347
348         private void actionAddContainer() {
349                 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.add-container.message") + ":", null, JOptionPane.INFORMATION_MESSAGE);
350                 if (containerName == null) {
351                         return;
352                 }
353                 containerName = containerName.trim();
354                 String filename = (String) projectFileList.getSelectedValue();
355                 FileOption fileOption = project.getFileOption(filename);
356                 fileOption.setContainer(containerName);
357                 rebuildContainerComboBox();
358                 fileOptionsContainerComboBox.setSelectedItem(containerName);
359         }
360
361         private void actionEditContainer() {
362                 String selectedFilename = (String) projectFileList.getSelectedValue();
363                 FileOption fileOption = project.getFileOption(selectedFilename);
364                 String oldContainerName = fileOption.getContainer();
365                 String containerName = JOptionPane.showInputDialog(wizard, I18n.getMessage("jsite.project-files.action.edit-container.message") + ":", oldContainerName);
366                 if (containerName == null) {
367                         return;
368                 }
369                 if (containerName.equals("")) {
370                         fileOption.setContainer("");
371                         fileOptionsContainerComboBox.setSelectedItem("");
372                         return;
373                 }
374                 List<String> files = getProjectFiles();
375                 for (String filename : files) {
376                         fileOption = project.getFileOption(filename);
377                         if (fileOption.getContainer().equals(oldContainerName)) {
378                                 fileOption.setContainer(containerName);
379                         }
380                 }
381                 rebuildContainerComboBox();
382                 fileOptionsContainerComboBox.setSelectedItem(containerName);
383         }
384
385         private void actionDeleteContainer() {
386                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.action.delete-container.message"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
387                         String containerName = (String) fileOptionsContainerComboBox.getSelectedItem();
388                         List<String> files = getProjectFiles();
389                         for (String filename : files) {
390                                 FileOption fileOption = project.getFileOption(filename);
391                                 if (fileOption.getContainer().equals(containerName)) {
392                                         fileOption.setContainer("");
393                                 }
394                         }
395                         fileOptionsContainerComboBox.setSelectedItem("");
396                 }
397         }
398
399         public void fileScannerFinished(FileScanner fileScanner) {
400                 final boolean error = fileScanner.isError();
401                 if (!error) {
402                         final List<String> files = fileScanner.getFiles();
403                         SwingUtilities.invokeLater(new Runnable() {
404
405                                 @SuppressWarnings("synthetic-access")
406                                 public void run() {
407                                         projectFileList.setListData(files.toArray(new String[files.size()]));
408                                         projectFileList.clearSelection();
409                                         rebuildContainerComboBox();
410                                 }
411                         });
412                         Set<String> entriesToRemove = new HashSet<String>();
413                         Iterator<String> filenames = project.getFileOptions().keySet().iterator();
414                         while (filenames.hasNext()) {
415                                 String filename = filenames.next();
416                                 if (!files.contains(filename)) {
417                                         entriesToRemove.add(filename);
418                                 }
419                         }
420                         for (String filename : entriesToRemove) {
421                                 project.setFileOption(filename, null);
422                         }
423                 } else {
424                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.scan-error"), null, JOptionPane.ERROR_MESSAGE);
425                 }
426                 SwingUtilities.invokeLater(new Runnable() {
427
428                         @SuppressWarnings("synthetic-access")
429                         public void run() {
430                                 wizard.setPreviousEnabled(true);
431                                 wizard.setNextEnabled(!error);
432                                 wizard.setQuitEnabled(true);
433                         }
434                 });
435         }
436
437         //
438         // INTERFACE ActionListener
439         //
440
441         /**
442          * {@inheritDoc}
443          */
444         public void actionPerformed(ActionEvent actionEvent) {
445                 String filename = (String) projectFileList.getSelectedValue();
446                 if (filename == null) {
447                         return;
448                 }
449                 FileOption fileOption = project.getFileOption(filename);
450                 Object source = actionEvent.getSource();
451                 if (source instanceof JCheckBox) {
452                         JCheckBox checkBox = (JCheckBox) source;
453                         if ("default-file".equals(checkBox.getName())) {
454                                 if (checkBox.isSelected()) {
455                                         project.setIndexFile(filename);
456                                 } else {
457                                         project.setIndexFile(null);
458                                 }
459                         } else if ("insert".equals(checkBox.getName())) {
460                                 boolean isInsert = checkBox.isSelected();
461                                 fileOptionsCustomKeyTextField.setEnabled(!isInsert);
462                                 fileOption.setInsert(isInsert);
463                                 if (!isInsert) {
464                                         fileOptionsContainerComboBox.setSelectedItem("");
465                                 }
466                         } else if ("project-files.replace-edition".equals(checkBox.getName())) {
467                                 boolean replaceEdition = checkBox.isSelected();
468                                 fileOption.setReplaceEdition(replaceEdition);
469                                 replaceEditionRangeSpinner.setEnabled(replaceEdition);
470                         }
471                 } else if (source instanceof JComboBox) {
472                         JComboBox comboBox = (JComboBox) source;
473                         if ("project-files.mime-type".equals(comboBox.getName())) {
474                                 fileOption.setMimeType((String) comboBox.getSelectedItem());
475                         } else if ("project-files.container".equals(comboBox.getName())) {
476                                 String containerName = (String) comboBox.getSelectedItem();
477                                 fileOption.setContainer(containerName);
478                                 boolean enabled = !"".equals(containerName);
479                                 editContainerAction.setEnabled(enabled);
480                                 deleteContainerAction.setEnabled(enabled);
481                                 if (enabled) {
482                                         fileOptionsInsertCheckBox.setSelected(true);
483                                 }
484                         }
485                 }
486         }
487
488         //
489         // INTERFACE ListSelectionListener
490         //
491
492         /**
493          * {@inheritDoc}
494          */
495         public void valueChanged(ListSelectionEvent e) {
496                 String filename = (String) projectFileList.getSelectedValue();
497                 boolean enabled = filename != null;
498                 boolean insert = fileOptionsInsertCheckBox.isSelected();
499                 defaultFileCheckBox.setEnabled(enabled);
500                 fileOptionsInsertCheckBox.setEnabled(enabled);
501                 fileOptionsCustomKeyTextField.setEnabled(enabled && !insert);
502                 fileOptionsMIMETypeComboBox.setEnabled(enabled);
503                 fileOptionsContainerComboBox.setEnabled(enabled);
504                 addContainerAction.setEnabled(enabled);
505                 editContainerAction.setEnabled(enabled);
506                 deleteContainerAction.setEnabled(enabled);
507                 replacementCheckBox.setEnabled(enabled && insert);
508                 if (filename != null) {
509                         FileOption fileOption = project.getFileOption(filename);
510                         defaultFileCheckBox.setSelected(filename.equals(project.getIndexFile()));
511                         fileOptionsInsertCheckBox.setSelected(fileOption.isInsert());
512                         fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey());
513                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType());
514                         fileOptionsContainerComboBox.setSelectedItem(fileOption.getContainer());
515                         replacementCheckBox.setSelected(fileOption.getReplaceEdition());
516                         replaceEditionRangeSpinner.setValue(fileOption.getEditionRange());
517                         replaceEditionRangeSpinner.setEnabled(fileOption.getReplaceEdition());
518                 } else {
519                         defaultFileCheckBox.setSelected(false);
520                         fileOptionsInsertCheckBox.setSelected(true);
521                         fileOptionsCustomKeyTextField.setText("CHK@");
522                         fileOptionsMIMETypeComboBox.getModel().setSelectedItem(DefaultMIMETypes.DEFAULT_MIME_TYPE);
523                         fileOptionsContainerComboBox.setSelectedItem("");
524                         replacementCheckBox.setSelected(false);
525                         replaceEditionRangeSpinner.setValue(0);
526                 }
527         }
528
529         //
530         // INTERFACE DocumentListener
531         //
532
533         private void processDocumentUpdate(DocumentEvent documentEvent) {
534                 String filename = (String) projectFileList.getSelectedValue();
535                 if (filename == null) {
536                         return;
537                 }
538                 FileOption fileOption = project.getFileOption(filename);
539                 Document document = documentEvent.getDocument();
540                 try {
541                         String text = document.getText(0, document.getLength());
542                         fileOption.setCustomKey(text);
543                 } catch (BadLocationException ble1) {
544                 }
545         }
546
547         /**
548          * {@inheritDoc}
549          */
550         public void changedUpdate(DocumentEvent documentEvent) {
551                 processDocumentUpdate(documentEvent);
552         }
553
554         /**
555          * {@inheritDoc}
556          */
557         public void insertUpdate(DocumentEvent documentEvent) {
558                 processDocumentUpdate(documentEvent);
559         }
560
561         /**
562          * {@inheritDoc}
563          */
564         public void removeUpdate(DocumentEvent documentEvent) {
565                 processDocumentUpdate(documentEvent);
566         }
567
568         //
569         // INTERFACE ChangeListener
570         //
571
572         /**
573          * {@inheritDoc}
574          */
575         public void stateChanged(ChangeEvent changeEvent) {
576                 String filename = (String) projectFileList.getSelectedValue();
577                 if (filename == null) {
578                         return;
579                 }
580                 FileOption fileOption = project.getFileOption(filename);
581                 Object source = changeEvent.getSource();
582                 if (source instanceof JSpinner) {
583                         JSpinner spinner = (JSpinner) source;
584                         fileOption.setEditionRange((Integer) spinner.getValue());
585                 }
586         }
587
588 }