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