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
[];
147 * Values for dividing sequence diagram into pages.
149 protected float fStepX
, fStepY
, sTX
, sTY
;
151 * Page which to print from.
155 * Page which to print to.
159 * Flag for enabling multi-selection.
161 protected boolean fMultiSelection
= false;
163 * Flag for enabling area selection.
165 protected boolean fAreaSelection
= false;
167 * Flag for printing all.
169 protected boolean fPrintAll
;
171 * Flag for printing current page only.
173 protected boolean fPrintCurrent
;
175 * Flag for printing a selection of pages.
177 protected boolean fPrintSelection
;
179 * Flag for printing a range of pages.
181 protected boolean fPrintRange
;
183 * Number of selected rows
185 protected int fNbRows
;
187 * Number of selected lines
189 protected int fNbLines
;
193 protected float fZoomFactor
;
195 * The printer data reference.
197 protected PrinterData fPrinterData
;
199 * The diagram tooltip to show if necessary.
201 protected DiagramToolTip fToolTip
= null;
203 * Label for current selection.
205 protected Label fCurrentSelection
;
207 * The shell reference.
209 protected Shell fShell
;
211 * Button to open printer dialog from OS.
213 protected Button fPrinterDialog
;
215 * Flag for showing print button.
217 protected boolean fShowPrintButton
;
221 protected int fTest
= 3;
223 * Parent wizard page if used as wizard
225 protected WizardPage fParentWizardPage
= null;
227 * Reference to parent print dialog.
229 protected SDPrintDialog fParentDialog
= null;
231 // ------------------------------------------------------------------------
233 // ------------------------------------------------------------------------
235 * Local sequence diagram widget used to display overview of sequence diagram to print.
238 protected class LocalSD
extends SDWidget
{
242 * @param c Parent composite
243 * @param s Style bits
245 public LocalSD(Composite c
, int s
) {
251 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsHeight()
254 public int getContentsHeight() {
255 if (fSdView
.getContentsHeight() > fSdView
.getContentsHeight()) {
256 return (int) (fSdView
.getVisibleHeight() / (float) fTest
/ fSdView
.fZoomValue
);
258 return super.getContentsHeight();
263 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsWidth()
266 public int getContentsWidth() {
267 if (fSdView
.getVisibleWidth() > fSdView
.getContentsWidth()) {
268 return (int) (fSdView
.getVisibleWidth() / (float) fTest
/ fSdView
.fZoomValue
);
270 return super.getContentsWidth();
275 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseHover(org.eclipse.swt.events.MouseEvent)
278 protected void contentsMouseHover(MouseEvent event
) {
282 * Creates page selection images.
284 * @param img - Overview image
285 * @param width -The width value
286 * @param stepX - Step X
287 * @param height - Height value
288 * @param stepY - Step Y
291 protected Image
createPagesSelectionImages(Image img
, int width
, float stepX
, int height
, float stepY
) {
293 Image over
= new Image(super.getShell().getDisplay(), img
.getImageData());
295 for (int pageIndex
= 0; pageIndex
< fPagesList
.length
; pageIndex
++) {
297 int pageNum
= fPagesList
[pageIndex
];
299 if (getPagesForSelection() > 0 && pageNum
> 0) {
300 int line
= pageNum
/ getNbRow();
301 int row
= pageNum
% getNbRow();
312 if (fOverviewCanvas
.isFocusControl()) {
313 over
= new Image(super.getShell().getDisplay(), drawRegionSelected(toDel
, new Rectangle(contentsToViewX((int) (row
* stepX
* fOverviewCanvas
.fZoomValue
)), contentsToViewY((int) (line
* stepY
* fOverviewCanvas
.fZoomValue
)),
314 ((int) (stepX
* fOverviewCanvas
.fZoomValue
)), ((int) (stepY
* fOverviewCanvas
.fZoomValue
))), new RGB(0, 0, 128)));
316 over
= new Image(super.getShell().getDisplay(), drawRegionSelected(toDel
, new Rectangle(contentsToViewX((int) (row
* stepX
* fOverviewCanvas
.fZoomValue
)), contentsToViewY((int) (line
* stepY
* fOverviewCanvas
.fZoomValue
)),
317 ((int) (stepX
* fOverviewCanvas
.fZoomValue
)), ((int) (stepY
* fOverviewCanvas
.fZoomValue
))), new RGB(221, 208, 200)));
323 Arrays
.sort(fPagesList
);
324 int pos
= Arrays
.binarySearch(fPagesList
, fPageNum
);
325 if ((pos
< 0) && (getPagesForSelection() > 0 && fPageNum
> 0)) {
326 int line
= fPageNum
/ getNbRow();
327 int row
= fPageNum
% getNbRow();
338 over
= new Image(super.getShell().getDisplay(), drawRegionSelected(toDel
, new Rectangle(contentsToViewX((int) (row
* stepX
* fOverviewCanvas
.fZoomValue
)), contentsToViewY((int) (line
* stepY
* fOverviewCanvas
.fZoomValue
)),
339 ((int) (stepX
* fOverviewCanvas
.fZoomValue
)), ((int) (stepY
* fOverviewCanvas
.fZoomValue
))), new RGB(221, 208, 200)));
343 GC imGC2
= new GC(over
);
344 imGC2
.setBackground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
345 NGC imGC
= new NGC(fOverviewCanvas
, imGC2
);
346 for (int i
= 0, x
= 0; x
<= width
&& stepX
> 0; i
++, x
= (int) (i
* stepX
)) {
347 imGC
.drawLine(x
, 0, x
, height
);
350 for (int j
= 0, y
= 0; y
<= height
&& stepY
> 0; j
++, y
= (int) (j
* stepY
)) {
351 imGC
.drawLine(0, y
, width
, y
);
361 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#drawContents(org.eclipse.swt.graphics.GC, int, int, int, int)
364 protected void drawContents(GC gc
, int clipx
, int clipy
, int clipw
, int cliph
) {
366 Image dbuffer
= getDrawBuffer();
370 int lw
= (int) (getContentsWidth() / fZoomValue
);
371 if (getContentsWidth() < getVisibleWidth()) {
372 lw
= (int) (getVisibleWidth() / fZoomValue
);
375 int lh
= (int) (getContentsHeight() / fZoomValue
);
376 if (getContentsHeight() < getVisibleHeight()) {
377 lh
= (int) (getVisibleHeight() / fZoomValue
);
379 d
= createPagesSelectionImages(dbuffer
, lw
, fStepX
, lh
, fStepY
);
383 d
= new Image(super.getShell().getDisplay(), drawRegionSelected(d
, new Rectangle(0, 0, lw
, lh
), new RGB(221, 208, 200)));
387 Rectangle area
= getClientArea();
388 int w
= d
.getBounds().width
;
389 int h
= d
.getBounds().height
;
390 gc
.drawImage(d
, 0, 0, w
, h
, 0, 0, area
.width
, area
.height
);
392 fTotalPages
.setText(Integer
.valueOf(maxNumOfPages()).toString());
402 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#keyPressedEvent(org.eclipse.swt.events.KeyEvent)
405 protected void keyPressedEvent(KeyEvent e
) {
406 if (e
.keyCode
== SWT
.CTRL
) {
407 fMultiSelection
= true;
409 if (e
.keyCode
== SWT
.SHIFT
) {
410 fAreaSelection
= true;
412 if (e
.keyCode
== SWT
.ARROW_DOWN
) {
413 if (fPageNum
+ getNbRow() <= maxNumOfPages()) {
414 fPageNum
+= getNbRow();
416 int line
= fPageNum
/ getNbRow();
417 int row
= fPageNum
% getNbRow();
421 if ((line
+ 1) * fStepY
> (fOverviewCanvas
.getContentsY() + fOverviewCanvas
.getVisibleHeight()) / fOverviewCanvas
.fZoomValue
) {
422 fOverviewCanvas
.scrollBy(0, (int) (fStepY
* fOverviewCanvas
.fZoomValue
));
425 if (e
.keyCode
== SWT
.ARROW_UP
) {
426 if (fPageNum
- getNbRow() > 0) {
427 fPageNum
-= getNbRow();
429 int line
= fPageNum
/ getNbRow();
430 int row
= fPageNum
% getNbRow();
434 if ((line
) * fStepY
<= fOverviewCanvas
.getContentsY() / fOverviewCanvas
.fZoomValue
) {
435 fOverviewCanvas
.scrollBy(0, -(int) (fStepY
* fOverviewCanvas
.fZoomValue
));
438 if (e
.keyCode
== SWT
.ARROW_LEFT
) {
439 if ((fPageNum
- 2) / getNbRow() == (fPageNum
- 1) / getNbRow() && fPageNum
> 1) {
442 int row
= fPageNum
% getNbRow();
443 if ((row
- 1) * fStepX
< (fOverviewCanvas
.getContentsX()) / fOverviewCanvas
.fZoomValue
) {
444 fOverviewCanvas
.scrollBy(-(int) (fStepX
* fOverviewCanvas
.fZoomValue
), 0);
447 if (e
.keyCode
== SWT
.ARROW_RIGHT
) {
448 if ((fPageNum
- 1) / getNbRow() == fPageNum
/ getNbRow()) {
451 int row
= fPageNum
% getNbRow();
455 if ((row
) * fStepX
> (fOverviewCanvas
.getContentsX() + fOverviewCanvas
.getVisibleWidth()) / fOverviewCanvas
.fZoomValue
) {
456 fOverviewCanvas
.scrollBy((int) (fStepX
* fOverviewCanvas
.fZoomValue
), 0);
460 if (e
.keyCode
== 32 && fPageNum
> -1) {
461 Arrays
.sort(fPagesList
);
462 int pos
= Arrays
.binarySearch(fPagesList
, fPageNum
);
464 addToPagesList(fPageNum
);
466 removeFromPagesList(fPageNum
);
470 if (!fAreaSelection
&& !fMultiSelection
) {
471 fFirstPage
= fPageNum
;
472 fPagesList
= new int[1];
473 fPagesList
[0] = fPageNum
;
474 } else if ((fPageNum
!= -1) && (fAreaSelection
) && (fFirstPage
!= -1)) {
475 fPagesList
= new int[0];
476 int line1
= fFirstPage
/ getNbRow();
477 int row1
= fFirstPage
% getNbRow();
484 int line2
= fPageNum
/ getNbRow();
485 int row2
= fPageNum
% getNbRow();
505 for (int i
= row1
- 1; i
< row2
; i
++) {
506 for (int j
= line1
- 1; j
< line2
; j
++) {
507 addToPagesList(i
+ j
* getNbRow() + 1);
512 fOverviewCanvas
.redraw();
517 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#keyReleasedEvent(org.eclipse.swt.events.KeyEvent)
520 protected void keyReleasedEvent(KeyEvent e
) {
521 if (e
.keyCode
== SWT
.CTRL
) {
522 fMultiSelection
= false;
524 if (e
.keyCode
== SWT
.SHIFT
) {
525 fAreaSelection
= false;
531 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseDownEvent(org.eclipse.swt.events.MouseEvent)
534 protected void contentsMouseDownEvent(MouseEvent event
) {
537 int x1
= (int) ((event
.x
/ fOverviewCanvas
.fZoomValue
) / fStepX
);
538 int x2
= (int) ((event
.y
/ fOverviewCanvas
.fZoomValue
) / fStepY
);
540 int oldPage
= fPageNum
;
542 fPageNum
= x1
+ x2
* getNbRow() + 1;
544 if (fPageNum
> maxNumOfPages()) {
549 if (!fAreaSelection
) {
550 fFirstPage
= fPageNum
;
553 if ((fPageNum
!= -1) && (fMultiSelection
)) {
554 Arrays
.sort(fPagesList
);
555 int pos
= Arrays
.binarySearch(fPagesList
, fPageNum
);
557 addToPagesList(fPageNum
);
559 removeFromPagesList(fPageNum
);
561 } else if ((fPageNum
!= -1) && (fAreaSelection
) && (fFirstPage
!= -1)) {
563 fPagesList
= new int[0];
565 int line1
= fFirstPage
/ getNbRow();
566 int row1
= fFirstPage
% getNbRow();
573 int line2
= fPageNum
/ getNbRow();
574 int row2
= fPageNum
% getNbRow();
594 for (int i
= row1
- 1; i
< row2
; i
++) {
595 for (int j
= line1
- 1; j
< line2
; j
++) {
596 addToPagesList(i
+ j
* getNbRow() + 1);
600 fPagesList
= new int[1];
601 fPagesList
[0] = fPageNum
;
603 if ((event
.stateMask
& SWT
.CTRL
) != 0) {
604 fMultiSelection
= true;
612 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#contentsMouseMoveEvent(org.eclipse.swt.events.MouseEvent)
615 protected void contentsMouseMoveEvent(MouseEvent e
) {
616 fToolTip
.hideToolTip();
621 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#resizeContents(int, int)
624 public void resizeContents(int w
, int h
) {
625 super.resizeContents(w
, h
);
631 * A traverse listener implementation.
633 protected static class LocalTraverseListener
implements TraverseListener
{
636 * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
639 public void keyTraversed(TraverseEvent e
) {
640 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
646 // ------------------------------------------------------------------------
648 // ------------------------------------------------------------------------
653 * The shell reference
655 * The sequence diagram widget reference
657 public SDPrintDialogUI(Shell shell
, SDWidget sdWidget
) {
658 this(shell
, sdWidget
, false);
665 * The shell reference
667 * The sequence diagram widget reference
668 * @param showPrintBtn
669 * Flag for showing print buttons
671 public SDPrintDialogUI(Shell shell
, SDWidget sdWidget
, boolean showPrintBtn
) {
674 fShowPrintButton
= showPrintBtn
;
676 fPrinterData
= Printer
.getDefaultPrinterData();
677 if (fPrinterData
!= null) {
678 fPrinterData
.scope
= PrinterData
.SELECTION
;
681 fPagesList
= new int[0];
683 fSelectionListener
= new SelectionListener() {
686 public void widgetSelected(SelectionEvent e
) {
687 if (fUseCurrentZoom
.getSelection()) {
688 fHorPagesNum
.setEnabled(false);
689 fVertPagesNum
.setEnabled(false);
691 if (fSetHPagesNumber
.getSelection()) {
692 fHorPagesNum
.setEnabled(true);
693 fVertPagesNum
.setEnabled(false);
694 if (fCurrentPage
.getSelection()) {
695 fCurrentPage
.setSelection(false);
696 fAllPages
.setSelection(true);
698 if ("".equals(fHorPagesNum
.getText())) { //$NON-NLS-1$
699 fHorPagesNum
.setText("1"); //$NON-NLS-1$
702 if (fSetVPagesNumber
.getSelection()) {
703 fHorPagesNum
.setEnabled(false);
704 fVertPagesNum
.setEnabled(true);
705 if (fCurrentPage
.getSelection()) {
706 fCurrentPage
.setSelection(false);
707 fAllPages
.setSelection(true);
709 if ("".equals(fVertPagesNum
.getText())) { //$NON-NLS-1$
710 fVertPagesNum
.setText("1"); //$NON-NLS-1$
713 if (fCurrentPage
.getSelection() || fAllPages
.getSelection() || fPageList
.getSelection()) {
714 fFromPage
.setEnabled(false);
715 fToPage
.setEnabled(false);
717 fFromPage
.setEnabled(true);
718 fToPage
.setEnabled(true);
721 fCurrentPage
.setEnabled(fUseCurrentZoom
.getSelection());
722 fOverviewCanvas
.setEnabled(fPageList
.getSelection());
723 if (fOverviewCanvas
.isEnabled() && (e
.widget
== fUseCurrentZoom
|| e
.widget
== fSetHPagesNumber
|| e
.widget
== fSetVPagesNumber
)) {
724 fPagesList
= new int[1];
728 } else if ((fOverviewCanvas
.isEnabled() && (e
.widget
== fPageList
)) &&
729 (fPagesList
== null || fPagesList
.length
<= 0)) {
731 fPagesList
= new int[1];
737 fTotalPages
.setText(Integer
.valueOf(maxNumOfPages()).toString());
738 fOverviewCanvas
.redraw();
739 fOverviewCanvas
.update();
744 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
747 public void widgetDefaultSelected(SelectionEvent e
) {
748 fPagesList
= new int[0];
750 fOverviewCanvas
.redraw();
755 fModifyListener
= new ModifyListener() {
758 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
761 public void modifyText(ModifyEvent e
) {
762 fPagesList
= new int[0];
764 fTotalPages
.setText(Integer
.valueOf(maxNumOfPages()).toString());
765 fOverviewCanvas
.redraw();
771 // ------------------------------------------------------------------------
773 // ------------------------------------------------------------------------
776 * Creates new grid data object.
778 * @param span horizontal span.
781 protected GridData
newGridData(int span
) {
782 GridData data
= new GridData(GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
);
783 data
.horizontalSpan
= span
;
788 * Creates the dialog area.
790 * @param parent The parent composite
791 * @return dialog control
793 public Control
createDialogArea(Composite parent
) {
795 GridLayout parentLayout
= new GridLayout();
796 parentLayout
.numColumns
= 6;
797 parent
.setLayout(parentLayout
);
799 Group g1
= new Group(parent
, SWT
.SHADOW_NONE
);
800 g1
.setText(SDMessages
._113
);
801 g1
.setLayoutData(newGridData(3));
802 GridLayout g1layout
= new GridLayout();
803 g1layout
.numColumns
= 2;
804 g1
.setLayout(g1layout
);
806 fUseCurrentZoom
= new Button(g1
, SWT
.RADIO
);
807 fUseCurrentZoom
.setText(SDMessages
._112
);
808 fUseCurrentZoom
.setLayoutData(newGridData(2));
809 fUseCurrentZoom
.addSelectionListener(fSelectionListener
);
811 fSetHPagesNumber
= new Button(g1
, SWT
.RADIO
);
812 fSetHPagesNumber
.setText(SDMessages
._110
);
813 fSetHPagesNumber
.setLayoutData(newGridData(1));
814 fSetHPagesNumber
.addSelectionListener(fSelectionListener
);
816 fHorPagesNum
= new Text(g1
, SWT
.SINGLE
| SWT
.BORDER
);
817 fHorPagesNum
.addModifyListener(fModifyListener
);
819 fSetVPagesNumber
= new Button(g1
, SWT
.RADIO
);
820 fSetVPagesNumber
.setText(SDMessages
._111
);
821 fSetVPagesNumber
.setLayoutData(newGridData(1));
822 fSetVPagesNumber
.addSelectionListener(fSelectionListener
);
824 fVertPagesNum
= new Text(g1
, SWT
.SINGLE
| SWT
.BORDER
);
825 fVertPagesNum
.addModifyListener(fModifyListener
);
827 Label nbTotal
= new Label(g1
, SWT
.SHADOW_NONE
| SWT
.RIGHT
);
828 nbTotal
.setText(SDMessages
._109
);
829 // nbTotal.setLayoutData(newGridData(1));
831 fTotalPages
= new Text(g1
, SWT
.SINGLE
| SWT
.BORDER
| SWT
.READ_ONLY
);
832 // nbHV.addModifyListener(modifListener);
834 Group g2
= new Group(parent
, SWT
.SHADOW_NONE
);
835 g2
.setText(SDMessages
._119
);
836 GridData data
= new GridData(GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
);
837 data
.horizontalSpan
= 3;
838 data
.verticalSpan
= 2;
839 g2
.setLayoutData(data
);
840 GridLayout g2layout
= new GridLayout();
842 g2layout
.numColumns
= 1;
843 // SVLayout g2layout = new SVLayout();
844 g2
.setLayout(g2layout
);
846 GridData data2
= new GridData(GridData
.GRAB_VERTICAL
| GridData
.VERTICAL_ALIGN_FILL
| GridData
.GRAB_HORIZONTAL
| GridData
.HORIZONTAL_ALIGN_FILL
);
847 data2
.horizontalSpan
= 1;
848 data2
.verticalSpan
= 1;
850 fOverviewCanvas
= new LocalSD(g2
, SWT
.NO_BACKGROUND
);
851 GridData seqDiagLayoutData
= new GridData(GridData
.HORIZONTAL_ALIGN_FILL
| GridData
.VERTICAL_ALIGN_FILL
);
852 fOverviewCanvas
.setLayoutData(seqDiagLayoutData
);
853 // overviewCanvas.resizeContents(100,100);
854 if (fSdView
.getContentsWidth() < fSdView
.getVisibleWidth() && fSdView
.getContentsHeight() < fSdView
.getVisibleHeight()) {
859 fOverviewCanvas
.setFrame(fSdView
.getFrame(), true);
860 fOverviewCanvas
.fZoomValue
= (float) 1 / fTest
;
861 fOverviewCanvas
.setCornerControl(null);
862 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getContentsWidth() / fTest
;
863 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getFrame().getWidth() / fTest
+ 15;
865 if (fSdView
.getVisibleWidth() < fSdView
.getContentsWidth()) {
866 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getContentsWidth() / fTest
;
867 if (seqDiagLayoutData
.widthHint
> Display
.getDefault().getClientArea().width
/ 4) {
868 seqDiagLayoutData
.widthHint
= Display
.getDefault().getClientArea().width
/ 4;
871 seqDiagLayoutData
.widthHint
= fOverviewCanvas
.getFrame().getWidth() / fTest
+ 15;
874 if (fSdView
.getVisibleHeight() < fSdView
.getContentsHeight()) {
875 seqDiagLayoutData
.heightHint
= fOverviewCanvas
.getContentsHeight() / fTest
;
876 if (seqDiagLayoutData
.heightHint
> Display
.getDefault().getClientArea().width
/ 4) {
877 seqDiagLayoutData
.heightHint
= Display
.getDefault().getClientArea().width
/ 4;
880 seqDiagLayoutData
.heightHint
= fOverviewCanvas
.getFrame().getHeight() / fTest
;
883 fOverviewCanvas
.setEnabled(false);
885 fCurrentSelection
= new Label(g2
, SWT
.SHADOW_NONE
| SWT
.LEFT
);
886 fCurrentSelection
.setLayoutData(newGridData(1));
888 Group g3
= new Group(parent
, SWT
.SHADOW_NONE
);
889 g3
.setText(SDMessages
._118
);
890 g3
.setLayoutData(newGridData(3));
891 GridLayout g3layout
= new GridLayout();
892 g3layout
.numColumns
= 4;
893 g3
.setLayout(g3layout
);
895 fAllPages
= new Button(g3
, SWT
.RADIO
);
896 fAllPages
.setText(SDMessages
._108
);
897 fAllPages
.setLayoutData(newGridData(4));
898 fAllPages
.addSelectionListener(fSelectionListener
);
900 fCurrentPage
= new Button(g3
, SWT
.RADIO
);
901 fCurrentPage
.setText(SDMessages
._107
);
902 fCurrentPage
.setLayoutData(newGridData(4));
903 fCurrentPage
.setEnabled(true);
904 fCurrentPage
.setSelection(true);
905 fCurrentPage
.addSelectionListener(fSelectionListener
);
907 fPageList
= new Button(g3
, SWT
.RADIO
);
908 fPageList
.setText(SDMessages
._106
);
909 fPageList
.setLayoutData(newGridData(4));
910 fPageList
.addSelectionListener(fSelectionListener
);
912 fPageRange
= new Button(g3
, SWT
.RADIO
);
913 fPageRange
.setText(SDMessages
._103
);
914 fPageRange
.setLayoutData(newGridData(1));
915 fPageRange
.addSelectionListener(fSelectionListener
);
917 fFromPage
= new Text(g3
, SWT
.SINGLE
| SWT
.BORDER
);
919 Label labelTo
= new Label(g3
, SWT
.CENTER
);
920 labelTo
.setText(SDMessages
._105
);
922 fToPage
= new Text(g3
, SWT
.SINGLE
| SWT
.BORDER
);
924 fToolTip
= new DiagramToolTip(fOverviewCanvas
);
926 fOverviewCanvas
.getViewControl().addMouseTrackListener(new MouseTrackListener() {
929 * @see org.eclipse.swt.events.MouseTrackListener#mouseEnter(org.eclipse.swt.events.MouseEvent)
932 public void mouseEnter(MouseEvent e
) {
933 fToolTip
.hideToolTip();
938 * @see org.eclipse.swt.events.MouseTrackListener#mouseExit(org.eclipse.swt.events.MouseEvent)
941 public void mouseExit(MouseEvent e
) {
942 fToolTip
.hideToolTip();
947 * @see org.eclipse.swt.events.MouseTrackListener#mouseHover(org.eclipse.swt.events.MouseEvent)
950 public void mouseHover(MouseEvent e
) {
951 int x1
= (int) (fOverviewCanvas
.viewToContentsX(e
.x
) / fOverviewCanvas
.fZoomValue
/ fStepX
);
952 int x2
= (int) (fOverviewCanvas
.viewToContentsY(e
.y
) / fOverviewCanvas
.fZoomValue
/ fStepY
);
953 int num
= x1
+ x2
* getNbRow() + 1;
954 if (num
> maxNumOfPages()) {
958 fToolTip
.showToolTip(String
.valueOf(num
));
961 fCurrentSelection
.setText("");//$NON-NLS-1$
962 fToolTip
.hideToolTip();
968 fOverviewCanvas
.addTraverseListener(new LocalTraverseListener());
970 fOverviewCanvas
.addFocusListener(new FocusListener() {
973 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
976 public void focusGained(FocusEvent e
) {
977 fOverviewCanvas
.redraw();
982 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
985 public void focusLost(FocusEvent e
) {
986 fOverviewCanvas
.redraw();
990 if (fShowPrintButton
) {
991 Composite printerDlg
= new Composite(parent
, SWT
.NONE
);
992 data
= new GridData(SWT
.FILL
, SWT
.DEFAULT
, true, false);
993 data
.horizontalSpan
= 6;
994 parentLayout
= new GridLayout();
995 parentLayout
.numColumns
= 2;
996 printerDlg
.setLayout(parentLayout
);
997 printerDlg
.setLayoutData(data
);
999 Label label
= new Label(printerDlg
, SWT
.NONE
);
1000 label
.setLayoutData(new GridData(SWT
.FILL
, SWT
.DEFAULT
, true, false));
1001 fPrinterDialog
= new Button(printerDlg
, SWT
.PUSH
);
1002 fPrinterDialog
.setText(SDMessages
._115
);
1004 fPrinterDialog
.addSelectionListener(new SelectionListener() {
1008 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
1011 public void widgetSelected(SelectionEvent e
) {
1012 printButtonSelected();
1017 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
1020 public void widgetDefaultSelected(SelectionEvent e
) {
1026 updatePrinterStatus();
1032 * Get number of pages for selection.
1033 * @return number of pages for selection.
1035 public int getPagesForSelection() {
1040 * Handler for when the OK button is pressed
1042 * @return True if the operation was successful, false if there was an error
1044 public boolean okPressed() {
1045 fPrintAll
= fAllPages
.getSelection();
1046 fPrintCurrent
= fCurrentPage
.getSelection();
1047 fPrintSelection
= fPageList
.getSelection();
1048 fPrintRange
= fPageRange
.getSelection();
1051 fFrom
= Integer
.valueOf(fFromPage
.getText()).intValue();
1052 fTo
= Integer
.valueOf(fToPage
.getText()).intValue();
1053 if (fFrom
> maxNumOfPages() || fTo
> maxNumOfPages() || fFrom
<= 0 || fTo
<= 0) {
1054 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._99
);
1057 } else if (fSetHPagesNumber
.getSelection() && fNbPages
<= 0) {
1058 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._101
);
1060 } else if (fSetVPagesNumber
.getSelection() && fNbPages
<= 0) {
1061 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._100
);
1063 } else if (fPrintSelection
&& getPageList().length
<= 0) {
1064 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._102
);
1068 } catch (Exception e
) {
1069 MessageDialog
.openError(getShell(), SDMessages
._98
, SDMessages
._99
);
1079 * Draws region that was selected
1080 * @param img The corresponding image
1081 * @param r The selected rectangle.
1082 * @param color The color to use for selection
1083 * @return image data reference
1085 public ImageData
drawRegionSelected(Image img
, Rectangle r
, RGB color
) {
1086 ImageData id
= img
.getImageData();
1087 for (int a
= 0; a
< r
.width
&& r
.x
+ a
< id
.width
; a
++) {
1088 for (int b
= 0; b
< r
.height
&& r
.y
+ b
< id
.height
; b
++) {
1089 int index
= id
.getPixel(r
.x
+ a
, r
.y
+ b
);
1090 RGB rgb
= id
.palette
.getRGB(index
);
1091 rgb
= combine(color
, rgb
);
1092 id
.setPixel(r
.x
+ a
, r
.y
+ b
, id
.palette
.getPixel(rgb
));
1099 * Combines two RGB colors.
1100 * @param front The front color
1101 * @param back The back color
1102 * @return new RGB color
1104 public static RGB
combine(RGB front
, RGB back
) {
1108 double af
= (_af
) / 255.0;
1109 double rf
= front
.red
;
1110 double gf
= front
.green
;
1111 double bf
= front
.blue
;
1113 double ab
= (_ab
) / 255.0;
1114 double rb
= back
.red
;
1115 double gb
= back
.green
;
1116 double bb
= back
.blue
;
1118 double k
= (1.0 - af
) * ab
;
1119 int r
= (int) ((af
* rf
+ k
* rb
));
1120 int g
= (int) ((af
* gf
+ k
* gb
));
1121 int b
= (int) ((af
* bf
+ k
* bb
));
1123 return new RGB(r
, g
, b
);
1127 * Computes value for X coordinates step and Y coordinates step.
1129 protected void computeStepXY() {
1130 float cw
= fOverviewCanvas
.getContentsWidth() / fOverviewCanvas
.fZoomValue
;
1131 float ch
= fOverviewCanvas
.getContentsHeight() / fOverviewCanvas
.fZoomValue
;
1133 if (fPrinterData
== null) {
1139 Printer printer
= new Printer(fPrinterData
);
1140 if (fSetHPagesNumber
.getSelection()) {
1141 fNbPages
= Integer
.valueOf(fHorPagesNum
.getText()).intValue();
1142 float z1
= fSdView
.getContentsWidth() / cw
;
1143 float z2
= printer
.getClientArea().width
/ ((float) fSdView
.getContentsWidth() / fNbPages
);
1145 fStepY
= printer
.getClientArea().height
/ z1
/ z2
;
1146 fStepX
= cw
/ fNbPages
;
1147 } else if (fSetVPagesNumber
.getSelection()) {
1148 fNbPages
= Integer
.valueOf(fVertPagesNum
.getText()).intValue();
1149 float z1
= fSdView
.getContentsHeight() / ch
;
1150 float z2
= printer
.getClientArea().height
/ ((float) fSdView
.getContentsHeight() / fNbPages
);
1151 fStepX
= printer
.getClientArea().width
/ z1
/ z2
;
1152 fStepY
= ch
/ fNbPages
;
1154 float z1
= fSdView
.getContentsWidth() / (cw
);
1155 fStepX
= fSdView
.getVisibleWidth() / z1
;
1156 fNbPages
= Math
.round(cw
/ fStepX
);
1157 if (fNbPages
== 0) {
1160 int pw
= printer
.getClientArea().width
;
1161 int ph
= printer
.getClientArea().height
;
1162 float z2
= pw
/ ((float) fSdView
.getContentsWidth() / fNbPages
);
1163 fStepY
= ph
/ z1
/ z2
;
1166 } catch (NumberFormatException e
) {
1167 fStepX
= fStepY
= fNbPages
= 0;
1170 sTX
= fStepX
* (fSdView
.getContentsWidth() / cw
);
1171 sTY
= fStepY
* (fSdView
.getContentsHeight() / ch
);
1173 if ((fSdView
.getVisibleWidth() > fSdView
.getContentsWidth()) && (fSetVPagesNumber
.getSelection() || fSetHPagesNumber
.getSelection())) {
1174 rat
= (float) fSdView
.getVisibleWidth() / (float) fSdView
.getContentsWidth();
1176 fZoomFactor
= (fOverviewCanvas
.getContentsWidth() / cw
) / fOverviewCanvas
.getZoomFactor() * rat
;
1180 * Returns the pages list.
1182 * @return the pages list.
1184 public int[] getPageList() {
1185 return Arrays
.copyOf(fPagesList
, fPagesList
.length
);
1189 * Adds a page to pages list.
1192 * The number of the the new page
1194 public void addToPagesList(int num
) {
1195 int temp
[] = new int[fPagesList
.length
+ 1];
1196 System
.arraycopy(fPagesList
, 0, temp
, 0, fPagesList
.length
);
1197 temp
[temp
.length
- 1] = num
;
1198 fPagesList
= new int[temp
.length
];
1199 System
.arraycopy(temp
, 0, fPagesList
, 0, temp
.length
);
1203 * Removes a page from the pages list.
1206 * The number of the page to remove
1208 public void removeFromPagesList(int num
) {
1209 int pos
= Arrays
.binarySearch(fPagesList
, num
);
1210 int temp
[] = new int[fPagesList
.length
- 1];
1211 System
.arraycopy(fPagesList
, 0, temp
, 0, pos
);
1212 System
.arraycopy(fPagesList
, pos
+ 1, temp
, pos
, fPagesList
.length
- pos
- 1);
1213 fPagesList
= new int[temp
.length
];
1214 System
.arraycopy(temp
, 0, fPagesList
, 0, temp
.length
);
1218 * Returns the maximum number of pages.
1220 * @return maximum number of pages.
1222 public int maxNumOfPages() {
1223 return (getNbRow() * getNbLines());
1227 * Returns the number of rows.
1229 * @return number of rows.
1231 public int getNbRow() {
1232 if (!fSetHPagesNumber
.isDisposed()) {
1233 int cw
= (int) (fOverviewCanvas
.getContentsWidth() / fOverviewCanvas
.fZoomValue
);
1236 row
= (int) (cw
/ fStepX
);
1237 if (fSetHPagesNumber
.getSelection()) {
1238 row
= Math
.round(cw
/ fStepX
);
1239 } else if ((cw
% fStepX
!= 0)) {
1249 * Returns the number of lines.
1251 * @return number of lines
1253 public int getNbLines() {
1254 if (!fSetVPagesNumber
.isDisposed()) {
1255 int ch
= (int) (fOverviewCanvas
.getContentsHeight() / fOverviewCanvas
.fZoomValue
);
1258 line
= (int) (ch
/ fStepY
);
1259 if (fSetVPagesNumber
.getSelection()) {
1260 line
= Math
.round(ch
/ fStepY
);
1261 } else if (ch
% fStepY
!= 0) {
1271 * Returns whether to print all pages or not.
1273 * @return <code>true</code> for all pages else <code>false</code>.
1275 public boolean printAll() {
1280 * Returns whether to print only current page
1282 * @return <code>true</code> for current page only else <code>false</code>..
1284 public boolean printCurrent() {
1285 return fPrintCurrent
;
1289 * Returns whether to print selected pages.
1291 * @return <code>true</code> for selected pages only else <code>false</code>.
1293 public boolean printSelection() {
1294 return fPrintSelection
;
1298 * Returns whether to print range of pages.
1300 * @return <code>true</code> for range of pages only else <code>false</code>.
1302 public boolean printRange() {
1307 * Returns the step in X direction.
1309 * @return step in X direction
1311 public float getStepX() {
1316 * Returns the step in Y direction.
1318 * @return step in Y direction
1320 public float getStepY() {
1325 * Returns the zoom factor
1327 * @return zoom factor
1329 public float getZoomFactor() {
1334 * Returns the printer data reference.
1336 * @return printer data reference
1338 public PrinterData
getPrinterData() {
1339 return fPrinterData
;
1343 * Returns the page number to start printing from.
1345 * @return page number to start printing from
1347 public int getFrom() {
1352 * Returns the page number to print to.
1354 * @return page number to print to
1356 public int getTo() {
1361 * Displays current number of pages
1363 protected void displayPageNum() {
1365 String message
= MessageFormat
.format(SDMessages
._117
, new Object
[] { Integer
.valueOf(fPageNum
) });
1366 fCurrentSelection
.setText(message
);
1367 fCurrentSelection
.getParent().layout();
1372 * Returns the shell reference.
1374 * @return the shell reference.
1376 public Shell
getShell() {
1383 * @param shell The shell reference.
1385 public void setShell(Shell shell
) {
1390 * Handle selection of print button.
1392 public void printButtonSelected() {
1393 PrintDialog printer
= new PrintDialog(getShell());
1394 if (fAllPages
.getSelection()) {
1395 printer
.setScope(PrinterData
.ALL_PAGES
);
1397 if (fCurrentPage
.getSelection()) {
1398 printer
.setScope(PrinterData
.SELECTION
);
1400 if (fPageList
.getSelection()) {
1401 printer
.setScope(PrinterData
.SELECTION
);
1403 if (fPageRange
.getSelection()) {
1404 printer
.setScope(PrinterData
.PAGE_RANGE
);
1405 fFrom
= Integer
.valueOf(fFromPage
.getText()).intValue();
1406 fTo
= Integer
.valueOf(fToPage
.getText()).intValue();
1407 printer
.setStartPage(fFrom
);
1408 printer
.setEndPage(fTo
);
1411 PrinterData newPrinterData
= printer
.open();
1412 if (newPrinterData
!= null) {
1413 fPrinterData
= newPrinterData
;
1415 updatePrinterStatus();
1417 if (printer
.getScope() == PrinterData
.ALL_PAGES
) {
1418 fAllPages
.setSelection(true);
1419 fCurrentPage
.setSelection(false);
1420 fPageList
.setSelection(false);
1421 fPageRange
.setSelection(false);
1422 fHorPagesNum
.setEnabled(false);
1423 fVertPagesNum
.setEnabled(false);
1425 if (printer
.getScope() == PrinterData
.PAGE_RANGE
) {
1426 fAllPages
.setSelection(false);
1427 fCurrentPage
.setSelection(false);
1428 fPageList
.setSelection(false);
1429 fPageRange
.setSelection(true);
1430 fFromPage
.setEnabled(true);
1431 fToPage
.setEnabled(true);
1432 fFromPage
.setText((Integer
.valueOf(printer
.getStartPage())).toString());
1433 fToPage
.setText((Integer
.valueOf(printer
.getEndPage())).toString());
1436 fOverviewCanvas
.redraw();
1440 * Sets parent wizard page
1442 * @param parent The parent wizard page
1444 public void setParentWizardPage(WizardPage parent
) {
1445 fParentWizardPage
= parent
;
1449 * Sets the parent dialog box.
1451 * @param parent The parent dialog box.
1453 public void setParentDialog(SDPrintDialog parent
) {
1454 fParentDialog
= parent
;
1458 * Updates the printer status
1460 protected void updatePrinterStatus() {
1461 if (fParentWizardPage
!= null) {
1462 // used in the wizard dialog
1463 if (fPrinterData
== null) {
1464 // show error message and disable Finish button
1465 fParentWizardPage
.setErrorMessage(SDMessages
._135
);
1466 fParentWizardPage
.setPageComplete(false);
1468 // clear error message and enable Finish button
1469 fParentWizardPage
.setErrorMessage(null);
1470 fParentWizardPage
.setPageComplete(true);
1472 } else if (fParentDialog
!= null) {
1473 // used in the print dialog
1474 if (fPrinterData
== null) {
1475 // show error message and disable OK button
1476 fParentDialog
.setErrorMessage(SDMessages
._135
);
1477 fParentDialog
.setPageComplete(false);
1479 // clear error message and enable OK button
1480 fParentDialog
.setErrorMessage(null);
1481 fParentDialog
.setPageComplete(true);