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.ClientPutDir.ManifestPutter;
44 import de.todesbaum.util.freenet.fcp2.PriorityClass;
45 import de.todesbaum.util.swing.TWizard;
46 import de.todesbaum.util.swing.TWizardPage;
49 * Page that shows some preferences that are valid for the complete application.
51 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
53 public class PreferencesPage extends TWizardPage {
55 /** Select default temp directory action. */
56 private Action selectDefaultTempDirectoryAction;
58 /** Select custom temp directory action. */
59 private Action selectCustomTempDirectoryAction;
61 /** Action that chooses a new temp directory. */
62 private Action chooseTempDirectoryAction;
64 /** Action when selecting “next to JAR file.” */
65 private Action nextToJarFileAction;
67 /** Action when selecting “home directory.” */
68 private Action homeDirectoryAction;
70 /** Action when selecting “custom directory.” */
71 private Action customDirectoryAction;
73 /** Action when selecting “use early encode.” */
74 private Action useEarlyEncodeAction;
76 /** Action when a priority was selected. */
77 private Action priorityAction;
79 /** The text field containing the directory. */
80 private JTextField tempDirectoryTextField;
82 /** The temp directory. */
83 private String tempDirectory;
85 /** The configuration location. */
86 private ConfigurationLocation configurationLocation;
88 /** Whether to use “early encode.” */
89 private boolean useEarlyEncode;
91 /** The prioriy for inserts. */
92 private PriorityClass priority;
94 /** The “default” button. */
95 private JRadioButton defaultTempDirectory;
97 /** The “custom” button. */
98 private JRadioButton customTempDirectory;
100 /** The “next to JAR file” checkbox. */
101 private JRadioButton nextToJarFile;
103 /** The “home directory” checkbox. */
104 private JRadioButton homeDirectory;
106 /** The “custom directory” checkbox. */
107 private JRadioButton customDirectory;
109 /** The “use early encode” checkbox. */
110 private JCheckBox useEarlyEncodeCheckBox;
112 /** The insert priority select box. */
113 private JComboBox insertPriorityComboBox;
115 /** The manifest putter select box. */
116 private JComboBox manifestPutterComboBox;
119 * Creates a new “preferences” page.
122 * The wizard this page belongs to
124 public PreferencesPage(TWizard wizard) {
127 setHeading(I18n.getMessage("jsite.preferences.heading"));
128 setDescription(I18n.getMessage("jsite.preferences.description"));
129 I18nContainer.getInstance().registerRunnable(new Runnable() {
135 setHeading(I18n.getMessage("jsite.preferences.heading"));
136 setDescription(I18n.getMessage("jsite.preferences.description"));
146 * Returns the temp directory.
148 * @return The temp directory, or {@code null} to use the default temp
151 public String getTempDirectory() {
152 return tempDirectory;
156 * Sets the temp directory.
158 * @param tempDirectory
159 * The temp directory, or {@code null} to use the default temp
162 public void setTempDirectory(String tempDirectory) {
163 this.tempDirectory = tempDirectory;
164 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
165 if (tempDirectory != null) {
166 customTempDirectory.setSelected(true);
167 chooseTempDirectoryAction.setEnabled(true);
169 defaultTempDirectory.setSelected(true);
174 * Returns the configuration location.
176 * @return The configuration location
178 public ConfigurationLocation getConfigurationLocation() {
179 return configurationLocation;
183 * Sets the configuration location.
185 * @param configurationLocation
186 * The configuration location
188 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
189 this.configurationLocation = configurationLocation;
190 switch (configurationLocation) {
191 case NEXT_TO_JAR_FILE:
192 nextToJarFile.setSelected(true);
195 homeDirectory.setSelected(true);
198 customDirectory.setSelected(true);
204 * Sets whether it is possible to select the “next to JAR file” option for
205 * the configuration location.
207 * @param nextToJarFile
208 * {@code true} if the configuration file can be saved next to
209 * the JAR file, {@code false} otherwise
211 public void setHasNextToJarConfiguration(boolean nextToJarFile) {
212 this.nextToJarFile.setEnabled(nextToJarFile);
216 * Sets whether it is possible to select the “custom location” option for
217 * the configuration location.
219 * @param customDirectory
220 * {@code true} if the configuration file can be saved to a
221 * custom location, {@code false} otherwise
223 public void setHasCustomConfiguration(boolean customDirectory) {
224 this.customDirectory.setEnabled(customDirectory);
228 * Returns whether to use the “early encode“ flag for the insert.
230 * @return {@code true} to set the “early encode” flag for the insert,
231 * {@code false} otherwise
233 public boolean useEarlyEncode() {
234 return useEarlyEncode;
238 * Sets whether to use the “early encode“ flag for the insert.
240 * @param useEarlyEncode
241 * {@code true} to set the “early encode” flag for the insert,
242 * {@code false} otherwise
244 public void setUseEarlyEncode(boolean useEarlyEncode) {
245 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
249 * Returns the configured insert priority.
251 * @return The insert priority
253 public PriorityClass getPriority() {
258 * Sets the insert priority.
261 * The insert priority
263 public void setPriority(PriorityClass priority) {
264 insertPriorityComboBox.setSelectedItem(priority);
268 * Returns the selected manifest putter.
270 * @return The selected manifest putter
272 public ManifestPutter getManifestPutter() {
273 return (ManifestPutter) manifestPutterComboBox.getSelectedItem();
277 * Sets the manifest putter.
279 * @param manifestPutter
280 * The manifest putter
282 public void setManifestPutter(ManifestPutter manifestPutter) {
283 manifestPutterComboBox.setSelectedItem(manifestPutter);
290 public void pageAdded(TWizard wizard) {
291 super.pageAdded(wizard);
292 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
293 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
294 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
295 this.wizard.setNextEnabled(false);
303 * Initializes this page.
305 private void pageInit() {
307 setLayout(new BorderLayout(12, 12));
308 add(createPreferencesPanel(), BorderLayout.CENTER);
312 * Creates all actions.
314 private void createActions() {
315 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
320 @SuppressWarnings("synthetic-access")
321 public void actionPerformed(ActionEvent actionEvent) {
322 selectDefaultTempDirectory();
325 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
330 @SuppressWarnings("synthetic-access")
331 public void actionPerformed(ActionEvent actionEvent) {
332 selectCustomTempDirectory();
335 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
337 @SuppressWarnings("synthetic-access")
338 public void actionPerformed(ActionEvent e) {
339 chooseTempDirectory();
342 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
344 @SuppressWarnings("synthetic-access")
345 public void actionPerformed(ActionEvent actionevent) {
346 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
349 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
351 @SuppressWarnings("synthetic-access")
352 public void actionPerformed(ActionEvent actionevent) {
353 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
356 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
358 @SuppressWarnings("synthetic-access")
359 public void actionPerformed(ActionEvent actionEvent) {
360 configurationLocation = ConfigurationLocation.CUSTOM;
363 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
365 @SuppressWarnings("synthetic-access")
366 public void actionPerformed(ActionEvent actionEvent) {
367 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
370 priorityAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.priority")) {
372 @SuppressWarnings("synthetic-access")
373 public void actionPerformed(ActionEvent actionEvent) {
374 priority = (PriorityClass) insertPriorityComboBox.getSelectedItem();
378 I18nContainer.getInstance().registerRunnable(new Runnable() {
380 @SuppressWarnings("synthetic-access")
382 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
383 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
384 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
385 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
386 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
387 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
388 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
394 * Creates the panel containing all preferences.
396 * @return The preferences panel
398 private JPanel createPreferencesPanel() {
399 JPanel preferencesPanel = new JPanel(new GridBagLayout());
400 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
402 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
403 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));
405 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
406 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));
408 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
409 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));
411 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
412 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
413 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
415 tempDirectoryTextField = new JTextField();
416 tempDirectoryTextField.setEditable(false);
417 if (tempDirectory != null) {
418 tempDirectoryTextField.setText(tempDirectory);
419 customTempDirectory.setSelected(true);
421 defaultTempDirectory.setSelected(true);
423 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
424 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));
426 JButton chooseButton = new JButton(chooseTempDirectoryAction);
427 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));
429 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
430 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));
432 nextToJarFile = new JRadioButton(nextToJarFileAction);
433 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));
435 homeDirectory = new JRadioButton(homeDirectoryAction);
436 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));
438 customDirectory = new JRadioButton(customDirectoryAction);
439 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));
441 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
442 configurationDirectoryButtonGroup.add(nextToJarFile);
443 configurationDirectoryButtonGroup.add(homeDirectory);
444 configurationDirectoryButtonGroup.add(customDirectory);
446 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
447 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));
449 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
450 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));
452 final JLabel insertPriorityLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.priority"));
453 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));
455 insertPriorityComboBox = new JComboBox(new PriorityClass[] { PriorityClass.MINIMUM, PriorityClass.PREFETCH, PriorityClass.BULK, PriorityClass.UPDATABLE, PriorityClass.SEMI_INTERACTIVE, PriorityClass.INTERACTIVE, PriorityClass.MAXIMUM });
456 insertPriorityComboBox.setAction(priorityAction);
457 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));
459 final JLabel manifestPutterLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.manifest-putter"));
460 preferencesPanel.add(manifestPutterLabel, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
462 manifestPutterComboBox = new JComboBox(ManifestPutter.values());
463 preferencesPanel.add(manifestPutterComboBox, new GridBagConstraints(1, 10, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 18, 0, 0), 0, 0));
465 I18nContainer.getInstance().registerRunnable(new Runnable() {
471 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
472 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
473 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
474 insertPriorityLabel.setText(I18n.getMessage("jsite.preferences.insert-options.priority"));
475 manifestPutterLabel.setText(I18n.getMessage("jsite.preferences.insert-options.manifest-putter"));
479 return preferencesPanel;
483 * Activates the default temp directory radio button.
485 private void selectDefaultTempDirectory() {
486 tempDirectoryTextField.setEnabled(false);
487 chooseTempDirectoryAction.setEnabled(false);
488 tempDirectory = null;
492 * Activates the custom temp directory radio button.
494 private void selectCustomTempDirectory() {
495 tempDirectoryTextField.setEnabled(true);
496 chooseTempDirectoryAction.setEnabled(true);
497 if (tempDirectoryTextField.getText().length() == 0) {
498 chooseTempDirectory();
499 if (tempDirectoryTextField.getText().length() == 0) {
500 defaultTempDirectory.setSelected(true);
506 * Lets the user choose a new temp directory.
508 private void chooseTempDirectory() {
509 JFileChooser fileChooser = new JFileChooser(tempDirectory);
510 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
511 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
512 if (returnValue == JFileChooser.CANCEL_OPTION) {
515 tempDirectory = fileChooser.getSelectedFile().getPath();
516 tempDirectoryTextField.setText(tempDirectory);