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