001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.handlers;
011:
012: import java.util.Hashtable;
013:
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IExecutableExtension;
016: import org.eclipse.jface.commands.RadioState;
017:
018: /**
019: * <p>
020: * A radio state that can be read from the registry. This stores a piece of
021: * boolean state information that is grouped with other boolean state to form a
022: * radio group. In a single radio group, there can be at most one state who
023: * value is {@link Boolean#TRUE} all the others must be {@link Boolean#FALSE}.
024: * </p>
025: * <p>
026: * When parsing from the registry, this state understands three parameters:
027: * <code>default</code>, which is the default value for this item;
028: * <code>persisted</code>, which is whether the state should be persisted
029: * between sessions; <code>id</code>, which is the identifier of the group to
030: * which this radio handler belongs. The <code>default</code> parameter
031: * defaults to <code>false</code>, and the <code>persisted</code> parameter
032: * defaults to <code>true</code>. If only one parameter is passed (i.e.,
033: * using the class name followed by a colon), then it is assumed to be the
034: * <code>id</code> parameter. The <code>id</code> is required for this class
035: * to function properly.
036: * </p>
037: * <p>
038: * Clients may instantiate this class, but must not extend.
039: * </p>
040: *
041: * @since 3.2
042: */
043: public final class RegistryRadioState extends RadioState implements
044: IExecutableExtension {
045:
046: /**
047: * Reads the <code>default</code> parameter from the given string. This
048: * converts the string to a boolean, using <code>true</code> as the
049: * default.
050: *
051: * @param defaultString
052: * The string to parse; may be <code>null</code>.
053: */
054: private final void readDefault(final String defaultString) {
055: if ("true".equalsIgnoreCase(defaultString)) { //$NON-NLS-1$
056: setValue(Boolean.TRUE);
057: }
058: }
059:
060: /**
061: * Reads the <code>persisted</code> parameter from the given string. This
062: * converts the string to a boolean, using <code>true</code> as the
063: * default.
064: *
065: * @param persistedString
066: * The string to parse; may be <code>null</code>.
067: */
068: private final void readPersisted(final String persistedString) {
069: if ("false".equalsIgnoreCase(persistedString)) { //$NON-NLS-1$
070: setShouldPersist(false);
071: }
072:
073: setShouldPersist(true);
074: }
075:
076: public final void setInitializationData(
077: final IConfigurationElement configurationElement,
078: final String propertyName, final Object data) {
079: if (data instanceof String) {
080: // This is the default value.
081: setRadioGroupIdentifier((String) data);
082: setValue(Boolean.FALSE);
083: setShouldPersist(true);
084:
085: } else if (data instanceof Hashtable) {
086: final Hashtable parameters = (Hashtable) data;
087:
088: final Object defaultObject = parameters.get("default"); //$NON-NLS-1$
089: if (defaultObject instanceof String) {
090: readDefault((String) defaultObject);
091: }
092:
093: final Object persistedObject = parameters.get("persisted"); //$NON-NLS-1$
094: if (persistedObject instanceof String) {
095: readPersisted((String) persistedObject);
096: }
097:
098: final Object idObject = parameters.get("id"); //$NON-NLS-1$
099: if (idObject instanceof String) {
100: setRadioGroupIdentifier((String) idObject);
101: }
102:
103: } else {
104: setShouldPersist(true);
105:
106: }
107: }
108: }
|