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