a2a13b0a26ced9e5d925e263d768f8b0d7375475
[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         /** Action that chooses a new temp directory. */
50         private Action chooseTempDirectoryAction;
51
52         /** The text field containing the directory. */
53         private JTextField tempDirectoryTextField;
54
55         /** The temp directory. */
56         private String tempDirectory;
57
58         /**
59          * Creates a new “preferences” page.
60          *
61          * @param wizard
62          *            The wizard this page belongs to
63          */
64         public PreferencesPage(TWizard wizard) {
65                 super(wizard);
66                 pageInit();
67                 setHeading(I18n.getMessage("jsite.preferences.heading"));
68                 setDescription(I18n.getMessage("jsite.preferences.description"));
69                 I18nContainer.getInstance().registerRunnable(new Runnable() {
70
71                         /**
72                          * {@inheritDoc}
73                          */
74                         @Override
75                         public void run() {
76                                 setHeading(I18n.getMessage("jsite.preferences.heading"));
77                                 setDescription(I18n.getMessage("jsite.preferences.description"));
78                         }
79                 });
80         }
81
82         //
83         // ACCESSORS
84         //
85
86         /**
87          * Returns the temp directory.
88          *
89          * @return The temp directory, or {@code null} to use the default temp
90          *         directory
91          */
92         public String getTempDirectory() {
93                 return tempDirectory;
94         }
95
96         /**
97          * Sets the temp directory.
98          *
99          * @param tempDirectory
100          *            The temp directory, or {@code null} to use the default temp
101          *            directory
102          */
103         public void setTempDirectory(String tempDirectory) {
104                 this.tempDirectory = tempDirectory;
105         }
106
107         //
108         // PRIVATE METHODS
109         //
110
111         /**
112          * Initializes this page.
113          */
114         private void pageInit() {
115                 createActions();
116                 setLayout(new BorderLayout(12, 12));
117                 add(createPreferencesPanel(), BorderLayout.CENTER);
118         }
119
120         /**
121          * Creates all actions.
122          */
123         private void createActions() {
124                 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
125
126                         @Override
127                         @SuppressWarnings("synthetic-access")
128                         public void actionPerformed(ActionEvent e) {
129                                 chooseTempDirectory();
130                         }
131                 };
132
133                 I18nContainer.getInstance().registerRunnable(new Runnable() {
134
135                         @Override
136                         @SuppressWarnings("synthetic-access")
137                         public void run() {
138                                 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
139                         }
140                 });
141         }
142
143         /**
144          * Creates the panel containing all preferences.
145          *
146          * @return The preferences panel
147          */
148         private JPanel createPreferencesPanel() {
149                 JPanel preferencesPanel = new JPanel(new BorderLayout(12, 12));
150
151                 JPanel tempDirectoryPanel = new JPanel(new GridBagLayout());
152                 preferencesPanel.add(tempDirectoryPanel, BorderLayout.CENTER);
153
154                 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
155                 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));
156
157                 final JRadioButton defaultTempDirectory = new JRadioButton(I18n.getMessage("jsite.preferences.temp-directory.default"));
158                 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));
159
160                 final JRadioButton customTempDirectory = new JRadioButton(I18n.getMessage("jsite.preferences.temp-directory.custom"));
161                 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));
162
163                 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
164                 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
165                 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
166
167                 tempDirectoryTextField = new JTextField();
168                 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));
169
170                 JButton chooseButton = new JButton(chooseTempDirectoryAction);
171                 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));
172
173                 I18nContainer.getInstance().registerRunnable(new Runnable() {
174
175                         /**
176                          * {@inheritDoc}
177                          */
178                         @Override
179                         public void run() {
180                                 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
181                                 defaultTempDirectory.setText(I18n.getMessage("jsite.preferences.temp-directory.default"));
182                                 customTempDirectory.setText(I18n.getMessage("jsite.preferences.temp-directory.custom"));
183                         }
184                 });
185
186                 return preferencesPanel;
187         }
188
189         /**
190          * Lets the user choose a new temp directory.
191          */
192         private void chooseTempDirectory() {
193                 /* TODO */
194         }
195
196 }