2 * jSite - PreferencesPage.java - Copyright © 2009–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.gui;
21 import java.awt.BorderLayout;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.event.ActionEvent;
27 import javax.swing.AbstractAction;
28 import javax.swing.Action;
29 import javax.swing.BorderFactory;
30 import javax.swing.ButtonGroup;
31 import javax.swing.JButton;
32 import javax.swing.JCheckBox;
33 import javax.swing.JFileChooser;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JRadioButton;
37 import javax.swing.JTextField;
39 import de.todesbaum.jsite.i18n.I18n;
40 import de.todesbaum.jsite.i18n.I18nContainer;
41 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
42 import de.todesbaum.util.swing.TWizard;
43 import de.todesbaum.util.swing.TWizardPage;
46 * Page that shows some preferences that are valid for the complete application.
48 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
50 public class PreferencesPage extends TWizardPage {
52 /** Select default temp directory action. */
53 private Action selectDefaultTempDirectoryAction;
55 /** Select custom temp directory action. */
56 private Action selectCustomTempDirectoryAction;
58 /** Action that chooses a new temp directory. */
59 private Action chooseTempDirectoryAction;
61 /** Action when selecting “next to JAR file.” */
62 private Action nextToJarFileAction;
64 /** Action when selecting “home directory.” */
65 private Action homeDirectoryAction;
67 /** Action when selecting “custom directory.” */
68 private Action customDirectoryAction;
70 /** Action when selecting “use early encode.” */
71 private Action useEarlyEncodeAction;
73 /** The text field containing the directory. */
74 private JTextField tempDirectoryTextField;
76 /** The temp directory. */
77 private String tempDirectory;
79 /** The configuration location. */
80 private ConfigurationLocation configurationLocation;
82 /** Whether to use “early encode.” */
83 private boolean useEarlyEncode;
85 /** The “default” button. */
86 private JRadioButton defaultTempDirectory;
88 /** The “custom” button. */
89 private JRadioButton customTempDirectory;
91 /** The “next to JAR file” checkbox. */
92 private JRadioButton nextToJarFile;
94 /** The “home directory” checkbox. */
95 private JRadioButton homeDirectory;
97 /** The “custom directory” checkbox. */
98 private JRadioButton customDirectory;
100 /** The “use early encode” checkbox. */
101 private JCheckBox useEarlyEncodeCheckBox;
104 * Creates a new “preferences” page.
107 * The wizard this page belongs to
109 public PreferencesPage(TWizard wizard) {
112 setHeading(I18n.getMessage("jsite.preferences.heading"));
113 setDescription(I18n.getMessage("jsite.preferences.description"));
114 I18nContainer.getInstance().registerRunnable(new Runnable() {
120 setHeading(I18n.getMessage("jsite.preferences.heading"));
121 setDescription(I18n.getMessage("jsite.preferences.description"));
131 * Returns the temp directory.
133 * @return The temp directory, or {@code null} to use the default temp
136 public String getTempDirectory() {
137 return tempDirectory;
141 * Sets the temp directory.
143 * @param tempDirectory
144 * The temp directory, or {@code null} to use the default temp
147 public void setTempDirectory(String tempDirectory) {
148 this.tempDirectory = tempDirectory;
149 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
150 if (tempDirectory != null) {
151 customTempDirectory.setSelected(true);
152 chooseTempDirectoryAction.setEnabled(true);
154 defaultTempDirectory.setSelected(true);
159 * Returns the configuration location.
161 * @return The configuration location
163 public ConfigurationLocation getConfigurationLocation() {
164 return configurationLocation;
168 * Sets the configuration location.
170 * @param configurationLocation
171 * The configuration location
173 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
174 this.configurationLocation = configurationLocation;
175 switch (configurationLocation) {
176 case NEXT_TO_JAR_FILE:
177 nextToJarFile.setSelected(true);
180 homeDirectory.setSelected(true);
183 customDirectory.setSelected(true);
189 * Sets whether it is possible to select the “next to JAR file” option for
190 * the configuration location.
192 * @param nextToJarFile
193 * {@code true} if the configuration file can be saved next to
194 * the JAR file, {@code false} otherwise
196 public void setHasNextToJarConfiguration(boolean nextToJarFile) {
197 this.nextToJarFile.setEnabled(nextToJarFile);
201 * Sets whether it is possible to select the “custom location” option for
202 * the configuration location.
204 * @param customDirectory
205 * {@code true} if the configuration file can be saved to a
206 * custom location, {@code false} otherwise
208 public void setHasCustomConfiguration(boolean customDirectory) {
209 this.customDirectory.setEnabled(customDirectory);
213 * Returns whether to use the “early encode“ flag for the insert.
215 * @return {@code true} to set the “early encode” flag for the insert,
216 * {@code false} otherwise
218 public boolean useEarlyEncode() {
219 return useEarlyEncode;
223 * Sets whether to use the “early encode“ flag for the insert.
225 * @param useEarlyEncode
226 * {@code true} to set the “early encode” flag for the insert,
227 * {@code false} otherwise
229 public void setUseEarlyEncode(boolean useEarlyEncode) {
230 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
237 public void pageAdded(TWizard wizard) {
238 super.pageAdded(wizard);
239 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
240 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
241 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
242 this.wizard.setNextEnabled(false);
250 * Initializes this page.
252 private void pageInit() {
254 setLayout(new BorderLayout(12, 12));
255 add(createPreferencesPanel(), BorderLayout.CENTER);
259 * Creates all actions.
261 private void createActions() {
262 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
267 @SuppressWarnings("synthetic-access")
268 public void actionPerformed(ActionEvent actionEvent) {
269 selectDefaultTempDirectory();
272 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
277 @SuppressWarnings("synthetic-access")
278 public void actionPerformed(ActionEvent actionEvent) {
279 selectCustomTempDirectory();
282 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
284 @SuppressWarnings("synthetic-access")
285 public void actionPerformed(ActionEvent e) {
286 chooseTempDirectory();
289 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
291 @SuppressWarnings("synthetic-access")
292 public void actionPerformed(ActionEvent actionevent) {
293 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
296 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
298 @SuppressWarnings("synthetic-access")
299 public void actionPerformed(ActionEvent actionevent) {
300 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
303 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
305 @SuppressWarnings("synthetic-access")
306 public void actionPerformed(ActionEvent actionEvent) {
307 configurationLocation = ConfigurationLocation.CUSTOM;
310 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
312 @SuppressWarnings("synthetic-access")
313 public void actionPerformed(ActionEvent actionEvent) {
314 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
318 I18nContainer.getInstance().registerRunnable(new Runnable() {
320 @SuppressWarnings("synthetic-access")
322 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
323 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
324 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
325 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
326 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
327 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
328 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
334 * Creates the panel containing all preferences.
336 * @return The preferences panel
338 private JPanel createPreferencesPanel() {
339 JPanel preferencesPanel = new JPanel(new GridBagLayout());
340 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
342 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
343 preferencesPanel.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));
345 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
346 preferencesPanel.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));
348 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
349 preferencesPanel.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));
351 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
352 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
353 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
355 tempDirectoryTextField = new JTextField();
356 tempDirectoryTextField.setEditable(false);
357 if (tempDirectory != null) {
358 tempDirectoryTextField.setText(tempDirectory);
359 customTempDirectory.setSelected(true);
361 defaultTempDirectory.setSelected(true);
363 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
364 preferencesPanel.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));
366 JButton chooseButton = new JButton(chooseTempDirectoryAction);
367 preferencesPanel.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));
369 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
370 preferencesPanel.add(configurationDirectoryLabel, new GridBagConstraints(0, 3, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
372 nextToJarFile = new JRadioButton(nextToJarFileAction);
373 preferencesPanel.add(nextToJarFile, new GridBagConstraints(0, 4, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 18, 0, 0), 0, 0));
375 homeDirectory = new JRadioButton(homeDirectoryAction);
376 preferencesPanel.add(homeDirectory, new GridBagConstraints(0, 5, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 18, 0, 0), 0, 0));
378 customDirectory = new JRadioButton(customDirectoryAction);
379 preferencesPanel.add(customDirectory, new GridBagConstraints(0, 6, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 18, 0, 0), 0, 0));
381 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
382 configurationDirectoryButtonGroup.add(nextToJarFile);
383 configurationDirectoryButtonGroup.add(homeDirectory);
384 configurationDirectoryButtonGroup.add(customDirectory);
386 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
387 preferencesPanel.add(insertOptionsLabel, new GridBagConstraints(0, 7, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
389 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
390 preferencesPanel.add(useEarlyEncodeCheckBox, new GridBagConstraints(0, 8, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
392 I18nContainer.getInstance().registerRunnable(new Runnable() {
398 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
399 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
400 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
404 return preferencesPanel;
408 * Activates the default temp directory radio button.
410 private void selectDefaultTempDirectory() {
411 tempDirectoryTextField.setEnabled(false);
412 chooseTempDirectoryAction.setEnabled(false);
413 tempDirectory = null;
417 * Activates the custom temp directory radio button.
419 private void selectCustomTempDirectory() {
420 tempDirectoryTextField.setEnabled(true);
421 chooseTempDirectoryAction.setEnabled(true);
422 if (tempDirectoryTextField.getText().length() == 0) {
423 chooseTempDirectory();
424 if (tempDirectoryTextField.getText().length() == 0) {
425 defaultTempDirectory.setSelected(true);
431 * Lets the user choose a new temp directory.
433 private void chooseTempDirectory() {
434 JFileChooser fileChooser = new JFileChooser(tempDirectory);
435 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
436 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
437 if (returnValue == JFileChooser.CANCEL_OPTION) {
440 tempDirectory = fileChooser.getSelectedFile().getPath();
441 tempDirectoryTextField.setText(tempDirectory);