Mercurial > projects > dwt-addons
comparison dwtx/draw2d/AncestorHelper.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.AncestorHelper; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 import dwtx.dwtxhelper.Bean; | |
17 | |
18 import dwtx.draw2d.IFigure; | |
19 import dwtx.draw2d.FigureListener; | |
20 import dwtx.draw2d.AncestorListener; | |
21 | |
22 /** | |
23 * A helper object which tracks the parent chain hierarchy. | |
24 * @since 2.1 | |
25 */ | |
26 class AncestorHelper | |
27 : PropertyChangeListener, FigureListener | |
28 { | |
29 | |
30 /** | |
31 * The base figure whose ancestor chain is being observed. | |
32 */ | |
33 protected final IFigure base; | |
34 /** | |
35 * The array of ancestor listeners. | |
36 */ | |
37 protected AncestorListener[] listeners; | |
38 | |
39 /** | |
40 * Constructs a new helper on the given base figure and starts listening to figure and | |
41 * property changes on the base figure's parent chain. When no longer needed, the helper | |
42 * should be disposed. | |
43 * @since 2.1 | |
44 * @param baseFigure | |
45 */ | |
46 public this(IFigure baseFigure) { | |
47 this.base = baseFigure; | |
48 addAncestors(baseFigure); | |
49 } | |
50 | |
51 /** | |
52 * Appends a new listener to the list of listeners. | |
53 * @param listener the listener | |
54 */ | |
55 public void addAncestorListener(AncestorListener listener) { | |
56 if (listeners is null) { | |
57 listeners = new AncestorListener[1]; | |
58 listeners[0] = listener; | |
59 } else { | |
60 int oldSize = listeners.length; | |
61 AncestorListener newListeners[] = new AncestorListener[oldSize + 1]; | |
62 SimpleType!(AncestorListener).arraycopy(listeners, 0, newListeners, 0, oldSize); | |
63 newListeners[oldSize] = listener; | |
64 listeners = newListeners; | |
65 } | |
66 } | |
67 | |
68 /** | |
69 * Hooks up internal listeners used for maintaining the proper figure listeners. | |
70 * @param rootFigure the root figure | |
71 */ | |
72 protected void addAncestors(IFigure rootFigure) { | |
73 for (IFigure ancestor = rootFigure; | |
74 ancestor !is null; | |
75 ancestor = ancestor.getParent()) { | |
76 ancestor.addFigureListener(this); | |
77 ancestor.addPropertyChangeListener("parent", this); //$NON-NLS-1$ | |
78 } | |
79 } | |
80 | |
81 /** | |
82 * Removes all internal listeners. | |
83 */ | |
84 public void dispose() { | |
85 removeAncestors(base); | |
86 listeners = null; | |
87 } | |
88 | |
89 /** | |
90 * @see dwtx.draw2d.FigureListener#figureMoved(dwtx.draw2d.IFigure) | |
91 */ | |
92 public void figureMoved(IFigure ancestor) { | |
93 fireAncestorMoved(ancestor); | |
94 } | |
95 | |
96 /** | |
97 * Fires notification to the listener list | |
98 * @param ancestor the figure which moved | |
99 */ | |
100 protected void fireAncestorMoved(IFigure ancestor) { | |
101 if (listeners is null) | |
102 return; | |
103 for (int i = 0; i < listeners.length; i++) | |
104 listeners[i].ancestorMoved(ancestor); | |
105 } | |
106 | |
107 /** | |
108 * Fires notification to the listener list | |
109 * @param ancestor the figure which moved | |
110 */ | |
111 protected void fireAncestorAdded(IFigure ancestor) { | |
112 if (listeners is null) | |
113 return; | |
114 for (int i = 0; i < listeners.length; i++) | |
115 listeners[i].ancestorAdded(ancestor); | |
116 } | |
117 | |
118 /** | |
119 * Fires notification to the listener list | |
120 * @param ancestor the figure which moved | |
121 */ | |
122 protected void fireAncestorRemoved(IFigure ancestor) { | |
123 if (listeners is null) | |
124 return; | |
125 for (int i = 0; i < listeners.length; i++) | |
126 listeners[i].ancestorRemoved(ancestor); | |
127 } | |
128 | |
129 /** | |
130 * Returns the total number of listeners. | |
131 * @return the number of listeners | |
132 */ | |
133 public bool isEmpty() { | |
134 return listeners is null; | |
135 } | |
136 | |
137 /** | |
138 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) | |
139 */ | |
140 public void propertyChange(PropertyChangeEvent event) { | |
141 if (event.getPropertyName().equals("parent")) { //$NON-NLS-1$ | |
142 IFigure oldParent = cast(IFigure)event.getOldValue(); | |
143 IFigure newParent = cast(IFigure)event.getNewValue(); | |
144 if (oldParent !is null) { | |
145 removeAncestors(oldParent); | |
146 fireAncestorRemoved(oldParent); | |
147 } | |
148 if (newParent !is null) { | |
149 addAncestors(newParent); | |
150 fireAncestorAdded(newParent); | |
151 } | |
152 } | |
153 } | |
154 | |
155 /** | |
156 * Removes the first occurence of the given listener | |
157 * @param listener the listener to remove | |
158 */ | |
159 public void removeAncestorListener(AncestorListener listener) { | |
160 if (listeners is null) | |
161 return; | |
162 for (int index = 0; index < listeners.length; index++) | |
163 if (listeners[index] is listener) { | |
164 int newSize = listeners.length - 1; | |
165 AncestorListener newListeners[] = null; | |
166 if (newSize !is 0) { | |
167 newListeners = new AncestorListener[newSize]; | |
168 SimpleType!(AncestorListener).arraycopy(listeners, 0, newListeners, 0, index); | |
169 SimpleType!(AncestorListener).arraycopy(listeners, index + 1, newListeners, index, newSize - index); | |
170 } | |
171 listeners = newListeners; | |
172 return; | |
173 } | |
174 } | |
175 | |
176 /** | |
177 * Unhooks listeners starting at the given figure | |
178 * @param rootFigure | |
179 */ | |
180 protected void removeAncestors(IFigure rootFigure) { | |
181 for (IFigure ancestor = rootFigure; | |
182 ancestor !is null; | |
183 ancestor = ancestor.getParent()) { | |
184 ancestor.removeFigureListener(this); | |
185 ancestor.removePropertyChangeListener("parent", this); //$NON-NLS-1$ | |
186 } | |
187 } | |
188 | |
189 } |