Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
4 | * | |
73005152 BH |
5 | * All rights reserved. This program and the accompanying materials |
6 | * are made available under the terms of the Eclipse Public License v1.0 | |
7 | * which accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
73005152 BH |
9 | * |
10 | * Contributors: | |
11 | * IBM - Initial API and implementation | |
12 | * Bernd Hufmann - Updated for TMF | |
13 | **********************************************************************/ | |
14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd; | |
15 | ||
16 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Frame; | |
17 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor; | |
18 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IFont; | |
19 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC; | |
20 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage; | |
21 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.impl.ColorImpl; | |
22 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref; | |
23 | import org.eclipse.swt.SWT; | |
24 | import org.eclipse.swt.graphics.Color; | |
25 | import org.eclipse.swt.graphics.Font; | |
26 | import org.eclipse.swt.graphics.FontData; | |
27 | import org.eclipse.swt.graphics.GC; | |
28 | import org.eclipse.swt.graphics.Image; | |
29 | import org.eclipse.swt.graphics.Point; | |
30 | import org.eclipse.swt.graphics.Rectangle; | |
31 | import org.eclipse.swt.widgets.Display; | |
32 | ||
33 | /** | |
df0b8ff4 BH |
34 | * <p> |
35 | * This class implements the graphical context for the sequence diagram widgets. | |
36 | * </p> | |
73005152 | 37 | * |
df0b8ff4 BH |
38 | * @version 1.0 |
39 | * @author sveyrier | |
73005152 BH |
40 | */ |
41 | public class NGC implements IGC { | |
42 | ||
df0b8ff4 BH |
43 | // ------------------------------------------------------------------------ |
44 | // Attributes | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
47 | /** | |
48 | * The graphical context. | |
49 | */ | |
73005152 | 50 | protected GC context; |
df0b8ff4 BH |
51 | /** |
52 | * The reference to the sequence diagram view. | |
53 | */ | |
73005152 | 54 | protected SDWidget view; |
df0b8ff4 BH |
55 | /** |
56 | * A reference to the last used font. | |
57 | */ | |
73005152 | 58 | protected Font tempFont = null; |
df0b8ff4 BH |
59 | /** |
60 | * The color of the gradient. | |
61 | */ | |
73005152 | 62 | protected IColor gradientColor = null; |
df0b8ff4 BH |
63 | /** |
64 | * The color of the background. | |
65 | */ | |
73005152 | 66 | protected IColor backGround = null; |
df0b8ff4 BH |
67 | /** |
68 | * The color of the foreground. | |
69 | */ | |
73005152 | 70 | protected IColor foreGround = null; |
df0b8ff4 BH |
71 | /** |
72 | * The current vertical screen bounds. | |
73 | */ | |
73005152 | 74 | protected int y_; |
df0b8ff4 BH |
75 | /** |
76 | * The current horizontal screen bounds. | |
77 | */ | |
73005152 | 78 | protected int x_; |
df0b8ff4 BH |
79 | /** |
80 | * The current yx value (view visible height - visible screen bounds) | |
81 | */ | |
73005152 | 82 | protected int yx; |
df0b8ff4 BH |
83 | /** |
84 | * The current xx value (view visible width - visible screen bounds) | |
85 | */ | |
73005152 | 86 | protected int xx; |
df0b8ff4 BH |
87 | /** |
88 | * <code>true</code> to draw with focus else <code>false</code>. | |
89 | */ | |
73005152 BH |
90 | protected boolean drawWithFocus = false; |
91 | ||
df0b8ff4 BH |
92 | /** |
93 | * The static visible screen bounds. | |
94 | */ | |
e6ace8bb BH |
95 | private static int vscreen_bounds = 0; |
96 | ||
df0b8ff4 BH |
97 | |
98 | // ------------------------------------------------------------------------ | |
99 | // Constructors | |
100 | // ------------------------------------------------------------------------ | |
101 | ||
102 | /** | |
103 | * Default constructor. | |
104 | * | |
105 | * @param scrollView A sequence diagram view reference. | |
106 | * @param gc A graphical context. | |
107 | */ | |
73005152 BH |
108 | public NGC(SDWidget scrollView, GC gc) { |
109 | context = gc; | |
110 | view = scrollView; | |
111 | } | |
112 | ||
df0b8ff4 BH |
113 | // ------------------------------------------------------------------------ |
114 | // Methods | |
115 | // ------------------------------------------------------------------------ | |
116 | ||
117 | /* | |
118 | * (non-Javadoc) | |
119 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setLineStyle(int) | |
120 | */ | |
73005152 BH |
121 | @Override |
122 | public void setLineStyle(int style) { | |
123 | context.setLineStyle(style); | |
124 | } | |
125 | ||
df0b8ff4 BH |
126 | /* |
127 | * (non-Javadoc) | |
128 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getLineStyle() | |
129 | */ | |
73005152 BH |
130 | @Override |
131 | public int getLineStyle() { | |
132 | return context.getLineStyle(); | |
133 | } | |
134 | ||
df0b8ff4 BH |
135 | /* |
136 | * (non-Javadoc) | |
137 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getContentsX() | |
138 | */ | |
73005152 BH |
139 | @Override |
140 | public int getContentsX() { | |
141 | return Math.round(view.getContentsX() / view.zoomValue); | |
142 | } | |
143 | ||
df0b8ff4 BH |
144 | /* |
145 | * (non-Javadoc) | |
146 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getContentsY() | |
147 | */ | |
73005152 BH |
148 | @Override |
149 | public int getContentsY() { | |
150 | return Math.round(view.getContentsY() / view.zoomValue); | |
151 | } | |
152 | ||
df0b8ff4 BH |
153 | /* |
154 | * (non-Javadoc) | |
155 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getVisibleWidth() | |
156 | */ | |
73005152 BH |
157 | @Override |
158 | public int getVisibleWidth() { | |
159 | return Math.round(view.getVisibleWidth() / view.zoomValue); | |
160 | } | |
161 | ||
df0b8ff4 BH |
162 | /* |
163 | * (non-Javadoc) | |
164 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getVisibleHeight() | |
165 | */ | |
73005152 BH |
166 | @Override |
167 | public int getVisibleHeight() { | |
168 | return Math.round(view.getVisibleHeight() / view.zoomValue); | |
169 | } | |
170 | ||
df0b8ff4 BH |
171 | /* |
172 | * (non-Javadoc) | |
173 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#contentsToViewX(int) | |
174 | */ | |
73005152 BH |
175 | @Override |
176 | public int contentsToViewX(int x) { | |
177 | return view.contentsToViewX(x); | |
178 | } | |
179 | ||
df0b8ff4 BH |
180 | /* |
181 | * (non-Javadoc) | |
182 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#contentsToViewY(int) | |
183 | */ | |
73005152 BH |
184 | @Override |
185 | public int contentsToViewY(int y) { | |
186 | return view.contentsToViewY(y); | |
187 | } | |
188 | ||
df0b8ff4 BH |
189 | /** |
190 | * Get code for drawings at given x and y position. | |
191 | * | |
192 | * @param x The x position | |
193 | * @param y The y position. | |
194 | * @return A code for top, bottom, right and left | |
195 | */ | |
73005152 BH |
196 | protected byte code(int x, int y) { |
197 | byte c = 0; | |
198 | y_ = vscreen_bounds; | |
199 | x_ = vscreen_bounds; | |
200 | yx = view.getVisibleHeight() + vscreen_bounds; | |
201 | xx = view.getVisibleWidth() + vscreen_bounds; | |
df0b8ff4 | 202 | if (y > yx) { |
73005152 | 203 | c |= 0x01; // top |
df0b8ff4 | 204 | } else if (y < y_) { |
73005152 | 205 | c |= 0x02; // bottom |
df0b8ff4 BH |
206 | } |
207 | ||
208 | if (x > xx) { | |
73005152 | 209 | c |= 0x04; // right |
df0b8ff4 | 210 | } else if (x < x_) { |
73005152 | 211 | c |= 0x08; // left |
df0b8ff4 | 212 | } |
73005152 BH |
213 | return c; |
214 | } | |
215 | ||
df0b8ff4 BH |
216 | /* |
217 | * (non-Javadoc) | |
218 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawLine(int, int, int, int) | |
219 | */ | |
73005152 BH |
220 | @Override |
221 | public void drawLine(int x1, int y1, int x2, int y2) { | |
222 | x1 = Math.round(x1 * view.zoomValue); | |
223 | y1 = Math.round(y1 * view.zoomValue); | |
224 | x2 = Math.round(x2 * view.zoomValue); | |
225 | y2 = Math.round(y2 * view.zoomValue); | |
226 | x1 = view.contentsToViewX(x1); | |
227 | y1 = view.contentsToViewY(y1); | |
228 | x2 = view.contentsToViewX(x2); | |
229 | y2 = view.contentsToViewY(y2); | |
230 | ||
231 | byte code1 = code(x1, y1); | |
232 | byte code2 = code(x2, y2); | |
233 | byte codex; | |
234 | boolean draw = false; | |
235 | boolean end = false; | |
236 | int x = 0, y = 0; | |
237 | ||
238 | do { | |
239 | if (code1 == 0 && code2 == 0) { | |
240 | draw = true; | |
241 | end = true; | |
242 | } else if ((code1 & code2) != 0) { | |
243 | end = true; | |
244 | } else { | |
245 | codex = (code1 != 0) ? code1 : code2; | |
df0b8ff4 | 246 | if ((codex & 0x01) != 0) { // top |
73005152 BH |
247 | x = x1 + ((x2 - x1) * (yx - y1)) / (y2 - y1); |
248 | y = yx; | |
df0b8ff4 | 249 | } else if ((codex & 0x02) != 0) { // bottom |
73005152 BH |
250 | x = x1 + ((x2 - x1) * (y_ - y1)) / (y2 - y1); |
251 | y = y_; | |
df0b8ff4 | 252 | } else if ((codex & 0x04) != 0) { // right |
73005152 BH |
253 | y = y1 + ((y2 - y1) * (xx - x1)) / (x2 - x1); |
254 | x = xx; | |
df0b8ff4 | 255 | } else if ((codex & 0x08) != 0) { // left |
73005152 BH |
256 | y = y1 + ((y2 - y1) * (x_ - x1)) / (x2 - x1); |
257 | x = x_; | |
258 | } | |
259 | ||
260 | if (codex == code1) { | |
261 | x1 = x; | |
262 | y1 = y; | |
263 | code1 = code(x1, y1); | |
264 | } else { | |
265 | x2 = x; | |
266 | y2 = y; | |
267 | code2 = code(x2, y2); | |
268 | } | |
269 | } | |
270 | } while (!end); | |
df0b8ff4 | 271 | |
73005152 BH |
272 | if (draw) { |
273 | context.drawLine(x1, y1, x2, y2); | |
274 | } | |
275 | } | |
276 | ||
df0b8ff4 BH |
277 | /* |
278 | * (non-Javadoc) | |
279 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawRectangle(int, int, int, int) | |
280 | */ | |
73005152 BH |
281 | @Override |
282 | public void drawRectangle(int x, int y, int width, int height) { | |
283 | x = Math.round(x * view.zoomValue); | |
284 | // Workaround to avoid problems for some special cases (not very nice) | |
285 | if (y != getContentsY()) { | |
286 | y = Math.round(y * view.zoomValue); | |
287 | y = view.contentsToViewY(y); | |
288 | } else | |
289 | y = 0; | |
290 | width = Math.round(width * view.zoomValue); | |
291 | height = Math.round(height * view.zoomValue); | |
292 | x = view.contentsToViewX(x); | |
293 | ||
294 | if (x < -vscreen_bounds) { | |
295 | width = width + x + vscreen_bounds; | |
296 | x = -vscreen_bounds; | |
297 | } | |
298 | if (y < -vscreen_bounds) { | |
299 | height = height + y + vscreen_bounds; | |
300 | y = -vscreen_bounds; | |
301 | } | |
df0b8ff4 | 302 | if ((width < -vscreen_bounds) && (x + width < -vscreen_bounds)) { |
73005152 | 303 | width = -vscreen_bounds; |
df0b8ff4 | 304 | } else if (width + x > view.getVisibleWidth() + vscreen_bounds) { |
73005152 | 305 | width = view.getVisibleWidth() + vscreen_bounds - x; |
df0b8ff4 BH |
306 | } |
307 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) { | |
73005152 | 308 | height = -vscreen_bounds; |
df0b8ff4 | 309 | } else if (height + y > view.getVisibleHeight() + vscreen_bounds) { |
73005152 | 310 | height = view.getVisibleHeight() + vscreen_bounds - y; |
df0b8ff4 | 311 | } |
73005152 BH |
312 | context.drawRectangle(x, y, width, height); |
313 | } | |
314 | ||
df0b8ff4 BH |
315 | /* |
316 | * (non-Javadoc) | |
317 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawFocus(int, int, int, int) | |
318 | */ | |
73005152 BH |
319 | @Override |
320 | public void drawFocus(int x, int y, int width, int height) { | |
321 | IColor bC = getBackground(); | |
322 | IColor fC = getForeground(); | |
323 | ||
324 | if (width < 0) { | |
325 | x = x + width; | |
326 | width = -width; | |
327 | } | |
328 | ||
329 | if (height < 0) { | |
330 | y = y + height; | |
331 | height = -height; | |
332 | } | |
333 | ||
334 | x = Math.round(x * view.zoomValue); | |
335 | y = Math.round(y * view.zoomValue); | |
336 | width = Math.round(width * view.zoomValue); | |
337 | height = Math.round(height * view.zoomValue); | |
338 | ||
339 | setForeground(Frame.getUserPref().getForeGroundColorSelection()); | |
340 | setBackground(Frame.getUserPref().getBackGroundColorSelection()); | |
341 | ||
342 | context.drawFocus(view.contentsToViewX(x - 1), view.contentsToViewY(y - 1), width + 3, height + 3); | |
343 | ||
344 | setBackground(bC); | |
345 | setForeground(fC); | |
346 | } | |
347 | ||
df0b8ff4 BH |
348 | /* |
349 | * (non-Javadoc) | |
350 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#fillPolygon(int[]) | |
351 | */ | |
73005152 BH |
352 | @Override |
353 | public void fillPolygon(int[] points) { | |
354 | int len = (points.length / 2) * 2; | |
355 | int[] localPoint = new int[len]; | |
356 | for (int i = 0; i < len; i++) { | |
357 | localPoint[i] = view.contentsToViewX(Math.round(points[i] * view.zoomValue)); | |
358 | i++; | |
359 | localPoint[i] = view.contentsToViewY(Math.round(points[i] * view.zoomValue)); | |
360 | } | |
361 | ||
362 | if (validatePolygonHeight(localPoint) <= 0) | |
363 | return; | |
364 | ||
365 | context.fillPolygon(localPoint); | |
366 | } | |
367 | ||
df0b8ff4 BH |
368 | /* |
369 | * (non-Javadoc) | |
370 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawPolygon(int[]) | |
371 | */ | |
73005152 BH |
372 | @Override |
373 | public void drawPolygon(int[] points) { | |
374 | int len = (points.length / 2) * 2; | |
375 | int[] localPoint = new int[len]; | |
376 | for (int i = 0; i < len; i++) { | |
377 | localPoint[i] = view.contentsToViewX(Math.round(points[i] * view.zoomValue)); | |
378 | i++; | |
379 | localPoint[i] = view.contentsToViewY(Math.round(points[i] * view.zoomValue)); | |
380 | } | |
381 | ||
df0b8ff4 | 382 | if (validatePolygonHeight(localPoint) <= 0) { |
73005152 | 383 | return; |
df0b8ff4 | 384 | } |
73005152 BH |
385 | |
386 | context.drawPolygon(localPoint); | |
387 | } | |
388 | ||
df0b8ff4 BH |
389 | /* |
390 | * (non-Javadoc) | |
391 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#fillRectangle(int, int, int, int) | |
392 | */ | |
73005152 BH |
393 | @Override |
394 | public void fillRectangle(int x, int y, int width, int height) { | |
395 | x = Math.round(x * view.zoomValue); | |
396 | // Workaround to avoid problems for some special cases (not very nice) | |
397 | if (y != getContentsY()) { | |
398 | y = Math.round(y * view.zoomValue); | |
399 | y = view.contentsToViewY(y) + 1; | |
df0b8ff4 | 400 | } else { |
73005152 | 401 | y = 1; |
df0b8ff4 | 402 | } |
73005152 BH |
403 | width = Math.round(width * view.zoomValue) - 1; |
404 | height = Math.round(height * view.zoomValue) - 1; | |
405 | x = view.contentsToViewX(x) + 1; | |
406 | if (x < -vscreen_bounds) { | |
407 | width = width + x + vscreen_bounds; | |
408 | x = -vscreen_bounds; | |
409 | } | |
410 | if (y < -vscreen_bounds) { | |
411 | height = height + y + vscreen_bounds; | |
412 | y = -vscreen_bounds; | |
413 | } | |
df0b8ff4 | 414 | if ((width < -vscreen_bounds) && (x + width < -vscreen_bounds)) { |
73005152 | 415 | width = -vscreen_bounds; |
df0b8ff4 | 416 | } else if (width + x > view.getVisibleWidth() + vscreen_bounds) { |
73005152 | 417 | width = view.getVisibleWidth() + vscreen_bounds - x; |
df0b8ff4 BH |
418 | } |
419 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) { | |
73005152 | 420 | height = -vscreen_bounds; |
df0b8ff4 | 421 | } else if (height + y > view.getVisibleHeight() + vscreen_bounds) { |
73005152 | 422 | height = view.getVisibleHeight() + vscreen_bounds - y; |
df0b8ff4 | 423 | } |
73005152 BH |
424 | context.fillRectangle(x, y, width, height); |
425 | ||
426 | } | |
427 | ||
df0b8ff4 BH |
428 | /* |
429 | * (non-Javadoc) | |
430 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#fillGradientRectangle(int, int, int, int, boolean) | |
431 | */ | |
73005152 BH |
432 | @Override |
433 | public void fillGradientRectangle(int x, int y, int width, int height, boolean vertical) { | |
434 | x = Math.round(x * view.zoomValue); | |
435 | y = Math.round(y * view.zoomValue); | |
436 | width = Math.round(width * view.zoomValue); | |
437 | height = Math.round(height * view.zoomValue); | |
438 | IColor tempColor = foreGround; | |
439 | setForeground(gradientColor); | |
440 | x = view.contentsToViewX(x); | |
441 | y = view.contentsToViewY(y); | |
442 | ||
443 | if (x < -vscreen_bounds) { | |
444 | width = width + x + vscreen_bounds; | |
445 | x = -vscreen_bounds; | |
446 | } | |
447 | if (y < -vscreen_bounds) { | |
448 | height = height + y + vscreen_bounds; | |
449 | y = -vscreen_bounds; | |
450 | } | |
df0b8ff4 BH |
451 | |
452 | if ((width < -vscreen_bounds) && (x + width < -vscreen_bounds)) { | |
73005152 | 453 | width = -vscreen_bounds; |
df0b8ff4 | 454 | } else if (width + x > view.getVisibleWidth() + vscreen_bounds) { |
73005152 | 455 | width = view.getVisibleWidth() + vscreen_bounds - x; |
df0b8ff4 BH |
456 | } |
457 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) { | |
73005152 | 458 | height = -vscreen_bounds; |
df0b8ff4 | 459 | } else if (height + y > view.getVisibleHeight() + vscreen_bounds) { |
73005152 | 460 | height = view.getVisibleHeight() + vscreen_bounds - y; |
df0b8ff4 | 461 | } |
73005152 BH |
462 | if (vertical) { |
463 | context.fillGradientRectangle(x, y, width, height, vertical); | |
464 | } | |
465 | else { | |
466 | context.fillGradientRectangle(x + width, y, -width, height + 1, vertical); | |
467 | } | |
468 | setForeground(tempColor); | |
469 | } | |
470 | ||
471 | ||
df0b8ff4 BH |
472 | /* |
473 | * (non-Javadoc) | |
474 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#textExtent(java.lang.String) | |
475 | */ | |
73005152 BH |
476 | @Override |
477 | public int textExtent(String name) { | |
478 | return ((Point) (context.textExtent(name))).x; | |
479 | } | |
480 | ||
df0b8ff4 BH |
481 | /* |
482 | * (non-Javadoc) | |
483 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawText(java.lang.String, int, int, boolean) | |
484 | */ | |
73005152 BH |
485 | @Override |
486 | public void drawText(String string, int x, int y, boolean trans) { | |
487 | x = Math.round(x * view.zoomValue); | |
488 | y = Math.round(y * view.zoomValue); | |
489 | context.drawText(string, view.contentsToViewX(x), view.contentsToViewY(y), trans); | |
490 | if (drawWithFocus) { | |
491 | Point r = context.textExtent(string); | |
492 | context.drawFocus(x - 1, y - 1, r.x + 2, r.y + 2); | |
493 | } | |
494 | } | |
495 | ||
df0b8ff4 BH |
496 | /* |
497 | * (non-Javadoc) | |
498 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawText(java.lang.String, int, int) | |
499 | */ | |
73005152 BH |
500 | @Override |
501 | public void drawText(String string, int x, int y) { | |
502 | x = Math.round(x * view.zoomValue); | |
503 | y = Math.round(y * view.zoomValue); | |
504 | context.drawText(string, view.contentsToViewX(x), view.contentsToViewY(y), true); | |
505 | if (drawWithFocus) { | |
506 | Point r = context.textExtent(string); | |
507 | context.drawFocus(x - 1, y - 1, r.x + 2, r.y + 2); | |
508 | } | |
509 | } | |
510 | ||
df0b8ff4 BH |
511 | /* |
512 | * (non-Javadoc) | |
513 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#fillOval(int, int, int, int) | |
514 | */ | |
73005152 BH |
515 | @Override |
516 | public void fillOval(int x, int y, int width, int height) { | |
517 | x = Math.round(x * view.zoomValue); | |
518 | y = Math.round(y * view.zoomValue); | |
519 | width = Math.round(width * view.zoomValue); | |
520 | height = Math.round(height * view.zoomValue); | |
521 | context.fillOval(view.contentsToViewX(x), view.contentsToViewY(y), width, height); | |
522 | } | |
523 | ||
df0b8ff4 BH |
524 | /* |
525 | * (non-Javadoc) | |
526 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getBackground() | |
527 | */ | |
73005152 BH |
528 | @Override |
529 | public IColor getBackground() { | |
df0b8ff4 BH |
530 | if ((backGround != null) && (backGround.getColor() instanceof Color) && (!((Color) (backGround.getColor())).isDisposed())) { |
531 | return backGround; | |
532 | } | |
73005152 BH |
533 | return ColorImpl.getSystemColor(SWT.COLOR_WHITE); |
534 | } | |
535 | ||
df0b8ff4 BH |
536 | /* |
537 | * (non-Javadoc) | |
538 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getForeground() | |
539 | */ | |
73005152 BH |
540 | @Override |
541 | public IColor getForeground() { | |
df0b8ff4 BH |
542 | if ((foreGround != null) && (foreGround.getColor() instanceof Color) && (!((Color) (foreGround.getColor())).isDisposed())) { |
543 | return foreGround; | |
544 | } | |
73005152 BH |
545 | return ColorImpl.getSystemColor(SWT.COLOR_WHITE); |
546 | } | |
547 | ||
df0b8ff4 BH |
548 | /* |
549 | * (non-Javadoc) | |
550 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setBackground(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor) | |
551 | */ | |
73005152 BH |
552 | @Override |
553 | public void setBackground(IColor color) { | |
df0b8ff4 | 554 | if (color == null) { |
73005152 | 555 | return; |
df0b8ff4 | 556 | } |
73005152 BH |
557 | if (color.getColor() instanceof Color) { |
558 | context.setBackground((Color) color.getColor()); | |
559 | backGround = color; | |
560 | } | |
561 | } | |
562 | ||
df0b8ff4 BH |
563 | /* |
564 | * (non-Javadoc) | |
565 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setForeground(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor) | |
566 | */ | |
73005152 BH |
567 | @Override |
568 | public void setForeground(IColor color) { | |
df0b8ff4 | 569 | if (color == null) { |
73005152 | 570 | return; |
df0b8ff4 | 571 | } |
73005152 BH |
572 | if (color.getColor() instanceof Color) { |
573 | Color c = (Color) color.getColor(); | |
574 | if (!c.isDisposed()) { | |
575 | context.setForeground(c); | |
576 | foreGround = color; | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
df0b8ff4 BH |
581 | /* |
582 | * (non-Javadoc) | |
583 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setGradientColor(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor) | |
584 | */ | |
73005152 BH |
585 | @Override |
586 | public void setGradientColor(IColor color) { | |
df0b8ff4 | 587 | if (color == null) { |
73005152 | 588 | return; |
df0b8ff4 BH |
589 | } |
590 | if (color.getColor() instanceof Color) { | |
73005152 | 591 | gradientColor = color; |
df0b8ff4 | 592 | } |
73005152 BH |
593 | } |
594 | ||
df0b8ff4 BH |
595 | /* |
596 | * (non-Javadoc) | |
597 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setLineWidth(int) | |
598 | */ | |
73005152 BH |
599 | @Override |
600 | public void setLineWidth(int width) { | |
df0b8ff4 | 601 | if (view.isPrinting()) { |
73005152 | 602 | context.setLineWidth(width * 2); |
df0b8ff4 BH |
603 | } |
604 | else { | |
73005152 | 605 | context.setLineWidth(width); |
df0b8ff4 | 606 | } |
73005152 BH |
607 | } |
608 | ||
df0b8ff4 BH |
609 | /* |
610 | * (non-Javadoc) | |
611 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getLineWidth() | |
612 | */ | |
73005152 BH |
613 | @Override |
614 | public int getLineWidth() { | |
615 | return context.getLineWidth(); | |
616 | } | |
617 | ||
df0b8ff4 BH |
618 | /** |
619 | * Method to draw a text in rectangle. (Linux GTK Workaround) | |
620 | * | |
621 | * @param string The text to draw. | |
622 | * @param x The x position. | |
623 | * @param y The y position. | |
624 | * @param isTransparent true for transparent else false | |
625 | */ | |
626 | protected void localDrawText(String string, int x, int y, boolean isTransparent) { | |
73005152 | 627 | Point r = context.textExtent(string); |
df0b8ff4 | 628 | if (!isTransparent) { |
73005152 | 629 | context.fillRectangle(x, y, r.x, r.y); |
df0b8ff4 BH |
630 | } |
631 | context.drawText(string, x, y, isTransparent); | |
632 | if ((drawWithFocus) && (string.length() > 1)) { | |
73005152 | 633 | context.drawFocus(x - 1, y - 1, r.x + 2, r.y + 2); |
df0b8ff4 | 634 | } |
73005152 BH |
635 | } |
636 | ||
df0b8ff4 BH |
637 | /* |
638 | * (non-Javadoc) | |
639 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawTextTruncatedCentred(java.lang.String, int, int, int, int, boolean) | |
640 | */ | |
73005152 BH |
641 | @Override |
642 | public void drawTextTruncatedCentred(String name, int _x, int _y, int width, int height, boolean trans) { | |
643 | Point tx = context.textExtent(name); | |
644 | _x = Math.round(_x * view.zoomValue); | |
645 | int y = 0; | |
646 | // Workaround to avoid round problems for some special cases (not very nice) | |
647 | if (_y != getContentsY()) { | |
648 | _y = Math.round(_y * view.zoomValue); | |
649 | y = view.contentsToViewY(_y); | |
650 | } | |
651 | width = Math.round(width * view.zoomValue); | |
652 | height = Math.round(height * view.zoomValue); | |
653 | int x = view.contentsToViewX(_x); | |
df0b8ff4 | 654 | if (tx.y > height) { |
73005152 | 655 | return; |
df0b8ff4 | 656 | } |
73005152 BH |
657 | |
658 | // Adjust height and y | |
659 | if (y < -vscreen_bounds) { | |
660 | height = height + y + vscreen_bounds; | |
661 | y = -vscreen_bounds; | |
662 | } | |
df0b8ff4 | 663 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) { |
73005152 | 664 | height = -vscreen_bounds; |
df0b8ff4 | 665 | } else if (height + y > view.getVisibleHeight() + vscreen_bounds) { |
73005152 | 666 | height = view.getVisibleHeight() + vscreen_bounds - y; |
df0b8ff4 | 667 | } |
73005152 BH |
668 | |
669 | if (tx.x <= width) { | |
670 | localDrawText(name, x + 1 + (width - tx.x) / 2, y + 1 + (height - tx.y) / 2, trans); | |
671 | } else { | |
672 | String nameToDisplay = name; | |
df0b8ff4 | 673 | for (int i = name.length() - 1; i >= 0 && context.textExtent(nameToDisplay).x >= width; i--) { |
73005152 | 674 | nameToDisplay = name.substring(0, i); |
df0b8ff4 | 675 | } |
73005152 | 676 | int dotCount = 0; |
df0b8ff4 | 677 | for (int i = 1; i <= 3 && nameToDisplay.length() - i > 0; i++) { |
73005152 | 678 | dotCount++; |
df0b8ff4 | 679 | } |
73005152 BH |
680 | nameToDisplay = nameToDisplay.substring(0, nameToDisplay.length() - dotCount); |
681 | StringBuffer buf = new StringBuffer(nameToDisplay); | |
682 | for (int i = 0; i < dotCount; i++) { | |
683 | buf.append("."); //$NON-NLS-1$ | |
73005152 BH |
684 | } |
685 | nameToDisplay = buf.toString(); | |
686 | localDrawText(nameToDisplay, x + 1 + (width - context.textExtent(nameToDisplay).x) / 2, y + 1 + (height - context.textExtent(nameToDisplay).y) / 2, trans); | |
687 | } | |
688 | } | |
689 | ||
df0b8ff4 BH |
690 | /* |
691 | * (non-Javadoc) | |
692 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawTextTruncated(java.lang.String, int, int, int, int, boolean) | |
693 | */ | |
73005152 BH |
694 | @Override |
695 | public void drawTextTruncated(String name, int _x, int _y, int width, int height, boolean trans) { | |
696 | _x = Math.round(_x * view.zoomValue); | |
697 | _y = Math.round(_y * view.zoomValue); | |
698 | width = Math.round(width * view.zoomValue); | |
699 | height = Math.round(height * view.zoomValue); | |
700 | int x = view.contentsToViewX(_x); | |
701 | int y = view.contentsToViewY(_y); | |
702 | if (context.textExtent(name).x <= width) { | |
703 | localDrawText(name, x + 1, y + 1 + height, trans); | |
704 | } else { | |
705 | String nameToDisplay = name; | |
df0b8ff4 | 706 | for (int i = name.length() - 1; i >= 0 && context.textExtent(nameToDisplay).x >= width; i--) { |
73005152 | 707 | nameToDisplay = name.substring(0, i); |
df0b8ff4 | 708 | } |
73005152 BH |
709 | int dotCount = 0; |
710 | for (int i = 1; i <= 3 && nameToDisplay.length() - i > 0; i++) | |
711 | dotCount++; | |
712 | nameToDisplay = nameToDisplay.substring(0, nameToDisplay.length() - dotCount); | |
713 | ||
714 | StringBuffer buf = new StringBuffer(nameToDisplay); | |
715 | ||
716 | for (int i = 0; i < dotCount; i++) { | |
717 | buf.append("."); //$NON-NLS-1$ | |
73005152 BH |
718 | } |
719 | nameToDisplay = buf.toString(); | |
720 | localDrawText(nameToDisplay, x + 1, y + 1 + height, trans); | |
721 | } | |
722 | } | |
723 | ||
df0b8ff4 BH |
724 | /* |
725 | * (non-Javadoc) | |
726 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawImage(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage, int, int, int, int) | |
727 | */ | |
73005152 BH |
728 | @Override |
729 | public void drawImage(IImage image, int _x, int _y, int maxWith, int maxHeight) { | |
730 | Image img = null; | |
731 | if (image != null && image.getImage() instanceof Image) | |
732 | img = (Image) image.getImage(); | |
733 | else { | |
734 | _x = Math.round(_x * view.zoomValue); | |
735 | _y = Math.round(_y * view.zoomValue); | |
736 | int x = view.contentsToViewX(_x); | |
737 | int y = view.contentsToViewY(_y); | |
738 | float tempZoom = view.zoomValue; | |
739 | int width = Math.round(maxWith * tempZoom); | |
740 | int height = Math.round(maxHeight * tempZoom); | |
741 | context.setBackground(view.getDisplay().getSystemColor(SWT.COLOR_RED)); | |
742 | context.fillRectangle(x, y, width, height); | |
743 | return; | |
744 | } | |
745 | _x = Math.round(_x * view.zoomValue); | |
746 | _y = Math.round(_y * view.zoomValue); | |
747 | int x = view.contentsToViewX(_x); | |
748 | int y = view.contentsToViewY(_y); | |
749 | Rectangle b = ((Image) image.getImage()).getBounds(); | |
750 | int width = b.width; | |
751 | int height = b.height; | |
df0b8ff4 | 752 | if (width > maxWith) { |
73005152 | 753 | width = maxWith; |
df0b8ff4 BH |
754 | } |
755 | if (height > maxHeight) { | |
73005152 | 756 | height = maxHeight; |
df0b8ff4 | 757 | } |
73005152 BH |
758 | float tempZoom = view.zoomValue; |
759 | width = Math.round(width * tempZoom); | |
760 | height = Math.round(height * tempZoom); | |
761 | ||
762 | if (view.printing && width > 0 && height > 0) { | |
763 | Image dbuffer = new Image(view.getDisplay(), width, height); | |
764 | GC tempgc = new GC(dbuffer); | |
765 | tempgc.drawImage(img, 0, 0, b.width, b.height, 0, 0, width, height); | |
766 | Image dbuffer2 = new Image(view.getDisplay(), dbuffer.getImageData()); | |
767 | context.drawImage(dbuffer2, x, y); | |
768 | tempgc.dispose(); | |
769 | dbuffer.dispose(); | |
770 | dbuffer2.dispose(); | |
771 | } else { | |
772 | context.drawImage(img, 0, 0, b.width, b.height, x, y, width, height); | |
773 | } | |
774 | } | |
775 | ||
df0b8ff4 BH |
776 | /* |
777 | * (non-Javadoc) | |
778 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#drawArc(int, int, int, int, int, int) | |
779 | */ | |
73005152 BH |
780 | @Override |
781 | public void drawArc(int x, int y, int width, int height, int startAngle, int endAngle) { | |
782 | x = Math.round(x * view.zoomValue); | |
783 | y = Math.round(y * view.zoomValue); | |
784 | width = Math.round(width * view.zoomValue); | |
785 | height = Math.round(height * view.zoomValue); | |
df0b8ff4 | 786 | if (width == 0 || height == 0 || endAngle == 0) { |
73005152 | 787 | return; |
df0b8ff4 | 788 | } |
73005152 BH |
789 | context.drawArc(view.contentsToViewX(x), view.contentsToViewY(y), width, height, startAngle, endAngle); |
790 | } | |
791 | ||
df0b8ff4 BH |
792 | /* |
793 | * (non-Javadoc) | |
794 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setFont(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IFont) | |
795 | */ | |
73005152 BH |
796 | @Override |
797 | public void setFont(IFont font) { | |
798 | if (font.getFont() != null && ((Font) font.getFont()).getFontData().length > 0) { | |
799 | FontData fontData = ((Font) font.getFont()).getFontData()[0]; | |
800 | if (SDViewPref.getInstance().fontLinked() || view.printing) { | |
801 | int h = Math.round(fontData.getHeight() * view.zoomValue); | |
df0b8ff4 | 802 | if (h > 0) { |
73005152 | 803 | fontData.setHeight(h); |
df0b8ff4 | 804 | } |
73005152 | 805 | } |
df0b8ff4 | 806 | if (tempFont != null) { |
73005152 | 807 | tempFont.dispose(); |
df0b8ff4 | 808 | } |
73005152 BH |
809 | tempFont = new Font(Display.getCurrent(), fontData); |
810 | context.setFont(tempFont); | |
811 | } | |
812 | } | |
813 | ||
df0b8ff4 BH |
814 | /* |
815 | * (non-Javadoc) | |
816 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getFontHeight(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IFont) | |
817 | */ | |
73005152 BH |
818 | @Override |
819 | public int getFontHeight(IFont font) { | |
820 | if (font.getFont() != null && (font.getFont() instanceof Font) && ((Font) font.getFont()).getFontData().length > 0) { | |
821 | Font toRestore = context.getFont(); | |
822 | context.setFont((Font) font.getFont()); | |
823 | int height = context.textExtent("lp").y;//$NON-NLS-1$ | |
824 | context.setFont(toRestore); | |
825 | return height; | |
826 | } | |
827 | return 0; | |
828 | } | |
829 | ||
df0b8ff4 BH |
830 | /** |
831 | * Returns the current font height. | |
832 | * | |
833 | * @return the current font height. | |
834 | */ | |
73005152 BH |
835 | protected int getCurrentFontHeight() { |
836 | return context.textExtent("lp").y; //$NON-NLS-1$ | |
837 | } | |
838 | ||
df0b8ff4 BH |
839 | /* |
840 | * (non-Javadoc) | |
841 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getFontWidth(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IFont) | |
842 | */ | |
73005152 BH |
843 | @Override |
844 | public int getFontWidth(IFont font) { | |
845 | if ((font.getFont() != null) && (font.getFont() instanceof Font)) { | |
846 | Font toRestore = context.getFont(); | |
847 | context.setFont((Font) font.getFont()); | |
848 | int width = context.getFontMetrics().getAverageCharWidth(); | |
849 | context.setFont(toRestore); | |
850 | return width; | |
851 | } | |
852 | return 0; | |
853 | } | |
854 | ||
df0b8ff4 BH |
855 | /** |
856 | * Disposes all created resources. | |
857 | */ | |
73005152 | 858 | public void dispose() { |
df0b8ff4 | 859 | if (tempFont != null) { |
73005152 | 860 | tempFont.dispose(); |
df0b8ff4 | 861 | } |
73005152 | 862 | tempFont = null; |
df0b8ff4 | 863 | if (context != null) { |
73005152 | 864 | context.dispose(); |
df0b8ff4 | 865 | } |
73005152 BH |
866 | context = null; |
867 | } | |
868 | ||
df0b8ff4 BH |
869 | /* |
870 | * (non-Javadoc) | |
871 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getZoom() | |
872 | */ | |
73005152 BH |
873 | @Override |
874 | public float getZoom() { | |
df0b8ff4 | 875 | if (view != null) { |
73005152 | 876 | return view.zoomValue; |
df0b8ff4 | 877 | } else { |
73005152 | 878 | return 1; |
df0b8ff4 | 879 | } |
73005152 BH |
880 | } |
881 | ||
df0b8ff4 BH |
882 | /* |
883 | * (non-Javadoc) | |
884 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getLineDotStyle() | |
885 | */ | |
73005152 BH |
886 | @Override |
887 | public int getLineDotStyle() { | |
888 | return SWT.LINE_DOT; | |
889 | } | |
890 | ||
df0b8ff4 BH |
891 | /* |
892 | * (non-Javadoc) | |
893 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getLineDashStyle() | |
894 | */ | |
73005152 BH |
895 | @Override |
896 | public int getLineDashStyle() { | |
897 | return SWT.LINE_DASH; | |
898 | } | |
899 | ||
df0b8ff4 BH |
900 | /* |
901 | * (non-Javadoc) | |
902 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#getLineSolidStyle() | |
903 | */ | |
73005152 BH |
904 | @Override |
905 | public int getLineSolidStyle() { | |
906 | return SWT.LINE_SOLID; | |
907 | } | |
908 | ||
df0b8ff4 BH |
909 | /* |
910 | * (non-Javadoc) | |
911 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#createColor(int, int, int) | |
912 | */ | |
73005152 BH |
913 | @Override |
914 | public IColor createColor(int r, int g, int b) { | |
915 | return new ColorImpl(Display.getDefault(), r, g, b); | |
916 | } | |
917 | ||
918 | /* | |
919 | * (non-Javadoc) | |
920 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setDrawTextWithFocusStyle(boolean) | |
921 | */ | |
922 | @Override | |
923 | public void setDrawTextWithFocusStyle(boolean focus) { | |
924 | drawWithFocus = focus; | |
925 | } | |
e6ace8bb | 926 | |
df0b8ff4 BH |
927 | /** |
928 | * Returns the screen bounds. | |
929 | * | |
930 | * @return the screen bounds. | |
931 | */ | |
e6ace8bb BH |
932 | protected static int getVscreenBounds() { |
933 | return vscreen_bounds; | |
934 | } | |
935 | ||
df0b8ff4 BH |
936 | /** |
937 | * Sets the visible screen bounds. | |
938 | * | |
939 | * @param vBounds the screen bounds. | |
940 | */ | |
e6ace8bb BH |
941 | protected static void setVscreenBounds(int vBounds) { |
942 | vscreen_bounds = vBounds; | |
943 | } | |
df0b8ff4 BH |
944 | |
945 | // ------------------------------------------------------------------------ | |
946 | // Helper methods | |
947 | // ------------------------------------------------------------------------ | |
948 | ||
949 | /** | |
950 | * Validates the polygon height | |
951 | * | |
952 | * @param localPoint array of points | |
953 | * @return height | |
954 | */ | |
955 | private int validatePolygonHeight(int[] localPoint) { | |
956 | int i = 1; | |
957 | int max = 0; | |
958 | int min = Integer.MAX_VALUE; | |
959 | while (i < localPoint.length) { | |
960 | max = Math.abs(localPoint[i]) > Math.abs(max) ? localPoint[i] : max; | |
961 | min = Math.abs(localPoint[i]) < Math.abs(min) ? localPoint[i] : min; | |
962 | i+=2; | |
963 | } | |
964 | int height = max - min; | |
965 | if (min < -vscreen_bounds) { | |
966 | height = height + min + vscreen_bounds; | |
967 | min = -vscreen_bounds; | |
968 | } | |
969 | if ((height < -vscreen_bounds) && (min + height < -vscreen_bounds)) { | |
970 | height = -vscreen_bounds; | |
971 | } else if (height + min > view.getVisibleHeight() + vscreen_bounds) { | |
972 | height = view.getVisibleHeight() + vscreen_bounds - min; | |
973 | } | |
974 | return height; | |
975 | } | |
73005152 | 976 | } |