Crossfire JXClient, Trunk
OptionManager.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient 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.
8  *
9  * JXClient 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.settings.options;
24 
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class OptionManager {
37 
41  @NotNull
42  private final Map<String, Entry> options = new HashMap<>();
43 
47  @NotNull
48  private final Settings settings;
49 
54  public OptionManager(@NotNull final Settings settings) {
55  this.settings = settings;
56  }
57 
65  public void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option option) throws OptionException {
66  if (options.containsKey(optionName)) {
67  throw new OptionException("duplicate option name: "+optionName);
68  }
69 
70  options.put(optionName, new Entry(option, documentation));
71  }
72 
77  public void removeOption(@NotNull final String optionName) {
78  options.remove(optionName);
79  }
80 
85  @NotNull
86  public Map<String, Entry> getOptions() {
87  return Collections.unmodifiableMap(options);
88  }
89 
96  @NotNull
97  public CheckBoxOption getCheckBoxOption(@NotNull final String optionName) throws OptionException {
98  final Entry entry = options.get(optionName);
99  if (entry == null || !(entry.getOption() instanceof CheckBoxOption)) {
100  throw new OptionException("Unknown option '"+optionName+"'");
101  }
102 
103  return (CheckBoxOption)entry.getOption();
104  }
105 
109  public void loadOptions() {
110  for (Map.Entry<String, Entry> e : options.entrySet()) {
111  final String optionName = e.getKey();
112  final Object option = e.getValue().getOption();
113  if (option instanceof final CheckBoxOption checkBoxOption) {
114  final boolean checked = settings.getBoolean(new SettingsEntry<>(optionName, checkBoxOption.isDefaultChecked(), null));
115  if (checkBoxOption.isChecked() == checked) {
116  // make sure the appropriate option command is executed
117  checkBoxOption.fireStateChangedEvent();
118  } else {
119  checkBoxOption.setChecked(checked);
120  }
121  } else {
122  throw new AssertionError();
123  }
124  }
125  }
126 
130  public void saveOptions() {
131  for (Map.Entry<String, Entry> e : options.entrySet()) {
132  final String optionName = e.getKey();
133  final Entry entry = e.getValue();
134  final Option option = entry.getOption();
135  if (!option.inhibitSave()) {
136  if (option instanceof final CheckBoxOption checkBoxOption) {
137  settings.putBoolean(new SettingsEntry<>(optionName, false, entry.getDocumentation()), checkBoxOption.isChecked());
138  } else {
139  throw new AssertionError();
140  }
141  }
142  }
143  }
144 
149  public static class Entry {
150 
154  @NotNull
155  private final Option option;
156 
160  @NotNull
161  private final String documentation;
162 
168  private Entry(@NotNull final Option option, @NotNull final String documentation) {
169  this.option = option;
170  this.documentation = documentation;
171  }
172 
177  @NotNull
178  public Option getOption() {
179  return option;
180  }
181 
186  @NotNull
187  public String getDocumentation() {
188  return documentation;
189  }
190 
191  }
192 
193 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.settings.options.OptionManager.saveOptions
void saveOptions()
Saves all options' states to the backing settings instance.
Definition: OptionManager.java:130
com.realtime.crossfire.jxclient.settings.options.Option.inhibitSave
boolean inhibitSave()
Returns whether the option should not be saved.
Definition: Option.java:72
com.realtime.crossfire.jxclient.settings.options.OptionManager.removeOption
void removeOption(@NotNull final String optionName)
Removes an option by name.
Definition: OptionManager.java:77
com.realtime.crossfire.jxclient.settings.options.OptionManager.options
final Map< String, Entry > options
Maps option name to option instance.
Definition: OptionManager.java:42
com.realtime.crossfire.jxclient.settings.Settings.getBoolean
boolean getBoolean(@NotNull final SettingsEntry< Boolean > key)
Returns the boolean associated with the specified key at a node or.
Definition: Settings.java:95
com.realtime.crossfire.jxclient.settings.options.OptionManager.addOption
void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option option)
Adds a new option.
Definition: OptionManager.java:65
com.realtime.crossfire.jxclient.settings.options.OptionManager.loadOptions
void loadOptions()
Loads all options' states from the backing settings instance.
Definition: OptionManager.java:109
com.realtime.crossfire.jxclient.settings.options.OptionManager.settings
final Settings settings
The settings instance for loading/saving option values.
Definition: OptionManager.java:48
com.realtime.crossfire.jxclient.settings.options.OptionManager.Entry.documentation
final String documentation
The corresponding documentation string.
Definition: OptionManager.java:161
com.realtime.crossfire.jxclient.settings.Settings.putBoolean
void putBoolean(@NotNull final SettingsEntry< Boolean > key, final boolean value)
Stores a key/value pair.
Definition: Settings.java:151
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.settings.options.OptionManager.getCheckBoxOption
CheckBoxOption getCheckBoxOption(@NotNull final String optionName)
Returns a checkbox option.
Definition: OptionManager.java:97
com.realtime.crossfire.jxclient.settings.options.OptionManager.OptionManager
OptionManager(@NotNull final Settings settings)
Creates a new instance.
Definition: OptionManager.java:54
com.realtime.crossfire.jxclient.settings.SettingsEntry
An entry in the settings file.
Definition: SettingsEntry.java:34
com.realtime.crossfire.jxclient.settings.options.OptionManager.Entry.option
final Option option
The Option instance.
Definition: OptionManager.java:155
com.realtime.crossfire.jxclient.settings.options.OptionManager
Maintains a set of named options.
Definition: OptionManager.java:36
com.realtime.crossfire.jxclient.settings.Settings
Maintains a set of key/value pairs.
Definition: Settings.java:45
com.realtime.crossfire.jxclient.settings.options.OptionManager.Entry
Pair of Option and corresponding documentation string.
Definition: OptionManager.java:149
com.realtime.crossfire.jxclient.settings.options.Option
The base class for all options.
Definition: Option.java:33
com.realtime.crossfire.jxclient.settings.options.OptionManager.Entry.Entry
Entry(@NotNull final Option option, @NotNull final String documentation)
Creates a new instance.
Definition: OptionManager.java:168
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.settings.options.OptionManager.getOptions
Map< String, Entry > getOptions()
Returns all defined option names.
Definition: OptionManager.java:86
com.realtime.crossfire.jxclient.settings.options.OptionManager.Entry.getDocumentation
String getDocumentation()
Returns the corresponding documentation string.
Definition: OptionManager.java:187
com.realtime.crossfire.jxclient.settings.options.CheckBoxOption
The base class for all check box options.
Definition: CheckBoxOption.java:32
com.realtime.crossfire.jxclient.settings.options.OptionManager.Entry.getOption
Option getOption()
Returns the Option instance.
Definition: OptionManager.java:178
com.realtime.crossfire.jxclient.settings.options.OptionException
Indicates about an incorrect option.
Definition: OptionException.java:31