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