view dwtx/core/commands/NamedHandleObjectWithState.d @ 3:6518c18a01f7

eclipse.core package without osgi dependencies
author Frank Benoit <benoit@tionex.de>
date Wed, 26 Mar 2008 00:57:19 +0100
parents
children ea8ff534f622
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.core.commands.NamedHandleObjectWithState;

import tango.util.collection.HashMap;
import tango.util.collection.model.Map;
// import tango.util.collection.model.Set;

import dwtx.core.commands.common.NamedHandleObject;
import dwtx.core.commands.common.NotDefinedException;
import dwtx.core.commands.IObjectWithState;
import dwtx.core.commands.State;
import dwtx.core.commands.INamedHandleStateIds;

import dwt.dwthelper.utils;

/**
 * <p>
 * A named handle object that can carry state with it. This state can be used to
 * override the name or description.
 * </p>
 * <p>
 * Clients may neither instantiate nor extend this class.
 * </p>
 *
 * @since 3.2
 */
abstract class NamedHandleObjectWithState : NamedHandleObject,
        IObjectWithState {

    /**
     * An empty string array, which can be returned from {@link #getStateIds()}
     * if there is no state.
     */
    private static const String[] NO_STATE = null;

    /**
     * The map of states currently held by this command. If this command has no
     * state, then this will be <code>null</code>.
     */
    private Map!(String,State) states = null;

    /**
     * Constructs a new instance of <code>NamedHandleObject<WithState/code>.
     *
     * @param id
     *            The identifier for this handle; must not be <code>null</code>.
     */
    protected this(String id) {
        super(id);
    }

    public void addState(String stateId, State state) {
        if (state is null) {
            throw new NullPointerException("Cannot add a null state"); //$NON-NLS-1$
        }

        if (states is null) {
            states = new HashMap!(String,State)/+(3)+/;
        }
        states.add(stateId, state);
    }

    public final String getDescription() {
        String description = super.getDescription(); // Trigger a NDE.

        State descriptionState = getState(INamedHandleStateIds.DESCRIPTION);
        if (descriptionState !is null) {
            Object value = descriptionState.getValue();
            if (value !is null) {
                return value.toString();
            }
        }

        return description;
    }

    public final String getName() {
        String name = super.getName(); // Trigger a NDE, if necessary.

        State nameState = getState(INamedHandleStateIds.NAME);
        if (nameState !is null) {
            final Object value = nameState.getValue();
            if (value !is null) {
                return value.toString();
            }
        }

        return name;
    }

    public final State getState(String stateId) {
        if ((states is null) || (states.drained())) {
            return null;
        }

        return states.get(stateId);
    }

    public final String[] getStateIds() {
        if ((states is null) || (states.drained())) {
            return NO_STATE;
        }

        String[] res;
        foreach( id, state; states){
            res ~= id;
        }
        return res;
    }

    public void removeState(String id) {
        if (id is null) {
            throw new NullPointerException("Cannot remove a null id"); //$NON-NLS-1$
        }

        if (states !is null) {
            states.removeKey(id);
            if (states.drained()) {
                states = null;
            }
        }
    }

}