Create radio buttons from actions.
[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 import javax.swing.JTextField;
36
37 import de.todesbaum.jsite.i18n.I18n;
38 import de.todesbaum.jsite.i18n.I18nContainer;
39 import de.todesbaum.util.swing.TWizard;
40 import de.todesbaum.util.swing.TWizardPage;
41
42 /**
43  * Page that shows some preferences that are valid for the complete application.
44  *
45  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46  */
47 public class PreferencesPage extends TWizardPage {
48
49         /** Select default temp directory action. */
50         private Action selectDefaultTempDirectoryAction;
51
52         /** Select custom temp directory action. */
53         private Action selectCustomTempDirectoryAction;
54
55         /** Action that chooses a new temp directory. */
56         private Action chooseTempDirectoryAction;
57
58         /** The text field containing the directory. */
59         private JTextField tempDirectoryTextField;
60
61         /** The temp directory. */
62         private String tempDirectory;
63
64         /**
65          * Creates a new “preferences” page.
66          *
67          * @param wizard
68          *            The wizard this page belongs to
69          */
70         public PreferencesPage(TWizard wizard) {
71                 super(wizard);
72                 pageInit();
73                 setHeading(I18n.getMessage("jsite.preferences.heading"));
74                 setDescription(I18n.getMessage("jsite.preferences.description"));
75                 I18nContainer.getInstance().registerRunnable(new Runnable() {
76
77                         /**
78                          * {@inheritDoc}
79                          */
80                         @Override
81                         public void run() {
82                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
83                                 setDescription(I18n.getMessage("jsite.preferences.description"));
84                         }
85                 });
86         }
87
88         //
89         // ACCESSORS
90         //
91
92         /**
93          * Returns the temp directory.
94          *
95          * @return The temp directory, or {@code null} to use the default temp
96          *         directory
97          */
98         public String getTempDirectory() {
99                 return tempDirectory;
100         }
101
102         /**
103          * Sets the temp directory.
104          *
105          * @param tempDirectory
106          *            The temp directory, or {@code null} to use the default temp
107          *            directory
108          */
109         public void setTempDirectory(String tempDirectory) {
110                 this.tempDirectory = tempDirectory;
111         }
112
113         //
114         // PRIVATE METHODS
115         //
116
117         /**
118          * Initializes this page.
119          */
120         private void pageInit() {
121                 createActions();
122                 setLayout(new BorderLayout(12, 12));
123                 add(createPreferencesPanel(), BorderLayout.CENTER);
124         }
125
126         /**
127          * Creates all actions.
128          */
129         private void createActions() {
130                 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
131
132                         /**
133                          * {@inheritDoc}
134                          */
135                         @Override
136                         @SuppressWarnings("synthetic-access")
137                         public void actionPerformed(ActionEvent actionEvent) {
138                                 selectDefaultTempDirectory();
139                         }
140                 };
141                 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
142
143                         /**
144                          * {@inheritDoc}
145                          */
146                         @Override
147                         @SuppressWarnings("synthetic-access")
148                         public void actionPerformed(ActionEvent actionEvent) {
149                                 selectCustomTempDirectory();
150                         }
151                 };
152                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
153
154                         @Override
155                         @SuppressWarnings("synthetic-access")
156                         public void actionPerformed(ActionEvent e) {
157                                 chooseTempDirectory();
158                         }
159                 };
160
161                 I18nContainer.getInstance().registerRunnable(new Runnable() {
162
163                         @Override
164                         @SuppressWarnings("synthetic-access")
165                         public void run() {
166                                 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
167                                 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
168                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
169                         }
170                 });
171         }
172
173         /**
174          * Creates the panel containing all preferences.
175          *
176          * @return The preferences panel
177          */
178         private JPanel createPreferencesPanel() {
179                 JPanel preferencesPanel = new JPanel(new BorderLayout(12, 12));
180
181                 JPanel tempDirectoryPanel = new JPanel(new GridBagLayout());
182                 preferencesPanel.add(tempDirectoryPanel, BorderLayout.CENTER);
183
184                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
185                 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));
186
187                 final JRadioButton defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
188                 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));
189
190                 final JRadioButton customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
191                 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));
192
193                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
194                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
195                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
196
197                 tempDirectoryTextField = new JTextField();
198                 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));
199
200                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
201                 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));
202
203                 I18nContainer.getInstance().registerRunnable(new Runnable() {
204
205                         /**
206                          * {@inheritDoc}
207                          */
208                         @Override
209                         public void run() {
210                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
211                         }
212                 });
213
214                 return preferencesPanel;
215         }
216
217         /**
218          * Activates the default temp directory radio button.
219          */
220         private void selectDefaultTempDirectory() {
221                 tempDirectoryTextField.setEnabled(false);
222                 chooseTempDirectoryAction.setEnabled(false);
223         }
224
225         /**
226          * Activates the custom temp directory radio button.
227          */
228         private void selectCustomTempDirectory() {
229                 tempDirectoryTextField.setEnabled(true);
230                 chooseTempDirectoryAction.setEnabled(true);
231         }
232
233         /**
234          * Lets the user choose a new temp directory.
235          */
236         private void chooseTempDirectory() {
237                 /* TODO */
238         }
239
240 }