Add “choose” button.
[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.ButtonGroup;
31 import javax.swing.JButton;
32 import javax.swing.JLabel;
33 import javax.swing.JPanel;
34 import javax.swing.JRadioButton;
35
36 import de.todesbaum.jsite.i18n.I18n;
37 import de.todesbaum.jsite.i18n.I18nContainer;
38 import de.todesbaum.util.swing.TWizard;
39 import de.todesbaum.util.swing.TWizardPage;
40
41 /**
42  * Page that shows some preferences that are valid for the complete application.
43  *
44  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
45  */
46 public class PreferencesPage extends TWizardPage {
47
48         /** Action that chooses a new temp directory. */
49         private Action chooseTempDirectoryAction;
50
51         /** The temp directory. */
52         private String tempDirectory;
53
54         /**
55          * Creates a new “preferences” page.
56          *
57          * @param wizard
58          *            The wizard this page belongs to
59          */
60         public PreferencesPage(TWizard wizard) {
61                 super(wizard);
62                 pageInit();
63                 setHeading(I18n.getMessage("jsite.preferences.heading"));
64                 setDescription(I18n.getMessage("jsite.preferences.description"));
65                 I18nContainer.getInstance().registerRunnable(new Runnable() {
66
67                         /**
68                          * {@inheritDoc}
69                          */
70                         @Override
71                         public void run() {
72                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
73                                 setDescription(I18n.getMessage("jsite.preferences.description"));
74                         }
75                 });
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         /**
83          * Returns the temp directory.
84          *
85          * @return The temp directory, or {@code null} to use the default temp
86          *         directory
87          */
88         public String getTempDirectory() {
89                 return tempDirectory;
90         }
91
92         /**
93          * Sets the temp directory.
94          *
95          * @param tempDirectory
96          *            The temp directory, or {@code null} to use the default temp
97          *            directory
98          */
99         public void setTempDirectory(String tempDirectory) {
100                 this.tempDirectory = tempDirectory;
101         }
102
103         //
104         // PRIVATE METHODS
105         //
106
107         /**
108          * Initializes this page.
109          */
110         private void pageInit() {
111                 createActions();
112                 setLayout(new BorderLayout(12, 12));
113                 add(createPreferencesPanel(), BorderLayout.CENTER);
114         }
115
116         /**
117          * Creates all actions.
118          */
119         private void createActions() {
120                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
121
122                         @Override
123                         @SuppressWarnings("synthetic-access")
124                         public void actionPerformed(ActionEvent e) {
125                                 chooseTempDirectory();
126                         }
127                 };
128
129                 I18nContainer.getInstance().registerRunnable(new Runnable() {
130
131                         @Override
132                         @SuppressWarnings("synthetic-access")
133                         public void run() {
134                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
135                         }
136                 });
137         }
138
139         /**
140          * Creates the panel containing all preferences.
141          *
142          * @return The preferences panel
143          */
144         private JPanel createPreferencesPanel() {
145                 JPanel preferencesPanel = new JPanel(new BorderLayout(12, 12));
146
147                 JPanel tempDirectoryPanel = new JPanel(new GridBagLayout());
148                 preferencesPanel.add(tempDirectoryPanel, BorderLayout.CENTER);
149
150                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
151                 tempDirectoryPanel.add(tempDirectoryLabel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
152
153                 final JRadioButton defaultTempDirectory = new JRadioButton(I18n.getMessage("jsite.preferences.temp-directory.default"));
154                 tempDirectoryPanel.add(defaultTempDirectory, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 18, 0, 0), 0, 0));
155
156                 final JRadioButton customTempDirectory = new JRadioButton(I18n.getMessage("jsite.preferences.temp-directory.custom"));
157                 tempDirectoryPanel.add(customTempDirectory, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 18, 0, 0), 0, 0));
158
159                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
160                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
161                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
162
163                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
164                 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));
165
166                 I18nContainer.getInstance().registerRunnable(new Runnable() {
167
168                         /**
169                          * {@inheritDoc}
170                          */
171                         @Override
172                         public void run() {
173                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
174                                 defaultTempDirectory.setText(I18n.getMessage("jsite.preferences.temp-directory.default"));
175                                 customTempDirectory.setText(I18n.getMessage("jsite.preferences.temp-directory.custom"));
176                         }
177                 });
178
179                 return preferencesPanel;
180         }
181
182         /**
183          * Lets the user choose a new temp directory.
184          */
185         private void chooseTempDirectory() {
186                 /* TODO */
187         }
188
189 }