2 * jSite - PreferencesPage.java -
3 * Copyright © 2009 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.gui;
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;
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;
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;
47 * Page that shows some preferences that are valid for the complete application.
49 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
51 public class PreferencesPage extends TWizardPage {
53 /** Select default temp directory action. */
54 private Action selectDefaultTempDirectoryAction;
56 /** Select custom temp directory action. */
57 private Action selectCustomTempDirectoryAction;
59 /** Action that chooses a new temp directory. */
60 private Action chooseTempDirectoryAction;
62 /** Action when selecting “next to JAR file.” */
63 private Action nextToJarFileAction;
65 /** Action when selecting “home directory.” */
66 private Action homeDirectoryAction;
68 /** The text field containing the directory. */
69 private JTextField tempDirectoryTextField;
71 /** The temp directory. */
72 private String tempDirectory;
74 /** The configuration. */
75 private Configuration configuration;
77 /** The configuration directory. */
78 private ConfigurationDirectory configurationDirectory;
80 /** The “default” button. */
81 private JRadioButton defaultTempDirectory;
83 /** The “custom” button. */
84 private JRadioButton customTempDirectory;
86 /** The “next to JAR file” checkbox. */
87 private JRadioButton nextToJarFile;
89 /** The “current directory” checkbox. */
90 private JRadioButton currentDirectory;
92 /** The “home directory” checkbox. */
93 private JRadioButton homeDirectory;
96 * Creates a new “preferences” page.
99 * The wizard this page belongs to
100 * @param configuration
101 * The configuration that is controlled
103 public PreferencesPage(TWizard wizard, Configuration configuration) {
106 setHeading(I18n.getMessage("jsite.preferences.heading"));
107 setDescription(I18n.getMessage("jsite.preferences.description"));
108 I18nContainer.getInstance().registerRunnable(new Runnable() {
114 setHeading(I18n.getMessage("jsite.preferences.heading"));
115 setDescription(I18n.getMessage("jsite.preferences.description"));
118 this.configuration = configuration;
126 * Returns the temp directory.
128 * @return The temp directory, or {@code null} to use the default temp
131 public String getTempDirectory() {
132 return tempDirectory;
136 * Sets the temp directory.
138 * @param tempDirectory
139 * The temp directory, or {@code null} to use the default temp
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);
149 defaultTempDirectory.setSelected(true);
154 * Returns the configuration directory.
156 * @return The configuration directory
158 public ConfigurationDirectory getConfigurationDirectory() {
159 return configurationDirectory;
163 * Sets the configuration directory.
165 * @param configurationDirectory
166 * The configuration directory
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);
176 homeDirectory.setSelected(true);
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);
198 * Initializes this page.
200 private void pageInit() {
202 setLayout(new BorderLayout(12, 12));
203 add(createPreferencesPanel(), BorderLayout.CENTER);
207 * Creates all actions.
209 private void createActions() {
210 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
215 @SuppressWarnings("synthetic-access")
216 public void actionPerformed(ActionEvent actionEvent) {
217 selectDefaultTempDirectory();
220 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
225 @SuppressWarnings("synthetic-access")
226 public void actionPerformed(ActionEvent actionEvent) {
227 selectCustomTempDirectory();
230 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
232 @SuppressWarnings("synthetic-access")
233 public void actionPerformed(ActionEvent e) {
234 chooseTempDirectory();
237 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
239 @SuppressWarnings("synthetic-access")
240 public void actionPerformed(ActionEvent actionevent) {
241 configurationDirectory = ConfigurationDirectory.NEXT_TO_JAR_FILE;
244 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
246 @SuppressWarnings("synthetic-access")
247 public void actionPerformed(ActionEvent actionevent) {
248 configurationDirectory = ConfigurationDirectory.HOME_DIRECTORY;
252 I18nContainer.getInstance().registerRunnable(new Runnable() {
254 @SuppressWarnings("synthetic-access")
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"));
266 * Creates the panel containing all preferences.
268 * @return The preferences panel
270 private JPanel createPreferencesPanel() {
271 JPanel preferencesPanel = new JPanel(new GridBagLayout());
272 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
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));
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));
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));
283 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
284 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
285 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
287 tempDirectoryTextField = new JTextField();
288 tempDirectoryTextField.setEditable(false);
289 if (tempDirectory != null) {
290 tempDirectoryTextField.setText(tempDirectory);
291 customTempDirectory.setSelected(true);
293 defaultTempDirectory.setSelected(true);
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));
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));
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));
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));
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));
310 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
311 configurationDirectoryButtonGroup.add(nextToJarFile);
312 configurationDirectoryButtonGroup.add(currentDirectory);
313 configurationDirectoryButtonGroup.add(homeDirectory);
315 I18nContainer.getInstance().registerRunnable(new Runnable() {
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>");
326 return preferencesPanel;
330 * Activates the default temp directory radio button.
332 private void selectDefaultTempDirectory() {
333 tempDirectoryTextField.setEnabled(false);
334 chooseTempDirectoryAction.setEnabled(false);
335 tempDirectory = null;
339 * Activates the custom temp directory radio button.
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);
353 * Lets the user choose a new temp directory.
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) {
362 tempDirectory = fileChooser.getSelectedFile().getPath();
363 tempDirectoryTextField.setText(tempDirectory);