5c4dcfa6d3f95192b73fd31728466b0fab21301a
[jSite.git] / src / main / java / de / todesbaum / jsite / gui / PreferencesPage.java
1 /*
2  * jSite - PreferencesPage.java - Copyright © 2009–2014 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                         @Override
131                         public void run() {
132                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
133                                 setDescription(I18n.getMessage("jsite.preferences.description"));
134                         }
135                 });
136         }
137
138         //
139         // ACCESSORS
140         //
141
142         /**
143          * Returns the temp directory.
144          *
145          * @return The temp directory, or {@code null} to use the default temp
146          *         directory
147          */
148         public String getTempDirectory() {
149                 return tempDirectory;
150         }
151
152         /**
153          * Sets the temp directory.
154          *
155          * @param tempDirectory
156          *            The temp directory, or {@code null} to use the default temp
157          *            directory
158          */
159         public void setTempDirectory(String tempDirectory) {
160                 this.tempDirectory = tempDirectory;
161                 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
162                 if (tempDirectory != null) {
163                         customTempDirectory.setSelected(true);
164                         chooseTempDirectoryAction.setEnabled(true);
165                 } else {
166                         defaultTempDirectory.setSelected(true);
167                 }
168         }
169
170         /**
171          * Returns the configuration location.
172          *
173          * @return The configuration location
174          */
175         public ConfigurationLocation getConfigurationLocation() {
176                 return configurationLocation;
177         }
178
179         /**
180          * Sets the configuration location.
181          *
182          * @param configurationLocation
183          *            The configuration location
184          */
185         public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
186                 this.configurationLocation = configurationLocation;
187                 switch (configurationLocation) {
188                 case NEXT_TO_JAR_FILE:
189                         nextToJarFile.setSelected(true);
190                         break;
191                 case HOME_DIRECTORY:
192                         homeDirectory.setSelected(true);
193                         break;
194                 case CUSTOM:
195                         customDirectory.setSelected(true);
196                         break;
197                 }
198         }
199
200         /**
201          * Sets whether it is possible to select the “next to JAR file” option for
202          * the configuration location.
203          *
204          * @param nextToJarFile
205          *            {@code true} if the configuration file can be saved next to
206          *            the JAR file, {@code false} otherwise
207          */
208         public void setHasNextToJarConfiguration(boolean nextToJarFile) {
209                 this.nextToJarFile.setEnabled(nextToJarFile);
210         }
211
212         /**
213          * Sets whether it is possible to select the “custom location” option for
214          * the configuration location.
215          *
216          * @param customDirectory
217          *            {@code true} if the configuration file can be saved to a
218          *            custom location, {@code false} otherwise
219          */
220         public void setHasCustomConfiguration(boolean customDirectory) {
221                 this.customDirectory.setEnabled(customDirectory);
222         }
223
224         /**
225          * Returns whether to use the “early encode“ flag for the insert.
226          *
227          * @return {@code true} to set the “early encode” flag for the insert,
228          *         {@code false} otherwise
229          */
230         public boolean useEarlyEncode() {
231                 return useEarlyEncode;
232         }
233
234         /**
235          * Sets whether to use the “early encode“ flag for the insert.
236          *
237          * @param useEarlyEncode
238          *            {@code true} to set the “early encode” flag for the insert,
239          *            {@code false} otherwise
240          */
241         public void setUseEarlyEncode(boolean useEarlyEncode) {
242                 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
243         }
244
245         /**
246          * Returns the configured insert priority.
247          *
248          * @return The insert priority
249          */
250         public PriorityClass getPriority() {
251                 return priority;
252         }
253
254         /**
255          * Sets the insert priority.
256          *
257          * @param priority
258          *            The insert priority
259          */
260         public void setPriority(PriorityClass priority) {
261                 insertPriorityComboBox.setSelectedItem(priority);
262         }
263
264         /**
265          * {@inheritDoc}
266          */
267         @Override
268         public void pageAdded(TWizard wizard) {
269                 super.pageAdded(wizard);
270                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
271                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
272                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
273                 this.wizard.setNextEnabled(false);
274         }
275
276         //
277         // PRIVATE METHODS
278         //
279
280         /**
281          * Initializes this page.
282          */
283         private void pageInit() {
284                 createActions();
285                 setLayout(new BorderLayout(12, 12));
286                 add(createPreferencesPanel(), BorderLayout.CENTER);
287         }
288
289         /**
290          * Creates all actions.
291          */
292         private void createActions() {
293                 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
294
295                         /**
296                          * {@inheritDoc}
297                          */
298                         @Override
299                         @SuppressWarnings("synthetic-access")
300                         public void actionPerformed(ActionEvent actionEvent) {
301                                 selectDefaultTempDirectory();
302                         }
303                 };
304                 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
305
306                         /**
307                          * {@inheritDoc}
308                          */
309                         @Override
310                         @SuppressWarnings("synthetic-access")
311                         public void actionPerformed(ActionEvent actionEvent) {
312                                 selectCustomTempDirectory();
313                         }
314                 };
315                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
316
317                         @Override
318                         @SuppressWarnings("synthetic-access")
319                         public void actionPerformed(ActionEvent e) {
320                                 chooseTempDirectory();
321                         }
322                 };
323                 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
324
325                         @Override
326                         @SuppressWarnings("synthetic-access")
327                         public void actionPerformed(ActionEvent actionevent) {
328                                 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
329                         }
330                 };
331                 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
332
333                         @Override
334                         @SuppressWarnings("synthetic-access")
335                         public void actionPerformed(ActionEvent actionevent) {
336                                 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
337                         }
338                 };
339                 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
340
341                         @Override
342                         @SuppressWarnings("synthetic-access")
343                         public void actionPerformed(ActionEvent actionEvent) {
344                                 configurationLocation = ConfigurationLocation.CUSTOM;
345                         }
346                 };
347                 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
348
349                         @Override
350                         @SuppressWarnings("synthetic-access")
351                         public void actionPerformed(ActionEvent actionEvent) {
352                                 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
353                         }
354                 };
355                 priorityAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.priority")) {
356
357                         @Override
358                         @SuppressWarnings("synthetic-access")
359                         public void actionPerformed(ActionEvent actionEvent) {
360                                 priority = (PriorityClass) insertPriorityComboBox.getSelectedItem();
361                         }
362                 };
363
364                 I18nContainer.getInstance().registerRunnable(new Runnable() {
365
366                         @Override
367                         @SuppressWarnings("synthetic-access")
368                         public void run() {
369                                 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
370                                 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
371                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
372                                 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
373                                 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
374                                 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
375                                 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
376                         }
377                 });
378         }
379
380         /**
381          * Creates the panel containing all preferences.
382          *
383          * @return The preferences panel
384          */
385         private JPanel createPreferencesPanel() {
386                 JPanel preferencesPanel = new JPanel(new GridBagLayout());
387                 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
388
389                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
390                 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));
391
392                 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
393                 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));
394
395                 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
396                 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));
397
398                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
399                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
400                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
401
402                 tempDirectoryTextField = new JTextField();
403                 tempDirectoryTextField.setEditable(false);
404                 if (tempDirectory != null) {
405                         tempDirectoryTextField.setText(tempDirectory);
406                         customTempDirectory.setSelected(true);
407                 } else {
408                         defaultTempDirectory.setSelected(true);
409                 }
410                 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
411                 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));
412
413                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
414                 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));
415
416                 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
417                 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));
418
419                 nextToJarFile = new JRadioButton(nextToJarFileAction);
420                 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));
421
422                 homeDirectory = new JRadioButton(homeDirectoryAction);
423                 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));
424
425                 customDirectory = new JRadioButton(customDirectoryAction);
426                 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));
427
428                 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
429                 configurationDirectoryButtonGroup.add(nextToJarFile);
430                 configurationDirectoryButtonGroup.add(homeDirectory);
431                 configurationDirectoryButtonGroup.add(customDirectory);
432
433                 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
434                 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));
435
436                 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
437                 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));
438
439                 final JLabel insertPriorityLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.priority"));
440                 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));
441
442                 insertPriorityComboBox = new JComboBox(new PriorityClass[] { PriorityClass.MINIMUM, PriorityClass.PREFETCH, PriorityClass.BULK, PriorityClass.UPDATABLE, PriorityClass.SEMI_INTERACTIVE, PriorityClass.INTERACTIVE, PriorityClass.MAXIMUM });
443                 insertPriorityComboBox.setAction(priorityAction);
444                 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));
445
446                 I18nContainer.getInstance().registerRunnable(new Runnable() {
447
448                         /**
449                          * {@inheritDoc}
450                          */
451                         @Override
452                         public void run() {
453                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
454                                 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
455                                 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
456                                 insertPriorityLabel.setText(I18n.getMessage("jsite.preferences.insert-options.priority"));
457                         }
458                 });
459
460                 return preferencesPanel;
461         }
462
463         /**
464          * Activates the default temp directory radio button.
465          */
466         private void selectDefaultTempDirectory() {
467                 tempDirectoryTextField.setEnabled(false);
468                 chooseTempDirectoryAction.setEnabled(false);
469                 tempDirectory = null;
470         }
471
472         /**
473          * Activates the custom temp directory radio button.
474          */
475         private void selectCustomTempDirectory() {
476                 tempDirectoryTextField.setEnabled(true);
477                 chooseTempDirectoryAction.setEnabled(true);
478                 if (tempDirectoryTextField.getText().length() == 0) {
479                         chooseTempDirectory();
480                         if (tempDirectoryTextField.getText().length() == 0) {
481                                 defaultTempDirectory.setSelected(true);
482                         }
483                 }
484         }
485
486         /**
487          * Lets the user choose a new temp directory.
488          */
489         private void chooseTempDirectory() {
490                 JFileChooser fileChooser = new JFileChooser(tempDirectory);
491                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
492                 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
493                 if (returnValue == JFileChooser.CANCEL_OPTION) {
494                         return;
495                 }
496                 tempDirectory = fileChooser.getSelectedFile().getPath();
497                 tempDirectoryTextField.setText(tempDirectory);
498         }
499
500 }