1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
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 * $Id: NGC.java,v 1.3 2008/01/24 02:29:01 apnan Exp $
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
;
15 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.core
.Frame
;
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
;
36 public class NGC
implements IGC
{
39 protected SDWidget view
;
40 protected Font tempFont
= null;
41 protected IColor gradientColor
= null;
42 protected IColor backGround
= null;
43 protected IColor foreGround
= null;
48 protected boolean drawWithFocus
= false;
50 private static int vscreen_bounds
= 0;
52 public NGC(SDWidget scrollView
, GC gc
) {
58 public void setLineStyle(int style
) {
59 context
.setLineStyle(style
);
63 public int getLineStyle() {
64 return context
.getLineStyle();
68 public int getContentsX() {
69 return Math
.round(view
.getContentsX() / view
.zoomValue
);
73 public int getContentsY() {
74 return Math
.round(view
.getContentsY() / view
.zoomValue
);
78 public int getVisibleWidth() {
79 return Math
.round(view
.getVisibleWidth() / view
.zoomValue
);
83 public int getVisibleHeight() {
84 return Math
.round(view
.getVisibleHeight() / view
.zoomValue
);
88 public int contentsToViewX(int x
) {
89 return view
.contentsToViewX(x
);
93 public int contentsToViewY(int y
) {
94 return view
.contentsToViewY(y
);
97 protected byte code(int x
, int y
) {
101 yx
= view
.getVisibleHeight() + vscreen_bounds
;
102 xx
= view
.getVisibleWidth() + vscreen_bounds
;
115 public void drawLine(int x1
, int y1
, int x2
, int y2
) {
116 x1
= Math
.round(x1
* view
.zoomValue
);
117 y1
= Math
.round(y1
* view
.zoomValue
);
118 x2
= Math
.round(x2
* view
.zoomValue
);
119 y2
= Math
.round(y2
* view
.zoomValue
);
120 x1
= view
.contentsToViewX(x1
);
121 y1
= view
.contentsToViewY(y1
);
122 x2
= view
.contentsToViewX(x2
);
123 y2
= view
.contentsToViewY(y2
);
125 byte code1
= code(x1
, y1
);
126 byte code2
= code(x2
, y2
);
128 boolean draw
= false;
133 if (code1
== 0 && code2
== 0) {
136 } else if ((code1
& code2
) != 0) {
139 codex
= (code1
!= 0) ? code1
: code2
;
140 if ((codex
& 0x01) != 0) // top
142 x
= x1
+ ((x2
- x1
) * (yx
- y1
)) / (y2
- y1
);
144 } else if ((codex
& 0x02) != 0) // bottom
146 x
= x1
+ ((x2
- x1
) * (y_
- y1
)) / (y2
- y1
);
148 } else if ((codex
& 0x04) != 0) // right
150 y
= y1
+ ((y2
- y1
) * (xx
- x1
)) / (x2
- x1
);
152 } else if ((codex
& 0x08) != 0) // left
154 y
= y1
+ ((y2
- y1
) * (x_
- x1
)) / (x2
- x1
);
158 if (codex
== code1
) {
161 code1
= code(x1
, y1
);
165 code2
= code(x2
, y2
);
170 context
.drawLine(x1
, y1
, x2
, y2
);
175 public void drawRectangle(int x
, int y
, int width
, int height
) {
176 x
= Math
.round(x
* view
.zoomValue
);
177 // Workaround to avoid problems for some special cases (not very nice)
178 if (y
!= getContentsY()) {
179 y
= Math
.round(y
* view
.zoomValue
);
180 y
= view
.contentsToViewY(y
);
183 width
= Math
.round(width
* view
.zoomValue
);
184 height
= Math
.round(height
* view
.zoomValue
);
185 x
= view
.contentsToViewX(x
);
187 if (x
< -vscreen_bounds
) {
188 width
= width
+ x
+ vscreen_bounds
;
191 if (y
< -vscreen_bounds
) {
192 height
= height
+ y
+ vscreen_bounds
;
195 if ((width
< -vscreen_bounds
) && (x
+ width
< -vscreen_bounds
))
196 width
= -vscreen_bounds
;
197 else if (width
+ x
> view
.getVisibleWidth() + vscreen_bounds
)
198 width
= view
.getVisibleWidth() + vscreen_bounds
- x
;
199 if ((height
< -vscreen_bounds
) && (y
+ height
< -vscreen_bounds
))
200 height
= -vscreen_bounds
;
201 else if (height
+ y
> view
.getVisibleHeight() + vscreen_bounds
)
202 height
= view
.getVisibleHeight() + vscreen_bounds
- y
;
203 context
.drawRectangle(x
, y
, width
, height
);
207 public void drawFocus(int x
, int y
, int width
, int height
) {
208 IColor bC
= getBackground();
209 IColor fC
= getForeground();
221 x
= Math
.round(x
* view
.zoomValue
);
222 y
= Math
.round(y
* view
.zoomValue
);
223 width
= Math
.round(width
* view
.zoomValue
);
224 height
= Math
.round(height
* view
.zoomValue
);
226 setForeground(Frame
.getUserPref().getForeGroundColorSelection());
227 setBackground(Frame
.getUserPref().getBackGroundColorSelection());
229 context
.drawFocus(view
.contentsToViewX(x
- 1), view
.contentsToViewY(y
- 1), width
+ 3, height
+ 3);
236 public void fillPolygon(int[] points
) {
237 int len
= (points
.length
/ 2) * 2;
238 int[] localPoint
= new int[len
];
239 for (int i
= 0; i
< len
; i
++) {
240 localPoint
[i
] = view
.contentsToViewX(Math
.round(points
[i
] * view
.zoomValue
));
242 localPoint
[i
] = view
.contentsToViewY(Math
.round(points
[i
] * view
.zoomValue
));
245 if (validatePolygonHeight(localPoint
) <= 0)
248 context
.fillPolygon(localPoint
);
252 public void drawPolygon(int[] points
) {
253 int len
= (points
.length
/ 2) * 2;
254 int[] localPoint
= new int[len
];
255 for (int i
= 0; i
< len
; i
++) {
256 localPoint
[i
] = view
.contentsToViewX(Math
.round(points
[i
] * view
.zoomValue
));
258 localPoint
[i
] = view
.contentsToViewY(Math
.round(points
[i
] * view
.zoomValue
));
261 if (validatePolygonHeight(localPoint
) <= 0)
264 context
.drawPolygon(localPoint
);
267 private int validatePolygonHeight(int[] localPoint
) {
270 int min
= Integer
.MAX_VALUE
;
271 while (i
< localPoint
.length
) {
272 max
= Math
.abs(localPoint
[i
]) > Math
.abs(max
) ? localPoint
[i
] : max
;
273 min
= Math
.abs(localPoint
[i
]) < Math
.abs(min
) ? localPoint
[i
] : min
;
276 int height
= max
- min
;
277 if (min
< -vscreen_bounds
) {
278 height
= height
+ min
+ vscreen_bounds
;
279 min
= -vscreen_bounds
;
281 if ((height
< -vscreen_bounds
) && (min
+ height
< -vscreen_bounds
))
282 height
= -vscreen_bounds
;
284 else if (height
+ min
> view
.getVisibleHeight() + vscreen_bounds
)
285 height
= view
.getVisibleHeight() + vscreen_bounds
- min
;
290 public void fillRectangle(int x
, int y
, int width
, int height
) {
291 x
= Math
.round(x
* view
.zoomValue
);
292 // Workaround to avoid problems for some special cases (not very nice)
293 if (y
!= getContentsY()) {
294 y
= Math
.round(y
* view
.zoomValue
);
295 y
= view
.contentsToViewY(y
) + 1;
298 width
= Math
.round(width
* view
.zoomValue
) - 1;
299 height
= Math
.round(height
* view
.zoomValue
) - 1;
300 x
= view
.contentsToViewX(x
) + 1;
301 if (x
< -vscreen_bounds
) {
302 width
= width
+ x
+ vscreen_bounds
;
305 if (y
< -vscreen_bounds
) {
306 height
= height
+ y
+ vscreen_bounds
;
309 if ((width
< -vscreen_bounds
) && (x
+ width
< -vscreen_bounds
))
310 width
= -vscreen_bounds
;
311 else if (width
+ x
> view
.getVisibleWidth() + vscreen_bounds
)
312 width
= view
.getVisibleWidth() + vscreen_bounds
- x
;
313 if ((height
< -vscreen_bounds
) && (y
+ height
< -vscreen_bounds
))
314 height
= -vscreen_bounds
;
315 else if (height
+ y
> view
.getVisibleHeight() + vscreen_bounds
)
316 height
= view
.getVisibleHeight() + vscreen_bounds
- y
;
317 context
.fillRectangle(x
, y
, width
, height
);
322 public void fillGradientRectangle(int x
, int y
, int width
, int height
, boolean vertical
) {
323 x
= Math
.round(x
* view
.zoomValue
);
324 y
= Math
.round(y
* view
.zoomValue
);
325 width
= Math
.round(width
* view
.zoomValue
);
326 height
= Math
.round(height
* view
.zoomValue
);
327 IColor tempColor
= foreGround
;
328 setForeground(gradientColor
);
329 x
= view
.contentsToViewX(x
);
330 y
= view
.contentsToViewY(y
);
332 if (x
< -vscreen_bounds
) {
333 width
= width
+ x
+ vscreen_bounds
;
336 if (y
< -vscreen_bounds
) {
337 height
= height
+ y
+ vscreen_bounds
;
340 if ((width
< -vscreen_bounds
) && (x
+ width
< -vscreen_bounds
))
341 width
= -vscreen_bounds
;
342 else if (width
+ x
> view
.getVisibleWidth() + vscreen_bounds
)
343 width
= view
.getVisibleWidth() + vscreen_bounds
- x
;
344 if ((height
< -vscreen_bounds
) && (y
+ height
< -vscreen_bounds
))
345 height
= -vscreen_bounds
;
346 else if (height
+ y
> view
.getVisibleHeight() + vscreen_bounds
)
347 height
= view
.getVisibleHeight() + vscreen_bounds
- y
;
349 context
.fillGradientRectangle(x
, y
, width
, height
, vertical
);
352 context
.fillGradientRectangle(x
+ width
, y
, -width
, height
+ 1, vertical
);
354 setForeground(tempColor
);
359 public int textExtent(String name
) {
360 return ((Point
) (context
.textExtent(name
))).x
;
364 public void drawText(String string
, int x
, int y
, boolean trans
) {
365 x
= Math
.round(x
* view
.zoomValue
);
366 y
= Math
.round(y
* view
.zoomValue
);
367 context
.drawText(string
, view
.contentsToViewX(x
), view
.contentsToViewY(y
), trans
);
369 Point r
= context
.textExtent(string
);
370 context
.drawFocus(x
- 1, y
- 1, r
.x
+ 2, r
.y
+ 2);
375 public void drawText(String string
, int x
, int y
) {
376 x
= Math
.round(x
* view
.zoomValue
);
377 y
= Math
.round(y
* view
.zoomValue
);
378 context
.drawText(string
, view
.contentsToViewX(x
), view
.contentsToViewY(y
), true);
380 Point r
= context
.textExtent(string
);
381 context
.drawFocus(x
- 1, y
- 1, r
.x
+ 2, r
.y
+ 2);
386 public void fillOval(int x
, int y
, int width
, int height
) {
387 x
= Math
.round(x
* view
.zoomValue
);
388 y
= Math
.round(y
* view
.zoomValue
);
389 width
= Math
.round(width
* view
.zoomValue
);
390 height
= Math
.round(height
* view
.zoomValue
);
391 context
.fillOval(view
.contentsToViewX(x
), view
.contentsToViewY(y
), width
, height
);
395 public IColor
getBackground() {
396 if (backGround
!= null)
397 if (backGround
.getColor() instanceof Color
)
398 if (!((Color
) (backGround
.getColor())).isDisposed())
400 return ColorImpl
.getSystemColor(SWT
.COLOR_WHITE
);
404 public IColor
getForeground() {
405 if (foreGround
!= null)
406 if (foreGround
.getColor() instanceof Color
)
407 if (!((Color
) (foreGround
.getColor())).isDisposed())
409 return ColorImpl
.getSystemColor(SWT
.COLOR_WHITE
);
413 public void setBackground(IColor color
) {
416 if (color
.getColor() instanceof Color
) {
417 context
.setBackground((Color
) color
.getColor());
423 public void setForeground(IColor color
) {
426 if (color
.getColor() instanceof Color
) {
427 Color c
= (Color
) color
.getColor();
428 if (!c
.isDisposed()) {
429 context
.setForeground(c
);
436 public void setGradientColor(IColor color
) {
439 if (color
.getColor() instanceof Color
)
440 gradientColor
= color
;
444 public void setLineWidth(int width
) {
445 if (view
.isPrinting())
446 context
.setLineWidth(width
* 2);
448 context
.setLineWidth(width
);
452 public int getLineWidth() {
453 return context
.getLineWidth();
456 // Linux GTK Workaround
457 protected void localDrawText(String string
, int x
, int y
, boolean trans
) {
458 Point r
= context
.textExtent(string
);
460 context
.fillRectangle(x
, y
, r
.x
, r
.y
);
461 context
.drawText(string
, x
, y
, trans
);
462 if ((drawWithFocus
) && (string
.length() > 1))
463 context
.drawFocus(x
- 1, y
- 1, r
.x
+ 2, r
.y
+ 2);
467 public void drawTextTruncatedCentred(String name
, int _x
, int _y
, int width
, int height
, boolean trans
) {
468 Point tx
= context
.textExtent(name
);
469 _x
= Math
.round(_x
* view
.zoomValue
);
471 // Workaround to avoid round problems for some special cases (not very nice)
472 if (_y
!= getContentsY()) {
473 _y
= Math
.round(_y
* view
.zoomValue
);
474 y
= view
.contentsToViewY(_y
);
476 width
= Math
.round(width
* view
.zoomValue
);
477 height
= Math
.round(height
* view
.zoomValue
);
478 int x
= view
.contentsToViewX(_x
);
482 // Adjust height and y
483 if (y
< -vscreen_bounds
) {
484 height
= height
+ y
+ vscreen_bounds
;
487 if ((height
< -vscreen_bounds
) && (y
+ height
< -vscreen_bounds
))
488 height
= -vscreen_bounds
;
489 else if (height
+ y
> view
.getVisibleHeight() + vscreen_bounds
)
490 height
= view
.getVisibleHeight() + vscreen_bounds
- y
;
493 localDrawText(name
, x
+ 1 + (width
- tx
.x
) / 2, y
+ 1 + (height
- tx
.y
) / 2, trans
);
495 String nameToDisplay
= name
;
496 for (int i
= name
.length() - 1; i
>= 0 && context
.textExtent(nameToDisplay
).x
>= width
; i
--)
497 nameToDisplay
= name
.substring(0, i
);
499 for (int i
= 1; i
<= 3 && nameToDisplay
.length() - i
> 0; i
++)
501 nameToDisplay
= nameToDisplay
.substring(0, nameToDisplay
.length() - dotCount
);
502 StringBuffer buf
= new StringBuffer(nameToDisplay
);
503 for (int i
= 0; i
< dotCount
; i
++) {
504 buf
.append("."); //$NON-NLS-1$
505 // nameToDisplay = nameToDisplay + "."; //$NON-NLS-1$
507 nameToDisplay
= buf
.toString();
508 localDrawText(nameToDisplay
, x
+ 1 + (width
- context
.textExtent(nameToDisplay
).x
) / 2, y
+ 1 + (height
- context
.textExtent(nameToDisplay
).y
) / 2, trans
);
513 public void drawTextTruncated(String name
, int _x
, int _y
, int width
, int height
, boolean trans
) {
514 _x
= Math
.round(_x
* view
.zoomValue
);
515 _y
= Math
.round(_y
* view
.zoomValue
);
516 width
= Math
.round(width
* view
.zoomValue
);
517 height
= Math
.round(height
* view
.zoomValue
);
518 int x
= view
.contentsToViewX(_x
);
519 int y
= view
.contentsToViewY(_y
);
520 if (context
.textExtent(name
).x
<= width
) {
521 localDrawText(name
, x
+ 1, y
+ 1 + height
, trans
);
523 String nameToDisplay
= name
;
524 for (int i
= name
.length() - 1; i
>= 0 && context
.textExtent(nameToDisplay
).x
>= width
; i
--)
525 nameToDisplay
= name
.substring(0, i
);
527 for (int i
= 1; i
<= 3 && nameToDisplay
.length() - i
> 0; i
++)
529 nameToDisplay
= nameToDisplay
.substring(0, nameToDisplay
.length() - dotCount
);
531 StringBuffer buf
= new StringBuffer(nameToDisplay
);
533 for (int i
= 0; i
< dotCount
; i
++) {
534 buf
.append("."); //$NON-NLS-1$
535 // nameToDisplay = nameToDisplay + "."; //$NON-NLS-1$
537 nameToDisplay
= buf
.toString();
538 localDrawText(nameToDisplay
, x
+ 1, y
+ 1 + height
, trans
);
543 public void drawImage(IImage image
, int _x
, int _y
, int maxWith
, int maxHeight
) {
545 if (image
!= null && image
.getImage() instanceof Image
)
546 img
= (Image
) image
.getImage();
548 _x
= Math
.round(_x
* view
.zoomValue
);
549 _y
= Math
.round(_y
* view
.zoomValue
);
550 int x
= view
.contentsToViewX(_x
);
551 int y
= view
.contentsToViewY(_y
);
552 float tempZoom
= view
.zoomValue
;
553 int width
= Math
.round(maxWith
* tempZoom
);
554 int height
= Math
.round(maxHeight
* tempZoom
);
555 context
.setBackground(view
.getDisplay().getSystemColor(SWT
.COLOR_RED
));
556 context
.fillRectangle(x
, y
, width
, height
);
559 _x
= Math
.round(_x
* view
.zoomValue
);
560 _y
= Math
.round(_y
* view
.zoomValue
);
561 int x
= view
.contentsToViewX(_x
);
562 int y
= view
.contentsToViewY(_y
);
563 Rectangle b
= ((Image
) image
.getImage()).getBounds();
565 int height
= b
.height
;
568 if (height
> maxHeight
)
570 float tempZoom
= view
.zoomValue
;
571 width
= Math
.round(width
* tempZoom
);
572 height
= Math
.round(height
* tempZoom
);
574 if (view
.printing
&& width
> 0 && height
> 0) {
575 Image dbuffer
= new Image(view
.getDisplay(), width
, height
);
576 GC tempgc
= new GC(dbuffer
);
577 tempgc
.drawImage(img
, 0, 0, b
.width
, b
.height
, 0, 0, width
, height
);
578 Image dbuffer2
= new Image(view
.getDisplay(), dbuffer
.getImageData());
579 context
.drawImage(dbuffer2
, x
, y
);
584 context
.drawImage(img
, 0, 0, b
.width
, b
.height
, x
, y
, width
, height
);
589 public void drawArc(int x
, int y
, int width
, int height
, int startAngle
, int endAngle
) {
590 x
= Math
.round(x
* view
.zoomValue
);
591 y
= Math
.round(y
* view
.zoomValue
);
592 width
= Math
.round(width
* view
.zoomValue
);
593 height
= Math
.round(height
* view
.zoomValue
);
594 if (width
== 0 || height
== 0 || endAngle
== 0)
596 context
.drawArc(view
.contentsToViewX(x
), view
.contentsToViewY(y
), width
, height
, startAngle
, endAngle
);
600 public void setFont(IFont font
) {
601 if (font
.getFont() != null && ((Font
) font
.getFont()).getFontData().length
> 0) {
602 FontData fontData
= ((Font
) font
.getFont()).getFontData()[0];
603 if (SDViewPref
.getInstance().fontLinked() || view
.printing
) {
604 int h
= Math
.round(fontData
.getHeight() * view
.zoomValue
);
606 fontData
.setHeight(h
);
608 if (tempFont
!= null)
610 tempFont
= new Font(Display
.getCurrent(), fontData
);
611 context
.setFont(tempFont
);
616 public int getFontHeight(IFont font
) {
617 if (font
.getFont() != null && (font
.getFont() instanceof Font
) && ((Font
) font
.getFont()).getFontData().length
> 0) {
618 Font toRestore
= context
.getFont();
619 context
.setFont((Font
) font
.getFont());
620 int height
= context
.textExtent("lp").y
;//$NON-NLS-1$
621 context
.setFont(toRestore
);
627 protected int getCurrentFontHeight() {
628 return context
.textExtent("lp").y
; //$NON-NLS-1$
632 public int getFontWidth(IFont font
) {
633 if ((font
.getFont() != null) && (font
.getFont() instanceof Font
)) {
634 Font toRestore
= context
.getFont();
635 context
.setFont((Font
) font
.getFont());
636 int width
= context
.getFontMetrics().getAverageCharWidth();
637 context
.setFont(toRestore
);
643 public void dispose() {
644 if (tempFont
!= null)
653 public float getZoom() {
655 return view
.zoomValue
;
661 public int getLineDotStyle() {
666 public int getLineDashStyle() {
667 return SWT
.LINE_DASH
;
671 public int getLineSolidStyle() {
672 return SWT
.LINE_SOLID
;
676 public IColor
createColor(int r
, int g
, int b
) {
677 return new ColorImpl(Display
.getDefault(), r
, g
, b
);
682 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setDrawTextWithFocusStyle(boolean)
685 public void setDrawTextWithFocusStyle(boolean focus
) {
686 drawWithFocus
= focus
;
689 protected static int getVscreenBounds() {
690 return vscreen_bounds
;
693 protected static void setVscreenBounds(int vBounds
) {
694 vscreen_bounds
= vBounds
;
This page took 0.047742 seconds and 5 git commands to generate.