Use XML classes from utils.
[jSite.git] / src / main / java / de / todesbaum / jsite / gui / PreferencesPage.java
1 /*
2  * jSite - PreferencesPage.java - Copyright © 2009–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.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.event.ActionEvent;
26
27 import javax.swing.AbstractAction;
28 import javax.swing.Action;
29 import javax.swing.BorderFactory;
30 import javax.swing.ButtonGroup;
31 import javax.swing.JButton;
32 import javax.swing.JCheckBox;
33 import javax.swing.JComboBox;
34 import javax.swing.JFileChooser;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.JRadioButton;
38 import javax.swing.JTextField;
39
40 import de.todesbaum.jsite.i18n.I18n;
41 import de.todesbaum.jsite.i18n.I18nContainer;
42 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
43 import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter;
44 import de.todesbaum.util.freenet.fcp2.PriorityClass;
45 import de.todesbaum.util.swing.TWizard;
46 import de.todesbaum.util.swing.TWizardPage;
47
48 /**
49  * Page that shows some preferences that are valid for the complete application.
50  *
51  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
52  */
53 public class PreferencesPage extends TWizardPage {
54
55         /** Select default temp directory action. */
56         private Action selectDefaultTempDirectoryAction;
57
58         /** Select custom temp directory action. */
59         private Action selectCustomTempDirectoryAction;
60
61         /** Action that chooses a new temp directory. */
62         private Action chooseTempDirectoryAction;
63
64         /** Action when selecting “next to JAR file.” */
65         private Action nextToJarFileAction;
66
67         /** Action when selecting “home directory.” */
68         private Action homeDirectoryAction;
69
70         /** Action when selecting “custom directory.” */
71         private Action customDirectoryAction;
72
73         /** Action when selecting “use early encode.” */
74         private Action useEarlyEncodeAction;
75
76         /** Action when a priority was selected. */
77         private Action priorityAction;
78
79         /** The text field containing the directory. */
80         private JTextField tempDirectoryTextField;
81
82         /** The temp directory. */
83         private String tempDirectory;
84
85         /** The configuration location. */
86         private ConfigurationLocation configurationLocation;
87
88         /** Whether to use “early encode.” */
89         private boolean useEarlyEncode;
90
91         /** The prioriy for inserts. */
92         private PriorityClass priority;
93
94         /** The “default” button. */
95         private JRadioButton defaultTempDirectory;
96
97         /** The “custom” button. */
98         private JRadioButton customTempDirectory;
99
100         /** The “next to JAR file” checkbox. */
101         private JRadioButton nextToJarFile;
102
103         /** The “home directory” checkbox. */
104         private JRadioButton homeDirectory;
105
106         /** The “custom directory” checkbox. */
107         private JRadioButton customDirectory;
108
109         /** The “use early encode” checkbox. */
110         private JCheckBox useEarlyEncodeCheckBox;
111
112         /** The insert priority select box. */
113         private JComboBox insertPriorityComboBox;
114
115         /** The manifest putter select box. */
116         private JComboBox manifestPutterComboBox;
117
118         /**
119          * Creates a new “preferences” page.
120          *
121          * @param wizard
122          *            The wizard this page belongs to
123          */
124         public PreferencesPage(TWizard wizard) {
125                 super(wizard);
126                 pageInit();
127                 setHeading(I18n.getMessage("jsite.preferences.heading"));
128                 setDescription(I18n.getMessage("jsite.preferences.description"));
129                 I18nContainer.getInstance().registerRunnable(new Runnable() {
130
131                         /**
132                          * {@inheritDoc}
133                          */
134                         public void run() {
135                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
136                                 setDescription(I18n.getMessage("jsite.preferences.description"));
137                         }
138                 });
139         }
140
141         //
142         // ACCESSORS
143         //
144
145         /**
146          * Returns the temp directory.
147          *
148          * @return The temp directory, or {@code null} to use the default temp
149          *         directory
150          */
151         public String getTempDirectory() {
152                 return tempDirectory;
153         }
154
155         /**
156          * Sets the temp directory.
157          *
158          * @param tempDirectory
159          *            The temp directory, or {@code null} to use the default temp
160          *            directory
161          */
162         public void setTempDirectory(String tempDirectory) {
163                 this.tempDirectory = tempDirectory;
164                 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
165                 if (tempDirectory != null) {
166                         customTempDirectory.setSelected(true);
167                         chooseTempDirectoryAction.setEnabled(true);
168                 } else {
169                         defaultTempDirectory.setSelected(true);
170                 }
171         }
172
173         /**
174          * Returns the configuration location.
175          *
176          * @return The configuration location
177          */
178         public ConfigurationLocation getConfigurationLocation() {
179                 return configurationLocation;
180         }
181
182         /**
183          * Sets the configuration location.
184          *
185          * @param configurationLocation
186          *            The configuration location
187          */
188         public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
189                 this.configurationLocation = configurationLocation;
190                 switch (configurationLocation) {
191                 case NEXT_TO_JAR_FILE:
192                         nextToJarFile.setSelected(true);
193                         break;
194                 case HOME_DIRECTORY:
195                         homeDirectory.setSelected(true);
196                         break;
197                 case CUSTOM:
198                         customDirectory.setSelected(true);
199                         break;
200                 }
201         }
202
203         /**
204          * Sets whether it is possible to select the “next to JAR file” option for
205          * the configuration location.
206          *
207          * @param nextToJarFile
208          *            {@code true} if the configuration file can be saved next to
209          *            the JAR file, {@code false} otherwise
210          */
211         public void setHasNextToJarConfiguration(boolean nextToJarFile) {
212                 this.nextToJarFile.setEnabled(nextToJarFile);
213         }
214
215         /**
216          * Sets whether it is possible to select the “custom location” option for
217          * the configuration location.
218          *
219          * @param customDirectory
220          *            {@code true} if the configuration file can be saved to a
221          *            custom location, {@code false} otherwise
222          */
223         public void setHasCustomConfiguration(boolean customDirectory) {
224                 this.customDirectory.setEnabled(customDirectory);
225         }
226
227         /**
228          * Returns whether to use the “early encode“ flag for the insert.
229          *
230          * @return {@code true} to set the “early encode” flag for the insert,
231          *         {@code false} otherwise
232          */
233         public boolean useEarlyEncode() {
234                 return useEarlyEncode;
235         }
236
237         /**
238          * Sets whether to use the “early encode“ flag for the insert.
239          *
240          * @param useEarlyEncode
241          *            {@code true} to set the “early encode” flag for the insert,
242          *            {@code false} otherwise
243          */
244         public void setUseEarlyEncode(boolean useEarlyEncode) {
245                 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
246         }
247
248         /**
249          * Returns the configured insert priority.
250          *
251          * @return The insert priority
252          */
253         public PriorityClass getPriority() {
254                 return priority;
255         }
256
257         /**
258          * Sets the insert priority.
259          *
260          * @param priority
261          *            The insert priority
262          */
263         public void setPriority(PriorityClass priority) {
264                 insertPriorityComboBox.setSelectedItem(priority);
265         }
266
267         /**
268          * Returns the selected manifest putter.
269          *
270          * @return The selected manifest putter
271          */
272         public ManifestPutter getManifestPutter() {
273                 return (ManifestPutter) manifestPutterComboBox.getSelectedItem();
274         }
275
276         /**
277          * Sets the manifest putter.
278          *
279          * @param manifestPutter
280          *            The manifest putter
281          */
282         public void setManifestPutter(ManifestPutter manifestPutter) {
283                 manifestPutterComboBox.setSelectedItem(manifestPutter);
284         }
285
286         /**
287          * {@inheritDoc}
288          */
289         @Override
290         public void pageAdded(TWizard wizard) {
291                 super.pageAdded(wizard);
292                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
293                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
294                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
295                 this.wizard.setNextEnabled(false);
296         }
297
298         //
299         // PRIVATE METHODS
300         //
301
302         /**
303          * Initializes this page.
304          */
305         private void pageInit() {
306                 createActions();
307                 setLayout(new BorderLayout(12, 12));
308                 add(createPreferencesPanel(), BorderLayout.CENTER);
309         }
310
311         /**
312          * Creates all actions.
313          */
314         private void createActions() {
315                 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
316
317                         /**
318                          * {@inheritDoc}
319                          */
320                         @SuppressWarnings("synthetic-access")
321                         public void actionPerformed(ActionEvent actionEvent) {
322                                 selectDefaultTempDirectory();
323                         }
324                 };
325                 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
326
327                         /**
328                          * {@inheritDoc}
329                          */
330                         @SuppressWarnings("synthetic-access")
331                         public void actionPerformed(ActionEvent actionEvent) {
332                                 selectCustomTempDirectory();
333                         }
334                 };
335                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
336
337                         @SuppressWarnings("synthetic-access")
338                         public void actionPerformed(ActionEvent e) {
339                                 chooseTempDirectory();
340                         }
341                 };
342                 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
343
344                         @SuppressWarnings("synthetic-access")
345                         public void actionPerformed(ActionEvent actionevent) {
346                                 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
347                         }
348                 };
349                 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
350
351                         @SuppressWarnings("synthetic-access")
352                         public void actionPerformed(ActionEvent actionevent) {
353                                 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
354                         }
355                 };
356                 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
357
358                         @SuppressWarnings("synthetic-access")
359                         public void actionPerformed(ActionEvent actionEvent) {
360                                 configurationLocation = ConfigurationLocation.CUSTOM;
361                         }
362                 };
363                 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
364
365                         @SuppressWarnings("synthetic-access")
366                         public void actionPerformed(ActionEvent actionEvent) {
367                                 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
368                         }
369                 };
370                 priorityAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.priority")) {
371
372                         @SuppressWarnings("synthetic-access")
373                         public void actionPerformed(ActionEvent actionEvent) {
374                                 priority = (PriorityClass) insertPriorityComboBox.getSelectedItem();
375                         }
376                 };
377
378                 I18nContainer.getInstance().registerRunnable(new Runnable() {
379
380                         @SuppressWarnings("synthetic-access")
381                         public void run() {
382                                 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
383                                 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
384                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
385                                 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
386                                 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
387                                 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
388                                 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
389                         }
390                 });
391         }
392
393         /**
394          * Creates the panel containing all preferences.
395          *
396          * @return The preferences panel
397          */
398         private JPanel createPreferencesPanel() {
399                 JPanel preferencesPanel = new JPanel(new GridBagLayout());
400                 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
401
402                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
403                 preferencesPanel.add(tempDirectoryLabel, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
404
405                 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
406                 preferencesPanel.add(defaultTempDirectory, new GridBagConstraints(0, 1, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 18, 0, 0), 0, 0));
407
408                 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
409                 preferencesPanel.add(customTempDirectory, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 18, 0, 0), 0, 0));
410
411                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
412                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
413                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
414
415                 tempDirectoryTextField = new JTextField();
416                 tempDirectoryTextField.setEditable(false);
417                 if (tempDirectory != null) {
418                         tempDirectoryTextField.setText(tempDirectory);
419                         customTempDirectory.setSelected(true);
420                 } else {
421                         defaultTempDirectory.setSelected(true);
422                 }
423                 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
424                 preferencesPanel.add(tempDirectoryTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 6, 0, 0), 0, 0));
425
426                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
427                 preferencesPanel.add(chooseButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.BOTH, new Insets(0, 6, 0, 0), 0, 0));
428
429                 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
430                 preferencesPanel.add(configurationDirectoryLabel, new GridBagConstraints(0, 3, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
431
432                 nextToJarFile = new JRadioButton(nextToJarFileAction);
433                 preferencesPanel.add(nextToJarFile, new GridBagConstraints(0, 4, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 18, 0, 0), 0, 0));
434
435                 homeDirectory = new JRadioButton(homeDirectoryAction);
436                 preferencesPanel.add(homeDirectory, new GridBagConstraints(0, 5, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 18, 0, 0), 0, 0));
437
438                 customDirectory = new JRadioButton(customDirectoryAction);
439                 preferencesPanel.add(customDirectory, new GridBagConstraints(0, 6, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 18, 0, 0), 0, 0));
440
441                 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
442                 configurationDirectoryButtonGroup.add(nextToJarFile);
443                 configurationDirectoryButtonGroup.add(homeDirectory);
444                 configurationDirectoryButtonGroup.add(customDirectory);
445
446                 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
447                 preferencesPanel.add(insertOptionsLabel, new GridBagConstraints(0, 7, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
448
449                 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
450                 preferencesPanel.add(useEarlyEncodeCheckBox, new GridBagConstraints(0, 8, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
451
452                 final JLabel insertPriorityLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.priority"));
453                 preferencesPanel.add(insertPriorityLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
454
455                 insertPriorityComboBox = new JComboBox(new PriorityClass[] { PriorityClass.MINIMUM, PriorityClass.PREFETCH, PriorityClass.BULK, PriorityClass.UPDATABLE, PriorityClass.SEMI_INTERACTIVE, PriorityClass.INTERACTIVE, PriorityClass.MAXIMUM });
456                 insertPriorityComboBox.setAction(priorityAction);
457                 preferencesPanel.add(insertPriorityComboBox, new GridBagConstraints(1, 9, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 18, 0, 0), 0, 0));
458
459                 final JLabel manifestPutterLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.manifest-putter"));
460                 preferencesPanel.add(manifestPutterLabel, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
461
462                 manifestPutterComboBox = new JComboBox(ManifestPutter.values());
463                 preferencesPanel.add(manifestPutterComboBox, new GridBagConstraints(1, 10, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 18, 0, 0), 0, 0));
464
465                 I18nContainer.getInstance().registerRunnable(new Runnable() {
466
467                         /**
468                          * {@inheritDoc}
469                          */
470                         public void run() {
471                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
472                                 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
473                                 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
474                                 insertPriorityLabel.setText(I18n.getMessage("jsite.preferences.insert-options.priority"));
475                                 manifestPutterLabel.setText(I18n.getMessage("jsite.preferences.insert-options.manifest-putter"));
476                         }
477                 });
478
479                 return preferencesPanel;
480         }
481
482         /**
483          * Activates the default temp directory radio button.
484          */
485         private void selectDefaultTempDirectory() {
486                 tempDirectoryTextField.setEnabled(false);
487                 chooseTempDirectoryAction.setEnabled(false);
488                 tempDirectory = null;
489         }
490
491         /**
492          * Activates the custom temp directory radio button.
493          */
494         private void selectCustomTempDirectory() {
495                 tempDirectoryTextField.setEnabled(true);
496                 chooseTempDirectoryAction.setEnabled(true);
497                 if (tempDirectoryTextField.getText().length() == 0) {
498                         chooseTempDirectory();
499                         if (tempDirectoryTextField.getText().length() == 0) {
500                                 defaultTempDirectory.setSelected(true);
501                         }
502                 }
503         }
504
505         /**
506          * Lets the user choose a new temp directory.
507          */
508         private void chooseTempDirectory() {
509                 JFileChooser fileChooser = new JFileChooser(tempDirectory);
510                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
511                 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
512                 if (returnValue == JFileChooser.CANCEL_OPTION) {
513                         return;
514                 }
515                 tempDirectory = fileChooser.getSelectedFile().getPath();
516                 tempDirectoryTextField.setText(tempDirectory);
517         }
518
519 }