KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > handlers > RegistryToggleState


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.handlers;
13
14 import java.util.Hashtable JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExecutableExtension;
18 import org.eclipse.jface.commands.ToggleState;
19
20 /**
21  * <p>
22  * A toggle state that can be read from the registry. This stores a piece of
23  * boolean state information.
24  * </p>
25  * <p>
26  * When parsing from the registry, this state understands two parameters:
27  * <code>default</code>, which is the default value for this item; and
28  * <code>persisted</code>, which is whether the state should be persisted
29  * between sessions. The <code>default</code> parameter defaults to
30  * <code>false</code>, and the <code>persisted</code> parameter defaults to
31  * <code>true</code>. If only one parameter is passed (i.e., using the class
32  * name followed by a colon), then it is assumed to be the <code>default</code>
33  * parameter.
34  * </p>
35  * <p>
36  * Clients may instantiate this class, but must not extend.
37  * </p>
38  *
39  * @since 3.2
40  */

41 public final class RegistryToggleState extends ToggleState implements
42         IExecutableExtension {
43
44     /**
45      * Reads the <code>default</code> parameter from the given string. This
46      * converts the string to a boolean, using <code>true</code> as the
47      * default.
48      *
49      * @param defaultString
50      * The string to parse; may be <code>null</code>.
51      */

52     private final void readDefault(final String JavaDoc defaultString) {
53         if ("true".equalsIgnoreCase(defaultString)) { //$NON-NLS-1$
54
setValue(Boolean.TRUE);
55         }
56     }
57
58     /**
59      * Reads the <code>persisted</code> parameter from the given string. This
60      * converts the string to a boolean, using <code>true</code> as the
61      * default.
62      *
63      * @param persistedString
64      * The string to parse; may be <code>null</code>.
65      */

66     private final void readPersisted(final String JavaDoc persistedString) {
67         if ("false".equalsIgnoreCase(persistedString)) { //$NON-NLS-1$
68
setShouldPersist(false);
69         }
70
71         setShouldPersist(true);
72     }
73
74     public final void setInitializationData(
75             final IConfigurationElement configurationElement,
76             final String JavaDoc propertyName, final Object JavaDoc data) {
77         if (data instanceof String JavaDoc) {
78             // This is the default value.
79
readDefault((String JavaDoc) data);
80             setShouldPersist(true);
81
82         } else if (data instanceof Hashtable JavaDoc) {
83             final Hashtable JavaDoc parameters = (Hashtable JavaDoc) data;
84             final Object JavaDoc defaultObject = parameters.get("default"); //$NON-NLS-1$
85
if (defaultObject instanceof String JavaDoc) {
86                 readDefault((String JavaDoc) defaultObject);
87             }
88
89             final Object JavaDoc persistedObject = parameters.get("persisted"); //$NON-NLS-1$
90
if (persistedObject instanceof String JavaDoc) {
91                 readPersisted((String JavaDoc) persistedObject);
92             }
93
94         } else {
95             setShouldPersist(true);
96             
97         }
98     }
99 }
100
Popular Tags