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