Gridarta Editor
UpdatePreferences.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.dialog.prefs;
21 
22 import java.awt.Component;
23 import java.awt.GridBagLayout;
24 import java.util.prefs.Preferences;
25 import javax.swing.AbstractButton;
26 import javax.swing.Box;
27 import javax.swing.JCheckBox;
28 import javax.swing.JComboBox;
29 import javax.swing.JComponent;
30 import javax.swing.JPanel;
31 import javax.swing.border.Border;
32 import javax.swing.border.CompoundBorder;
33 import javax.swing.border.TitledBorder;
34 import net.sf.gridarta.MainControl;
38 import net.sf.japi.swing.action.ActionBuilder;
39 import net.sf.japi.swing.action.ActionBuilderFactory;
40 import net.sf.japi.swing.action.ActionMethod;
41 import net.sf.japi.swing.prefs.AbstractPrefs;
42 import org.jetbrains.annotations.NotNull;
43 
49 public class UpdatePreferences extends AbstractPrefs {
50 
54  private static final long serialVersionUID = 1L;
55 
59  @NotNull
60  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
61 
65  @NotNull
66  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
67 
71  @NotNull
72  private AbstractButton autoUpdate;
73 
77  @NotNull
78  private JComboBox<String> interval;
79 
83  public UpdatePreferences() {
84  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsUpdate.title"));
85  setListLabelIcon(ACTION_BUILDER.getIcon("prefsUpdate.icon"));
86 
87  add(createUpdatePanel());
88  add(Box.createVerticalGlue());
89  }
90 
96  @NotNull
97  private static Border createTitledBorder(@NotNull final String titleKey) {
98  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
99  }
100 
101  @Override
102  public void apply() {
103  PREFERENCES.putBoolean(UpdaterManager.AUTO_CHECK_KEY, autoUpdate.isSelected());
104  PREFERENCES.putInt(UpdaterManager.INTERVAL_KEY, interval.getSelectedIndex());
105  }
106 
107  @Override
108  public void revert() {
109  autoUpdate.setSelected(PREFERENCES.getBoolean(UpdaterManager.AUTO_CHECK_KEY, UpdaterManager.AUTO_CHECK_DEFAULT));
110  interval.setSelectedIndex(PREFERENCES.getInt(UpdaterManager.INTERVAL_KEY, UpdaterManager.INTERVAL_DEFAULT));
111  }
112 
113  @Override
114  public void defaults() {
115  autoUpdate.setSelected(UpdaterManager.AUTO_CHECK_DEFAULT);
116  interval.setSelectedIndex(UpdaterManager.INTERVAL_DEFAULT);
117  }
118 
119  @Override
120  public boolean isChanged() {
121  return !(autoUpdate.isSelected() == PREFERENCES.getBoolean(UpdaterManager.AUTO_CHECK_KEY, UpdaterManager.AUTO_CHECK_DEFAULT) && interval.getSelectedIndex() == PREFERENCES.getInt(UpdaterManager.INTERVAL_KEY, UpdaterManager.INTERVAL_DEFAULT));
122  }
123 
128  @NotNull
129  private Component createUpdatePanel() {
130  final JComponent panel = new JPanel(new GridBagLayout());
131  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
132  panel.setBorder(createTitledBorder("optionsUpdate"));
133  autoUpdate = new JCheckBox(ACTION_BUILDER.createToggle(false, "autoUpdate", this));
134  autoUpdate.setSelected(PREFERENCES.getBoolean(UpdaterManager.AUTO_CHECK_KEY, UpdaterManager.AUTO_CHECK_DEFAULT));
135  preferencesHelper.addComponent(autoUpdate);
136  preferencesHelper.addComponent(createComboBox());
137  return panel;
138  }
139 
144  @ActionMethod
145  public void setAutoUpdate(final boolean autoUpdate) {
146  interval.setEnabled(autoUpdate);
147  }
148 
153  @ActionMethod
154  public boolean isAutoUpdate() {
155  return interval.isEnabled();
156  }
157 
162  @NotNull
163  private Component createComboBox() {
164  final String[] items = { ActionBuilderUtils.getString(ACTION_BUILDER, "prefsUpdateAuto0.text"), ActionBuilderUtils.getString(ACTION_BUILDER, "prefsUpdateAuto1.text"), ActionBuilderUtils.getString(ACTION_BUILDER, "prefsUpdateAuto2.text"), ActionBuilderUtils.getString(ACTION_BUILDER, "prefsUpdateAuto3.text"), };
165  interval = new JComboBox<>(items);
166  interval.setEditable(false);
167  interval.setSelectedIndex(PREFERENCES.getInt(UpdaterManager.INTERVAL_KEY, UpdaterManager.INTERVAL_DEFAULT));
168  interval.setEnabled(PREFERENCES.getBoolean(UpdaterManager.AUTO_CHECK_KEY, UpdaterManager.AUTO_CHECK_DEFAULT));
169  return interval;
170  }
171 
172 }
AbstractButton autoUpdate
Checkbox whether to automatically check for updates on startup.
Graphical User Interface of Gridarta.
JComboBox< String > interval
ComboBox with selected interval.
Component createUpdatePanel()
Creates the sub-panel with the update settings.
static Border createTitledBorder(@NotNull final String titleKey)
Creates a titled border.
void addComponent(@NotNull final Component component)
Adds a component to the container.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
boolean isAutoUpdate()
Returns the auto-update state.
Base package of all Gridarta classes.
static final boolean AUTO_CHECK_DEFAULT
Preferences default value for AUTO_CHECK_KEY.
This class handles updating the map editor.
static final long serialVersionUID
The serial version UID.
Interface used as preferences location.
static final String AUTO_CHECK_KEY
Preferences key whether to automatically check for updates.
Preferences Module for update preferences.
Utility class for ActionBuilder related functions.
Component createComboBox()
Creates the component with update choices.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static final String INTERVAL_KEY
Preferences key for selected update interval.
void setAutoUpdate(final boolean autoUpdate)
Sets the auto-update state.
static final ActionBuilder ACTION_BUILDER
Action Builder.
static final Preferences PREFERENCES
Preferences.
static final int INTERVAL_DEFAULT
Preferences default value for INTERVAL_KEY.
Defines common UI constants used in different dialogs.