debugger tooltip - non-automatic mode
[fedora-idea.git] / platform-api / src / com / intellij / openapi / util / registry / RegistryValue.java
blob0a7fe46ca886c657d06e96a60db6a543656545b4
1 package com.intellij.openapi.util.registry;
3 import com.intellij.openapi.util.Disposer;
4 import com.intellij.openapi.Disposable;
6 import java.util.concurrent.CopyOnWriteArraySet;
7 import java.util.MissingResourceException;
9 public class RegistryValue {
11 private Registry myRegistry;
12 private String myKey;
14 private CopyOnWriteArraySet<RegistryValueListener> myListeners = new CopyOnWriteArraySet<RegistryValueListener>();
16 private boolean myChangedSinceStart;
18 private String myStringCachedValue;
19 private Integer myIntCachedValue;
20 private Boolean myBooleanCachedValue;
22 RegistryValue(Registry registry, String key) {
23 myRegistry = registry;
24 myKey = key;
27 public String getKey() {
28 return myKey;
32 public String asString() {
33 final String value = get(myKey, null, true);
34 assert value != null : myKey;
35 return value;
38 public boolean asBoolean() {
39 if (myBooleanCachedValue == null) {
40 myBooleanCachedValue = Boolean.valueOf(get(myKey, "false", true));
43 return myBooleanCachedValue.booleanValue();
46 public int asInteger() {
47 if (myIntCachedValue == null) {
48 myIntCachedValue = Integer.valueOf(get(myKey, "0", true));
51 return myIntCachedValue.intValue();
54 public String getDescription() {
55 return get(myKey + ".description", "", false);
58 public boolean isRestartRequired() {
59 return Boolean.valueOf(get(myKey + ".restartRequired", "false", false));
62 public boolean isChangedFromDefault() {
63 return !getBundleValue(myKey, true).equals(asString());
66 private String get(String key, String defaultValue, boolean isValue) {
67 if (isValue) {
68 if (myStringCachedValue == null) {
69 myStringCachedValue = _get(key, defaultValue, isValue);
72 return myStringCachedValue;
73 } else {
74 return _get(key, defaultValue, isValue);
78 private String _get(String key, String defaultValue, boolean mustExistInBundle) {
79 final String userValue = myRegistry.getUserProperties().get(key);
80 if (userValue == null) {
81 final String bundleValue = getBundleValue(key, mustExistInBundle);
82 if (bundleValue != null) {
83 return bundleValue;
84 } else {
85 return defaultValue;
87 } else {
88 return userValue;
92 private String getBundleValue(String key, boolean mustExist) {
93 try {
94 return myRegistry.getBundle().getString(key);
96 catch (MissingResourceException e) {
97 if (mustExist) {
98 throw e;
102 return null;
105 public void setValue(boolean value) {
106 setValue(Boolean.valueOf(value).toString());
109 public void setValue(String value) {
110 resetCache();
112 for (RegistryValueListener each : myListeners) {
113 each.beforeValueChanged(this);
116 myRegistry.getUserProperties().put(myKey, value);
118 for (RegistryValueListener each : myListeners) {
119 each.afterValueChanged(this);
122 if (!isChangedFromDefault()) {
123 myRegistry.getUserProperties().remove(myKey);
126 myChangedSinceStart = true;
129 public boolean isChangedSinceAppStart() {
130 return myChangedSinceStart;
133 public void resetToDefault() {
134 setValue(getBundleValue(myKey, true));
137 public void addListener(final RegistryValueListener listener, Disposable parent) {
138 myListeners.add(listener);
139 Disposer.register(parent, new Disposable() {
140 public void dispose() {
141 myListeners.remove(listener);
146 @Override
147 public String toString() {
148 return myKey + "=" + asString();
151 void resetCache() {
152 myStringCachedValue = null;
153 myIntCachedValue = null;
154 myBooleanCachedValue = null;