view dwtx/jface/commands/ToggleState.d @ 43:ea8ff534f622

Fix override and super aliases
author Frank Benoit <benoit@tionex.de>
date Fri, 11 Apr 2008 01:24:25 +0200
parents 103ab03b77eb
children 46a6e0e6ccd4
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/

module dwtx.jface.commands.ToggleState;

import dwtx.jface.menus.IMenuStateIds;
import dwtx.jface.preference.IPreferenceStore;

import dwtx.jface.commands.PersistentState;

import dwt.dwthelper.utils;

/**
 * <p>
 * A piece of state storing a {@link bool}.
 * </p>
 * <p>
 * If this state is registered using {@link IMenuStateIds#STYLE}, then it will
 * control the presentation of the command if displayed in the menus, tool bars
 * or status line.
 * </p>
 * <p>
 * Clients may instantiate this class, but must not extend.
 * </p>
 *
 * @since 3.2
 */
public class ToggleState : PersistentState {

    /**
     * Constructs a new <code>ToggleState</code>. By default, the toggle is
     * off (e.g., <code>false</code>).
     */
    public this() {
        setValue(new ValueWrapperBool(false));
    }

    public override final void load(IPreferenceStore store,
            String preferenceKey) {
        bool currentValue = (cast(ValueWrapperBool) getValue()).value;
        store.setDefault(preferenceKey, currentValue);
        if (shouldPersist() && (store.contains(preferenceKey))) {
            bool value = store.getBoolean(preferenceKey);
            setValue( new ValueWrapperBool( value ));
        }
    }

    public override final void save(IPreferenceStore store,
            String preferenceKey) {
        if (shouldPersist()) {
            Object value = getValue();
            if ( auto v = cast(ValueWrapperBool)value ) {
                store.setValue(preferenceKey, v.value);
            }
        }
    }

    public override void setValue(Object value) {
        if (!(cast(ValueWrapperBool)value)) {
            throw new IllegalArgumentException(
                    "ToggleState takes a bool as a value"); //$NON-NLS-1$
        }

        super.setValue(value);
    }
}