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