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() {
136 setHeading(I18n.getMessage("jsite.preferences.heading"));
137 setDescription(I18n.getMessage("jsite.preferences.description"));
147 * Returns the temp directory.
149 * @return The temp directory, or {@code null} to use the default temp
152 public String getTempDirectory() {
153 return tempDirectory;
157 * Sets the temp directory.
159 * @param tempDirectory
160 * The temp directory, or {@code null} to use the default temp
163 public void setTempDirectory(String tempDirectory) {
164 this.tempDirectory = tempDirectory;
165 tempDirectoryTextField.setText((tempDirectory != null) ? tempDirectory : "");
166 if (tempDirectory != null) {
167 customTempDirectory.setSelected(true);
168 chooseTempDirectoryAction.setEnabled(true);
170 defaultTempDirectory.setSelected(true);
175 * Returns the configuration location.
177 * @return The configuration location
179 public ConfigurationLocation getConfigurationLocation() {
180 return configurationLocation;
184 * Sets the configuration location.
186 * @param configurationLocation
187 * The configuration location
189 public void setConfigurationLocation(ConfigurationLocation configurationLocation) {
190 this.configurationLocation = configurationLocation;
191 switch (configurationLocation) {
192 case NEXT_TO_JAR_FILE:
193 nextToJarFile.setSelected(true);
196 homeDirectory.setSelected(true);
199 customDirectory.setSelected(true);
205 * Sets whether it is possible to select the “next to JAR file” option for
206 * the configuration location.
208 * @param nextToJarFile
209 * {@code true} if the configuration file can be saved next to
210 * the JAR file, {@code false} otherwise
212 public void setHasNextToJarConfiguration(boolean nextToJarFile) {
213 this.nextToJarFile.setEnabled(nextToJarFile);
217 * Sets whether it is possible to select the “custom location” option for
218 * the configuration location.
220 * @param customDirectory
221 * {@code true} if the configuration file can be saved to a
222 * custom location, {@code false} otherwise
224 public void setHasCustomConfiguration(boolean customDirectory) {
225 this.customDirectory.setEnabled(customDirectory);
229 * Returns whether to use the “early encode“ flag for the insert.
231 * @return {@code true} to set the “early encode” flag for the insert,
232 * {@code false} otherwise
234 public boolean useEarlyEncode() {
235 return useEarlyEncode;
239 * Sets whether to use the “early encode“ flag for the insert.
241 * @param useEarlyEncode
242 * {@code true} to set the “early encode” flag for the insert,
243 * {@code false} otherwise
245 public void setUseEarlyEncode(boolean useEarlyEncode) {
246 useEarlyEncodeCheckBox.setSelected(useEarlyEncode);
250 * Returns the configured insert priority.
252 * @return The insert priority
254 public PriorityClass getPriority() {
259 * Sets the insert priority.
262 * The insert priority
264 public void setPriority(PriorityClass priority) {
265 insertPriorityComboBox.setSelectedItem(priority);
269 * Returns the selected manifest putter.
271 * @return The selected manifest putter
273 public ManifestPutter getManifestPutter() {
274 return (ManifestPutter) manifestPutterComboBox.getSelectedItem();
278 * Sets the manifest putter.
280 * @param manifestPutter
281 * The manifest putter
283 public void setManifestPutter(ManifestPutter manifestPutter) {
284 manifestPutterComboBox.setSelectedItem(manifestPutter);
291 public void pageAdded(TWizard wizard) {
292 super.pageAdded(wizard);
293 this.wizard.setPreviousName(I18n.getMessage("jsite.menu.nodes.manage-nodes"));
294 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
295 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
296 this.wizard.setNextEnabled(false);
304 * Initializes this page.
306 private void pageInit() {
308 setLayout(new BorderLayout(12, 12));
309 add(createPreferencesPanel(), BorderLayout.CENTER);
313 * Creates all actions.
315 private void createActions() {
316 selectDefaultTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.default")) {
322 @SuppressWarnings("synthetic-access")
323 public void actionPerformed(ActionEvent actionEvent) {
324 selectDefaultTempDirectory();
327 selectCustomTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.custom")) {
333 @SuppressWarnings("synthetic-access")
334 public void actionPerformed(ActionEvent actionEvent) {
335 selectCustomTempDirectory();
338 chooseTempDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.temp-directory.choose")) {
341 @SuppressWarnings("synthetic-access")
342 public void actionPerformed(ActionEvent e) {
343 chooseTempDirectory();
346 nextToJarFileAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.jar")) {
349 @SuppressWarnings("synthetic-access")
350 public void actionPerformed(ActionEvent actionevent) {
351 configurationLocation = ConfigurationLocation.NEXT_TO_JAR_FILE;
354 homeDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.home")) {
357 @SuppressWarnings("synthetic-access")
358 public void actionPerformed(ActionEvent actionevent) {
359 configurationLocation = ConfigurationLocation.HOME_DIRECTORY;
362 customDirectoryAction = new AbstractAction(I18n.getMessage("jsite.preferences.config-directory.custom")) {
365 @SuppressWarnings("synthetic-access")
366 public void actionPerformed(ActionEvent actionEvent) {
367 configurationLocation = ConfigurationLocation.CUSTOM;
370 useEarlyEncodeAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.use-early-encode")) {
373 @SuppressWarnings("synthetic-access")
374 public void actionPerformed(ActionEvent actionEvent) {
375 useEarlyEncode = useEarlyEncodeCheckBox.isSelected();
378 priorityAction = new AbstractAction(I18n.getMessage("jsite.preferences.insert-options.priority")) {
381 @SuppressWarnings("synthetic-access")
382 public void actionPerformed(ActionEvent actionEvent) {
383 priority = (PriorityClass) insertPriorityComboBox.getSelectedItem();
387 I18nContainer.getInstance().registerRunnable(new Runnable() {
390 @SuppressWarnings("synthetic-access")
392 selectDefaultTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.default"));
393 selectCustomTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.custom"));
394 chooseTempDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.temp-directory.choose"));
395 nextToJarFileAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.jar"));
396 homeDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.home"));
397 customDirectoryAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.config-directory.custom"));
398 useEarlyEncodeAction.putValue(Action.NAME, I18n.getMessage("jsite.preferences.insert-options.use-early-encode"));
404 * Creates the panel containing all preferences.
406 * @return The preferences panel
408 private JPanel createPreferencesPanel() {
409 JPanel preferencesPanel = new JPanel(new GridBagLayout());
410 preferencesPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
412 final JLabel tempDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
413 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));
415 defaultTempDirectory = new JRadioButton(selectDefaultTempDirectoryAction);
416 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));
418 customTempDirectory = new JRadioButton(selectCustomTempDirectoryAction);
419 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));
421 ButtonGroup tempDirectoryButtonGroup = new ButtonGroup();
422 defaultTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
423 customTempDirectory.getModel().setGroup(tempDirectoryButtonGroup);
425 tempDirectoryTextField = new JTextField();
426 tempDirectoryTextField.setEditable(false);
427 if (tempDirectory != null) {
428 tempDirectoryTextField.setText(tempDirectory);
429 customTempDirectory.setSelected(true);
431 defaultTempDirectory.setSelected(true);
433 chooseTempDirectoryAction.setEnabled(tempDirectory != null);
434 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));
436 JButton chooseButton = new JButton(chooseTempDirectoryAction);
437 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));
439 final JLabel configurationDirectoryLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
440 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));
442 nextToJarFile = new JRadioButton(nextToJarFileAction);
443 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));
445 homeDirectory = new JRadioButton(homeDirectoryAction);
446 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));
448 customDirectory = new JRadioButton(customDirectoryAction);
449 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));
451 ButtonGroup configurationDirectoryButtonGroup = new ButtonGroup();
452 configurationDirectoryButtonGroup.add(nextToJarFile);
453 configurationDirectoryButtonGroup.add(homeDirectory);
454 configurationDirectoryButtonGroup.add(customDirectory);
456 final JLabel insertOptionsLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
457 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));
459 useEarlyEncodeCheckBox = new JCheckBox(useEarlyEncodeAction);
460 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));
462 final JLabel insertPriorityLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.priority"));
463 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));
465 insertPriorityComboBox = new JComboBox(new PriorityClass[] { PriorityClass.MINIMUM, PriorityClass.PREFETCH, PriorityClass.BULK, PriorityClass.UPDATABLE, PriorityClass.SEMI_INTERACTIVE, PriorityClass.INTERACTIVE, PriorityClass.MAXIMUM });
466 insertPriorityComboBox.setAction(priorityAction);
467 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));
469 final JLabel manifestPutterLabel = new JLabel(I18n.getMessage("jsite.preferences.insert-options.manifest-putter"));
470 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));
472 manifestPutterComboBox = new JComboBox(ManifestPutter.values());
473 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));
475 I18nContainer.getInstance().registerRunnable(new Runnable() {
482 tempDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.temp-directory") + "</b></html>");
483 configurationDirectoryLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.config-directory") + "</b></html>");
484 insertOptionsLabel.setText("<html><b>" + I18n.getMessage("jsite.preferences.insert-options") + "</b></html>");
485 insertPriorityLabel.setText(I18n.getMessage("jsite.preferences.insert-options.priority"));
486 manifestPutterLabel.setText(I18n.getMessage("jsite.preferences.insert-options.manifest-putter"));
490 return preferencesPanel;
494 * Activates the default temp directory radio button.
496 private void selectDefaultTempDirectory() {
497 tempDirectoryTextField.setEnabled(false);
498 chooseTempDirectoryAction.setEnabled(false);
499 tempDirectory = null;
503 * Activates the custom temp directory radio button.
505 private void selectCustomTempDirectory() {
506 tempDirectoryTextField.setEnabled(true);
507 chooseTempDirectoryAction.setEnabled(true);
508 if (tempDirectoryTextField.getText().length() == 0) {
509 chooseTempDirectory();
510 if (tempDirectoryTextField.getText().length() == 0) {
511 defaultTempDirectory.setSelected(true);
517 * Lets the user choose a new temp directory.
519 private void chooseTempDirectory() {
520 JFileChooser fileChooser = new JFileChooser(tempDirectory);
521 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
522 int returnValue = fileChooser.showDialog(wizard, I18n.getMessage("jsite.preferences.temp-directory.choose.approve"));
523 if (returnValue == JFileChooser.CANCEL_OPTION) {
526 tempDirectory = fileChooser.getSelectedFile().getPath();
527 tempDirectoryTextField.setText(tempDirectory);