1 11 12 package org.eclipse.ui.internal.commands; 13 14 import org.eclipse.core.commands.IStateListener; 15 import org.eclipse.core.commands.State; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.jface.commands.PersistentState; 21 import org.eclipse.jface.preference.IPreferenceStore; 22 import org.eclipse.ui.internal.WorkbenchPlugin; 23 24 43 public final class CommandStateProxy extends PersistentState { 44 45 50 private IConfigurationElement configurationElement; 51 52 55 private String preferenceKey; 56 57 60 private IPreferenceStore preferenceStore; 61 62 67 private State state = null; 68 69 73 private final String stateAttributeName; 74 75 93 public CommandStateProxy(final IConfigurationElement configurationElement, 94 final String stateAttributeName, 95 final IPreferenceStore preferenceStore, final String preferenceKey) { 96 97 if (configurationElement == null) { 98 throw new NullPointerException ( 99 "The configuration element backing a state proxy cannot be null"); } 101 102 if (stateAttributeName == null) { 103 throw new NullPointerException ( 104 "The attribute containing the state class must be known"); } 106 107 this.configurationElement = configurationElement; 108 this.stateAttributeName = stateAttributeName; 109 this.preferenceKey = preferenceKey; 110 this.preferenceStore = preferenceStore; 111 } 112 113 public final void addListener(final IStateListener listener) { 114 if (state == null) { 115 addListenerObject(listener); 116 } else { 117 state.addListener(listener); 118 } 119 } 120 121 public final void dispose() { 122 if (state != null) { 123 state.dispose(); 124 if (state instanceof PersistentState) { 125 final PersistentState persistableState = (PersistentState) state; 126 if (persistableState.shouldPersist() && preferenceStore != null 127 && preferenceKey != null) { 128 persistableState.save(preferenceStore, preferenceKey); 129 } 130 } 131 } 132 } 133 134 public final Object getValue() { 135 if (loadState()) { 136 return state.getValue(); 137 } 138 139 return null; 140 } 141 142 public final void load(final IPreferenceStore store, 143 final String preferenceKey) { 144 if (loadState() && state instanceof PersistentState) { 145 final PersistentState persistableState = (PersistentState) state; 146 if (persistableState.shouldPersist() && preferenceStore != null 147 && preferenceKey != null) { 148 persistableState.load(preferenceStore, preferenceKey); 149 } 150 } 151 } 152 153 161 private final boolean loadState() { 162 return loadState(false); 163 } 164 165 175 private final boolean loadState(final boolean readPersistence) { 176 if (state == null) { 177 try { 178 state = (State) configurationElement 179 .createExecutableExtension(stateAttributeName); 180 state.setId(getId()); 181 configurationElement = null; 182 183 if (readPersistence && state instanceof PersistentState) { 185 final PersistentState persistentState = (PersistentState) state; 186 persistentState.setShouldPersist(true); 187 } 188 load(preferenceStore, preferenceKey); 189 190 final Object [] listenerArray = getListeners(); 192 for (int i = 0; i < listenerArray.length; i++) { 193 state.addListener((IStateListener) listenerArray[i]); 194 } 195 clearListeners(); 196 197 return true; 198 199 } catch (final ClassCastException e) { 200 final String message = "The proxied state was the wrong class"; final IStatus status = new Status(IStatus.ERROR, 202 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 203 WorkbenchPlugin.log(message, status); 204 return false; 205 206 } catch (final CoreException e) { 207 final String message = "The proxied state for '" + configurationElement.getAttribute(stateAttributeName) + "' could not be loaded"; IStatus status = new Status(IStatus.ERROR, 210 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 211 WorkbenchPlugin.log(message, status); 212 return false; 213 } 214 } 215 216 return true; 217 } 218 219 public final void removeListener(final IStateListener listener) { 220 if (state == null) { 221 removeListenerObject(listener); 222 } else { 223 state.removeListener(listener); 224 } 225 } 226 227 public final void save(final IPreferenceStore store, 228 final String preferenceKey) { 229 if (loadState() && state instanceof PersistentState) { 230 ((PersistentState) state).save(store, preferenceKey); 231 } 232 } 233 234 public final void setId(final String id) { 235 super.setId(id); 236 if (state != null) { 237 state.setId(id); 238 } 239 } 240 241 public final void setShouldPersist(final boolean persisted) { 242 if (loadState(persisted) && state instanceof PersistentState) { 243 ((PersistentState) state).setShouldPersist(persisted); 244 } 245 } 246 247 public final void setValue(final Object value) { 248 if (loadState()) { 249 state.setValue(value); 250 } 251 } 252 253 public final boolean shouldPersist() { 254 if (loadState() && state instanceof PersistentState) { 255 return ((PersistentState) state).shouldPersist(); 256 } 257 258 return false; 259 } 260 261 public final String toString() { 262 if (state == null) { 263 return configurationElement.getAttribute(stateAttributeName); 264 } 265 266 return state.toString(); 267 } 268 } 269 | Popular Tags |