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