01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.handlers;
11:
12: import java.util.Hashtable;
13:
14: import org.eclipse.core.runtime.IConfigurationElement;
15: import org.eclipse.core.runtime.IExecutableExtension;
16: import org.eclipse.jface.commands.ToggleState;
17:
18: /**
19: * <p>
20: * A toggle state that can be read from the registry. This stores a piece of
21: * boolean state information.
22: * </p>
23: * <p>
24: * When parsing from the registry, this state understands two parameters:
25: * <code>default</code>, which is the default value for this item; and
26: * <code>persisted</code>, which is whether the state should be persisted
27: * between sessions. The <code>default</code> parameter defaults to
28: * <code>false</code>, and the <code>persisted</code> parameter defaults to
29: * <code>true</code>. If only one parameter is passed (i.e., using the class
30: * name followed by a colon), then it is assumed to be the <code>default</code>
31: * parameter.
32: * </p>
33: * <p>
34: * Clients may instantiate this class, but must not extend.
35: * </p>
36: *
37: * @since 3.2
38: */
39: public final class RegistryToggleState extends ToggleState implements
40: IExecutableExtension {
41:
42: /**
43: * Reads the <code>default</code> parameter from the given string. This
44: * converts the string to a boolean, using <code>true</code> as the
45: * default.
46: *
47: * @param defaultString
48: * The string to parse; may be <code>null</code>.
49: */
50: private final void readDefault(final String defaultString) {
51: if ("true".equalsIgnoreCase(defaultString)) { //$NON-NLS-1$
52: setValue(Boolean.TRUE);
53: }
54: }
55:
56: /**
57: * Reads the <code>persisted</code> parameter from the given string. This
58: * converts the string to a boolean, using <code>true</code> as the
59: * default.
60: *
61: * @param persistedString
62: * The string to parse; may be <code>null</code>.
63: */
64: private final void readPersisted(final String persistedString) {
65: if ("false".equalsIgnoreCase(persistedString)) { //$NON-NLS-1$
66: setShouldPersist(false);
67: }
68:
69: setShouldPersist(true);
70: }
71:
72: public final void setInitializationData(
73: final IConfigurationElement configurationElement,
74: final String propertyName, final Object data) {
75: if (data instanceof String) {
76: // This is the default value.
77: readDefault((String) data);
78: setShouldPersist(true);
79:
80: } else if (data instanceof Hashtable) {
81: final Hashtable parameters = (Hashtable) data;
82: final Object defaultObject = parameters.get("default"); //$NON-NLS-1$
83: if (defaultObject instanceof String) {
84: readDefault((String) defaultObject);
85: }
86:
87: final Object persistedObject = parameters.get("persisted"); //$NON-NLS-1$
88: if (persistedObject instanceof String) {
89: readPersisted((String) persistedObject);
90: }
91:
92: } else {
93: setShouldPersist(true);
94:
95: }
96: }
97: }
|