Project

General

Profile

Download (6.55 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 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
 *******************************************************************************/
11
package org.eclipse.draw2d;
12

    
13
import java.util.Map;
14

    
15
import org.eclipse.swt.graphics.GC;
16

    
17
import org.eclipse.draw2d.geometry.Rectangle;
18

    
19
/**
20
 * Update managers handle the job of laying out and repainting figures. A
21
 * desirable implementation is to batches work to be done and collapses any
22
 * redundant work. For example, clients may be making multiple changes to
23
 * figures, which require laying out the same container or repainting the same
24
 * region.
25
 * <P>
26
 * The update manager receives requests to validate certain figures, and repaint
27
 * certain areas of figures. An update manager could process every request
28
 * synchronously, or it could batch these requests and process them
29
 * asynchronously.
30
 * <P>
31
 * The update process occurs in two phases. The first phase is laying out
32
 * invalid figures. This phase comes first because it usually introduces
33
 * additional damage regions. In some cases, while validating figures, new
34
 * invalid figures may be appended to the update manager. Of course, damage
35
 * regions will be reported too as figures are layed out.
36
 * <P>
37
 * The second phase is to repaint all damaged areas. The update manager will
38
 * typically batch, clip, and union, all rectangles and perform a single paint
39
 * of the overall damaged area.
40
 * 
41
 */
42
public abstract class UpdateManager {
43

    
44
	private UpdateListener listeners[] = new UpdateListener[0];
45
	private boolean disposed;
46

    
47
	/**
48
	 * Adds the dirty region defined by the coordinates on the IFigure
49
	 * <b>figure</b>. The update manager should repaint the dirty region in a
50
	 * timely fashion.
51
	 * 
52
	 * @param figure
53
	 *            the dirty figure
54
	 * @param x
55
	 *            the x coordinate of the dirty region
56
	 * @param y
57
	 *            the y coordinate of the dirty region
58
	 * @param w
59
	 *            the width of the dirty region
60
	 * @param h
61
	 *            the height of the dirty region
62
	 */
63
	public abstract void addDirtyRegion(IFigure figure, int x, int y, int w,
64
			int h);
65

    
66
	/**
67
	 * @see #addDirtyRegion(IFigure, int, int, int, int)
68
	 */
69
	public void addDirtyRegion(IFigure figure, Rectangle rect) {
70
		addDirtyRegion(figure, rect.x, rect.y, rect.width, rect.height);
71
	}
72

    
73
	/**
74
	 * Causes an update to occur at some time, and the given runnable to be
75
	 * executed following the update.
76
	 * 
77
	 * @since 3.1
78
	 * @param run
79
	 *            the runnable
80
	 */
81
	public void runWithUpdate(Runnable run) {
82
	}
83

    
84
	/**
85
	 * The receiver should call validate() on the IFigure <i>figure</i> in a
86
	 * timely fashion.
87
	 * 
88
	 * @param figure
89
	 *            the invalid figure
90
	 */
91
	public abstract void addInvalidFigure(IFigure figure);
92

    
93
	/**
94
	 * Adds the given listener to the list of listeners to be notified of
95
	 * painting and validation.
96
	 * 
97
	 * @param listener
98
	 *            the listener to add
99
	 */
100
	public void addUpdateListener(UpdateListener listener) {
101
		if (listener == null)
102
			throw new IllegalArgumentException();
103
		if (listeners == null) {
104
			listeners = new UpdateListener[1];
105
			listeners[0] = listener;
106
		} else {
107
			int oldSize = listeners.length;
108
			UpdateListener newListeners[] = new UpdateListener[oldSize + 1];
109
			System.arraycopy(listeners, 0, newListeners, 0, oldSize);
110
			newListeners[oldSize] = listener;
111
			listeners = newListeners;
112
		}
113
	}
114

    
115
	/**
116
	 * Called when the EditPartViewer is being disposed.
117
	 */
118
	public void dispose() {
119
		disposed = true;
120
	}
121

    
122
	/**
123
	 * Notifies listeners that painting is about to occur, passing them the
124
	 * damaged rectangle and the map of dirty regions.
125
	 * 
126
	 * @param damage
127
	 *            the damaged rectangle
128
	 * @param dirtyRegions
129
	 *            map of dirty regions to figures
130
	 */
131
	protected void firePainting(Rectangle damage, Map dirtyRegions) {
132
		UpdateListener localListeners[] = listeners;
133
		for (int i = 0; i < localListeners.length; i++)
134
			localListeners[i].notifyPainting(damage, dirtyRegions);
135
	}
136

    
137
	/**
138
	 * Notifies listeners that validation is about to occur.
139
	 */
140
	protected void fireValidating() {
141
		UpdateListener localListeners[] = listeners;
142
		for (int i = 0; i < localListeners.length; i++)
143
			localListeners[i].notifyValidating();
144
	}
145

    
146
	/**
147
	 * @return whether this update manager has been disposed.
148
	 */
149
	protected boolean isDisposed() {
150
		return disposed;
151
	}
152

    
153
	/**
154
	 * Forces an update to occur. Update managers will perform updates
155
	 * automatically, but may do so asynchronously. Calling this method forces a
156
	 * synchronous update.
157
	 */
158
	public abstract void performUpdate();
159

    
160
	void paint(GC gc) {
161
		performUpdate(new Rectangle(gc.getClipping()));
162
	}
163

    
164
	/**
165
	 * Performs an update on the given exposed rectangle.
166
	 * 
167
	 * @param exposed
168
	 *            the exposed rectangle
169
	 */
170
	public abstract void performUpdate(Rectangle exposed);
171

    
172
	/**
173
	 * Removes one occurrence of the given UpdateListener by identity.
174
	 * 
175
	 * @param listener
176
	 *            the listener to remove
177
	 */
178
	public void removeUpdateListener(UpdateListener listener) {
179
		if (listener == null)
180
			throw new IllegalArgumentException();
181
		for (int index = 0; index < listeners.length; index++)
182
			if (listeners[index] == listener) {
183
				int newSize = listeners.length - 1;
184
				UpdateListener newListeners[] = null;
185
				if (newSize != 0) {
186
					newListeners = new UpdateListener[newSize];
187
					System.arraycopy(listeners, 0, newListeners, 0, index);
188
					System.arraycopy(listeners, index + 1, newListeners, index,
189
							newSize - index);
190
				} else {
191
					newListeners = new UpdateListener[0];
192
				}
193
				listeners = newListeners;
194
				return;
195
			}
196
	}
197

    
198
	/**
199
	 * Sets the GraphicsSource for this update manager.
200
	 * 
201
	 * @param gs
202
	 *            the new GraphicsSource
203
	 */
204
	public abstract void setGraphicsSource(GraphicsSource gs);
205

    
206
	/**
207
	 * Sets the root figure.
208
	 * 
209
	 * @param figure
210
	 *            the new root figure
211
	 */
212
	public abstract void setRoot(IFigure figure);
213

    
214
	/**
215
	 * Performs a partial update if supported (validation only). Fires
216
	 * notification to listeners that validation has been performed. By default
217
	 * this method calls {@link #performUpdate()}. Subclasses should override
218
	 * this method to support validation without repainting.
219
	 * 
220
	 * @since 3.2
221
	 */
222
	public void performValidation() {
223
		performUpdate();
224
	}
225

    
226
}
(164-164/171)