Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 483940ca7f5e44e7d68a1dd90c8bc60525f4c7e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2000, 2013 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
 *******************************************************************************/
package org.eclipse.swt.examples.paint;


import org.eclipse.swt.graphics.*;

/**
 * Container for Figure objects with stacking preview mechanism.
 */
public class ContainerFigure extends Figure {
	private static final int INITIAL_ARRAY_SIZE = 16;

	Figure[]   objectStack = null;
	int      nextIndex = 0;

	/**
	 * Constructs an empty Container
	 */
	public ContainerFigure() {
	}
	/**
	 * Adds an object to the container for later drawing.
	 *
	 * @param object the object to add to the drawing list
	 */
	public void add(Figure object) {
		if (objectStack == null) {
			objectStack = new Figure[INITIAL_ARRAY_SIZE];
		} else if (objectStack.length <= nextIndex) {
			Figure[] newObjectStack = new Figure[objectStack.length * 2];
			System.arraycopy(objectStack, 0, newObjectStack, 0, objectStack.length);
			objectStack = newObjectStack;
		}
		objectStack[nextIndex] = object;
		++nextIndex;
	}
	/**
	 * Determines if the container is empty.
	 * @return true if the container is empty
	 */
	public boolean isEmpty() {
		return nextIndex == 0;
	}
	/**
	 * Adds an object to the container and draws its preview then updates the supplied preview state.
	 *
	 * @param object the object to add to the drawing list
	 * @param gc the GC to draw on
	 * @param offset the offset to add to virtual coordinates to get display coordinates
	 * @param rememberedState the state returned by a previous drawPreview() or addAndPreview()
	 *        using this Container, may be null if there was no such previous call
	 * @return object state that must be passed to erasePreview() later to erase this object
	 */
//	public Object addAndPreview(Figure object, GC gc, Point offset, Object rememberedState) {
//		Object[] stateStack = (Object[]) rememberedState;
//		if (stateStack == null) {
//			stateStack = new Object[INITIAL_ARRAY_SIZE];
//		} else if (stateStack.length <= nextIndex) {
//			Object[] newStateStack = new Object[stateStack.length * 2];
//			System.arraycopy(stateStack, 0, newStateStack, 0, stateStack.length);
//			stateStack = newStateStack;
//		}
//		add(object);
//		stateStack[nextIndex - 1] = object.drawPreview(gc, offset);
//		return stateStack;
//	}
	/**
	 * Clears the container.
	 * <p>
	 * Note that erasePreview() cannot be called after this point to erase any previous
	 * drawPreview()'s.
	 * </p>
	 */
	public void clear() {
		while (--nextIndex > 0) objectStack[nextIndex] = null;
		nextIndex = 0;
	}
	@Override
	public void draw(FigureDrawContext fdc) {
		for (int i = 0; i < nextIndex; ++i) objectStack[i].draw(fdc);
	}
	@Override
	public void addDamagedRegion(FigureDrawContext fdc, Region region) {
		for (int i = 0; i < nextIndex; ++i) objectStack[i].addDamagedRegion(fdc, region);
	}
}

Back to the top