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.JComboBox;
34 import javax.swing.JFileChooser;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.JRadioButton;
38 import javax.swing.JTextField;
40 import de.todesbaum.jsite.i18n.I18n;
41 import de.todesbaum.jsite.i18n.I18nContainer;
42 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
43 import de.todesbaum.util.freenet.fcp2.PriorityClass;
44 import de.todesbaum.util.swing.TWizard;
45 import de.todesbaum.util.swing.TWizardPage;
48 * Page that shows some preferences that are valid for the complete application.
50 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
52 public class PreferencesPage extends TWizardPage {
54 /** Select default temp directory action. */
55 private Action selectDefaultTempDirectoryAction;
57 /** Select custom temp directory action. */
58 private Action selectCustomTempDirectoryAction;
60 /** Action that chooses a new temp directory. */
61 private Action chooseTempDirectoryAction;
63 /** Action when selecting “next to JAR file.” */
64 private Action nextToJarFileAction;
66 /** Action when selecting “home directory.” */
67 private Action homeDirectoryAction;
69 /** Action when selecting “custom directory.” */
70 private Action customDirectoryAction;
72 /** Action when selecting “use early encode.” */
73 private Action useEarlyEncodeAction;
75 /** Action when a priority was selected. */
76 private Action priorityAction;
78 /** The text field containing the directory. */
79 private JTextField tempDirectoryTextField;
81 /** The temp directory. */
82 private String tempDirectory;
84 /** The configuration location. */
85 private ConfigurationLocation configurationLocation;
87 /** Whether to use “early encode.” */
88 private boolean useEarlyEncode;
90 /** The prioriy for inserts. */
91 private PriorityClass priority;
93 /** The “default” button. */
94 private JRadioButton defaultTempDirectory;
96 /** The “custom” button. */
97 private JRadioButton customTempDirectory;
99 /** The “next to JAR file” checkbox. */
100 private JRadioButton nextToJarFile;
102 /** The “home directory” checkbox. */
103 private JRadioButton homeDirectory;
105 /** The “custom directory” checkbox. */
106 private JRadioButton customDirectory;
108 /** The “use early encode” checkbox. */
109 private JCheckBox useEarlyEncodeCheckBox;
111 /** The insert priority select box. */
112 private JComboBox insertPriorityComboBox;
115 * Creates a new “preferences” page.
118 * The wizard this page belongs to
120 public PreferencesPage(TWizard wizard) {
123 setHeading(I18n.getMessage("jsite.preferences.heading"));
124 setDescription(I18n.getMessage("jsite.preferences.description"));
125 I18nContainer.getInstance().registerRunnable(new Runnable() {
131 setHeading(I18n.getMessage("jsite.preferences.heading"));
132 setDescription(I18n.getMessage("jsite.preferences.description"));
142 * Returns the temp directory.
144 * @return The temp directory, or {@code null} to use the default temp
147 public String getTempDirectory() {
148 return tempDirectory;
152 * Sets the temp directory.
154 * @param tempDirectory
155 * The temp directory, or {@code null} to use the default temp
158 public void setTempDirectory(String tempDirectory) {
159 this.tempDirectory = tempDirectory;
160 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
161 if (tempDirectory != null) {
162 customTempDirectory.setSelected(true);
163 chooseTempDirectoryAction.setEnabled(true);
165 defaultTempDirectory.setSelected(true);
170 * Returns the configuration location.
172 * @return The configuration location
174 public ConfigurationLocation getConfigurationLocation() {
175 return configurationLocation;
179 * Sets the configuration location.
181 * @param configurationLocation
182 * The configuration location
184 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
185 this.configurationLocation = configurationLocation;
186 switch (configurationLocation) {
187 case NEXT_TO_JAR_FILE:
188 nextToJarFile.setSelected(true);
191 homeDirectory.setSelected(true);
194 customDirectory.setSelected(true);
200 * Sets whether it is possible to select the “next to JAR file” option for
201 * the configuration location.
203 * @param nextToJarFile
204 * {@code true} if the configuration file can be saved next to
205 * the JAR file, {@code false} otherwise
207 public void setHasNextToJarConfiguration(boolean nextToJarFile) {
208 this.nextToJarFile.setEnabled(nextToJarFile);
212 * Sets whether it is possible to select the “custom location” option for
213 * the configuration location.
215 * @param customDirectory
216 * {@code true} if the configuration file can be saved to a
217 * custom location, {@code false} otherwise
219 public void setHasCustomConfiguration(boolean customDirectory) {
220 this.customDirectory.setEnabled(customDirectory);
224 * Returns whether to use the “early encode“ flag for the insert.
226 * @return {@code true} to set the “early encode” flag for the insert,
227 * {@code false} otherwise
229 public boolean useEarlyEncode() {
230 return useEarlyEncode;
234 * Sets whether to use the “early encode“ flag for the insert.
236 * @param useEarlyEncode
237 * {@code true} to set the “early encode” flag for the insert,
238 * {@code false} otherwise
240 public void setUseEarlyEncode(boolean useEarlyEncode) {
241 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
245 * Returns the configured insert priority.
247 * @return The insert priority
249 public PriorityClass getPriority() {
254 * Sets the insert priority.
257 * The insert priority
259 public void setPriority(PriorityClass priority) {
260 insertPriorityComboBox.setSelectedItem(priority);
267 public void pageAdded(TWizard wizard) {
268 super.pageAdded(wizard);
269 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
270 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
271 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
272 this.wizard.setNextEnabled(false);
280 * Initializes this page.
282 private void pageInit() {
284 setLayout(new BorderLayout(12, 12));
285 add(createPreferencesPanel(), BorderLayout.CENTER);
289 * Creates all actions.
291 private void createActions() {
292 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
297 @SuppressWarnings("synthetic-access")
298 public void actionPerformed(ActionEvent actionEvent) {
299 selectDefaultTempDirectory();
302 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
307 @SuppressWarnings("synthetic-access")
308 public void actionPerformed(ActionEvent actionEvent) {
309 selectCustomTempDirectory();
312 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
314 @SuppressWarnings("synthetic-access")
315 public void actionPerformed(ActionEvent e) {
316 chooseTempDirectory();
319 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
321 @SuppressWarnings("synthetic-access")
322 public void actionPerformed(ActionEvent actionevent) {
323 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
326 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
328 @SuppressWarnings("synthetic-access")
329 public void actionPerformed(ActionEvent actionevent) {
330 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
333 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
335 @SuppressWarnings("synthetic-access")
336 public void actionPerformed(ActionEvent actionEvent) {
337 configurationLocation = ConfigurationLocation.CUSTOM;
340 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
342 @SuppressWarnings("synthetic-access")
343 public void actionPerformed(ActionEvent actionEvent) {
344 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
347 priorityAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.priority")) {
349 @SuppressWarnings("synthetic-access")
350 public void actionPerformed(ActionEvent actionEvent) {
351 priority = (PriorityClass) insertPriorityComboBox.getSelectedItem();
355 I18nContainer.getInstance().registerRunnable(new Runnable() {
357 @SuppressWarnings("synthetic-access")
359 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
360 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
361 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
362 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
363 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
364 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
365 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
371 * Creates the panel containing all preferences.
373 * @return The preferences panel
375 private JPanel createPreferencesPanel() {
376 JPanel preferencesPanel = new JPanel(new GridBagLayout());
377 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
379 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
380 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));
382 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
383 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));
385 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
386 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));
388 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
389 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
390 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
392 tempDirectoryTextField = new JTextField();
393 tempDirectoryTextField.setEditable(false);
394 if (tempDirectory != null) {
395 tempDirectoryTextField.setText(tempDirectory);
396 customTempDirectory.setSelected(true);
398 defaultTempDirectory.setSelected(true);
400 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
401 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));
403 JButton chooseButton = new JButton(chooseTempDirectoryAction);
404 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));
406 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
407 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));
409 nextToJarFile = new JRadioButton(nextToJarFileAction);
410 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));
412 homeDirectory = new JRadioButton(homeDirectoryAction);
413 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));
415 customDirectory = new JRadioButton(customDirectoryAction);
416 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));
418 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
419 configurationDirectoryButtonGroup.add(nextToJarFile);
420 configurationDirectoryButtonGroup.add(homeDirectory);
421 configurationDirectoryButtonGroup.add(customDirectory);
423 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
424 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));
426 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
427 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));
429 final JLabel insertPriorityLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.priority"));
430 preferencesPanel.add(insertPriorityLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
432 insertPriorityComboBox = new JComboBox(new PriorityClass[] { PriorityClass.MINIMUM, PriorityClass.PREFETCH, PriorityClass.BULK, PriorityClass.UPDATABLE, PriorityClass.SEMI_INTERACTIVE, PriorityClass.INTERACTIVE, PriorityClass.MAXIMUM });
433 insertPriorityComboBox.setAction(priorityAction);
434 preferencesPanel.add(insertPriorityComboBox, new GridBagConstraints(1, 9, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.VERTICAL, new Insets(0, 18, 0, 0), 0, 0));
436 I18nContainer.getInstance().registerRunnable(new Runnable() {
442 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
443 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
444 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
445 insertPriorityLabel.setText(I18n.getMessage("jsite.preferences.insert-options.priority"));
449 return preferencesPanel;
453 * Activates the default temp directory radio button.
455 private void selectDefaultTempDirectory() {
456 tempDirectoryTextField.setEnabled(false);
457 chooseTempDirectoryAction.setEnabled(false);
458 tempDirectory = null;
462 * Activates the custom temp directory radio button.
464 private void selectCustomTempDirectory() {
465 tempDirectoryTextField.setEnabled(true);
466 chooseTempDirectoryAction.setEnabled(true);
467 if (tempDirectoryTextField.getText().length() == 0) {
468 chooseTempDirectory();
469 if (tempDirectoryTextField.getText().length() == 0) {
470 defaultTempDirectory.setSelected(true);
476 * Lets the user choose a new temp directory.
478 private void chooseTempDirectory() {
479 JFileChooser fileChooser = new JFileChooser(tempDirectory);
480 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
481 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
482 if (returnValue == JFileChooser.CANCEL_OPTION) {
485 tempDirectory = fileChooser.getSelectedFile().getPath();
486 tempDirectoryTextField.setText(tempDirectory);