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.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.util.swing.TWizard;
41 import de.todesbaum.util.swing.TWizardPage;
44 * Page that shows some preferences that are valid for the complete application.
46 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
48 public class PreferencesPage extends TWizardPage {
50 /** Select default temp directory action. */
51 private Action selectDefaultTempDirectoryAction;
53 /** Select custom temp directory action. */
54 private Action selectCustomTempDirectoryAction;
56 /** Action that chooses a new temp directory. */
57 private Action chooseTempDirectoryAction;
59 /** The text field containing the directory. */
60 private JTextField tempDirectoryTextField;
62 /** The temp directory. */
63 private String tempDirectory;
65 /** The “default” button. */
66 private JRadioButton defaultTempDirectory;
68 /** The “custom” button. */
69 private JRadioButton customTempDirectory;
72 * Creates a new “preferences” page.
75 * The wizard this page belongs to
77 public PreferencesPage(TWizard wizard) {
80 setHeading(I18n.getMessage("jsite.preferences.heading"));
81 setDescription(I18n.getMessage("jsite.preferences.description"));
82 I18nContainer.getInstance().registerRunnable(new Runnable() {
88 setHeading(I18n.getMessage("jsite.preferences.heading"));
89 setDescription(I18n.getMessage("jsite.preferences.description"));
99 * Returns the temp directory.
101 * @return The temp directory, or {@code null} to use the default temp
104 public String getTempDirectory() {
105 return tempDirectory;
109 * Sets the temp directory.
111 * @param tempDirectory
112 * The temp directory, or {@code null} to use the default temp
115 public void setTempDirectory(String tempDirectory) {
116 this.tempDirectory = tempDirectory;
117 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
118 if (tempDirectory != null) {
119 customTempDirectory.setSelected(true);
120 chooseTempDirectoryAction.setEnabled(true);
122 defaultTempDirectory.setSelected(true);
130 public void pageAdded(TWizard wizard) {
131 super.pageAdded(wizard);
132 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
133 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
134 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
135 this.wizard.setNextEnabled(false);
143 * Initializes this page.
145 private void pageInit() {
147 setLayout(new BorderLayout(12, 12));
148 add(createPreferencesPanel(), BorderLayout.CENTER);
152 * Creates all actions.
154 private void createActions() {
155 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
160 @SuppressWarnings("synthetic-access")
161 public void actionPerformed(ActionEvent actionEvent) {
162 selectDefaultTempDirectory();
165 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
170 @SuppressWarnings("synthetic-access")
171 public void actionPerformed(ActionEvent actionEvent) {
172 selectCustomTempDirectory();
175 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
177 @SuppressWarnings("synthetic-access")
178 public void actionPerformed(ActionEvent e) {
179 chooseTempDirectory();
183 I18nContainer.getInstance().registerRunnable(new Runnable() {
185 @SuppressWarnings("synthetic-access")
187 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
188 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
189 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
195 * Creates the panel containing all preferences.
197 * @return The preferences panel
199 private JPanel createPreferencesPanel() {
200 JPanel preferencesPanel = new JPanel(new BorderLayout(12, 12));
202 JPanel tempDirectoryPanel = new JPanel(new GridBagLayout());
203 preferencesPanel.add(tempDirectoryPanel, BorderLayout.CENTER);
205 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
206 tempDirectoryPanel.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));
208 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
209 tempDirectoryPanel.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));
211 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
212 tempDirectoryPanel.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));
214 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
215 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
216 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
218 tempDirectoryTextField = new JTextField();
219 tempDirectoryTextField.setEditable(false);
220 if (tempDirectory != null) {
221 tempDirectoryTextField.setText(tempDirectory);
222 customTempDirectory.setSelected(true);
224 defaultTempDirectory.setSelected(true);
226 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
227 tempDirectoryPanel.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));
229 JButton chooseButton = new JButton(chooseTempDirectoryAction);
230 tempDirectoryPanel.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));
232 I18nContainer.getInstance().registerRunnable(new Runnable() {
238 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
242 return preferencesPanel;
246 * Activates the default temp directory radio button.
248 private void selectDefaultTempDirectory() {
249 tempDirectoryTextField.setEnabled(false);
250 chooseTempDirectoryAction.setEnabled(false);
251 tempDirectory = null;
255 * Activates the custom temp directory radio button.
257 private void selectCustomTempDirectory() {
258 tempDirectoryTextField.setEnabled(true);
259 chooseTempDirectoryAction.setEnabled(true);
260 if (tempDirectoryTextField.getText().length() == 0) {
261 chooseTempDirectory();
262 if (tempDirectoryTextField.getText().length() == 0) {
263 defaultTempDirectory.setSelected(true);
269 * Lets the user choose a new temp directory.
271 private void chooseTempDirectory() {
272 JFileChooser fileChooser = new JFileChooser(tempDirectory);
273 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
274 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
275 if (returnValue == JFileChooser.CANCEL_OPTION) {
278 tempDirectory = fileChooser.getSelectedFile().getPath();
279 tempDirectoryTextField.setText(tempDirectory);