1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
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
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.dialogs
;
16 import java
.text
.MessageFormat
;
17 import java
.util
.Arrays
;
19 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
20 import org
.eclipse
.jface
.wizard
.WizardPage
;
21 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.DiagramToolTip
;
22 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.NGC
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.SDWidget
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SDMessages
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.events
.FocusEvent
;
27 import org
.eclipse
.swt
.events
.FocusListener
;
28 import org
.eclipse
.swt
.events
.KeyEvent
;
29 import org
.eclipse
.swt
.events
.ModifyEvent
;
30 import org
.eclipse
.swt
.events
.ModifyListener
;
31 import org
.eclipse
.swt
.events
.MouseEvent
;
32 import org
.eclipse
.swt
.events
.MouseTrackListener
;
33 import org
.eclipse
.swt
.events
.SelectionEvent
;
34 import org
.eclipse
.swt
.events
.SelectionListener
;
35 import org
.eclipse
.swt
.events
.TraverseEvent
;
36 import org
.eclipse
.swt
.events
.TraverseListener
;
37 import org
.eclipse
.swt
.graphics
.GC
;
38 import org
.eclipse
.swt
.graphics
.Image
;
39 import org
.eclipse
.swt
.graphics
.ImageData
;
40 import org
.eclipse
.swt
.graphics
.RGB
;
41 import org
.eclipse
.swt
.graphics
.Rectangle
;
42 import org
.eclipse
.swt
.layout
.GridData
;
43 import org
.eclipse
.swt
.layout
.GridLayout
;
44 import org
.eclipse
.swt
.printing
.PrintDialog
;
45 import org
.eclipse
.swt
.printing
.Printer
;
46 import org
.eclipse
.swt
.printing
.PrinterData
;
47 import org
.eclipse
.swt
.widgets
.Button
;
48 import org
.eclipse
.swt
.widgets
.Composite
;
49 import org
.eclipse
.swt
.widgets
.Control
;
50 import org
.eclipse
.swt
.widgets
.Display
;
51 import org
.eclipse
.swt
.widgets
.Group
;
52 import org
.eclipse
.swt
.widgets
.Label
;
53 import org
.eclipse
.swt
.widgets
.Shell
;
54 import org
.eclipse
.swt
.widgets
.Text
;
57 * The class implements the actual print dialog UI for collecting printing data.
62 public class SDPrintDialogUI
{
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 * The set horizontal pages number.
69 protected Button fSetHPagesNumber
;
71 * The set vertical pages number.
73 protected Button fSetVPagesNumber
;
75 * Flag whether to use current zoom or not.
77 protected Button fUseCurrentZoom
;
79 * Flag whether to print all pages or not
81 protected Button fAllPages
;
83 * Flag whether to print current page only
85 protected Button fCurrentPage
;
87 * Button to select a page list.
89 protected Button fPageList
;
91 * Button to select a page range.
93 protected Button fPageRange
;
95 * Text field to enter from page.
97 protected Text fFromPage
;
99 * Text field to enter to page.
101 protected Text fToPage
;
103 * The sequence diagram widget reference.
105 protected SDWidget fSdView
;
107 * Text field for number of horizontal pages
109 protected Text fHorPagesNum
;
111 * Text field for number of vertical pages
113 protected Text fVertPagesNum
;
115 * Text field for toal number of pages
117 protected Text fTotalPages
;
119 * A modify listener implementation to handle modifications.
121 protected ModifyListener fModifyListener
;
123 * A selection listener implementation to handle selections.
125 protected SelectionListener fSelectionListener
;
127 * Local canvas displaying sequence diagram overview.
129 protected LocalSD fOverviewCanvas
;
133 protected int fNbPages
= 0;
135 * Number of selected pages.
137 protected int fPageNum
= -1;
139 * Number of first page.
141 protected int fFirstPage
= -1;
143 * List of pages to print.
145 protected int fPagesList
[];
148 * Value for dividing the sequence diagram into pages
150 protected float fStepX
;
153 * Value for dividing the sequence diagram into pages
155 protected float fStepY
;
158 * Value for dividing the sequence diagram into pages
163 * Value for dividing the sequence diagram into pages
168 * Page which to print from.
172 * Page which to print to.
176 * Flag for enabling multi-selection.
178 protected boolean fMultiSelection
= false;
180 * Flag for enabling area selection.
182 protected boolean fAreaSelection
= false;
184 * Flag for printing all.
186 protected boolean fPrintAll
;
188 * Flag for printing current page only.
190 protected boolean fPrintCurrent
;
192 * Flag for printing a selection of pages.
194 protected boolean fPrintSelection
;
196 * Flag for printing a range of pages.
198 protected boolean fPrintRange
;
200 * Number of selected rows
202 protected int fNbRows
;
204 * Number of selected lines
206 protected int fNbLines
;
210 protected float fZoomFactor
;
212 * The printer data reference.
214 protected PrinterData fPrinterData
;
216 * The diagram tooltip to show if necessary.
218 protected DiagramToolTip fToolTip
= null;
220 * Label for current selection.
222 protected Label fCurrentSelection
;
224 * The shell reference.
226 protected Shell fShell
;
228 * Button to open printer dialog from OS.
230 protected Button fPrinterDialog
;
232 * Flag for showing print button.
234 protected boolean fShowPrintButton
;
238 protected int fTest
= 3;
240 * Parent wizard page if used as wizard
242 protected WizardPage fParentWizardPage
= null;
244 * Reference to parent print dialog.
246 protected SDPrintDialog fParentDialog
= null;
248 // ------------------------------------------------------------------------
250 // ------------------------------------------------------------------------
252 * Local sequence diagram widget used to display overview of sequence diagram to print.
255 protected class LocalSD
extends SDWidget
{
259 * @param c Parent composite
260 * @param s Style bits
262 public LocalSD(Composite c
, int s
) {
268 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsHeight()
271 public int getContentsHeight() {
272 if (fSdView
.getContentsHeight() > fSdView
.getContentsHeight()) {
273 return (int) (fSdView
.getVisibleHeight() / (float) fTest
/ fSdView
.fZoomValue
);
275 return super.getContentsHeight();
280 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsWidth()
283 public int getContentsWidth() {
284 if (fSdView
.getVisibleWidth() > fSdView
.getContentsWidth()) {
285 return (int) (fSdView
.getVisibleWidth() / (float) fTest
/ fSdView
.fZoomValue
);
287 return super.getContentsWidth();
292 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseHover(org.eclipse.swt.events.MouseEvent)
295 protected void contentsMouseHover(MouseEvent event
) {
299 * Creates page selection images.
301 * @param img - Overview image
302 * @param width -The width value
303 * @param stepX - Step X
304 * @param height - Height value
305 * @param stepY - Step Y
308 protected Image
createPagesSelectionImages(Image img
, int width
, float stepX
, int height
, float stepY
) {
310 Image over
= new Image(super.getShell().getDisplay(), img
.getImageData());
312 for (int pageIndex
= 0; pageIndex
< fPagesList
.length
; pageIndex
++) {
314 int pageNum
= fPagesList
[pageIndex
];
316 if (getPagesForSelection() > 0 && pageNum
> 0) {
317 int line
= pageNum
/ getNbRow();
318 int row
= pageNum
% getNbRow();
329 if (fOverviewCanvas
.isFocusControl()) {
330 over
= new Image(super.getShell().getDisplay(), drawRegionSelected(toDel
, new Rectangle(contentsToViewX((int) (row
* stepX
* fOverviewCanvas
.fZoomValue
)), contentsToViewY((int) (line
* stepY
* fOverviewCanvas
.fZoomValue
)),
331 ((int) (stepX
* fOverviewCanvas
.fZoomValue
)), ((int) (stepY
* fOverviewCanvas
.fZoomValue
))), new RGB(0, 0, 128)));
333 over
= new Image(super.getShell().getDisplay(), drawRegionSelected(toDel
, new Rectangle(contentsToViewX((int) (row
* stepX
* fOverviewCanvas
.fZoomValue
)), contentsToViewY((int) (line
* stepY
* fOverviewCanvas
.fZoomValue
)),
334 ((int) (stepX
* fOverviewCanvas
.fZoomValue
)), ((int) (stepY
* fOverviewCanvas
.fZoomValue
))), new RGB(221, 208, 200)));
340 Arrays
.sort(fPagesList
);
341 int pos
= Arrays
.binarySearch(fPagesList
, fPageNum
);
342 if ((pos
< 0) && (getPagesForSelection() > 0 && fPageNum
> 0)) {
343 int line
= fPageNum
/ getNbRow();
344 int row
= fPageNum
% getNbRow();
355 over
= new Image(super.getShell().getDisplay(), drawRegionSelected(toDel
, new Rectangle(contentsToViewX((int) (row
* stepX
* fOverviewCanvas
.fZoomValue
)), contentsToViewY((int) (line
* stepY
* fOverviewCanvas
.fZoomValue
)),
356 ((int) (stepX
* fOverviewCanvas
.fZoomValue
)), ((int) (stepY
* fOverviewCanvas
.fZoomValue
))), new RGB(221, 208, 200)));
360 GC imGC2
= new GC(over
);
361 imGC2
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
362 NGC imGC
= new NGC(fOverviewCanvas
, imGC2
);
363 for (int i
= 0, x
= 0; x
<= width
&& stepX
> 0; i
++, x
= (int) (i
* stepX
)) {
364 imGC
.drawLine(x
, 0, x
, height
);
367 for (int j
= 0, y
= 0; y
<= height
&& stepY
> 0; j
++, y
= (int) (j
* stepY
)) {
368 imGC
.drawLine(0, y
, width
, y
);
378 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#drawContents(org.eclipse.swt.graphics.GC, int, int, int, int)
381 protected void drawContents(GC gc
, int clipx
, int clipy
, int clipw
, int cliph
) {
383 Image dbuffer
= getDrawBuffer();
387 int lw
= (int) (getContentsWidth() / fZoomValue
);
388 if (getContentsWidth() < getVisibleWidth()) {
389 lw
= (int) (getVisibleWidth() / fZoomValue
);
392 int lh
= (int) (getContentsHeight() / fZoomValue
);
393 if (getContentsHeight() < getVisibleHeight()) {
394 lh
= (int) (getVisibleHeight() / fZoomValue
);
396 d
= createPagesSelectionImages(dbuffer
, lw
, fStepX
, lh
, fStepY
);
400 d
= new Image(super.getShell().getDisplay(), drawRegionSelected(d
, new Rectangle(0, 0, lw
, lh
), new RGB(221, 208, 200)));
404 Rectangle area
= getClientArea();
405 int w
= d
.getBounds().width
;
406 int h
= d
.getBounds().height
;
407 gc
.drawImage(d
, 0, 0, w
, h
, 0, 0, area
.width
, area
.height
);
409 fTotalPages
.setText(Integer
.valueOf(maxNumOfPages()).toString());
419 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#keyPressedEvent(org.eclipse.swt.events.KeyEvent)
422 protected void keyPressedEvent(KeyEvent e
) {
423 if (e
.keyCode
== SWT
.CTRL
) {
424 fMultiSelection
= true;
426 if (e
.keyCode
== SWT
.SHIFT
) {
427 fAreaSelection
= true;
429 if (e
.keyCode
== SWT
.ARROW_DOWN
) {
430 if (fPageNum
+ getNbRow() <= maxNumOfPages()) {
431 fPageNum
+= getNbRow();
433 int line
= fPageNum
/ getNbRow();
434 int row
= fPageNum
% getNbRow();
438 if ((line
+ 1) * fStepY
> (fOverviewCanvas
.getContentsY() + fOverviewCanvas
.getVisibleHeight()) / fOverviewCanvas
.fZoomValue
) {
439 fOverviewCanvas
.scrollBy(0, (int) (fStepY
* fOverviewCanvas
.fZoomValue
));
442 if (e
.keyCode
== SWT
.ARROW_UP
) {
443 if (fPageNum
- getNbRow() > 0) {
444 fPageNum
-= getNbRow();
446 int line
= fPageNum
/ getNbRow();
447 int row
= fPageNum
% getNbRow();
451 if ((line
) * fStepY
<= fOverviewCanvas
.getContentsY() / fOverviewCanvas
.fZoomValue
) {
452 fOverviewCanvas
.scrollBy(0, -(int) (fStepY
* fOverviewCanvas
.fZoomValue
));
455 if (e
.keyCode
== SWT
.ARROW_LEFT
) {
456 if ((fPageNum
- 2) / getNbRow() == (fPageNum
- 1) / getNbRow() && fPageNum
> 1) {
459 int row
= fPageNum
% getNbRow();
460 if ((row
- 1) * fStepX
< (fOverviewCanvas
.getContentsX()) / fOverviewCanvas
.fZoomValue
) {
461 fOverviewCanvas
.scrollBy(-(int) (fStepX
* fOverviewCanvas
.fZoomValue
), 0);
464 if (e
.keyCode
== SWT
.ARROW_RIGHT
) {
465 if ((fPageNum
- 1) / getNbRow() == fPageNum
/ getNbRow()) {
468 int row
= fPageNum
% getNbRow();
472 if ((row
) * fStepX
> (fOverviewCanvas
.getContentsX() + fOverviewCanvas
.getVisibleWidth()) / fOverviewCanvas
.fZoomValue
) {
473 fOverviewCanvas
.scrollBy((int) (fStepX
* fOverviewCanvas
.fZoomValue
), 0);
477 if (e
.keyCode
== 32 && fPageNum
> -1) {
478 Arrays
.sort(fPagesList
);
479 int pos
= Arrays
.binarySearch(fPagesList
, fPageNum
);
481 addToPagesList(fPageNum
);
483 removeFromPagesList(fPageNum
);
487 if (!fAreaSelection
&& !fMultiSelection
) {
488 fFirstPage
= fPageNum
;
489 fPagesList
= new int[1];
490 fPagesList
[0] = fPageNum
;
491 } else if ((fPageNum
!= -1) && (fAreaSelection
) && (fFirstPage
!= -1)) {
492 fPagesList
= new int[0];
493 int line1
= fFirstPage
/ getNbRow();
494 int row1
= fFirstPage
% getNbRow();
501 int line2
= fPageNum
/ getNbRow();
502 int row2
= fPageNum
% getNbRow();
522 for (int i
= row1
- 1; i
< row2
; i
++) {
523 for (int j
= line1
- 1; j
< line2
; j
++) {
524 addToPagesList(i
+ j
* getNbRow() + 1);
529 fOverviewCanvas
.redraw();
534 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#keyReleasedEvent(org.eclipse.swt.events.KeyEvent)
537 protected void keyReleasedEvent(KeyEvent e
) {
538 if (e
.keyCode
== SWT
.CTRL
) {
539 fMultiSelection
= false;
541 if (e
.keyCode
== SWT
.SHIFT
) {
542 fAreaSelection
= false;
548 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseDownEvent(org.eclipse.swt.events.MouseEvent)
551 protected void contentsMouseDownEvent(MouseEvent event
) {
554 int x1
= (int) ((event
.x
/ fOverviewCanvas
.fZoomValue
) / fStepX
);
555 int x2
= (int) ((event
.y
/ fOverviewCanvas
.fZoomValue
) / fStepY
);
557 int oldPage
= fPageNum
;
559 fPageNum
= x1
+ x2
* getNbRow() + 1;
561 if (fPageNum
> maxNumOfPages()) {
566 if (!fAreaSelection
) {
567 fFirstPage
= fPageNum
;
570 if ((fPageNum
!= -1) && (fMultiSelection
)) {
571 Arrays
.sort(fPagesList
);
572 int pos
= Arrays
.binarySearch(fPagesList
, fPageNum
);
574 addToPagesList(fPageNum
);
576 removeFromPagesList(fPageNum
);
578 } else if ((fPageNum
!= -1) && (fAreaSelection
) && (fFirstPage
!= -1)) {
580 fPagesList
= new int[0];
582 int line1
= fFirstPage
/ getNbRow();
583 int row1
= fFirstPage
% getNbRow();
590 int line2
= fPageNum
/ getNbRow();
591 int row2
= fPageNum
% getNbRow();
611 for (int i
= row1
- 1; i
< row2
; i
++) {
612 for (int j
= line1
- 1; j
< line2
; j
++) {
613 addToPagesList(i
+ j
* getNbRow() + 1);
617 fPagesList
= new int[1];
618 fPagesList
[0] = fPageNum
;
620 if ((event
.stateMask
& SWT
.CTRL
) != 0) {
621 fMultiSelection
= true;
629 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseMoveEvent(org.eclipse.swt.events.MouseEvent)
632 protected void contentsMouseMoveEvent(MouseEvent e
) {
633 fToolTip
.hideToolTip();
638 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#resizeContents(int, int)
641 public void resizeContents(int w
, int h
) {
642 super.resizeContents(w
, h
);
648 * A traverse listener implementation.
650 protected static class LocalTraverseListener
implements TraverseListener
{
653 * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
656 public void keyTraversed(TraverseEvent e
) {
657 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
663 // ------------------------------------------------------------------------
665 // ------------------------------------------------------------------------
670 * The shell reference
672 * The sequence diagram widget reference
674 public SDPrintDialogUI(Shell shell
, SDWidget sdWidget
) {
675 this(shell
, sdWidget
, false);
682 * The shell reference
684 * The sequence diagram widget reference
685 * @param showPrintBtn
686 * Flag for showing print buttons
688 public SDPrintDialogUI(Shell shell
, SDWidget sdWidget
, boolean showPrintBtn
) {
691 fShowPrintButton
= showPrintBtn
;
693 fPrinterData
= Printer
.getDefaultPrinterData();
694 if (fPrinterData
!= null) {
695 fPrinterData
.scope
= PrinterData
.SELECTION
;
698 fPagesList
= new int[0];
700 fSelectionListener
= new SelectionListener() {
703 public void widgetSelected(SelectionEvent e
) {
704 if (fUseCurrentZoom
.getSelection()) {
705 fHorPagesNum
.setEnabled(false);
706 fVertPagesNum
.setEnabled(false);
708 if (fSetHPagesNumber
.getSelection()) {
709 fHorPagesNum
.setEnabled(true);
710 fVertPagesNum
.setEnabled(false);
711 if (fCurrentPage
.getSelection()) {
712 fCurrentPage
.setSelection(false);
713 fAllPages
.setSelection(true);
715 if ("".equals(fHorPagesNum
.getText())) { //$NON-NLS-1$
716 fHorPagesNum
.setText("1"); //$NON-NLS-1$
719 if (fSetVPagesNumber
.getSelection()) {
720 fHorPagesNum
.setEnabled(false);
721 fVertPagesNum
.setEnabled(true);
722 if (fCurrentPage
.getSelection()) {
723 fCurrentPage
.setSelection(false);
724 fAllPages
.setSelection(true);
726 if ("".equals(fVertPagesNum
.getText())) { //$NON-NLS-1$
727 fVertPagesNum
.setText("1"); //$NON-NLS-1$
730 if (fCurrentPage
.getSelection() || fAllPages
.getSelection() || fPageList
.getSelection()) {
731 fFromPage
.setEnabled(false);
732 fToPage
.setEnabled(false);
734 fFromPage
.setEnabled(true);
735 fToPage
.setEnabled(true);
738 fCurrentPage
.setEnabled(fUseCurrentZoom
.getSelection());
739 fOverviewCanvas
.setEnabled(fPageList
.getSelection());
740 if (fOverviewCanvas
.isEnabled() && (e
.widget
== fUseCurrentZoom
|| e
.widget
== fSetHPagesNumber
|| e
.widget
== fSetVPagesNumber
)) {
741 fPagesList
= new int[1];
745 } else if ((fOverviewCanvas
.isEnabled() && (e
.widget
== fPageList
)) &&
746 (fPagesList
== null || fPagesList
.length
<= 0)) {
748 fPagesList
= new int[1];
754 fTotalPages
.setText(Integer
.valueOf(maxNumOfPages()).toString());
755 fOverviewCanvas
.redraw();
756 fOverviewCanvas
.update();
761 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
764 public void widgetDefaultSelected(SelectionEvent e
) {
765 fPagesList
= new int[0];
767 fOverviewCanvas
.redraw();
772 fModifyListener
= new ModifyListener() {
775 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
778 public void modifyText(ModifyEvent e
) {
779 fPagesList
= new int[0];
781 fTotalPages
.setText(Integer
.valueOf(maxNumOfPages()).toString());
782 fOverviewCanvas
.redraw();
788 // ------------------------------------------------------------------------
790 // ------------------------------------------------------------------------
793 * Creates new grid data object.
795 * @param span horizontal span.
798 protected GridData
newGridData(int span
) {
799 GridData data
= new GridData(GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
);
800 data
.horizontalSpan
= span
;
805 * Creates the dialog area.
807 * @param parent The parent composite
808 * @return dialog control
810 public Control
createDialogArea(Composite parent
) {
812 GridLayout parentLayout
= new GridLayout();
813 parentLayout
.numColumns
= 6;
814 parent
.setLayout(parentLayout
);
816 Group g1
= new Group(parent
, SWT
.SHADOW_NONE
);
817 g1
.setText(SDMessages
._113
);
818 g1
.setLayoutData(newGridData(3));
819 GridLayout g1layout
= new GridLayout();
820 g1layout
.numColumns
= 2;
821 g1
.setLayout(g1layout
);
823 fUseCurrentZoom
= new Button(g1
, SWT
.RADIO
);
824 fUseCurrentZoom
.setText(SDMessages
._112
);
825 fUseCurrentZoom
.setLayoutData(newGridData(2));
826 fUseCurrentZoom
.addSelectionListener(fSelectionListener
);
828 fSetHPagesNumber
= new Button(g1
, SWT
.RADIO
);
829 fSetHPagesNumber
.setText(SDMessages
._110
);
830 fSetHPagesNumber
.setLayoutData(newGridData(1));
831 fSetHPagesNumber
.addSelectionListener(fSelectionListener
);
833 fHorPagesNum
= new Text(g1
, SWT
.SINGLE
| SWT
.BORDER
);
834 fHorPagesNum
.addModifyListener(fModifyListener
);
836 fSetVPagesNumber
= new Button(g1
, SWT
.RADIO
);
837 fSetVPagesNumber
.setText(SDMessages
._111
);
838 fSetVPagesNumber
.setLayoutData(newGridData(1));
839 fSetVPagesNumber
.addSelectionListener(fSelectionListener
);
841 fVertPagesNum
= new Text(g1
, SWT
.SINGLE
| SWT
.BORDER
);
842 fVertPagesNum
.addModifyListener(fModifyListener
);
844 Label nbTotal
= new Label(g1
, SWT
.SHADOW_NONE
| SWT
.RIGHT
);
845 nbTotal
.setText(SDMessages
._109
);
846 // nbTotal.setLayoutData(newGridData(1));
848 fTotalPages
= new Text(g1
, SWT
.SINGLE
| SWT
.BORDER
| SWT
.READ_ONLY
);
849 // nbHV.addModifyListener(modifListener);
851 Group g2
= new Group(parent
, SWT
.SHADOW_NONE
);
852 g2
.setText(SDMessages
._119
);
853 GridData data
= new GridData(GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
);
854 data
.horizontalSpan
= 3;
855 data
.verticalSpan
= 2;
856 g2
.setLayoutData(data
);
857 GridLayout g2layout
= new GridLayout();
859 g2layout
.numColumns
= 1;
860 // SVLayout g2layout = new SVLayout();
861 g2
.setLayout(g2layout
);
863 GridData data2
= new GridData(GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
);
864 data2
.horizontalSpan
= 1;
865 data2
.verticalSpan
= 1;
867 fOverviewCanvas
= new LocalSD(g2
, SWT
.NO_BACKGROUND
);
868 GridData seqDiagLayoutData
= new GridData(GridData
.HORIZONTAL_ALIGN_FILL
| GridData
.VERTICAL_ALIGN_FILL
);
869 fOverviewCanvas
.setLayoutData(seqDiagLayoutData
);
870 // overviewCanvas.resizeContents(100,100);
871 if (fSdView
.getContentsWidth() < fSdView
.getVisibleWidth() && fSdView
.getContentsHeight() < fSdView
.getVisibleHeight()) {
876 fOverviewCanvas
.setFrame(fSdView
.getFrame(), true);
877 fOverviewCanvas
.fZoomValue
= (float) 1 / fTest
;
878 fOverviewCanvas
.setCornerControl(null);
879 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getContentsWidth() / fTest
;
880 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getFrame().getWidth() / fTest
+ 15;
882 if (fSdView
.getVisibleWidth() < fSdView
.getContentsWidth()) {
883 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getContentsWidth() / fTest
;
884 if (seqDiagLayoutData
.widthHint
> Display
.getDefault().getClientArea().width
/ 4) {
885 seqDiagLayoutData
.widthHint
= Display
.getDefault().getClientArea().width
/ 4;
888 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getFrame().getWidth() / fTest
+ 15;
891 if (fSdView
.getVisibleHeight() < fSdView
.getContentsHeight()) {
892 seqDiagLayoutData
.heightHint
= fOverviewCanvas
.getContentsHeight() / fTest
;
893 if (seqDiagLayoutData
.heightHint
> Display
.getDefault().getClientArea().width
/ 4) {
894 seqDiagLayoutData
.heightHint
= Display
.getDefault().getClientArea().width
/ 4;
897 seqDiagLayoutData
.heightHint
= fOverviewCanvas
.getFrame().getHeight() / fTest
;
900 fOverviewCanvas
.setEnabled(false);
902 fCurrentSelection
= new Label(g2
, SWT
.SHADOW_NONE
| SWT
.LEFT
);
903 fCurrentSelection
.setLayoutData(newGridData(1));
905 Group g3
= new Group(parent
, SWT
.SHADOW_NONE
);
906 g3
.setText(SDMessages
._118
);
907 g3
.setLayoutData(newGridData(3));
908 GridLayout g3layout
= new GridLayout();
909 g3layout
.numColumns
= 4;
910 g3
.setLayout(g3layout
);
912 fAllPages
= new Button(g3
, SWT
.RADIO
);
913 fAllPages
.setText(SDMessages
._108
);
914 fAllPages
.setLayoutData(newGridData(4));
915 fAllPages
.addSelectionListener(fSelectionListener
);
917 fCurrentPage
= new Button(g3
, SWT
.RADIO
);
918 fCurrentPage
.setText(SDMessages
._107
);
919 fCurrentPage
.setLayoutData(newGridData(4));
920 fCurrentPage
.setEnabled(true);
921 fCurrentPage
.setSelection(true);
922 fCurrentPage
.addSelectionListener(fSelectionListener
);
924 fPageList
= new Button(g3
, SWT
.RADIO
);
925 fPageList
.setText(SDMessages
._106
);
926 fPageList
.setLayoutData(newGridData(4));
927 fPageList
.addSelectionListener(fSelectionListener
);
929 fPageRange
= new Button(g3
, SWT
.RADIO
);
930 fPageRange
.setText(SDMessages
._103
);
931 fPageRange
.setLayoutData(newGridData(1));
932 fPageRange
.addSelectionListener(fSelectionListener
);
934 fFromPage
= new Text(g3
, SWT
.SINGLE
| SWT
.BORDER
);
936 Label labelTo
= new Label(g3
, SWT
.CENTER
);
937 labelTo
.setText(SDMessages
._105
);
939 fToPage
= new Text(g3
, SWT
.SINGLE
| SWT
.BORDER
);
941 fToolTip
= new DiagramToolTip(fOverviewCanvas
);
943 fOverviewCanvas
.getViewControl().addMouseTrackListener(new MouseTrackListener() {
946 * @see org.eclipse.swt.events.MouseTrackListener#mouseEnter(org.eclipse.swt.events.MouseEvent)
949 public void mouseEnter(MouseEvent e
) {
950 fToolTip
.hideToolTip();
955 * @see org.eclipse.swt.events.MouseTrackListener#mouseExit(org.eclipse.swt.events.MouseEvent)
958 public void mouseExit(MouseEvent e
) {
959 fToolTip
.hideToolTip();
964 * @see org.eclipse.swt.events.MouseTrackListener#mouseHover(org.eclipse.swt.events.MouseEvent)
967 public void mouseHover(MouseEvent e
) {
968 int x1
= (int) (fOverviewCanvas
.viewToContentsX(e
.x
) / fOverviewCanvas
.fZoomValue
/ fStepX
);
969 int x2
= (int) (fOverviewCanvas
.viewToContentsY(e
.y
) / fOverviewCanvas
.fZoomValue
/ fStepY
);
970 int num
= x1
+ x2
* getNbRow() + 1;
971 if (num
> maxNumOfPages()) {
975 fToolTip
.showToolTip(String
.valueOf(num
));
978 fCurrentSelection
.setText("");//$NON-NLS-1$
979 fToolTip
.hideToolTip();
985 fOverviewCanvas
.addTraverseListener(new LocalTraverseListener());
987 fOverviewCanvas
.addFocusListener(new FocusListener() {
990 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
993 public void focusGained(FocusEvent e
) {
994 fOverviewCanvas
.redraw();
999 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
1002 public void focusLost(FocusEvent e
) {
1003 fOverviewCanvas
.redraw();
1007 if (fShowPrintButton
) {
1008 Composite printerDlg
= new Composite(parent
, SWT
.NONE
);
1009 data
= new GridData(SWT
.FILL
, SWT
.DEFAULT
, true, false);
1010 data
.horizontalSpan
= 6;
1011 parentLayout
= new GridLayout();
1012 parentLayout
.numColumns
= 2;
1013 printerDlg
.setLayout(parentLayout
);
1014 printerDlg
.setLayoutData(data
);
1016 Label label
= new Label(printerDlg
, SWT
.NONE
);
1017 label
.setLayoutData(new GridData(SWT
.FILL
, SWT
.DEFAULT
, true, false));
1018 fPrinterDialog
= new Button(printerDlg
, SWT
.PUSH
);
1019 fPrinterDialog
.setText(SDMessages
._115
);
1021 fPrinterDialog
.addSelectionListener(new SelectionListener() {
1025 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
1028 public void widgetSelected(SelectionEvent e
) {
1029 printButtonSelected();
1034 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
1037 public void widgetDefaultSelected(SelectionEvent e
) {
1043 updatePrinterStatus();
1049 * Get number of pages for selection.
1050 * @return number of pages for selection.
1052 public int getPagesForSelection() {
1057 * Handler for when the OK button is pressed
1059 * @return True if the operation was successful, false if there was an error
1061 public boolean okPressed() {
1062 fPrintAll
= fAllPages
.getSelection();
1063 fPrintCurrent
= fCurrentPage
.getSelection();
1064 fPrintSelection
= fPageList
.getSelection();
1065 fPrintRange
= fPageRange
.getSelection();
1068 fFrom
= Integer
.valueOf(fFromPage
.getText()).intValue();
1069 fTo
= Integer
.valueOf(fToPage
.getText()).intValue();
1070 if (fFrom
> maxNumOfPages() || fTo
> maxNumOfPages() || fFrom
<= 0 || fTo
<= 0) {
1071 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._99
);
1074 } else if (fSetHPagesNumber
.getSelection() && fNbPages
<= 0) {
1075 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._101
);
1077 } else if (fSetVPagesNumber
.getSelection() && fNbPages
<= 0) {
1078 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._100
);
1080 } else if (fPrintSelection
&& getPageList().length
<= 0) {
1081 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._102
);
1085 } catch (Exception e
) {
1086 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._99
);
1096 * Draws region that was selected
1097 * @param img The corresponding image
1098 * @param r The selected rectangle.
1099 * @param color The color to use for selection
1100 * @return image data reference
1102 public ImageData
drawRegionSelected(Image img
, Rectangle r
, RGB color
) {
1103 ImageData id
= img
.getImageData();
1104 for (int a
= 0; a
< r
.width
&& r
.x
+ a
< id
.width
; a
++) {
1105 for (int b
= 0; b
< r
.height
&& r
.y
+ b
< id
.height
; b
++) {
1106 int index
= id
.getPixel(r
.x
+ a
, r
.y
+ b
);
1107 RGB rgb
= id
.palette
.getRGB(index
);
1108 rgb
= combine(color
, rgb
);
1109 id
.setPixel(r
.x
+ a
, r
.y
+ b
, id
.palette
.getPixel(rgb
));
1116 * Combines two RGB colors.
1117 * @param front The front color
1118 * @param back The back color
1119 * @return new RGB color
1121 public static RGB
combine(RGB front
, RGB back
) {
1125 double af
= (_af
) / 255.0;
1126 double rf
= front
.red
;
1127 double gf
= front
.green
;
1128 double bf
= front
.blue
;
1130 double ab
= (_ab
) / 255.0;
1131 double rb
= back
.red
;
1132 double gb
= back
.green
;
1133 double bb
= back
.blue
;
1135 double k
= (1.0 - af
) * ab
;
1136 int r
= (int) ((af
* rf
+ k
* rb
));
1137 int g
= (int) ((af
* gf
+ k
* gb
));
1138 int b
= (int) ((af
* bf
+ k
* bb
));
1140 return new RGB(r
, g
, b
);
1144 * Computes value for X coordinates step and Y coordinates step.
1146 protected void computeStepXY() {
1147 float cw
= fOverviewCanvas
.getContentsWidth() / fOverviewCanvas
.fZoomValue
;
1148 float ch
= fOverviewCanvas
.getContentsHeight() / fOverviewCanvas
.fZoomValue
;
1150 if (fPrinterData
== null) {
1156 Printer printer
= new Printer(fPrinterData
);
1157 if (fSetHPagesNumber
.getSelection()) {
1158 fNbPages
= Integer
.valueOf(fHorPagesNum
.getText()).intValue();
1159 float z1
= fSdView
.getContentsWidth() / cw
;
1160 float z2
= printer
.getClientArea().width
/ ((float) fSdView
.getContentsWidth() / fNbPages
);
1162 fStepY
= printer
.getClientArea().height
/ z1
/ z2
;
1163 fStepX
= cw
/ fNbPages
;
1164 } else if (fSetVPagesNumber
.getSelection()) {
1165 fNbPages
= Integer
.valueOf(fVertPagesNum
.getText()).intValue();
1166 float z1
= fSdView
.getContentsHeight() / ch
;
1167 float z2
= printer
.getClientArea().height
/ ((float) fSdView
.getContentsHeight() / fNbPages
);
1168 fStepX
= printer
.getClientArea().width
/ z1
/ z2
;
1169 fStepY
= ch
/ fNbPages
;
1171 float z1
= fSdView
.getContentsWidth() / (cw
);
1172 fStepX
= fSdView
.getVisibleWidth() / z1
;
1173 fNbPages
= Math
.round(cw
/ fStepX
);
1174 if (fNbPages
== 0) {
1177 int pw
= printer
.getClientArea().width
;
1178 int ph
= printer
.getClientArea().height
;
1179 float z2
= pw
/ ((float) fSdView
.getContentsWidth() / fNbPages
);
1180 fStepY
= ph
/ z1
/ z2
;
1183 } catch (NumberFormatException e
) {
1184 fStepX
= fStepY
= fNbPages
= 0;
1187 sTX
= fStepX
* (fSdView
.getContentsWidth() / cw
);
1188 sTY
= fStepY
* (fSdView
.getContentsHeight() / ch
);
1190 if ((fSdView
.getVisibleWidth() > fSdView
.getContentsWidth()) && (fSetVPagesNumber
.getSelection() || fSetHPagesNumber
.getSelection())) {
1191 rat
= (float) fSdView
.getVisibleWidth() / (float) fSdView
.getContentsWidth();
1193 fZoomFactor
= (fOverviewCanvas
.getContentsWidth() / cw
) / fOverviewCanvas
.getZoomFactor() * rat
;
1197 * Returns the pages list.
1199 * @return the pages list.
1201 public int[] getPageList() {
1202 return Arrays
.copyOf(fPagesList
, fPagesList
.length
);
1206 * Adds a page to pages list.
1209 * The number of the the new page
1211 public void addToPagesList(int num
) {
1212 int temp
[] = new int[fPagesList
.length
+ 1];
1213 System
.arraycopy(fPagesList
, 0, temp
, 0, fPagesList
.length
);
1214 temp
[temp
.length
- 1] = num
;
1215 fPagesList
= new int[temp
.length
];
1216 System
.arraycopy(temp
, 0, fPagesList
, 0, temp
.length
);
1220 * Removes a page from the pages list.
1223 * The number of the page to remove
1225 public void removeFromPagesList(int num
) {
1226 int pos
= Arrays
.binarySearch(fPagesList
, num
);
1227 int temp
[] = new int[fPagesList
.length
- 1];
1228 System
.arraycopy(fPagesList
, 0, temp
, 0, pos
);
1229 System
.arraycopy(fPagesList
, pos
+ 1, temp
, pos
, fPagesList
.length
- pos
- 1);
1230 fPagesList
= new int[temp
.length
];
1231 System
.arraycopy(temp
, 0, fPagesList
, 0, temp
.length
);
1235 * Returns the maximum number of pages.
1237 * @return maximum number of pages.
1239 public int maxNumOfPages() {
1240 return (getNbRow() * getNbLines());
1244 * Returns the number of rows.
1246 * @return number of rows.
1248 public int getNbRow() {
1249 if (!fSetHPagesNumber
.isDisposed()) {
1250 int cw
= (int) (fOverviewCanvas
.getContentsWidth() / fOverviewCanvas
.fZoomValue
);
1253 row
= (int) (cw
/ fStepX
);
1254 if (fSetHPagesNumber
.getSelection()) {
1255 row
= Math
.round(cw
/ fStepX
);
1256 } else if ((cw
% fStepX
!= 0)) {
1266 * Returns the number of lines.
1268 * @return number of lines
1270 public int getNbLines() {
1271 if (!fSetVPagesNumber
.isDisposed()) {
1272 int ch
= (int) (fOverviewCanvas
.getContentsHeight() / fOverviewCanvas
.fZoomValue
);
1275 line
= (int) (ch
/ fStepY
);
1276 if (fSetVPagesNumber
.getSelection()) {
1277 line
= Math
.round(ch
/ fStepY
);
1278 } else if (ch
% fStepY
!= 0) {
1288 * Returns whether to print all pages or not.
1290 * @return <code>true</code> for all pages else <code>false</code>.
1292 public boolean printAll() {
1297 * Returns whether to print only current page
1299 * @return <code>true</code> for current page only else <code>false</code>..
1301 public boolean printCurrent() {
1302 return fPrintCurrent
;
1306 * Returns whether to print selected pages.
1308 * @return <code>true</code> for selected pages only else <code>false</code>.
1310 public boolean printSelection() {
1311 return fPrintSelection
;
1315 * Returns whether to print range of pages.
1317 * @return <code>true</code> for range of pages only else <code>false</code>.
1319 public boolean printRange() {
1324 * Returns the step in X direction.
1326 * @return step in X direction
1328 public float getStepX() {
1333 * Returns the step in Y direction.
1335 * @return step in Y direction
1337 public float getStepY() {
1342 * Returns the zoom factor
1344 * @return zoom factor
1346 public float getZoomFactor() {
1351 * Returns the printer data reference.
1353 * @return printer data reference
1355 public PrinterData
getPrinterData() {
1356 return fPrinterData
;
1360 * Returns the page number to start printing from.
1362 * @return page number to start printing from
1364 public int getFrom() {
1369 * Returns the page number to print to.
1371 * @return page number to print to
1373 public int getTo() {
1378 * Displays current number of pages
1380 protected void displayPageNum() {
1382 String message
= MessageFormat
.format(SDMessages
._117
, new Object
[] { Integer
.valueOf(fPageNum
) });
1383 fCurrentSelection
.setText(message
);
1384 fCurrentSelection
.getParent().layout();
1389 * Returns the shell reference.
1391 * @return the shell reference.
1393 public Shell
getShell() {
1400 * @param shell The shell reference.
1402 public void setShell(Shell shell
) {
1407 * Handle selection of print button.
1409 public void printButtonSelected() {
1410 PrintDialog printer
= new PrintDialog(getShell());
1411 if (fAllPages
.getSelection()) {
1412 printer
.setScope(PrinterData
.ALL_PAGES
);
1414 if (fCurrentPage
.getSelection()) {
1415 printer
.setScope(PrinterData
.SELECTION
);
1417 if (fPageList
.getSelection()) {
1418 printer
.setScope(PrinterData
.SELECTION
);
1420 if (fPageRange
.getSelection()) {
1421 printer
.setScope(PrinterData
.PAGE_RANGE
);
1422 fFrom
= Integer
.valueOf(fFromPage
.getText()).intValue();
1423 fTo
= Integer
.valueOf(fToPage
.getText()).intValue();
1424 printer
.setStartPage(fFrom
);
1425 printer
.setEndPage(fTo
);
1428 PrinterData newPrinterData
= printer
.open();
1429 if (newPrinterData
!= null) {
1430 fPrinterData
= newPrinterData
;
1432 updatePrinterStatus();
1434 if (printer
.getScope() == PrinterData
.ALL_PAGES
) {
1435 fAllPages
.setSelection(true);
1436 fCurrentPage
.setSelection(false);
1437 fPageList
.setSelection(false);
1438 fPageRange
.setSelection(false);
1439 fHorPagesNum
.setEnabled(false);
1440 fVertPagesNum
.setEnabled(false);
1442 if (printer
.getScope() == PrinterData
.PAGE_RANGE
) {
1443 fAllPages
.setSelection(false);
1444 fCurrentPage
.setSelection(false);
1445 fPageList
.setSelection(false);
1446 fPageRange
.setSelection(true);
1447 fFromPage
.setEnabled(true);
1448 fToPage
.setEnabled(true);
1449 fFromPage
.setText((Integer
.valueOf(printer
.getStartPage())).toString());
1450 fToPage
.setText((Integer
.valueOf(printer
.getEndPage())).toString());
1453 fOverviewCanvas
.redraw();
1457 * Sets parent wizard page
1459 * @param parent The parent wizard page
1461 public void setParentWizardPage(WizardPage parent
) {
1462 fParentWizardPage
= parent
;
1466 * Sets the parent dialog box.
1468 * @param parent The parent dialog box.
1470 public void setParentDialog(SDPrintDialog parent
) {
1471 fParentDialog
= parent
;
1475 * Updates the printer status
1477 protected void updatePrinterStatus() {
1478 if (fParentWizardPage
!= null) {
1479 // used in the wizard dialog
1480 if (fPrinterData
== null) {
1481 // show error message and disable Finish button
1482 fParentWizardPage
.setErrorMessage(SDMessages
._135
);
1483 fParentWizardPage
.setPageComplete(false);
1485 // clear error message and enable Finish button
1486 fParentWizardPage
.setErrorMessage(null);
1487 fParentWizardPage
.setPageComplete(true);
1489 } else if (fParentDialog
!= null) {
1490 // used in the print dialog
1491 if (fPrinterData
== null) {
1492 // show error message and disable OK button
1493 fParentDialog
.setErrorMessage(SDMessages
._135
);
1494 fParentDialog
.setPageComplete(false);
1496 // clear error message and enable OK button
1497 fParentDialog
.setErrorMessage(null);
1498 fParentDialog
.setPageComplete(true);