| 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.dialogs; |
| 14 | |
| 15 | import java.text.MessageFormat; |
| 16 | import java.util.Arrays; |
| 17 | |
| 18 | import org.eclipse.jface.dialogs.MessageDialog; |
| 19 | import org.eclipse.jface.wizard.WizardPage; |
| 20 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.DiagramToolTip; |
| 21 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.NGC; |
| 22 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget; |
| 23 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages; |
| 24 | import org.eclipse.swt.SWT; |
| 25 | import org.eclipse.swt.events.FocusEvent; |
| 26 | import org.eclipse.swt.events.FocusListener; |
| 27 | import org.eclipse.swt.events.KeyEvent; |
| 28 | import org.eclipse.swt.events.ModifyEvent; |
| 29 | import org.eclipse.swt.events.ModifyListener; |
| 30 | import org.eclipse.swt.events.MouseEvent; |
| 31 | import org.eclipse.swt.events.MouseTrackListener; |
| 32 | import org.eclipse.swt.events.SelectionEvent; |
| 33 | import org.eclipse.swt.events.SelectionListener; |
| 34 | import org.eclipse.swt.events.TraverseEvent; |
| 35 | import org.eclipse.swt.events.TraverseListener; |
| 36 | import org.eclipse.swt.graphics.GC; |
| 37 | import org.eclipse.swt.graphics.Image; |
| 38 | import org.eclipse.swt.graphics.ImageData; |
| 39 | import org.eclipse.swt.graphics.RGB; |
| 40 | import org.eclipse.swt.graphics.Rectangle; |
| 41 | import org.eclipse.swt.layout.GridData; |
| 42 | import org.eclipse.swt.layout.GridLayout; |
| 43 | import org.eclipse.swt.printing.PrintDialog; |
| 44 | import org.eclipse.swt.printing.Printer; |
| 45 | import org.eclipse.swt.printing.PrinterData; |
| 46 | import org.eclipse.swt.widgets.Button; |
| 47 | import org.eclipse.swt.widgets.Composite; |
| 48 | import org.eclipse.swt.widgets.Control; |
| 49 | import org.eclipse.swt.widgets.Display; |
| 50 | import org.eclipse.swt.widgets.Group; |
| 51 | import org.eclipse.swt.widgets.Label; |
| 52 | import org.eclipse.swt.widgets.Shell; |
| 53 | import org.eclipse.swt.widgets.Text; |
| 54 | |
| 55 | /** |
| 56 | * The class implements the actual print dialog UI for collecting printing data. |
| 57 | * |
| 58 | * @version 1.0 |
| 59 | * @author sveyrier |
| 60 | */ |
| 61 | public class SDPrintDialogUI { |
| 62 | // ------------------------------------------------------------------------ |
| 63 | // Attributes |
| 64 | // ------------------------------------------------------------------------ |
| 65 | /** |
| 66 | * The set horizontal pages number. |
| 67 | */ |
| 68 | protected Button fSetHPagesNumber; |
| 69 | /** |
| 70 | * The set vertical pages number. |
| 71 | */ |
| 72 | protected Button fSetVPagesNumber; |
| 73 | /** |
| 74 | * Flag whether to use current zoom or not. |
| 75 | */ |
| 76 | protected Button fUseCurrentZoom; |
| 77 | /** |
| 78 | * Flag whether to print all pages or not |
| 79 | */ |
| 80 | protected Button fAllPages; |
| 81 | /** |
| 82 | * Flag whether to print current page only |
| 83 | */ |
| 84 | protected Button fCurrentPage; |
| 85 | /** |
| 86 | * Button to select a page list. |
| 87 | */ |
| 88 | protected Button fPageList; |
| 89 | /** |
| 90 | * Button to select a page range. |
| 91 | */ |
| 92 | protected Button fPageRange; |
| 93 | /** |
| 94 | * Text field to enter from page. |
| 95 | */ |
| 96 | protected Text fFromPage; |
| 97 | /** |
| 98 | * Text field to enter to page. |
| 99 | */ |
| 100 | protected Text fToPage; |
| 101 | /** |
| 102 | * The sequence diagram widget reference. |
| 103 | */ |
| 104 | protected SDWidget fSdView; |
| 105 | /** |
| 106 | * Text field for number of horizontal pages |
| 107 | */ |
| 108 | protected Text fHorPagesNum; |
| 109 | /** |
| 110 | * Text field for number of vertical pages |
| 111 | */ |
| 112 | protected Text fVertPagesNum; |
| 113 | /** |
| 114 | * Text field for toal number of pages |
| 115 | */ |
| 116 | protected Text fTotalPages; |
| 117 | /** |
| 118 | * A modify listener implementation to handle modifications. |
| 119 | */ |
| 120 | protected ModifyListener fModifyListener; |
| 121 | /** |
| 122 | * A selection listener implementation to handle selections. |
| 123 | */ |
| 124 | protected SelectionListener fSelectionListener; |
| 125 | /** |
| 126 | * Local canvas displaying sequence diagram overview. |
| 127 | */ |
| 128 | protected LocalSD fOverviewCanvas; |
| 129 | /** |
| 130 | * Number of pages |
| 131 | */ |
| 132 | protected int fNbPages = 0; |
| 133 | /** |
| 134 | * Number of selected pages. |
| 135 | */ |
| 136 | protected int fPageNum = -1; |
| 137 | /** |
| 138 | * Number of first page. |
| 139 | */ |
| 140 | protected int fFirstPage = -1; |
| 141 | /** |
| 142 | * List of pages to print. |
| 143 | */ |
| 144 | protected int fPagesList[]; |
| 145 | |
| 146 | /** |
| 147 | * Value for dividing the sequence diagram into pages |
| 148 | */ |
| 149 | protected float fStepX; |
| 150 | |
| 151 | /** |
| 152 | * Value for dividing the sequence diagram into pages |
| 153 | */ |
| 154 | protected float fStepY; |
| 155 | |
| 156 | /** |
| 157 | * Value for dividing the sequence diagram into pages |
| 158 | */ |
| 159 | protected float sTX; |
| 160 | |
| 161 | /** |
| 162 | * Value for dividing the sequence diagram into pages |
| 163 | */ |
| 164 | protected float sTY; |
| 165 | |
| 166 | /** |
| 167 | * Page which to print from. |
| 168 | */ |
| 169 | protected int fFrom; |
| 170 | /** |
| 171 | * Page which to print to. |
| 172 | */ |
| 173 | protected int fTo; |
| 174 | /** |
| 175 | * Flag for enabling multi-selection. |
| 176 | */ |
| 177 | protected boolean fMultiSelection = false; |
| 178 | /** |
| 179 | * Flag for enabling area selection. |
| 180 | */ |
| 181 | protected boolean fAreaSelection = false; |
| 182 | /** |
| 183 | * Flag for printing all. |
| 184 | */ |
| 185 | protected boolean fPrintAll; |
| 186 | /** |
| 187 | * Flag for printing current page only. |
| 188 | */ |
| 189 | protected boolean fPrintCurrent; |
| 190 | /** |
| 191 | * Flag for printing a selection of pages. |
| 192 | */ |
| 193 | protected boolean fPrintSelection; |
| 194 | /** |
| 195 | * Flag for printing a range of pages. |
| 196 | */ |
| 197 | protected boolean fPrintRange; |
| 198 | /** |
| 199 | * Number of selected rows |
| 200 | */ |
| 201 | protected int fNbRows; |
| 202 | /** |
| 203 | * Number of selected lines |
| 204 | */ |
| 205 | protected int fNbLines; |
| 206 | /** |
| 207 | * The zoom factor. |
| 208 | */ |
| 209 | protected float fZoomFactor; |
| 210 | /** |
| 211 | * The printer data reference. |
| 212 | */ |
| 213 | protected PrinterData fPrinterData; |
| 214 | /** |
| 215 | * The diagram tooltip to show if necessary. |
| 216 | */ |
| 217 | protected DiagramToolTip fToolTip = null; |
| 218 | /** |
| 219 | * Label for current selection. |
| 220 | */ |
| 221 | protected Label fCurrentSelection; |
| 222 | /** |
| 223 | * The shell reference. |
| 224 | */ |
| 225 | protected Shell fShell; |
| 226 | /** |
| 227 | * Button to open printer dialog from OS. |
| 228 | */ |
| 229 | protected Button fPrinterDialog; |
| 230 | /** |
| 231 | * Flag for showing print button. |
| 232 | */ |
| 233 | protected boolean fShowPrintButton; |
| 234 | /** |
| 235 | * Test value |
| 236 | */ |
| 237 | protected int fTest = 3; |
| 238 | /** |
| 239 | * Parent wizard page if used as wizard |
| 240 | */ |
| 241 | protected WizardPage fParentWizardPage = null; |
| 242 | /** |
| 243 | * Reference to parent print dialog. |
| 244 | */ |
| 245 | protected SDPrintDialog fParentDialog = null; |
| 246 | |
| 247 | // ------------------------------------------------------------------------ |
| 248 | // Helper Class |
| 249 | // ------------------------------------------------------------------------ |
| 250 | /** |
| 251 | * Local sequence diagram widget used to display overview of sequence diagram to print. |
| 252 | * @version 1.0 |
| 253 | */ |
| 254 | protected class LocalSD extends SDWidget { |
| 255 | |
| 256 | /** |
| 257 | * Constructor |
| 258 | * @param c Parent composite |
| 259 | * @param s Style bits |
| 260 | */ |
| 261 | public LocalSD(Composite c, int s) { |
| 262 | super(c, s); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * (non-Javadoc) |
| 267 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsHeight() |
| 268 | */ |
| 269 | @Override |
| 270 | public int getContentsHeight() { |
| 271 | if (fSdView.getContentsHeight() > fSdView.getContentsHeight()) { |
| 272 | return (int) (fSdView.getVisibleHeight() / (float) fTest / fSdView.fZoomValue); |
| 273 | } |
| 274 | return super.getContentsHeight(); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * (non-Javadoc) |
| 279 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsWidth() |
| 280 | */ |
| 281 | @Override |
| 282 | public int getContentsWidth() { |
| 283 | if (fSdView.getVisibleWidth() > fSdView.getContentsWidth()) { |
| 284 | return (int) (fSdView.getVisibleWidth() / (float) fTest / fSdView.fZoomValue); |
| 285 | } |
| 286 | return super.getContentsWidth(); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * (non-Javadoc) |
| 291 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseHover(org.eclipse.swt.events.MouseEvent) |
| 292 | */ |
| 293 | @Override |
| 294 | protected void contentsMouseHover(MouseEvent event) { |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Creates page selection images. |
| 299 | * |
| 300 | * @param img - Overview image |
| 301 | * @param width -The width value |
| 302 | * @param stepX - Step X |
| 303 | * @param height - Height value |
| 304 | * @param stepY - Step Y |
| 305 | * @return new image |
| 306 | */ |
| 307 | protected Image createPagesSelectionImages(Image img, int width, float stepX, int height, float stepY) { |
| 308 | |
| 309 | Image over = new Image(super.getShell().getDisplay(), img.getImageData()); |
| 310 | |
| 311 | for (int pageIndex = 0; pageIndex < fPagesList.length; pageIndex++) { |
| 312 | |
| 313 | int pageNum = fPagesList[pageIndex]; |
| 314 | |
| 315 | if (getPagesForSelection() > 0 && pageNum > 0) { |
| 316 | int line = pageNum / getNbRow(); |
| 317 | int row = pageNum % getNbRow(); |
| 318 | if (row != 0) { |
| 319 | line++; |
| 320 | } else { |
| 321 | row = getNbRow(); |
| 322 | } |
| 323 | |
| 324 | line--; |
| 325 | row--; |
| 326 | |
| 327 | Image toDel = over; |
| 328 | if (fOverviewCanvas.isFocusControl()) { |
| 329 | over = new Image(super.getShell().getDisplay(), drawRegionSelected(toDel, new Rectangle(contentsToViewX((int) (row * stepX * fOverviewCanvas.fZoomValue)), contentsToViewY((int) (line * stepY * fOverviewCanvas.fZoomValue)), |
| 330 | ((int) (stepX * fOverviewCanvas.fZoomValue)), ((int) (stepY * fOverviewCanvas.fZoomValue))), new RGB(0, 0, 128))); |
| 331 | } else { |
| 332 | over = new Image(super.getShell().getDisplay(), drawRegionSelected(toDel, new Rectangle(contentsToViewX((int) (row * stepX * fOverviewCanvas.fZoomValue)), contentsToViewY((int) (line * stepY * fOverviewCanvas.fZoomValue)), |
| 333 | ((int) (stepX * fOverviewCanvas.fZoomValue)), ((int) (stepY * fOverviewCanvas.fZoomValue))), new RGB(221, 208, 200))); |
| 334 | } |
| 335 | toDel.dispose(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | Arrays.sort(fPagesList); |
| 340 | int pos = Arrays.binarySearch(fPagesList, fPageNum); |
| 341 | if ((pos < 0) && (getPagesForSelection() > 0 && fPageNum > 0)) { |
| 342 | int line = fPageNum / getNbRow(); |
| 343 | int row = fPageNum % getNbRow(); |
| 344 | if (row != 0) { |
| 345 | line++; |
| 346 | } else { |
| 347 | row = getNbRow(); |
| 348 | } |
| 349 | |
| 350 | line--; |
| 351 | row--; |
| 352 | |
| 353 | Image toDel = over; |
| 354 | over = new Image(super.getShell().getDisplay(), drawRegionSelected(toDel, new Rectangle(contentsToViewX((int) (row * stepX * fOverviewCanvas.fZoomValue)), contentsToViewY((int) (line * stepY * fOverviewCanvas.fZoomValue)), |
| 355 | ((int) (stepX * fOverviewCanvas.fZoomValue)), ((int) (stepY * fOverviewCanvas.fZoomValue))), new RGB(221, 208, 200))); |
| 356 | toDel.dispose(); |
| 357 | } |
| 358 | |
| 359 | GC imGC2 = new GC(over); |
| 360 | imGC2.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
| 361 | NGC imGC = new NGC(fOverviewCanvas, imGC2); |
| 362 | for (int i = 0, x = 0; x <= width && stepX > 0; i++, x = (int) (i * stepX)) { |
| 363 | imGC.drawLine(x, 0, x, height); |
| 364 | } |
| 365 | |
| 366 | for (int j = 0, y = 0; y <= height && stepY > 0; j++, y = (int) (j * stepY)) { |
| 367 | imGC.drawLine(0, y, width, y); |
| 368 | } |
| 369 | |
| 370 | imGC2.dispose(); |
| 371 | imGC.dispose(); |
| 372 | return over; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * (non-Javadoc) |
| 377 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#drawContents(org.eclipse.swt.graphics.GC, int, int, int, int) |
| 378 | */ |
| 379 | @Override |
| 380 | protected void drawContents(GC gc, int clipx, int clipy, int clipw, int cliph) { |
| 381 | |
| 382 | Image dbuffer = getDrawBuffer(); |
| 383 | computeStepXY(); |
| 384 | Image d; |
| 385 | |
| 386 | int lw = (int) (getContentsWidth() / fZoomValue); |
| 387 | if (getContentsWidth() < getVisibleWidth()) { |
| 388 | lw = (int) (getVisibleWidth() / fZoomValue); |
| 389 | } |
| 390 | |
| 391 | int lh = (int) (getContentsHeight() / fZoomValue); |
| 392 | if (getContentsHeight() < getVisibleHeight()) { |
| 393 | lh = (int) (getVisibleHeight() / fZoomValue); |
| 394 | } |
| 395 | d = createPagesSelectionImages(dbuffer, lw, fStepX, lh, fStepY); |
| 396 | |
| 397 | if (!isEnabled()) { |
| 398 | Image toDel = d; |
| 399 | d = new Image(super.getShell().getDisplay(), drawRegionSelected(d, new Rectangle(0, 0, lw, lh), new RGB(221, 208, 200))); |
| 400 | toDel.dispose(); |
| 401 | } |
| 402 | |
| 403 | Rectangle area = getClientArea(); |
| 404 | int w = d.getBounds().width; |
| 405 | int h = d.getBounds().height; |
| 406 | gc.drawImage(d, 0, 0, w, h, 0, 0, area.width, area.height); |
| 407 | |
| 408 | fTotalPages.setText(Integer.valueOf(maxNumOfPages()).toString()); |
| 409 | displayPageNum(); |
| 410 | dbuffer.dispose(); |
| 411 | d.dispose(); |
| 412 | gc.dispose(); |
| 413 | |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * (non-Javadoc) |
| 418 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#keyPressedEvent(org.eclipse.swt.events.KeyEvent) |
| 419 | */ |
| 420 | @Override |
| 421 | protected void keyPressedEvent(KeyEvent e) { |
| 422 | if (e.keyCode == SWT.CTRL) { |
| 423 | fMultiSelection = true; |
| 424 | } |
| 425 | if (e.keyCode == SWT.SHIFT) { |
| 426 | fAreaSelection = true; |
| 427 | } |
| 428 | if (e.keyCode == SWT.ARROW_DOWN) { |
| 429 | if (fPageNum + getNbRow() <= maxNumOfPages()) { |
| 430 | fPageNum += getNbRow(); |
| 431 | } |
| 432 | int line = fPageNum / getNbRow(); |
| 433 | int row = fPageNum % getNbRow(); |
| 434 | if (row == 0) { |
| 435 | line--; |
| 436 | } |
| 437 | if ((line + 1) * fStepY > (fOverviewCanvas.getContentsY() + fOverviewCanvas.getVisibleHeight()) / fOverviewCanvas.fZoomValue) { |
| 438 | fOverviewCanvas.scrollBy(0, (int) (fStepY * fOverviewCanvas.fZoomValue)); |
| 439 | } |
| 440 | } |
| 441 | if (e.keyCode == SWT.ARROW_UP) { |
| 442 | if (fPageNum - getNbRow() > 0) { |
| 443 | fPageNum -= getNbRow(); |
| 444 | } |
| 445 | int line = fPageNum / getNbRow(); |
| 446 | int row = fPageNum % getNbRow(); |
| 447 | if (row == 0) { |
| 448 | line--; |
| 449 | } |
| 450 | if ((line) * fStepY <= fOverviewCanvas.getContentsY() / fOverviewCanvas.fZoomValue) { |
| 451 | fOverviewCanvas.scrollBy(0, -(int) (fStepY * fOverviewCanvas.fZoomValue)); |
| 452 | } |
| 453 | } |
| 454 | if (e.keyCode == SWT.ARROW_LEFT) { |
| 455 | if ((fPageNum - 2) / getNbRow() == (fPageNum - 1) / getNbRow() && fPageNum > 1) { |
| 456 | fPageNum--; |
| 457 | } |
| 458 | int row = fPageNum % getNbRow(); |
| 459 | if ((row - 1) * fStepX < (fOverviewCanvas.getContentsX()) / fOverviewCanvas.fZoomValue) { |
| 460 | fOverviewCanvas.scrollBy(-(int) (fStepX * fOverviewCanvas.fZoomValue), 0); |
| 461 | } |
| 462 | } |
| 463 | if (e.keyCode == SWT.ARROW_RIGHT) { |
| 464 | if ((fPageNum - 1) / getNbRow() == fPageNum / getNbRow()) { |
| 465 | fPageNum++; |
| 466 | } |
| 467 | int row = fPageNum % getNbRow(); |
| 468 | if (row == 0) { |
| 469 | row = getNbRow(); |
| 470 | } |
| 471 | if ((row) * fStepX > (fOverviewCanvas.getContentsX() + fOverviewCanvas.getVisibleWidth()) / fOverviewCanvas.fZoomValue) { |
| 472 | fOverviewCanvas.scrollBy((int) (fStepX * fOverviewCanvas.fZoomValue), 0); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (e.keyCode == 32 && fPageNum > -1) { |
| 477 | Arrays.sort(fPagesList); |
| 478 | int pos = Arrays.binarySearch(fPagesList, fPageNum); |
| 479 | if (pos < 0) { |
| 480 | addToPagesList(fPageNum); |
| 481 | } else { |
| 482 | removeFromPagesList(fPageNum); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (!fAreaSelection && !fMultiSelection) { |
| 487 | fFirstPage = fPageNum; |
| 488 | fPagesList = new int[1]; |
| 489 | fPagesList[0] = fPageNum; |
| 490 | } else if ((fPageNum != -1) && (fAreaSelection) && (fFirstPage != -1)) { |
| 491 | fPagesList = new int[0]; |
| 492 | int line1 = fFirstPage / getNbRow(); |
| 493 | int row1 = fFirstPage % getNbRow(); |
| 494 | if (row1 != 0) { |
| 495 | line1++; |
| 496 | } else { |
| 497 | row1 = getNbRow(); |
| 498 | } |
| 499 | |
| 500 | int line2 = fPageNum / getNbRow(); |
| 501 | int row2 = fPageNum % getNbRow(); |
| 502 | if (row2 != 0) { |
| 503 | line2++; |
| 504 | } else { |
| 505 | row2 = getNbRow(); |
| 506 | } |
| 507 | |
| 508 | int temp; |
| 509 | if (line1 > line2) { |
| 510 | temp = line2; |
| 511 | line2 = line1; |
| 512 | line1 = temp; |
| 513 | } |
| 514 | |
| 515 | if (row1 > row2) { |
| 516 | temp = row2; |
| 517 | row2 = row1; |
| 518 | row1 = temp; |
| 519 | } |
| 520 | |
| 521 | for (int i = row1 - 1; i < row2; i++) { |
| 522 | for (int j = line1 - 1; j < line2; j++) { |
| 523 | addToPagesList(i + j * getNbRow() + 1); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | displayPageNum(); |
| 528 | fOverviewCanvas.redraw(); |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * (non-Javadoc) |
| 533 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#keyReleasedEvent(org.eclipse.swt.events.KeyEvent) |
| 534 | */ |
| 535 | @Override |
| 536 | protected void keyReleasedEvent(KeyEvent e) { |
| 537 | if (e.keyCode == SWT.CTRL) { |
| 538 | fMultiSelection = false; |
| 539 | } |
| 540 | if (e.keyCode == SWT.SHIFT) { |
| 541 | fAreaSelection = false; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * (non-Javadoc) |
| 547 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseDownEvent(org.eclipse.swt.events.MouseEvent) |
| 548 | */ |
| 549 | @Override |
| 550 | protected void contentsMouseDownEvent(MouseEvent event) { |
| 551 | |
| 552 | computeStepXY(); |
| 553 | int x1 = (int) ((event.x / fOverviewCanvas.fZoomValue) / fStepX); |
| 554 | int x2 = (int) ((event.y / fOverviewCanvas.fZoomValue) / fStepY); |
| 555 | |
| 556 | int oldPage = fPageNum; |
| 557 | |
| 558 | fPageNum = x1 + x2 * getNbRow() + 1; |
| 559 | |
| 560 | if (fPageNum > maxNumOfPages()) { |
| 561 | fPageNum = oldPage; |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | if (!fAreaSelection) { |
| 566 | fFirstPage = fPageNum; |
| 567 | } |
| 568 | |
| 569 | if ((fPageNum != -1) && (fMultiSelection)) { |
| 570 | Arrays.sort(fPagesList); |
| 571 | int pos = Arrays.binarySearch(fPagesList, fPageNum); |
| 572 | if (pos < 0) { |
| 573 | addToPagesList(fPageNum); |
| 574 | } else { |
| 575 | removeFromPagesList(fPageNum); |
| 576 | } |
| 577 | } else if ((fPageNum != -1) && (fAreaSelection) && (fFirstPage != -1)) { |
| 578 | |
| 579 | fPagesList = new int[0]; |
| 580 | |
| 581 | int line1 = fFirstPage / getNbRow(); |
| 582 | int row1 = fFirstPage % getNbRow(); |
| 583 | if (row1 != 0) { |
| 584 | line1++; |
| 585 | } else { |
| 586 | row1 = getNbRow(); |
| 587 | } |
| 588 | |
| 589 | int line2 = fPageNum / getNbRow(); |
| 590 | int row2 = fPageNum % getNbRow(); |
| 591 | if (row2 != 0) { |
| 592 | line2++; |
| 593 | } else { |
| 594 | row2 = getNbRow(); |
| 595 | } |
| 596 | |
| 597 | int temp; |
| 598 | if (line1 > line2) { |
| 599 | temp = line2; |
| 600 | line2 = line1; |
| 601 | line1 = temp; |
| 602 | } |
| 603 | |
| 604 | if (row1 > row2) { |
| 605 | temp = row2; |
| 606 | row2 = row1; |
| 607 | row1 = temp; |
| 608 | } |
| 609 | |
| 610 | for (int i = row1 - 1; i < row2; i++) { |
| 611 | for (int j = line1 - 1; j < line2; j++) { |
| 612 | addToPagesList(i + j * getNbRow() + 1); |
| 613 | } |
| 614 | } |
| 615 | } else { |
| 616 | fPagesList = new int[1]; |
| 617 | fPagesList[0] = fPageNum; |
| 618 | } |
| 619 | if ((event.stateMask & SWT.CTRL) != 0) { |
| 620 | fMultiSelection = true; |
| 621 | } |
| 622 | displayPageNum(); |
| 623 | redraw(); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * (non-Javadoc) |
| 628 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseMoveEvent(org.eclipse.swt.events.MouseEvent) |
| 629 | */ |
| 630 | @Override |
| 631 | protected void contentsMouseMoveEvent(MouseEvent e) { |
| 632 | fToolTip.hideToolTip(); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * (non-Javadoc) |
| 637 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#resizeContents(int, int) |
| 638 | */ |
| 639 | @Override |
| 640 | public void resizeContents(int w, int h) { |
| 641 | super.resizeContents(w, h); |
| 642 | } |
| 643 | |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * A traverse listener implementation. |
| 648 | */ |
| 649 | protected static class LocalTraverseListener implements TraverseListener { |
| 650 | /* |
| 651 | * (non-Javadoc) |
| 652 | * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent) |
| 653 | */ |
| 654 | @Override |
| 655 | public void keyTraversed(TraverseEvent e) { |
| 656 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { |
| 657 | e.doit = true; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // ------------------------------------------------------------------------ |
| 663 | // Constructors |
| 664 | // ------------------------------------------------------------------------ |
| 665 | /** |
| 666 | * Constructor |
| 667 | * |
| 668 | * @param shell |
| 669 | * The shell reference |
| 670 | * @param sdWidget |
| 671 | * The sequence diagram widget reference |
| 672 | */ |
| 673 | public SDPrintDialogUI(Shell shell, SDWidget sdWidget) { |
| 674 | this(shell, sdWidget, false); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Constructor |
| 679 | * |
| 680 | * @param shell |
| 681 | * The shell reference |
| 682 | * @param sdWidget |
| 683 | * The sequence diagram widget reference |
| 684 | * @param showPrintBtn |
| 685 | * Flag for showing print buttons |
| 686 | */ |
| 687 | public SDPrintDialogUI(Shell shell, SDWidget sdWidget, boolean showPrintBtn) { |
| 688 | fShell = shell; |
| 689 | fSdView = sdWidget; |
| 690 | fShowPrintButton = showPrintBtn; |
| 691 | |
| 692 | fPrinterData = Printer.getDefaultPrinterData(); |
| 693 | if (fPrinterData != null) { |
| 694 | fPrinterData.scope = PrinterData.SELECTION; |
| 695 | } |
| 696 | |
| 697 | fPagesList = new int[0]; |
| 698 | |
| 699 | fSelectionListener = new SelectionListener() { |
| 700 | |
| 701 | @Override |
| 702 | public void widgetSelected(SelectionEvent e) { |
| 703 | if (fUseCurrentZoom.getSelection()) { |
| 704 | fHorPagesNum.setEnabled(false); |
| 705 | fVertPagesNum.setEnabled(false); |
| 706 | } |
| 707 | if (fSetHPagesNumber.getSelection()) { |
| 708 | fHorPagesNum.setEnabled(true); |
| 709 | fVertPagesNum.setEnabled(false); |
| 710 | if (fCurrentPage.getSelection()) { |
| 711 | fCurrentPage.setSelection(false); |
| 712 | fAllPages.setSelection(true); |
| 713 | } |
| 714 | if ("".equals(fHorPagesNum.getText())) { //$NON-NLS-1$ |
| 715 | fHorPagesNum.setText("1"); //$NON-NLS-1$ |
| 716 | } |
| 717 | } |
| 718 | if (fSetVPagesNumber.getSelection()) { |
| 719 | fHorPagesNum.setEnabled(false); |
| 720 | fVertPagesNum.setEnabled(true); |
| 721 | if (fCurrentPage.getSelection()) { |
| 722 | fCurrentPage.setSelection(false); |
| 723 | fAllPages.setSelection(true); |
| 724 | } |
| 725 | if ("".equals(fVertPagesNum.getText())) { //$NON-NLS-1$ |
| 726 | fVertPagesNum.setText("1"); //$NON-NLS-1$ |
| 727 | } |
| 728 | } |
| 729 | if (fCurrentPage.getSelection() || fAllPages.getSelection() || fPageList.getSelection()) { |
| 730 | fFromPage.setEnabled(false); |
| 731 | fToPage.setEnabled(false); |
| 732 | } else { |
| 733 | fFromPage.setEnabled(true); |
| 734 | fToPage.setEnabled(true); |
| 735 | } |
| 736 | |
| 737 | fCurrentPage.setEnabled(fUseCurrentZoom.getSelection()); |
| 738 | fOverviewCanvas.setEnabled(fPageList.getSelection()); |
| 739 | if (fOverviewCanvas.isEnabled() && (e.widget == fUseCurrentZoom || e.widget == fSetHPagesNumber || e.widget == fSetVPagesNumber)) { |
| 740 | fPagesList = new int[1]; |
| 741 | fPagesList[0] = 1; |
| 742 | fPageNum = 1; |
| 743 | fFirstPage = 1; |
| 744 | } else if ((fOverviewCanvas.isEnabled() && (e.widget == fPageList)) && |
| 745 | (fPagesList == null || fPagesList.length <= 0)) { |
| 746 | |
| 747 | fPagesList = new int[1]; |
| 748 | fPagesList[0] = 1; |
| 749 | fPageNum = 1; |
| 750 | fFirstPage = 1; |
| 751 | } |
| 752 | computeStepXY(); |
| 753 | fTotalPages.setText(Integer.valueOf(maxNumOfPages()).toString()); |
| 754 | fOverviewCanvas.redraw(); |
| 755 | fOverviewCanvas.update(); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * (non-Javadoc) |
| 760 | * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
| 761 | */ |
| 762 | @Override |
| 763 | public void widgetDefaultSelected(SelectionEvent e) { |
| 764 | fPagesList = new int[0]; |
| 765 | computeStepXY(); |
| 766 | fOverviewCanvas.redraw(); |
| 767 | } |
| 768 | |
| 769 | }; |
| 770 | |
| 771 | fModifyListener = new ModifyListener() { |
| 772 | /* |
| 773 | * (non-Javadoc) |
| 774 | * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) |
| 775 | */ |
| 776 | @Override |
| 777 | public void modifyText(ModifyEvent e) { |
| 778 | fPagesList = new int[0]; |
| 779 | computeStepXY(); |
| 780 | fTotalPages.setText(Integer.valueOf(maxNumOfPages()).toString()); |
| 781 | fOverviewCanvas.redraw(); |
| 782 | } |
| 783 | |
| 784 | }; |
| 785 | } |
| 786 | |
| 787 | // ------------------------------------------------------------------------ |
| 788 | // Methods |
| 789 | // ------------------------------------------------------------------------ |
| 790 | |
| 791 | /** |
| 792 | * Creates new grid data object. |
| 793 | * |
| 794 | * @param span horizontal span. |
| 795 | * @return grid data |
| 796 | */ |
| 797 | protected GridData newGridData(int span) { |
| 798 | GridData data = new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL); |
| 799 | data.horizontalSpan = span; |
| 800 | return data; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Creates the dialog area. |
| 805 | * |
| 806 | * @param parent The parent composite |
| 807 | * @return dialog control |
| 808 | */ |
| 809 | public Control createDialogArea(Composite parent) { |
| 810 | |
| 811 | GridLayout parentLayout = new GridLayout(); |
| 812 | parentLayout.numColumns = 6; |
| 813 | parent.setLayout(parentLayout); |
| 814 | |
| 815 | Group g1 = new Group(parent, SWT.SHADOW_NONE); |
| 816 | g1.setText(SDMessages._113); |
| 817 | g1.setLayoutData(newGridData(3)); |
| 818 | GridLayout g1layout = new GridLayout(); |
| 819 | g1layout.numColumns = 2; |
| 820 | g1.setLayout(g1layout); |
| 821 | |
| 822 | fUseCurrentZoom = new Button(g1, SWT.RADIO); |
| 823 | fUseCurrentZoom.setText(SDMessages._112); |
| 824 | fUseCurrentZoom.setLayoutData(newGridData(2)); |
| 825 | fUseCurrentZoom.addSelectionListener(fSelectionListener); |
| 826 | |
| 827 | fSetHPagesNumber = new Button(g1, SWT.RADIO); |
| 828 | fSetHPagesNumber.setText(SDMessages._110); |
| 829 | fSetHPagesNumber.setLayoutData(newGridData(1)); |
| 830 | fSetHPagesNumber.addSelectionListener(fSelectionListener); |
| 831 | |
| 832 | fHorPagesNum = new Text(g1, SWT.SINGLE | SWT.BORDER); |
| 833 | fHorPagesNum.addModifyListener(fModifyListener); |
| 834 | |
| 835 | fSetVPagesNumber = new Button(g1, SWT.RADIO); |
| 836 | fSetVPagesNumber.setText(SDMessages._111); |
| 837 | fSetVPagesNumber.setLayoutData(newGridData(1)); |
| 838 | fSetVPagesNumber.addSelectionListener(fSelectionListener); |
| 839 | |
| 840 | fVertPagesNum = new Text(g1, SWT.SINGLE | SWT.BORDER); |
| 841 | fVertPagesNum.addModifyListener(fModifyListener); |
| 842 | |
| 843 | Label nbTotal = new Label(g1, SWT.SHADOW_NONE | SWT.RIGHT); |
| 844 | nbTotal.setText(SDMessages._109); |
| 845 | // nbTotal.setLayoutData(newGridData(1)); |
| 846 | |
| 847 | fTotalPages = new Text(g1, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); |
| 848 | // nbHV.addModifyListener(modifListener); |
| 849 | |
| 850 | Group g2 = new Group(parent, SWT.SHADOW_NONE); |
| 851 | g2.setText(SDMessages._119); |
| 852 | GridData data = new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL); |
| 853 | data.horizontalSpan = 3; |
| 854 | data.verticalSpan = 2; |
| 855 | g2.setLayoutData(data); |
| 856 | GridLayout g2layout = new GridLayout(); |
| 857 | // g2layout. |
| 858 | g2layout.numColumns = 1; |
| 859 | // SVLayout g2layout = new SVLayout(); |
| 860 | g2.setLayout(g2layout); |
| 861 | |
| 862 | GridData data2 = new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL); |
| 863 | data2.horizontalSpan = 1; |
| 864 | data2.verticalSpan = 1; |
| 865 | |
| 866 | fOverviewCanvas = new LocalSD(g2, SWT.NO_BACKGROUND); |
| 867 | GridData seqDiagLayoutData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL); |
| 868 | fOverviewCanvas.setLayoutData(seqDiagLayoutData); |
| 869 | // overviewCanvas.resizeContents(100,100); |
| 870 | if (fSdView.getContentsWidth() < fSdView.getVisibleWidth() && fSdView.getContentsHeight() < fSdView.getVisibleHeight()) { |
| 871 | fTest = 3; |
| 872 | } else { |
| 873 | fTest = 10; |
| 874 | } |
| 875 | fOverviewCanvas.setFrame(fSdView.getFrame(), true); |
| 876 | fOverviewCanvas.fZoomValue = (float) 1 / fTest; |
| 877 | fOverviewCanvas.setCornerControl(null); |
| 878 | seqDiagLayoutData.widthHint = fOverviewCanvas.getContentsWidth() / fTest; |
| 879 | seqDiagLayoutData.widthHint = fOverviewCanvas.getFrame().getWidth() / fTest + 15; |
| 880 | |
| 881 | if (fSdView.getVisibleWidth() < fSdView.getContentsWidth()) { |
| 882 | seqDiagLayoutData.widthHint = fOverviewCanvas.getContentsWidth() / fTest; |
| 883 | if (seqDiagLayoutData.widthHint > Display.getDefault().getClientArea().width / 4) { |
| 884 | seqDiagLayoutData.widthHint = Display.getDefault().getClientArea().width / 4; |
| 885 | } |
| 886 | } else { |
| 887 | seqDiagLayoutData.widthHint = fOverviewCanvas.getFrame().getWidth() / fTest + 15; |
| 888 | } |
| 889 | |
| 890 | if (fSdView.getVisibleHeight() < fSdView.getContentsHeight()) { |
| 891 | seqDiagLayoutData.heightHint = fOverviewCanvas.getContentsHeight() / fTest; |
| 892 | if (seqDiagLayoutData.heightHint > Display.getDefault().getClientArea().width / 4) { |
| 893 | seqDiagLayoutData.heightHint = Display.getDefault().getClientArea().width / 4; |
| 894 | } |
| 895 | } else { |
| 896 | seqDiagLayoutData.heightHint = fOverviewCanvas.getFrame().getHeight() / fTest; |
| 897 | } |
| 898 | |
| 899 | fOverviewCanvas.setEnabled(false); |
| 900 | |
| 901 | fCurrentSelection = new Label(g2, SWT.SHADOW_NONE | SWT.LEFT); |
| 902 | fCurrentSelection.setLayoutData(newGridData(1)); |
| 903 | |
| 904 | Group g3 = new Group(parent, SWT.SHADOW_NONE); |
| 905 | g3.setText(SDMessages._118); |
| 906 | g3.setLayoutData(newGridData(3)); |
| 907 | GridLayout g3layout = new GridLayout(); |
| 908 | g3layout.numColumns = 4; |
| 909 | g3.setLayout(g3layout); |
| 910 | |
| 911 | fAllPages = new Button(g3, SWT.RADIO); |
| 912 | fAllPages.setText(SDMessages._108); |
| 913 | fAllPages.setLayoutData(newGridData(4)); |
| 914 | fAllPages.addSelectionListener(fSelectionListener); |
| 915 | |
| 916 | fCurrentPage = new Button(g3, SWT.RADIO); |
| 917 | fCurrentPage.setText(SDMessages._107); |
| 918 | fCurrentPage.setLayoutData(newGridData(4)); |
| 919 | fCurrentPage.setEnabled(true); |
| 920 | fCurrentPage.setSelection(true); |
| 921 | fCurrentPage.addSelectionListener(fSelectionListener); |
| 922 | |
| 923 | fPageList = new Button(g3, SWT.RADIO); |
| 924 | fPageList.setText(SDMessages._106); |
| 925 | fPageList.setLayoutData(newGridData(4)); |
| 926 | fPageList.addSelectionListener(fSelectionListener); |
| 927 | |
| 928 | fPageRange = new Button(g3, SWT.RADIO); |
| 929 | fPageRange.setText(SDMessages._103); |
| 930 | fPageRange.setLayoutData(newGridData(1)); |
| 931 | fPageRange.addSelectionListener(fSelectionListener); |
| 932 | |
| 933 | fFromPage = new Text(g3, SWT.SINGLE | SWT.BORDER); |
| 934 | |
| 935 | Label labelTo = new Label(g3, SWT.CENTER); |
| 936 | labelTo.setText(SDMessages._105); |
| 937 | |
| 938 | fToPage = new Text(g3, SWT.SINGLE | SWT.BORDER); |
| 939 | |
| 940 | fToolTip = new DiagramToolTip(fOverviewCanvas); |
| 941 | |
| 942 | fOverviewCanvas.getViewControl().addMouseTrackListener(new MouseTrackListener() { |
| 943 | /* |
| 944 | * (non-Javadoc) |
| 945 | * @see org.eclipse.swt.events.MouseTrackListener#mouseEnter(org.eclipse.swt.events.MouseEvent) |
| 946 | */ |
| 947 | @Override |
| 948 | public void mouseEnter(MouseEvent e) { |
| 949 | fToolTip.hideToolTip(); |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * (non-Javadoc) |
| 954 | * @see org.eclipse.swt.events.MouseTrackListener#mouseExit(org.eclipse.swt.events.MouseEvent) |
| 955 | */ |
| 956 | @Override |
| 957 | public void mouseExit(MouseEvent e) { |
| 958 | fToolTip.hideToolTip(); |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * (non-Javadoc) |
| 963 | * @see org.eclipse.swt.events.MouseTrackListener#mouseHover(org.eclipse.swt.events.MouseEvent) |
| 964 | */ |
| 965 | @Override |
| 966 | public void mouseHover(MouseEvent e) { |
| 967 | int x1 = (int) (fOverviewCanvas.viewToContentsX(e.x) / fOverviewCanvas.fZoomValue / fStepX); |
| 968 | int x2 = (int) (fOverviewCanvas.viewToContentsY(e.y) / fOverviewCanvas.fZoomValue / fStepY); |
| 969 | int num = x1 + x2 * getNbRow() + 1; |
| 970 | if (num > maxNumOfPages()) { |
| 971 | return; |
| 972 | } |
| 973 | if (num > 0) { |
| 974 | fToolTip.showToolTip(String.valueOf(num)); |
| 975 | displayPageNum(); |
| 976 | } else { |
| 977 | fCurrentSelection.setText("");//$NON-NLS-1$ |
| 978 | fToolTip.hideToolTip(); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | }); |
| 983 | |
| 984 | fOverviewCanvas.addTraverseListener(new LocalTraverseListener()); |
| 985 | |
| 986 | fOverviewCanvas.addFocusListener(new FocusListener() { |
| 987 | /* |
| 988 | * (non-Javadoc) |
| 989 | * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent) |
| 990 | */ |
| 991 | @Override |
| 992 | public void focusGained(FocusEvent e) { |
| 993 | fOverviewCanvas.redraw(); |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * (non-Javadoc) |
| 998 | * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent) |
| 999 | */ |
| 1000 | @Override |
| 1001 | public void focusLost(FocusEvent e) { |
| 1002 | fOverviewCanvas.redraw(); |
| 1003 | } |
| 1004 | }); |
| 1005 | |
| 1006 | if (fShowPrintButton) { |
| 1007 | Composite printerDlg = new Composite(parent, SWT.NONE); |
| 1008 | data = new GridData(SWT.FILL, SWT.DEFAULT, true, false); |
| 1009 | data.horizontalSpan = 6; |
| 1010 | parentLayout = new GridLayout(); |
| 1011 | parentLayout.numColumns = 2; |
| 1012 | printerDlg.setLayout(parentLayout); |
| 1013 | printerDlg.setLayoutData(data); |
| 1014 | |
| 1015 | Label label = new Label(printerDlg, SWT.NONE); |
| 1016 | label.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false)); |
| 1017 | fPrinterDialog = new Button(printerDlg, SWT.PUSH); |
| 1018 | fPrinterDialog.setText(SDMessages._115); |
| 1019 | |
| 1020 | fPrinterDialog.addSelectionListener(new SelectionListener() { |
| 1021 | |
| 1022 | /* |
| 1023 | * (non-Javadoc) |
| 1024 | * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
| 1025 | */ |
| 1026 | @Override |
| 1027 | public void widgetSelected(SelectionEvent e) { |
| 1028 | printButtonSelected(); |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * (non-Javadoc) |
| 1033 | * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
| 1034 | */ |
| 1035 | @Override |
| 1036 | public void widgetDefaultSelected(SelectionEvent e) { |
| 1037 | } |
| 1038 | |
| 1039 | }); |
| 1040 | } |
| 1041 | |
| 1042 | updatePrinterStatus(); |
| 1043 | |
| 1044 | return parent; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Get number of pages for selection. |
| 1049 | * @return number of pages for selection. |
| 1050 | */ |
| 1051 | public int getPagesForSelection() { |
| 1052 | return fNbPages; |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * Handler for when the OK button is pressed |
| 1057 | * |
| 1058 | * @return True if the operation was successful, false if there was an error |
| 1059 | */ |
| 1060 | public boolean okPressed() { |
| 1061 | fPrintAll = fAllPages.getSelection(); |
| 1062 | fPrintCurrent = fCurrentPage.getSelection(); |
| 1063 | fPrintSelection = fPageList.getSelection(); |
| 1064 | fPrintRange = fPageRange.getSelection(); |
| 1065 | try { |
| 1066 | if (fPrintRange) { |
| 1067 | fFrom = Integer.valueOf(fFromPage.getText()).intValue(); |
| 1068 | fTo = Integer.valueOf(fToPage.getText()).intValue(); |
| 1069 | if (fFrom > maxNumOfPages() || fTo > maxNumOfPages() || fFrom <= 0 || fTo <= 0) { |
| 1070 | MessageDialog.openError(getShell(), SDMessages._98, SDMessages._99); |
| 1071 | return false; |
| 1072 | } |
| 1073 | } else if (fSetHPagesNumber.getSelection() && fNbPages <= 0) { |
| 1074 | MessageDialog.openError(getShell(), SDMessages._98, SDMessages._101); |
| 1075 | return false; |
| 1076 | } else if (fSetVPagesNumber.getSelection() && fNbPages <= 0) { |
| 1077 | MessageDialog.openError(getShell(), SDMessages._98, SDMessages._100); |
| 1078 | return false; |
| 1079 | } else if (fPrintSelection && getPageList().length <= 0) { |
| 1080 | MessageDialog.openError(getShell(), SDMessages._98, SDMessages._102); |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
| 1084 | } catch (Exception e) { |
| 1085 | MessageDialog.openError(getShell(), SDMessages._98, SDMessages._99); |
| 1086 | fFrom = 0; |
| 1087 | fTo = 0; |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | return true; |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * Draws region that was selected |
| 1096 | * @param img The corresponding image |
| 1097 | * @param r The selected rectangle. |
| 1098 | * @param color The color to use for selection |
| 1099 | * @return image data reference |
| 1100 | */ |
| 1101 | public ImageData drawRegionSelected(Image img, Rectangle r, RGB color) { |
| 1102 | ImageData id = img.getImageData(); |
| 1103 | for (int a = 0; a < r.width && r.x + a < id.width; a++) { |
| 1104 | for (int b = 0; b < r.height && r.y + b < id.height; b++) { |
| 1105 | int index = id.getPixel(r.x + a, r.y + b); |
| 1106 | RGB rgb = id.palette.getRGB(index); |
| 1107 | rgb = combine(color, rgb); |
| 1108 | id.setPixel(r.x + a, r.y + b, id.palette.getPixel(rgb)); |
| 1109 | } |
| 1110 | } |
| 1111 | return id; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Combines two RGB colors. |
| 1116 | * @param front The front color |
| 1117 | * @param back The back color |
| 1118 | * @return new RGB color |
| 1119 | */ |
| 1120 | public static RGB combine(RGB front, RGB back) { |
| 1121 | int _af = 128; |
| 1122 | int _ab = 200; |
| 1123 | |
| 1124 | double af = (_af) / 255.0; |
| 1125 | double rf = front.red; |
| 1126 | double gf = front.green; |
| 1127 | double bf = front.blue; |
| 1128 | |
| 1129 | double ab = (_ab) / 255.0; |
| 1130 | double rb = back.red; |
| 1131 | double gb = back.green; |
| 1132 | double bb = back.blue; |
| 1133 | |
| 1134 | double k = (1.0 - af) * ab; |
| 1135 | int r = (int) ((af * rf + k * rb)); |
| 1136 | int g = (int) ((af * gf + k * gb)); |
| 1137 | int b = (int) ((af * bf + k * bb)); |
| 1138 | |
| 1139 | return new RGB(r, g, b); |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * Computes value for X coordinates step and Y coordinates step. |
| 1144 | */ |
| 1145 | protected void computeStepXY() { |
| 1146 | float cw = fOverviewCanvas.getContentsWidth() / fOverviewCanvas.fZoomValue; |
| 1147 | float ch = fOverviewCanvas.getContentsHeight() / fOverviewCanvas.fZoomValue; |
| 1148 | try { |
| 1149 | if (fPrinterData == null) { |
| 1150 | fStepX = 0; |
| 1151 | fStepY = 0; |
| 1152 | fNbPages = 0; |
| 1153 | fZoomFactor = 0; |
| 1154 | } else { |
| 1155 | Printer printer = new Printer(fPrinterData); |
| 1156 | if (fSetHPagesNumber.getSelection()) { |
| 1157 | fNbPages = Integer.valueOf(fHorPagesNum.getText()).intValue(); |
| 1158 | float z1 = fSdView.getContentsWidth() / cw; |
| 1159 | float z2 = printer.getClientArea().width / ((float) fSdView.getContentsWidth() / fNbPages); |
| 1160 | |
| 1161 | fStepY = printer.getClientArea().height / z1 / z2; |
| 1162 | fStepX = cw / fNbPages; |
| 1163 | } else if (fSetVPagesNumber.getSelection()) { |
| 1164 | fNbPages = Integer.valueOf(fVertPagesNum.getText()).intValue(); |
| 1165 | float z1 = fSdView.getContentsHeight() / ch; |
| 1166 | float z2 = printer.getClientArea().height / ((float) fSdView.getContentsHeight() / fNbPages); |
| 1167 | fStepX = printer.getClientArea().width / z1 / z2; |
| 1168 | fStepY = ch / fNbPages; |
| 1169 | } else { |
| 1170 | float z1 = fSdView.getContentsWidth() / (cw); |
| 1171 | fStepX = fSdView.getVisibleWidth() / z1; |
| 1172 | fNbPages = Math.round(cw / fStepX); |
| 1173 | if (fNbPages == 0) { |
| 1174 | fNbPages = 1; |
| 1175 | } |
| 1176 | int pw = printer.getClientArea().width; |
| 1177 | int ph = printer.getClientArea().height; |
| 1178 | float z2 = pw / ((float) fSdView.getContentsWidth() / fNbPages); |
| 1179 | fStepY = ph / z1 / z2; |
| 1180 | } |
| 1181 | } |
| 1182 | } catch (NumberFormatException e) { |
| 1183 | fStepX = fStepY = fNbPages = 0; |
| 1184 | fZoomFactor = 0; |
| 1185 | } |
| 1186 | sTX = fStepX * (fSdView.getContentsWidth() / cw); |
| 1187 | sTY = fStepY * (fSdView.getContentsHeight() / ch); |
| 1188 | float rat = 1; |
| 1189 | if ((fSdView.getVisibleWidth() > fSdView.getContentsWidth()) && (fSetVPagesNumber.getSelection() || fSetHPagesNumber.getSelection())) { |
| 1190 | rat = (float) fSdView.getVisibleWidth() / (float) fSdView.getContentsWidth(); |
| 1191 | } |
| 1192 | fZoomFactor = (fOverviewCanvas.getContentsWidth() / cw) / fOverviewCanvas.getZoomFactor() * rat; |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * Returns the pages list. |
| 1197 | * |
| 1198 | * @return the pages list. |
| 1199 | */ |
| 1200 | public int[] getPageList() { |
| 1201 | return Arrays.copyOf(fPagesList, fPagesList.length); |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * Adds a page to pages list. |
| 1206 | * |
| 1207 | * @param num |
| 1208 | * The number of the the new page |
| 1209 | */ |
| 1210 | public void addToPagesList(int num) { |
| 1211 | int temp[] = new int[fPagesList.length + 1]; |
| 1212 | System.arraycopy(fPagesList, 0, temp, 0, fPagesList.length); |
| 1213 | temp[temp.length - 1] = num; |
| 1214 | fPagesList = new int[temp.length]; |
| 1215 | System.arraycopy(temp, 0, fPagesList, 0, temp.length); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Removes a page from the pages list. |
| 1220 | * |
| 1221 | * @param num |
| 1222 | * The number of the page to remove |
| 1223 | */ |
| 1224 | public void removeFromPagesList(int num) { |
| 1225 | int pos = Arrays.binarySearch(fPagesList, num); |
| 1226 | int temp[] = new int[fPagesList.length - 1]; |
| 1227 | System.arraycopy(fPagesList, 0, temp, 0, pos); |
| 1228 | System.arraycopy(fPagesList, pos + 1, temp, pos, fPagesList.length - pos - 1); |
| 1229 | fPagesList = new int[temp.length]; |
| 1230 | System.arraycopy(temp, 0, fPagesList, 0, temp.length); |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Returns the maximum number of pages. |
| 1235 | * |
| 1236 | * @return maximum number of pages. |
| 1237 | */ |
| 1238 | public int maxNumOfPages() { |
| 1239 | return (getNbRow() * getNbLines()); |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Returns the number of rows. |
| 1244 | * |
| 1245 | * @return number of rows. |
| 1246 | */ |
| 1247 | public int getNbRow() { |
| 1248 | if (!fSetHPagesNumber.isDisposed()) { |
| 1249 | int cw = (int) (fOverviewCanvas.getContentsWidth() / fOverviewCanvas.fZoomValue); |
| 1250 | int row = 1; |
| 1251 | if (fStepX != 0) { |
| 1252 | row = (int) (cw / fStepX); |
| 1253 | if (fSetHPagesNumber.getSelection()) { |
| 1254 | row = Math.round(cw / fStepX); |
| 1255 | } else if ((cw % fStepX != 0)) { |
| 1256 | row++; |
| 1257 | } |
| 1258 | } |
| 1259 | fNbRows = row; |
| 1260 | } |
| 1261 | return fNbRows; |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * Returns the number of lines. |
| 1266 | * |
| 1267 | * @return number of lines |
| 1268 | */ |
| 1269 | public int getNbLines() { |
| 1270 | if (!fSetVPagesNumber.isDisposed()) { |
| 1271 | int ch = (int) (fOverviewCanvas.getContentsHeight() / fOverviewCanvas.fZoomValue); |
| 1272 | int line = 1; |
| 1273 | if (fStepY != 0) { |
| 1274 | line = (int) (ch / fStepY); |
| 1275 | if (fSetVPagesNumber.getSelection()) { |
| 1276 | line = Math.round(ch / fStepY); |
| 1277 | } else if (ch % fStepY != 0) { |
| 1278 | line++; |
| 1279 | } |
| 1280 | } |
| 1281 | fNbLines = line; |
| 1282 | } |
| 1283 | return fNbLines; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * Returns whether to print all pages or not. |
| 1288 | * |
| 1289 | * @return <code>true</code> for all pages else <code>false</code>. |
| 1290 | */ |
| 1291 | public boolean printAll() { |
| 1292 | return fPrintAll; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * Returns whether to print only current page |
| 1297 | * |
| 1298 | * @return <code>true</code> for current page only else <code>false</code>.. |
| 1299 | */ |
| 1300 | public boolean printCurrent() { |
| 1301 | return fPrintCurrent; |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Returns whether to print selected pages. |
| 1306 | * |
| 1307 | * @return <code>true</code> for selected pages only else <code>false</code>. |
| 1308 | */ |
| 1309 | public boolean printSelection() { |
| 1310 | return fPrintSelection; |
| 1311 | } |
| 1312 | |
| 1313 | /** |
| 1314 | * Returns whether to print range of pages. |
| 1315 | * |
| 1316 | * @return <code>true</code> for range of pages only else <code>false</code>. |
| 1317 | */ |
| 1318 | public boolean printRange() { |
| 1319 | return fPrintRange; |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * Returns the step in X direction. |
| 1324 | * |
| 1325 | * @return step in X direction |
| 1326 | */ |
| 1327 | public float getStepX() { |
| 1328 | return sTX; |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * Returns the step in Y direction. |
| 1333 | * |
| 1334 | * @return step in Y direction |
| 1335 | */ |
| 1336 | public float getStepY() { |
| 1337 | return sTY; |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * Returns the zoom factor |
| 1342 | * |
| 1343 | * @return zoom factor |
| 1344 | */ |
| 1345 | public float getZoomFactor() { |
| 1346 | return fZoomFactor; |
| 1347 | } |
| 1348 | |
| 1349 | /** |
| 1350 | * Returns the printer data reference. |
| 1351 | * |
| 1352 | * @return printer data reference |
| 1353 | */ |
| 1354 | public PrinterData getPrinterData() { |
| 1355 | return fPrinterData; |
| 1356 | } |
| 1357 | |
| 1358 | /** |
| 1359 | * Returns the page number to start printing from. |
| 1360 | * |
| 1361 | * @return page number to start printing from |
| 1362 | */ |
| 1363 | public int getFrom() { |
| 1364 | return fFrom; |
| 1365 | } |
| 1366 | |
| 1367 | /** |
| 1368 | * Returns the page number to print to. |
| 1369 | * |
| 1370 | * @return page number to print to |
| 1371 | */ |
| 1372 | public int getTo() { |
| 1373 | return fTo; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Displays current number of pages |
| 1378 | */ |
| 1379 | protected void displayPageNum() { |
| 1380 | if (fPageNum > 0) { |
| 1381 | String message = MessageFormat.format(SDMessages._117, new Object[] { Integer.valueOf(fPageNum) }); |
| 1382 | fCurrentSelection.setText(message); |
| 1383 | fCurrentSelection.getParent().layout(); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * Returns the shell reference. |
| 1389 | * |
| 1390 | * @return the shell reference. |
| 1391 | */ |
| 1392 | public Shell getShell() { |
| 1393 | return fShell; |
| 1394 | } |
| 1395 | |
| 1396 | /** |
| 1397 | * Sets the shell. |
| 1398 | * |
| 1399 | * @param shell The shell reference. |
| 1400 | */ |
| 1401 | public void setShell(Shell shell) { |
| 1402 | fShell = shell; |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * Handle selection of print button. |
| 1407 | */ |
| 1408 | public void printButtonSelected() { |
| 1409 | PrintDialog printer = new PrintDialog(getShell()); |
| 1410 | if (fAllPages.getSelection()) { |
| 1411 | printer.setScope(PrinterData.ALL_PAGES); |
| 1412 | } |
| 1413 | if (fCurrentPage.getSelection()) { |
| 1414 | printer.setScope(PrinterData.SELECTION); |
| 1415 | } |
| 1416 | if (fPageList.getSelection()) { |
| 1417 | printer.setScope(PrinterData.SELECTION); |
| 1418 | } |
| 1419 | if (fPageRange.getSelection()) { |
| 1420 | printer.setScope(PrinterData.PAGE_RANGE); |
| 1421 | fFrom = Integer.valueOf(fFromPage.getText()).intValue(); |
| 1422 | fTo = Integer.valueOf(fToPage.getText()).intValue(); |
| 1423 | printer.setStartPage(fFrom); |
| 1424 | printer.setEndPage(fTo); |
| 1425 | } |
| 1426 | |
| 1427 | PrinterData newPrinterData = printer.open(); |
| 1428 | if (newPrinterData != null) { |
| 1429 | fPrinterData = newPrinterData; |
| 1430 | } |
| 1431 | updatePrinterStatus(); |
| 1432 | |
| 1433 | if (printer.getScope() == PrinterData.ALL_PAGES) { |
| 1434 | fAllPages.setSelection(true); |
| 1435 | fCurrentPage.setSelection(false); |
| 1436 | fPageList.setSelection(false); |
| 1437 | fPageRange.setSelection(false); |
| 1438 | fHorPagesNum.setEnabled(false); |
| 1439 | fVertPagesNum.setEnabled(false); |
| 1440 | } |
| 1441 | if (printer.getScope() == PrinterData.PAGE_RANGE) { |
| 1442 | fAllPages.setSelection(false); |
| 1443 | fCurrentPage.setSelection(false); |
| 1444 | fPageList.setSelection(false); |
| 1445 | fPageRange.setSelection(true); |
| 1446 | fFromPage.setEnabled(true); |
| 1447 | fToPage.setEnabled(true); |
| 1448 | fFromPage.setText((Integer.valueOf(printer.getStartPage())).toString()); |
| 1449 | fToPage.setText((Integer.valueOf(printer.getEndPage())).toString()); |
| 1450 | } |
| 1451 | computeStepXY(); |
| 1452 | fOverviewCanvas.redraw(); |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * Sets parent wizard page |
| 1457 | * |
| 1458 | * @param parent The parent wizard page |
| 1459 | */ |
| 1460 | public void setParentWizardPage(WizardPage parent) { |
| 1461 | fParentWizardPage = parent; |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * Sets the parent dialog box. |
| 1466 | * |
| 1467 | * @param parent The parent dialog box. |
| 1468 | */ |
| 1469 | public void setParentDialog(SDPrintDialog parent) { |
| 1470 | fParentDialog = parent; |
| 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * Updates the printer status |
| 1475 | */ |
| 1476 | protected void updatePrinterStatus() { |
| 1477 | if (fParentWizardPage != null) { |
| 1478 | // used in the wizard dialog |
| 1479 | if (fPrinterData == null) { |
| 1480 | // show error message and disable Finish button |
| 1481 | fParentWizardPage.setErrorMessage(SDMessages._135); |
| 1482 | fParentWizardPage.setPageComplete(false); |
| 1483 | } else { |
| 1484 | // clear error message and enable Finish button |
| 1485 | fParentWizardPage.setErrorMessage(null); |
| 1486 | fParentWizardPage.setPageComplete(true); |
| 1487 | } |
| 1488 | } else if (fParentDialog != null) { |
| 1489 | // used in the print dialog |
| 1490 | if (fPrinterData == null) { |
| 1491 | // show error message and disable OK button |
| 1492 | fParentDialog.setErrorMessage(SDMessages._135); |
| 1493 | fParentDialog.setPageComplete(false); |
| 1494 | } else { |
| 1495 | // clear error message and enable OK button |
| 1496 | fParentDialog.setErrorMessage(null); |
| 1497 | fParentDialog.setPageComplete(true); |
| 1498 | } |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | } |