513c23ff9ea270792118f342a48eb4a97e704a4b
[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         /**
66          * Creates a new “preferences” page.
67          *
68          * @param wizard
69          *            The wizard this page belongs to
70          */
71         public PreferencesPage(TWizard wizard) {
72                 super(wizard);
73                 pageInit();
74                 setHeading(I18n.getMessage("jsite.preferences.heading"));
75                 setDescription(I18n.getMessage("jsite.preferences.description"));
76                 I18nContainer.getInstance().registerRunnable(new Runnable() {
77
78                         /**
79                          * {@inheritDoc}
80                          */
81                         @Override
82                         public void run() {
83                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
84                                 setDescription(I18n.getMessage("jsite.preferences.description"));
85                         }
86                 });
87         }
88
89         //
90         // ACCESSORS
91         //
92
93         /**
94          * Returns the temp directory.
95          *
96          * @return The temp directory, or {@code null} to use the default temp
97          *         directory
98          */
99         public String getTempDirectory() {
100                 return tempDirectory;
101         }
102
103         /**
104          * Sets the temp directory.
105          *
106          * @param tempDirectory
107          *            The temp directory, or {@code null} to use the default temp
108          *            directory
109          */
110         public void setTempDirectory(String tempDirectory) {
111                 this.tempDirectory = tempDirectory;
112         }
113
114         //
115         // PRIVATE METHODS
116         //
117
118         /**
119          * Initializes this page.
120          */
121         private void pageInit() {
122                 createActions();
123                 setLayout(new BorderLayout(12, 12));
124                 add(createPreferencesPanel(), BorderLayout.CENTER);
125         }
126
127         /**
128          * Creates all actions.
129          */
130         private void createActions() {
131                 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
132
133                         /**
134                          * {@inheritDoc}
135                          */
136                         @Override
137                         @SuppressWarnings("synthetic-access")
138                         public void actionPerformed(ActionEvent actionEvent) {
139                                 selectDefaultTempDirectory();
140                         }
141                 };
142                 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
143
144                         /**
145                          * {@inheritDoc}
146                          */
147                         @Override
148                         @SuppressWarnings("synthetic-access")
149                         public void actionPerformed(ActionEvent actionEvent) {
150                                 selectCustomTempDirectory();
151                         }
152                 };
153                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
154
155                         @Override
156                         @SuppressWarnings("synthetic-access")
157                         public void actionPerformed(ActionEvent e) {
158                                 chooseTempDirectory();
159                         }
160                 };
161
162                 I18nContainer.getInstance().registerRunnable(new Runnable() {
163
164                         @Override
165                         @SuppressWarnings("synthetic-access")
166                         public void run() {
167                                 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
168                                 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
169                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
170                         }
171                 });
172         }
173
174         /**
175          * Creates the panel containing all preferences.
176          *
177          * @return The preferences panel
178          */
179         private JPanel createPreferencesPanel() {
180                 JPanel preferencesPanel = new JPanel(new BorderLayout(12, 12));
181
182                 JPanel tempDirectoryPanel = new JPanel(new GridBagLayout());
183                 preferencesPanel.add(tempDirectoryPanel, BorderLayout.CENTER);
184
185                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
186                 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));
187
188                 final JRadioButton defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
189                 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));
190
191                 final JRadioButton customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
192                 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));
193
194                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
195                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
196                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
197
198                 tempDirectoryTextField = new JTextField();
199                 tempDirectoryTextField.setEditable(false);
200                 if (tempDirectory != null) {
201                         tempDirectoryTextField.setText(tempDirectory);
202                         customTempDirectory.setSelected(true);
203                 } else {
204                         defaultTempDirectory.setSelected(true);
205                 }
206                 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
207                 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));
208
209                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
210                 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));
211
212                 I18nContainer.getInstance().registerRunnable(new Runnable() {
213
214                         /**
215                          * {@inheritDoc}
216                          */
217                         @Override
218                         public void run() {
219                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
220                         }
221                 });
222
223                 return preferencesPanel;
224         }
225
226         /**
227          * Activates the default temp directory radio button.
228          */
229         private void selectDefaultTempDirectory() {
230                 tempDirectoryTextField.setEnabled(false);
231                 chooseTempDirectoryAction.setEnabled(false);
232         }
233
234         /**
235          * Activates the custom temp directory radio button.
236          */
237         private void selectCustomTempDirectory() {
238                 tempDirectoryTextField.setEnabled(true);
239                 chooseTempDirectoryAction.setEnabled(true);
240         }
241
242         /**
243          * Lets the user choose a new temp directory.
244          */
245         private void chooseTempDirectory() {
246                 JFileChooser fileChooser = new JFileChooser(tempDirectory);
247                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
248                 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
249                 if (returnValue == JFileChooser.CANCEL_OPTION) {
250                         return;
251                 }
252                 tempDirectory = fileChooser.getSelectedFile().getPath();
253                 tempDirectoryTextField.setText(tempDirectory);
254         }
255
256 }