Gridarta Editor
AppPreferences.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.io.File;
25 import javax.swing.Box;
26 import javax.swing.JComponent;
27 import javax.swing.JFileChooser;
28 import javax.swing.JPanel;
29 import javax.swing.border.Border;
30 import javax.swing.border.CompoundBorder;
31 import javax.swing.border.TitledBorder;
35 import net.sf.japi.swing.action.ActionBuilder;
36 import net.sf.japi.swing.action.ActionBuilderFactory;
37 import net.sf.japi.swing.prefs.AbstractPrefs;
38 import org.jetbrains.annotations.NotNull;
39 
45 public class AppPreferences extends AbstractPrefs {
46 
50  private static final long serialVersionUID = 1L;
51 
55  @NotNull
56  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
57 
61  @NotNull
63 
67  @NotNull
69 
73  @NotNull
75 
79  @NotNull
81 
86  public AppPreferences(@NotNull final AppPreferencesModel appPreferencesModel) {
87  this.appPreferencesModel = appPreferencesModel;
88  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsApp.title"));
89  setListLabelIcon(ACTION_BUILDER.getIcon("prefsApp.icon"));
90 
91  add(createAppPanel());
92  add(Box.createVerticalGlue());
93  }
94 
100  @NotNull
101  private static Border createTitledBorder(@NotNull final String titleKey) {
102  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
103  }
104 
105  @Override
106  public void apply() {
107  AppPreferencesModel.setServer(serverField.getFile().getPath());
108  AppPreferencesModel.setClient(clientField.getFile().getPath());
109  AppPreferencesModel.setEditor(editorField.getFile().getPath());
110  }
111 
112  @Override
113  public void revert() {
114  serverField.setFile(new File(appPreferencesModel.getServer()));
115  clientField.setFile(new File(appPreferencesModel.getClient()));
116  editorField.setFile(new File(appPreferencesModel.getEditor()));
117  }
118 
119  @Override
120  public void defaults() {
121  serverField.setFile(new File(appPreferencesModel.getServerDefault()));
122  clientField.setFile(new File(appPreferencesModel.getClientDefault()));
123  editorField.setFile(new File(appPreferencesModel.getEditorDefault()));
124  }
125 
126  @Override
127  public boolean isChanged() {
128  return !(serverField.getFile().equals(new File(appPreferencesModel.getServer())) && clientField.getFile().equals(new File(appPreferencesModel.getClient())) && editorField.getFile().equals(new File(appPreferencesModel.getEditor())));
129  }
130 
135  @NotNull
136  private Component createAppPanel() {
137  final JComponent panel = new JPanel(new GridBagLayout());
138  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
139  panel.setBorder(createTitledBorder("optionsApps"));
140 
141  serverField = new JFileField(this, "optionsAppServer", null, new File(appPreferencesModel.getServer()), JFileChooser.FILES_ONLY);
142  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsAppServer"));
143  preferencesHelper.addComponent(serverField);
144  clientField = new JFileField(this, "optionsAppClient", null, new File(appPreferencesModel.getClient()), JFileChooser.FILES_ONLY);
145  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsAppClient"));
146  preferencesHelper.addComponent(clientField);
147  editorField = new JFileField(this, "optionsAppEditor", null, new File(appPreferencesModel.getEditor()), JFileChooser.FILES_ONLY);
148  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsAppEditor"));
149  preferencesHelper.addComponent(editorField);
150 
151  return panel;
152  }
153 
154 }
Graphical User Interface of Gridarta.
JFileField editorField
TextField for external editor executable.
A component for selecting files.
Definition: JFileField.java:45
JFileField clientField
TextField for client executable.
static void setEditor(@NotNull final CharSequence editor)
Sets the editor setting.
static Border createTitledBorder(@NotNull final String titleKey)
Creates a titled border.
static final ActionBuilder ACTION_BUILDER
Action Builder.
Preferences Module for application preferences.
void addComponent(@NotNull final Component component)
Adds a component to the container.
Maintains the application preferences state.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
static void setClient(@NotNull final CharSequence client)
Sets the client setting.
String getClientDefault()
Returns the client setting's default value.
JFileField serverField
TextField for server executable.
void setFile(@NotNull final File file)
Sets the currently selected file.
String getEditorDefault()
Returns the editor setting's default value.
static void setServer(@NotNull final CharSequence server)
Sets the server setting.
AppPreferences(@NotNull final AppPreferencesModel appPreferencesModel)
Creates a new instance.
Utility class for ActionBuilder related functions.
final AppPreferencesModel appPreferencesModel
The model.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
Component createAppPanel()
Creates the sub-panel with the external applications.
static final long serialVersionUID
The serial version UID.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
File getFile()
Returns the currently selected file.
String getServerDefault()
Returns the server setting's default value.
Defines common UI constants used in different dialogs.