Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.settings.options;
23 
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class OptionManager {
35 
39  @NotNull
40  private final Map<String, Entry> options = new HashMap<>();
41 
45  @NotNull
46  private final Settings settings;
47 
52  public OptionManager(@NotNull final Settings settings) {
53  this.settings = settings;
54  }
55 
63  public void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option option) throws OptionException {
64  if (options.containsKey(optionName)) {
65  throw new OptionException("duplicate option name: "+optionName);
66  }
67 
68  options.put(optionName, new Entry(option, documentation));
69  }
70 
75  public void removeOption(@NotNull final String optionName) {
76  options.remove(optionName);
77  }
78 
85  @NotNull
86  public CheckBoxOption getCheckBoxOption(@NotNull final String optionName) throws OptionException {
87  final Entry entry = options.get(optionName);
88  if (entry == null || !(entry.getOption() instanceof CheckBoxOption)) {
89  throw new OptionException("Unknown option '"+optionName+"'");
90  }
91 
92  return (CheckBoxOption)entry.getOption();
93  }
94 
98  public void loadOptions() {
99  for (final Map.Entry<String, Entry> e : options.entrySet()) {
100  final String optionName = e.getKey();
101  final Object option = e.getValue().getOption();
102  if (option instanceof CheckBoxOption) {
103  final CheckBoxOption checkBoxOption = (CheckBoxOption)option;
104  final boolean checked = settings.getBoolean(new SettingsEntry<>(optionName, checkBoxOption.isDefaultChecked(), null));
105  if (checkBoxOption.isChecked() == checked) {
106  // make sure the appropriate option command is executed
107  checkBoxOption.fireStateChangedEvent();
108  } else {
109  checkBoxOption.setChecked(checked);
110  }
111  } else {
112  throw new AssertionError();
113  }
114  }
115  }
116 
120  public void saveOptions() {
121  for (final Map.Entry<String, Entry> e : options.entrySet()) {
122  final String optionName = e.getKey();
123  final Entry entry = e.getValue();
124  final Option option = entry.getOption();
125  if (!option.inhibitSave()) {
126  if (option instanceof CheckBoxOption) {
127  final CheckBoxOption checkBoxOption = (CheckBoxOption)option;
128  settings.putBoolean(new SettingsEntry<>(optionName, false, entry.getDocumentation()), checkBoxOption.isChecked());
129  } else {
130  throw new AssertionError();
131  }
132  }
133  }
134  }
135 
140  private static class Entry {
141 
145  @NotNull
146  private final Option option;
147 
151  @NotNull
152  private final String documentation;
153 
159  private Entry(@NotNull final Option option, @NotNull final String documentation) {
160  this.option = option;
161  this.documentation = documentation;
162  }
163 
168  @NotNull
169  public Option getOption() {
170  return option;
171  }
172 
177  @NotNull
178  public String getDocumentation() {
179  return documentation;
180  }
181 
182  }
183 
184 }
final Settings settings
The settings instance for loading/saving option values.
void putBoolean(@NotNull final SettingsEntry< Boolean > key, final boolean value)
Stores a key/value pair.
Definition: Settings.java:142
final Map< String, Entry > options
Maps option name to option instance.
void setChecked(final boolean checked)
Sets the current state.
OptionManager(@NotNull final Settings settings)
Creates a new instance.
Pair of Option and corresponding documentation string.
CheckBoxOption getCheckBoxOption(@NotNull final String optionName)
Returns a check box option.
void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option option)
Adds a new option.
final String documentation
The corresponding documentation string.
boolean getBoolean(@NotNull final SettingsEntry< Boolean > key)
Returns the boolean associated with the specified key at a node or.
Definition: Settings.java:90
void removeOption(@NotNull final String optionName)
Removes an option by name.
boolean inhibitSave()
Returns whether the option should not be saved.
Definition: Option.java:71
Maintains a set of key/value pairs.
Definition: Settings.java:43
void loadOptions()
Loads all options&#39; states from the backing settings instance.
String getDocumentation()
Returns the corresponding documentation string.
void saveOptions()
Saves all options&#39; states to the backing settings instance.
Entry(@NotNull final Option option, @NotNull final String documentation)
Creates a new instance.
abstract boolean isDefaultChecked()
Returns the default value of isChecked().