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