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