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