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