2 * jSite - PreferencesPage.java - Copyright © 2009–2011 David Roden
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.
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.
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.
19 package de.todesbaum.jsite.gui;
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;
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;
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;
45 * Page that shows some preferences that are valid for the complete application.
47 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49 public class PreferencesPage extends TWizardPage {
51 /** Select default temp directory action. */
52 private Action selectDefaultTempDirectoryAction;
54 /** Select custom temp directory action. */
55 private Action selectCustomTempDirectoryAction;
57 /** Action that chooses a new temp directory. */
58 private Action chooseTempDirectoryAction;
60 /** Action when selecting “next to JAR file.” */
61 private Action nextToJarFileAction;
63 /** Action when selecting “home directory.” */
64 private Action homeDirectoryAction;
66 /** Action when selecting “custom directory.” */
67 private Action customDirectoryAction;
69 /** The text field containing the directory. */
70 private JTextField tempDirectoryTextField;
72 /** The temp directory. */
73 private String tempDirectory;
75 /** The configuration location. */
76 private ConfigurationLocation configurationLocation;
78 /** The “default” button. */
79 private JRadioButton defaultTempDirectory;
81 /** The “custom” button. */
82 private JRadioButton customTempDirectory;
84 /** The “next to JAR file” checkbox. */
85 private JRadioButton nextToJarFile;
87 /** The “home directory” checkbox. */
88 private JRadioButton homeDirectory;
90 /** The “custom directory” checkbox. */
91 private JRadioButton customDirectory;
94 * Creates a new “preferences” page.
97 * The wizard this page belongs to
99 public PreferencesPage(TWizard wizard) {
102 setHeading(I18n.getMessage("jsite.preferences.heading"));
103 setDescription(I18n.getMessage("jsite.preferences.description"));
104 I18nContainer.getInstance().registerRunnable(new Runnable() {
110 setHeading(I18n.getMessage("jsite.preferences.heading"));
111 setDescription(I18n.getMessage("jsite.preferences.description"));
121 * Returns the temp directory.
123 * @return The temp directory, or {@code null} to use the default temp
126 public String getTempDirectory() {
127 return tempDirectory;
131 * Sets the temp directory.
133 * @param tempDirectory
134 * The temp directory, or {@code null} to use the default temp
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);
144 defaultTempDirectory.setSelected(true);
149 * Returns the configuration location.
151 * @return The configuration location
153 public ConfigurationLocation getConfigurationLocation() {
154 return configurationLocation;
158 * Sets the configuration location.
160 * @param configurationLocation
161 * The configuration location
163 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
164 this.configurationLocation = configurationLocation;
165 switch (configurationLocation) {
166 case NEXT_TO_JAR_FILE:
167 nextToJarFile.setSelected(true);
170 homeDirectory.setSelected(true);
173 customDirectory.setSelected(true);
179 * Sets whether it is possible to select the “next to JAR file” option for
180 * the configuration location.
182 * @param nextToJarFile
183 * {@code true} if the configuration file can be saved next to
184 * the JAR file, {@code false} otherwise
186 public void setHasNextToJarConfiguration(boolean nextToJarFile) {
187 this.nextToJarFile.setEnabled(nextToJarFile);
191 * Sets whether it is possible to select the “custom location” option for
192 * the configuration location.
194 * @param customDirectory
195 * {@code true} if the configuration file can be saved to a
196 * custom location, {@code false} otherwise
198 public void setHasCustomConfiguration(boolean customDirectory) {
199 this.customDirectory.setEnabled(customDirectory);
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);
219 * Initializes this page.
221 private void pageInit() {
223 setLayout(new BorderLayout(12, 12));
224 add(createPreferencesPanel(), BorderLayout.CENTER);
228 * Creates all actions.
230 private void createActions() {
231 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
236 @SuppressWarnings("synthetic-access")
237 public void actionPerformed(ActionEvent actionEvent) {
238 selectDefaultTempDirectory();
241 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
246 @SuppressWarnings("synthetic-access")
247 public void actionPerformed(ActionEvent actionEvent) {
248 selectCustomTempDirectory();
251 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
253 @SuppressWarnings("synthetic-access")
254 public void actionPerformed(ActionEvent e) {
255 chooseTempDirectory();
258 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
260 @SuppressWarnings("synthetic-access")
261 public void actionPerformed(ActionEvent actionevent) {
262 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
265 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
267 @SuppressWarnings("synthetic-access")
268 public void actionPerformed(ActionEvent actionevent) {
269 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
272 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
274 @SuppressWarnings("synthetic-access")
275 public void actionPerformed(ActionEvent actionEvent) {
276 configurationLocation = ConfigurationLocation.CUSTOM;
280 I18nContainer.getInstance().registerRunnable(new Runnable() {
282 @SuppressWarnings("synthetic-access")
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"));
295 * Creates the panel containing all preferences.
297 * @return The preferences panel
299 private JPanel createPreferencesPanel() {
300 JPanel preferencesPanel = new JPanel(new GridBagLayout());
301 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
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));
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));
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));
312 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
313 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
314 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
316 tempDirectoryTextField = new JTextField();
317 tempDirectoryTextField.setEditable(false);
318 if (tempDirectory != null) {
319 tempDirectoryTextField.setText(tempDirectory);
320 customTempDirectory.setSelected(true);
322 defaultTempDirectory.setSelected(true);
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));
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));
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));
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));
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));
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));
342 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
343 configurationDirectoryButtonGroup.add(nextToJarFile);
344 configurationDirectoryButtonGroup.add(homeDirectory);
345 configurationDirectoryButtonGroup.add(customDirectory);
347 I18nContainer.getInstance().registerRunnable(new Runnable() {
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>");
358 return preferencesPanel;
362 * Activates the default temp directory radio button.
364 private void selectDefaultTempDirectory() {
365 tempDirectoryTextField.setEnabled(false);
366 chooseTempDirectoryAction.setEnabled(false);
367 tempDirectory = null;
371 * Activates the custom temp directory radio button.
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);
385 * Lets the user choose a new temp directory.
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) {
394 tempDirectory = fileChooser.getSelectedFile().getPath();
395 tempDirectoryTextField.setText(tempDirectory);