Add JAR file location preferences on preferences page.
[jSite.git] / src / de / todesbaum / jsite / gui / PreferencesPage.java
1 /*
2  * jSite - PreferencesPage.java -
3  * Copyright © 2009 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.event.ActionEvent;
27
28 import javax.swing.AbstractAction;
29 import javax.swing.Action;
30 import javax.swing.BorderFactory;
31 import javax.swing.ButtonGroup;
32 import javax.swing.JButton;
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.Configuration;
42 import de.todesbaum.jsite.main.Configuration.ConfigurationDirectory;
43 import de.todesbaum.util.swing.TWizard;
44 import de.todesbaum.util.swing.TWizardPage;
45
46 /**
47  * Page that shows some preferences that are valid for the complete application.
48  *
49  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
50  */
51 public class PreferencesPage extends TWizardPage {
52
53         /** Select default temp directory action. */
54         private Action selectDefaultTempDirectoryAction;
55
56         /** Select custom temp directory action. */
57         private Action selectCustomTempDirectoryAction;
58
59         /** Action that chooses a new temp directory. */
60         private Action chooseTempDirectoryAction;
61
62         /** Action when selecting “next to JAR file.” */
63         private Action nextToJarFileAction;
64
65         /** Action when selecting “home directory.” */
66         private Action homeDirectoryAction;
67
68         /** The text field containing the directory. */
69         private JTextField tempDirectoryTextField;
70
71         /** The temp directory. */
72         private String tempDirectory;
73
74         /** The configuration. */
75         private Configuration configuration;
76
77         /** The configuration directory. */
78         private ConfigurationDirectory configurationDirectory;
79
80         /** The “default” button. */
81         private JRadioButton defaultTempDirectory;
82
83         /** The “custom” button. */
84         private JRadioButton customTempDirectory;
85
86         /** The “next to JAR file” checkbox. */
87         private JRadioButton nextToJarFile;
88
89         /** The “current directory” checkbox. */
90         private JRadioButton currentDirectory;
91
92         /** The “home directory” checkbox. */
93         private JRadioButton homeDirectory;
94
95         /**
96          * Creates a new “preferences” page.
97          *
98          * @param wizard
99          *            The wizard this page belongs to
100          * @param configuration
101          *            The configuration that is controlled
102          */
103         public PreferencesPage(TWizard wizard, Configuration configuration) {
104                 super(wizard);
105                 pageInit();
106                 setHeading(I18n.getMessage("jsite.preferences.heading"));
107                 setDescription(I18n.getMessage("jsite.preferences.description"));
108                 I18nContainer.getInstance().registerRunnable(new Runnable() {
109
110                         /**
111                          * {@inheritDoc}
112                          */
113                         public void run() {
114                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
115                                 setDescription(I18n.getMessage("jsite.preferences.description"));
116                         }
117                 });
118                 this.configuration = configuration;
119         }
120
121         //
122         // ACCESSORS
123         //
124
125         /**
126          * Returns the temp directory.
127          *
128          * @return The temp directory, or {@code null} to use the default temp
129          *         directory
130          */
131         public String getTempDirectory() {
132                 return tempDirectory;
133         }
134
135         /**
136          * Sets the temp directory.
137          *
138          * @param tempDirectory
139          *            The temp directory, or {@code null} to use the default temp
140          *            directory
141          */
142         public void setTempDirectory(String tempDirectory) {
143                 this.tempDirectory = tempDirectory;
144                 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
145                 if (tempDirectory != null) {
146                         customTempDirectory.setSelected(true);
147                         chooseTempDirectoryAction.setEnabled(true);
148                 } else {
149                         defaultTempDirectory.setSelected(true);
150                 }
151         }
152
153         /**
154          * Returns the configuration directory.
155          *
156          * @return The configuration directory
157          */
158         public ConfigurationDirectory getConfigurationDirectory() {
159                 return configurationDirectory;
160         }
161
162         /**
163          * Sets the configuration directory.
164          *
165          * @param configurationDirectory
166          *            The configuration directory
167          */
168         public void setConfigurationDirectory(ConfigurationDirectory configurationDirectory) {
169                 this.configurationDirectory = configurationDirectory;
170                 configuration.setConfigurationDirectory(configurationDirectory);
171                 switch (configurationDirectory) {
172                 case NEXT_TO_JAR_FILE:
173                         nextToJarFile.setSelected(true);
174                         break;
175                 case HOME_DIRECTORY:
176                         homeDirectory.setSelected(true);
177                         break;
178                 }
179         }
180
181         /**
182          * {@inheritDoc}
183          */
184         @Override
185         public void pageAdded(TWizard wizard) {
186                 super.pageAdded(wizard);
187                 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
188                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
189                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
190                 this.wizard.setNextEnabled(false);
191         }
192
193         //
194         // PRIVATE METHODS
195         //
196
197         /**
198          * Initializes this page.
199          */
200         private void pageInit() {
201                 createActions();
202                 setLayout(new BorderLayout(12, 12));
203                 add(createPreferencesPanel(), BorderLayout.CENTER);
204         }
205
206         /**
207          * Creates all actions.
208          */
209         private void createActions() {
210                 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
211
212                         /**
213                          * {@inheritDoc}
214                          */
215                         @SuppressWarnings("synthetic-access")
216                         public void actionPerformed(ActionEvent actionEvent) {
217                                 selectDefaultTempDirectory();
218                         }
219                 };
220                 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
221
222                         /**
223                          * {@inheritDoc}
224                          */
225                         @SuppressWarnings("synthetic-access")
226                         public void actionPerformed(ActionEvent actionEvent) {
227                                 selectCustomTempDirectory();
228                         }
229                 };
230                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
231
232                         @SuppressWarnings("synthetic-access")
233                         public void actionPerformed(ActionEvent e) {
234                                 chooseTempDirectory();
235                         }
236                 };
237                 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
238
239                         @SuppressWarnings("synthetic-access")
240                         public void actionPerformed(ActionEvent actionevent) {
241                                 configurationDirectory = ConfigurationDirectory.NEXT_TO_JAR_FILE;
242                         }
243                 };
244                 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
245
246                         @SuppressWarnings("synthetic-access")
247                         public void actionPerformed(ActionEvent actionevent) {
248                                 configurationDirectory = ConfigurationDirectory.HOME_DIRECTORY;
249                         }
250                 };
251
252                 I18nContainer.getInstance().registerRunnable(new Runnable() {
253
254                         @SuppressWarnings("synthetic-access")
255                         public void run() {
256                                 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
257                                 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
258                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
259                                 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
260                                 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
261                         }
262                 });
263         }
264
265         /**
266          * Creates the panel containing all preferences.
267          *
268          * @return The preferences panel
269          */
270         private JPanel createPreferencesPanel() {
271                 JPanel preferencesPanel = new JPanel(new GridBagLayout());
272                 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
273
274                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
275                 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));
276
277                 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
278                 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));
279
280                 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
281                 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));
282
283                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
284                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
285                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
286
287                 tempDirectoryTextField = new JTextField();
288                 tempDirectoryTextField.setEditable(false);
289                 if (tempDirectory != null) {
290                         tempDirectoryTextField.setText(tempDirectory);
291                         customTempDirectory.setSelected(true);
292                 } else {
293                         defaultTempDirectory.setSelected(true);
294                 }
295                 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
296                 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));
297
298                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
299                 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));
300
301                 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
302                 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));
303
304                 nextToJarFile = new JRadioButton(nextToJarFileAction);
305                 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));
306
307                 homeDirectory = new JRadioButton(homeDirectoryAction);
308                 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));
309
310                 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
311                 configurationDirectoryButtonGroup.add(nextToJarFile);
312                 configurationDirectoryButtonGroup.add(currentDirectory);
313                 configurationDirectoryButtonGroup.add(homeDirectory);
314
315                 I18nContainer.getInstance().registerRunnable(new Runnable() {
316
317                         /**
318                          * {@inheritDoc}
319                          */
320                         public void run() {
321                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
322                                 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
323                         }
324                 });
325
326                 return preferencesPanel;
327         }
328
329         /**
330          * Activates the default temp directory radio button.
331          */
332         private void selectDefaultTempDirectory() {
333                 tempDirectoryTextField.setEnabled(false);
334                 chooseTempDirectoryAction.setEnabled(false);
335                 tempDirectory = null;
336         }
337
338         /**
339          * Activates the custom temp directory radio button.
340          */
341         private void selectCustomTempDirectory() {
342                 tempDirectoryTextField.setEnabled(true);
343                 chooseTempDirectoryAction.setEnabled(true);
344                 if (tempDirectoryTextField.getText().length() == 0) {
345                         chooseTempDirectory();
346                         if (tempDirectoryTextField.getText().length() == 0) {
347                                 defaultTempDirectory.setSelected(true);
348                         }
349                 }
350         }
351
352         /**
353          * Lets the user choose a new temp directory.
354          */
355         private void chooseTempDirectory() {
356                 JFileChooser fileChooser = new JFileChooser(tempDirectory);
357                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
358                 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
359                 if (returnValue == JFileChooser.CANCEL_OPTION) {
360                         return;
361                 }
362                 tempDirectory = fileChooser.getSelectedFile().getPath();
363                 tempDirectoryTextField.setText(tempDirectory);
364         }
365
366 }