00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.settings.options;
00023
00024 import com.realtime.crossfire.jxclient.settings.Settings;
00025 import java.util.HashMap;
00026 import java.util.Map;
00027 import org.jetbrains.annotations.NotNull;
00028
00033 public class OptionManager {
00034
00038 @NotNull
00039 private final Map<String, Entry> options = new HashMap<String, Entry>();
00040
00044 @NotNull
00045 private final Settings settings;
00046
00051 public OptionManager(@NotNull final Settings settings) {
00052 this.settings = settings;
00053 }
00054
00062 public void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option option) throws OptionException {
00063 if (options.containsKey(optionName)) {
00064 throw new OptionException("duplicate option name: "+optionName);
00065 }
00066
00067 options.put(optionName, new Entry(option, documentation));
00068 }
00069
00074 public void removeOption(@NotNull final String optionName) {
00075 options.remove(optionName);
00076 }
00077
00084 @NotNull
00085 public CheckBoxOption getCheckBoxOption(@NotNull final String optionName) throws OptionException {
00086 final Entry entry = options.get(optionName);
00087 if (entry == null || !(entry.getOption() instanceof CheckBoxOption)) {
00088 throw new OptionException("Unknown option '"+optionName+"'");
00089 }
00090
00091 return (CheckBoxOption)entry.getOption();
00092 }
00093
00097 public void loadOptions() {
00098 for (final Map.Entry<String, Entry> e : options.entrySet()) {
00099 final String optionName = e.getKey();
00100 final Object option = e.getValue().getOption();
00101 if (option instanceof CheckBoxOption) {
00102 final CheckBoxOption checkBoxOption = (CheckBoxOption)option;
00103 final boolean checked = settings.getBoolean(optionName, checkBoxOption.isDefaultChecked());
00104 if (checkBoxOption.isChecked() == checked) {
00105
00106 checkBoxOption.fireStateChangedEvent();
00107 } else {
00108 checkBoxOption.setChecked(checked);
00109 }
00110 } else {
00111 throw new AssertionError();
00112 }
00113 }
00114 }
00115
00119 public void saveOptions() {
00120 for (final Map.Entry<String, Entry> e : options.entrySet()) {
00121 final String optionName = e.getKey();
00122 final Entry entry = e.getValue();
00123 final Option option = entry.getOption();
00124 if (!option.inhibitSave()) {
00125 if (option instanceof CheckBoxOption) {
00126 final CheckBoxOption checkBoxOption = (CheckBoxOption)option;
00127 settings.putBoolean(optionName, checkBoxOption.isChecked(), entry.getDocumentation());
00128 } else {
00129 throw new AssertionError();
00130 }
00131 }
00132 }
00133 }
00134
00139 private static class Entry {
00140
00144 @NotNull
00145 private final Option option;
00146
00150 @NotNull
00151 private final String documentation;
00152
00158 private Entry(@NotNull final Option option, @NotNull final String documentation) {
00159 this.option = option;
00160 this.documentation = documentation;
00161 }
00162
00167 @NotNull
00168 public Option getOption() {
00169 return option;
00170 }
00171
00176 @NotNull
00177 public String getDocumentation() {
00178 return documentation;
00179 }
00180
00181 }
00182
00183 }