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