comparison dwtx/core/commands/State.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
comparison
equal deleted inserted replaced
2:a012107a911c 3:6518c18a01f7
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module dwtx.core.commands.State;
15
16 import dwtx.core.commands.common.EventManager;
17 import dwtx.core.internal.commands.util.Util;
18 import dwtx.core.commands.IStateListener;
19
20 import dwt.dwthelper.utils;
21
22 /**
23 * <p>
24 * A piece of state information that can be shared between objects, and might be
25 * persisted between sessions. This can be used for commands that toggle between
26 * two states and wish to pass this state information between different
27 * handlers.
28 * </p>
29 * <p>
30 * This state object can either be used as a single state object shared between
31 * several commands, or one state object per command -- depending on the needs
32 * of the application.
33 * </p>
34 * <p>
35 * Clients may instantiate or extend this class.
36 * </p>
37 *
38 * @since 3.2
39 */
40 public class State : EventManager {
41
42 /**
43 * The identifier of the state; may be <code>null</code> if it has not
44 * been initialized.
45 */
46 private String id;
47
48 /**
49 * The value held by this state; may be anything at all.
50 */
51 private Object value;
52
53 /**
54 * Adds a listener to changes for this state.
55 *
56 * @param listener
57 * The listener to add; must not be <code>null</code>.
58 */
59 public void addListener(IStateListener listener) {
60 addListenerObject(cast(Object)listener);
61 }
62
63 /**
64 * Disposes of this state. This allows the state to unregister itself with
65 * any managers or as a listener.
66 */
67 public void dispose() {
68 // The default implementation does nothing.
69 }
70
71 /**
72 * Notifies listeners to this state that it has changed in some way.
73 *
74 * @param oldValue
75 * The old value; may be anything.
76 */
77 protected final void fireStateChanged(Object oldValue) {
78 final Object[] listeners = getListeners();
79 for (int i = 0; i < listeners.length; i++) {
80 IStateListener listener = cast(IStateListener) listeners[i];
81 listener.handleStateChange(this, oldValue);
82 }
83 }
84
85 /**
86 * Returns the identifier for this state.
87 *
88 * @return The id; may be <code>null</code>.
89 */
90 public final String getId() {
91 return id;
92 }
93
94 /**
95 * The current value associated with this state. This can be any type of
96 * object, but implementations will usually restrict this value to a
97 * particular type.
98 *
99 * @return The current value; may be anything.
100 */
101
102 public Object getValue() {
103 return value;
104 }
105
106 /**
107 * Removes a listener to changes from this state.
108 *
109 * @param listener
110 * The listener to remove; must not be <code>null</code>.
111 */
112
113 public void removeListener(IStateListener listener) {
114 removeListenerObject(cast(Object)listener);
115 }
116
117 /**
118 * Sets the identifier for this object. This method should only be called
119 * by the command framework. Clients should not call this method.
120 *
121 * @param id
122 * The id; must not be <code>null</code>.
123 */
124 public void setId(String id) {
125 this.id = id;
126 }
127
128 /**
129 * Sets the value for this state object.
130 *
131 * @param value
132 * The value to set; may be anything.
133 */
134 public void setValue(Object value) {
135 if (!Util.equals(this.value, value)) {
136 final Object oldValue = this.value;
137 this.value = value;
138 fireStateChanged(oldValue);
139 }
140 }
141 }