2 * jSite - PreferencesPage.java - Copyright © 2009–2014 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() {
132 setHeading(I18n.getMessage("jsite.preferences.heading"));
133 setDescription(I18n.getMessage("jsite.preferences.description"));
143 * Returns the temp directory.
145 * @return The temp directory, or {@code null} to use the default temp
148 public String getTempDirectory() {
149 return tempDirectory;
153 * Sets the temp directory.
155 * @param tempDirectory
156 * The temp directory, or {@code null} to use the default temp
159 public void setTempDirectory(String tempDirectory) {
160 this.tempDirectory = tempDirectory;
161 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
162 if (tempDirectory != null) {
163 customTempDirectory.setSelected(true);
164 chooseTempDirectoryAction.setEnabled(true);
166 defaultTempDirectory.setSelected(true);
171 * Returns the configuration location.
173 * @return The configuration location
175 public ConfigurationLocation getConfigurationLocation() {
176 return configurationLocation;
180 * Sets the configuration location.
182 * @param configurationLocation
183 * The configuration location
185 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
186 this.configurationLocation = configurationLocation;
187 switch (configurationLocation) {
188 case NEXT_TO_JAR_FILE:
189 nextToJarFile.setSelected(true);
192 homeDirectory.setSelected(true);
195 customDirectory.setSelected(true);
201 * Sets whether it is possible to select the “next to JAR file” option for
202 * the configuration location.
204 * @param nextToJarFile
205 * {@code true} if the configuration file can be saved next to
206 * the JAR file, {@code false} otherwise
208 public void setHasNextToJarConfiguration(boolean nextToJarFile) {
209 this.nextToJarFile.setEnabled(nextToJarFile);
213 * Sets whether it is possible to select the “custom location” option for
214 * the configuration location.
216 * @param customDirectory
217 * {@code true} if the configuration file can be saved to a
218 * custom location, {@code false} otherwise
220 public void setHasCustomConfiguration(boolean customDirectory) {
221 this.customDirectory.setEnabled(customDirectory);
225 * Returns whether to use the “early encode“ flag for the insert.
227 * @return {@code true} to set the “early encode” flag for the insert,
228 * {@code false} otherwise
230 public boolean useEarlyEncode() {
231 return useEarlyEncode;
235 * Sets whether to use the “early encode“ flag for the insert.
237 * @param useEarlyEncode
238 * {@code true} to set the “early encode” flag for the insert,
239 * {@code false} otherwise
241 public void setUseEarlyEncode(boolean useEarlyEncode) {
242 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
246 * Returns the configured insert priority.
248 * @return The insert priority
250 public PriorityClass getPriority() {
255 * Sets the insert priority.
258 * The insert priority
260 public void setPriority(PriorityClass priority) {
261 insertPriorityComboBox.setSelectedItem(priority);
268 public void pageAdded(TWizard wizard) {
269 super.pageAdded(wizard);
270 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
271 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
272 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
273 this.wizard.setNextEnabled(false);
281 * Initializes this page.
283 private void pageInit() {
285 setLayout(new BorderLayout(12, 12));
286 add(createPreferencesPanel(), BorderLayout.CENTER);
290 * Creates all actions.
292 private void createActions() {
293 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
299 @SuppressWarnings("synthetic-access")
300 public void actionPerformed(ActionEvent actionEvent) {
301 selectDefaultTempDirectory();
304 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
310 @SuppressWarnings("synthetic-access")
311 public void actionPerformed(ActionEvent actionEvent) {
312 selectCustomTempDirectory();
315 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
318 @SuppressWarnings("synthetic-access")
319 public void actionPerformed(ActionEvent e) {
320 chooseTempDirectory();
323 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
326 @SuppressWarnings("synthetic-access")
327 public void actionPerformed(ActionEvent actionevent) {
328 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
331 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
334 @SuppressWarnings("synthetic-access")
335 public void actionPerformed(ActionEvent actionevent) {
336 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
339 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
342 @SuppressWarnings("synthetic-access")
343 public void actionPerformed(ActionEvent actionEvent) {
344 configurationLocation = ConfigurationLocation.CUSTOM;
347 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
350 @SuppressWarnings("synthetic-access")
351 public void actionPerformed(ActionEvent actionEvent) {
352 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
355 priorityAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.priority")) {
358 @SuppressWarnings("synthetic-access")
359 public void actionPerformed(ActionEvent actionEvent) {
360 priority = (PriorityClass) insertPriorityComboBox.getSelectedItem();
364 I18nContainer.getInstance().registerRunnable(new Runnable() {
367 @SuppressWarnings("synthetic-access")
369 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
370 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
371 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
372 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
373 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
374 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
375 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
381 * Creates the panel containing all preferences.
383 * @return The preferences panel
385 private JPanel createPreferencesPanel() {
386 JPanel preferencesPanel = new JPanel(new GridBagLayout());
387 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
389 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
390 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));
392 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
393 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));
395 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
396 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));
398 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
399 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
400 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
402 tempDirectoryTextField = new JTextField();
403 tempDirectoryTextField.setEditable(false);
404 if (tempDirectory != null) {
405 tempDirectoryTextField.setText(tempDirectory);
406 customTempDirectory.setSelected(true);
408 defaultTempDirectory.setSelected(true);
410 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
411 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));
413 JButton chooseButton = new JButton(chooseTempDirectoryAction);
414 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));
416 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
417 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));
419 nextToJarFile = new JRadioButton(nextToJarFileAction);
420 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));
422 homeDirectory = new JRadioButton(homeDirectoryAction);
423 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));
425 customDirectory = new JRadioButton(customDirectoryAction);
426 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));
428 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
429 configurationDirectoryButtonGroup.add(nextToJarFile);
430 configurationDirectoryButtonGroup.add(homeDirectory);
431 configurationDirectoryButtonGroup.add(customDirectory);
433 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
434 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));
436 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
437 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));
439 final JLabel insertPriorityLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.priority"));
440 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));
442 insertPriorityComboBox = new JComboBox(new PriorityClass[] { PriorityClass.MINIMUM, PriorityClass.PREFETCH, PriorityClass.BULK, PriorityClass.UPDATABLE, PriorityClass.SEMI_INTERACTIVE, PriorityClass.INTERACTIVE, PriorityClass.MAXIMUM });
443 insertPriorityComboBox.setAction(priorityAction);
444 preferencesPanel.add(insertPriorityComboBox, new GridBagConstraints(1, 9, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 18, 0, 0), 0, 0));
446 I18nContainer.getInstance().registerRunnable(new Runnable() {
453 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
454 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
455 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
456 insertPriorityLabel.setText(I18n.getMessage("jsite.preferences.insert-options.priority"));
460 return preferencesPanel;
464 * Activates the default temp directory radio button.
466 private void selectDefaultTempDirectory() {
467 tempDirectoryTextField.setEnabled(false);
468 chooseTempDirectoryAction.setEnabled(false);
469 tempDirectory = null;
473 * Activates the custom temp directory radio button.
475 private void selectCustomTempDirectory() {
476 tempDirectoryTextField.setEnabled(true);
477 chooseTempDirectoryAction.setEnabled(true);
478 if (tempDirectoryTextField.getText().length() == 0) {
479 chooseTempDirectory();
480 if (tempDirectoryTextField.getText().length() == 0) {
481 defaultTempDirectory.setSelected(true);
487 * Lets the user choose a new temp directory.
489 private void chooseTempDirectory() {
490 JFileChooser fileChooser = new JFileChooser(tempDirectory);
491 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
492 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
493 if (returnValue == JFileChooser.CANCEL_OPTION) {
496 tempDirectory = fileChooser.getSelectedFile().getPath();
497 tempDirectoryTextField.setText(tempDirectory);