Mercurial > projects > dwt-addons
comparison dwtx/draw2d/DefaultRangeModel.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2005 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 module dwtx.draw2d.DefaultRangeModel; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 import dwtx.dwtxhelper.Bean; | |
17 import tango.text.convert.Format; | |
18 | |
19 import dwtx.draw2d.RangeModel; | |
20 | |
21 /** | |
22 * Generic implementation for a RangeModel. | |
23 * | |
24 * | |
25 *<pre> | |
26 * |<----extent--->| | |
27 * ----|-----------|---------------|---------------|---- | |
28 * min | max | |
29 * value | |
30 * </pre> | |
31 */ | |
32 public class DefaultRangeModel | |
33 : RangeModel | |
34 { | |
35 | |
36 /** | |
37 * Listeners interested in the range model's property changes. | |
38 */ | |
39 protected PropertyChangeSupport propertyListeners; | |
40 private int minimum = 0; | |
41 private int maximum = 100; | |
42 private int extent = 20; | |
43 private int value = 0; | |
44 | |
45 public this(){ | |
46 propertyListeners = new PropertyChangeSupport(this); | |
47 } | |
48 | |
49 /** | |
50 * Registers the given listener as a PropertyChangeListener. | |
51 * | |
52 * @param listener the listener to be added | |
53 * @since 2.0 | |
54 */ | |
55 public void addPropertyChangeListener(PropertyChangeListener listener) { | |
56 propertyListeners.addPropertyChangeListener(listener); | |
57 } | |
58 | |
59 /** | |
60 * Notifies any listening PropertyChangeListeners that the property with the given id has | |
61 * changed. | |
62 * | |
63 * @param string the property name | |
64 * @param oldValue the old value | |
65 * @param newValue the new value | |
66 * @since 2.0 | |
67 */ | |
68 protected void firePropertyChange(String string, int oldValue, int newValue) { | |
69 propertyListeners.firePropertyChange(string, oldValue, newValue); | |
70 } | |
71 | |
72 /** | |
73 * @return the extent | |
74 */ | |
75 public int getExtent() { | |
76 return extent; | |
77 } | |
78 | |
79 /** | |
80 * @return the maximum value | |
81 */ | |
82 public int getMaximum() { | |
83 return maximum; | |
84 } | |
85 | |
86 /** | |
87 * @return the minimum value | |
88 */ | |
89 public int getMinimum() { | |
90 return minimum; | |
91 } | |
92 | |
93 /** | |
94 * @return the current value | |
95 */ | |
96 public int getValue() { | |
97 return value; | |
98 } | |
99 | |
100 /** | |
101 * @return whether the extent is between the minimum and maximum values | |
102 */ | |
103 public bool isEnabled() { | |
104 return (getMaximum() - getMinimum()) > getExtent(); | |
105 } | |
106 | |
107 /** | |
108 * Removes the given PropertyChangeListener from the list of listeners. | |
109 * | |
110 * @param listener the listener to be removed | |
111 */ | |
112 public void removePropertyChangeListener(PropertyChangeListener listener) { | |
113 propertyListeners.removePropertyChangeListener(listener); | |
114 } | |
115 | |
116 /** | |
117 * @see dwtx.draw2d.RangeModel#setAll(int, int, int) | |
118 */ | |
119 public void setAll(int min, int ext, int max) { | |
120 int oldMin = minimum; | |
121 int oldExtent = extent; | |
122 int oldMax = maximum; | |
123 maximum = max; | |
124 minimum = min; | |
125 extent = ext; | |
126 if (oldMax !is max) | |
127 firePropertyChange(PROPERTY_MAXIMUM, oldMax, max); | |
128 if (oldExtent !is ext) | |
129 firePropertyChange(PROPERTY_EXTENT, oldExtent, ext); | |
130 if (oldMin !is min) | |
131 firePropertyChange(PROPERTY_MINIMUM, oldMin, min); | |
132 setValue(getValue()); | |
133 } | |
134 | |
135 /** | |
136 * Sets this RangeModel's extent and fires a property change if the given value is | |
137 * different from the current extent. | |
138 * | |
139 * @param extent the new extent value | |
140 */ | |
141 public void setExtent(int extent) { | |
142 if (this.extent is extent) | |
143 return; | |
144 int oldValue = this.extent; | |
145 this.extent = extent; | |
146 firePropertyChange(PROPERTY_EXTENT, oldValue, extent); | |
147 setValue(getValue()); | |
148 } | |
149 | |
150 /** | |
151 * Sets this RangeModel's maximum value and fires a property change if the given value is | |
152 * different from the current maximum value. | |
153 * | |
154 * @param maximum the new maximum value | |
155 */ | |
156 public void setMaximum(int maximum) { | |
157 if (this.maximum is maximum) | |
158 return; | |
159 int oldValue = this.maximum; | |
160 this.maximum = maximum; | |
161 firePropertyChange(PROPERTY_MAXIMUM, oldValue, maximum); | |
162 setValue(getValue()); | |
163 } | |
164 | |
165 /** | |
166 * Sets this RangeModel's minimum value and fires a property change if the given value is | |
167 * different from the current minimum value. | |
168 * | |
169 * @param minimum the new minumum value | |
170 */ | |
171 public void setMinimum(int minimum) { | |
172 if (this.minimum is minimum) | |
173 return; | |
174 int oldValue = this.minimum; | |
175 this.minimum = minimum; | |
176 firePropertyChange(PROPERTY_MINIMUM, oldValue, minimum); | |
177 setValue(getValue()); | |
178 } | |
179 | |
180 /** | |
181 * Sets this RangeModel's current value. If the given value is greater than the maximum, | |
182 * the maximum value is used. If the given value is less than the minimum, the minimum | |
183 * value is used. If the adjusted value is different from the current value, a property | |
184 * change is fired. | |
185 * | |
186 * @param value the new value | |
187 */ | |
188 public void setValue(int value) { | |
189 value = Math.max(getMinimum(), Math.min(getMaximum() - getExtent(), value)); | |
190 if (this.value is value) | |
191 return; | |
192 int oldValue = this.value; | |
193 this.value = value; | |
194 firePropertyChange(PROPERTY_VALUE, oldValue, value); | |
195 } | |
196 | |
197 /** | |
198 * @see java.lang.Object#toString() | |
199 */ | |
200 public String toString() { | |
201 return Format( "{} ({}, {}, {}, {})", super.toString(), minimum, maximum //$NON-NLS-2$ //$NON-NLS-1$ | |
202 , extent, value ); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ | |
203 } | |
204 | |
205 } |