Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b62752e19ebd5093a58863ad179b34607e41eb34 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************************
 * Copyright (c) 2012, 2014 Tilera 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:
 *     William R. Swanson (Tilera Corporation)
 *     Xavier Raynaud (Kalray) - Bug 430804
 *******************************************************************************/

package org.eclipse.cdt.visualizer.ui.canvas;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;


// ---------------------------------------------------------------------------
// GraphicObject
// ---------------------------------------------------------------------------

/**
 * Graphic object base class.
 * Base class for objects that can be displayed and manipulated on a GraphicCanvas.
 */
public class GraphicObject
	implements IGraphicObject
{
	// --- members ---

	/** Data object, if any, associated with this graphic object. */
	protected Object m_data = null;
	
	/** Bounding rectangle of this element. */
	protected Rectangle m_bounds = new Rectangle(0,0,0,0);
	
	/** Whether this element is visible. */
	protected boolean m_visible = true;
	
	/** Whether this element is selected. */
	protected boolean m_selected = false;
	
	/** Foreground color (null means inherit from canvas) */
	protected Color m_foreground = null;
	
	/** Background color (null means inherit from canvas) */
	protected Color m_background = null;
	
	
	// --- constructors/destructors ---
	
	/** Constructor. */
	public GraphicObject() {
	}
	
	/** Constructor. */
	public GraphicObject(int x, int y, int width, int height) {
		m_bounds.x = x;
		m_bounds.y = y;
		m_bounds.width = width;
		m_bounds.height = height;
	}
	
	/** Constructor. */
	public GraphicObject(Rectangle bounds) {
		m_bounds.x = bounds.x;
		m_bounds.y = bounds.y;
		m_bounds.width = bounds.width;
		m_bounds.height = bounds.height;
	}
	
	/** Constructor. */
	public GraphicObject(Object data) {
		m_data = data;
	}
	
	/** Constructor. */
	public GraphicObject(int x, int y, int width, int height, Object data) {
		m_bounds.x = x;
		m_bounds.y = y;
		m_bounds.width = width;
		m_bounds.height = height;
		m_data = data;
	}
	

	/** Constructor. */
	public GraphicObject(Rectangle bounds, Object data)
	{
		m_bounds.x = bounds.x;
		m_bounds.y = bounds.y;
		m_bounds.width = bounds.width;
		m_bounds.height = bounds.height;
		m_data = data;
	}
	
	/** Dispose method. */
	public void dispose() {
		m_data = null;
	}

	
	// --- accessors ---
	
	/** Gets data object associated with this view element. */
	public Object getData() {
		return m_data;
	}
	/** Sets data object associated with this view element. */
	public void setData(Object data) {
		m_data = data;
	}
	
	
	/** Gets x location of this element */
	public int getX() { return m_bounds.x; }
	/** Sets x location of this element */
	public void setX(int x) { 
		m_bounds.x = x; 
	}

	/** Gets y location of this element */
	public int getY() { return m_bounds.y; }
	/** Sets y location of this element */
	public void setY(int y) { m_bounds.y = y; }
	
	/** Sets x/y position of this element */
	public void setPosition(int x, int y) {
		m_bounds.x = x; m_bounds.y = y;
	}
	
	
	/** Gets width of this element */
	public int getWidth() { return m_bounds.width; }
	/** Sets width of this element */
	public void setWidth(int w) { m_bounds.width = w; }

	/** Gets y location of this element */
	public int getHeight() { return m_bounds.height; }
	/** Sets y location of this element */
	public void setHeight(int h) { m_bounds.height = h; }
	
	/** Sets width/height of this element */
	public void setSize(int w, int h) {
		m_bounds.width = w; m_bounds.height = h;
	}
	
	/** Gets bounding rectangle of this element. */
	public Rectangle getBounds() {
		return m_bounds;
	}
	/** Sets bounding rectangle of this element. */
	public void setBounds(int x, int y, int w, int h) {
		m_bounds.x = x;
		m_bounds.y = y;
		m_bounds.width = w;
		m_bounds.height = h;
	}
	/** Sets bounding rectangle of this element. */
	public void setBounds(Rectangle bounds) {
		m_bounds.x = bounds.x;
		m_bounds.y = bounds.y;
		m_bounds.width = bounds.width;
		m_bounds.height = bounds.height;
	}
	
	/** Returns true if element bounds contains point. */
	public boolean contains(int x, int y) {
		return m_bounds.contains(x,y);
	}
	/** Returns true if element bounds are within specified rectangle. */
	public boolean isWithin(Rectangle region) {
		return (region.x <= m_bounds.x &&
				region.y <= m_bounds.y &&
				region.x + region.width >= m_bounds.x + m_bounds.width &&
				region.y + region.height >= m_bounds.y + m_bounds.height);
	}
	
	/** Gets whether this element is visible. */
	public boolean isVisible() {
		return m_visible;
	}

	/** Sets whether this element is visible. */
	public void setVisible(boolean visible) {
		m_visible = visible;
	}
	
	/** Gets whether this element is selected. */
	public boolean isSelected() {
		return m_selected;
	}

	/** Sets whether this element is selected. */
	public void setSelected(boolean selected) {
		m_selected = selected;
	}
	
	/** Sets foreground color (null means inherit from container) */
	public void setForeground(Color color) {
		m_foreground = color;
	}
	/** Gets foreground color (null means inherit from container) */
	public Color getForeground() {
		return m_foreground;
	}

	/** Sets foreground color (null means inherit from container) */
	public void setBackground(Color color) {
		m_background = color;
	}
	/** Gets background color (null means inherit from container) */
	public Color getBackground() {
		return m_background;
	}

	
	// --- methods ---
	
	/** Invoked to allow element to paint itself on the viewer canvas */
	public void paint(GC gc, boolean decorations) {
		if (isVisible()) {
			// Set GC to reflect object properties, if set.
			Color oldForeground = null;
			Color oldBackground = null;
			if (m_foreground != null) {
				oldForeground = gc.getForeground();
				gc.setForeground(m_foreground);
			}
			if (m_background != null) {
				oldBackground = gc.getBackground();
				gc.setBackground(m_background);
			}

			// Paint the object.
			if (! decorations)
				paintContent(gc);
			else
				paintDecorations(gc);

			// Restore old state.
			if (m_foreground != null) gc.setForeground(oldForeground);
			if (m_background != null) gc.setBackground(oldBackground);
		}
	}
	
	/**
	 * Paints content of graphic object.
	 * GC has already been set to this object's
	 * current foreground/background colors.
	 * Default implementation draws object's bounding box.
	 */
	public void paintContent(GC gc) {
		// Draw boundary rectangle of object.
		gc.drawRectangle(m_bounds);
		
		// Display selection as thicker boundary.
		if (isSelected()) {
			int x = m_bounds.x + 1;
			int y = m_bounds.y + 1;
			int width = m_bounds.width - 2;   if (width < 0) width = 0;
			int height = m_bounds.height - 2; if (height < 0) height = 0;
			gc.drawRectangle(x,y,width,height);
		}
	}
	
	/** Returns true if object has decorations to paint. */
	public boolean hasDecorations() {
		return false;
	}
	
	/** Invoked to allow element to paint decorations
	 *  on top of other items drawn on top of it.
	 */
	public void paintDecorations(GC gc) {
	}

	@Override
	public String getTooltip(int x, int y) {
		return null;
	}
}

Back to the top