Internalize some more TMF APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / SDWidget.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 * $Id: SDWidget.java,v 1.6 2008/12/12 22:21:46 jcayne Exp $
8 *
9 * Contributors:
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd;
14
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Timer;
19 import java.util.TimerTask;
20
21 import org.eclipse.jface.contexts.IContextIds;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants;
27 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
29 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
30 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage;
31 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BasicExecutionOccurrence;
32 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Frame;
33 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode;
34 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange;
35 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline;
36 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Metrics;
37 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor;
38 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences;
39 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDCollapseProvider;
40 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.LoadersManager;
41 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref;
42 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
43 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDPrintDialog;
44 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDPrintDialogUI;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.accessibility.ACC;
47 import org.eclipse.swt.accessibility.Accessible;
48 import org.eclipse.swt.accessibility.AccessibleAdapter;
49 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
50 import org.eclipse.swt.accessibility.AccessibleControlEvent;
51 import org.eclipse.swt.accessibility.AccessibleEvent;
52 import org.eclipse.swt.events.DisposeEvent;
53 import org.eclipse.swt.events.DisposeListener;
54 import org.eclipse.swt.events.FocusEvent;
55 import org.eclipse.swt.events.FocusListener;
56 import org.eclipse.swt.events.KeyEvent;
57 import org.eclipse.swt.events.MouseEvent;
58 import org.eclipse.swt.events.SelectionEvent;
59 import org.eclipse.swt.events.SelectionListener;
60 import org.eclipse.swt.events.TraverseEvent;
61 import org.eclipse.swt.events.TraverseListener;
62 import org.eclipse.swt.graphics.GC;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.graphics.ImageData;
65 import org.eclipse.swt.graphics.Rectangle;
66 import org.eclipse.swt.printing.Printer;
67 import org.eclipse.swt.printing.PrinterData;
68 import org.eclipse.swt.widgets.Canvas;
69 import org.eclipse.swt.widgets.Caret;
70 import org.eclipse.swt.widgets.Composite;
71 import org.eclipse.swt.widgets.Control;
72 import org.eclipse.swt.widgets.Display;
73 import org.eclipse.swt.widgets.Event;
74 import org.eclipse.swt.widgets.Listener;
75 import org.eclipse.swt.widgets.MenuItem;
76 import org.eclipse.ui.contexts.IContextService;
77 import org.eclipse.ui.part.ViewPart;
78
79 /**
80 *
81 * @author sveyrier
82 *
83 */
84 public class SDWidget extends ScrollView implements SelectionListener, IPropertyChangeListener, DisposeListener, ITimeCompressionListener {
85
86 protected Frame frame;
87 protected Image overView = null;
88 protected MenuItem zoomIn = null;
89 protected MenuItem zoomOut = null;
90 protected SDWidgetSelectionProvider selProvider = null;
91 public float zoomValue = 1;
92 protected boolean zoomInMode = false;
93 protected boolean zoomOutMode = false;
94
95 protected List<GraphNode> selectedNodeList = null;
96 protected boolean ctrlSelection = false;
97
98 protected ViewPart site = null;
99
100 public GraphNode currentGraphNode = null;
101 public GraphNode listStart = null;
102 public ArrayList<GraphNode> prevList = null;
103
104 protected TimeCompressionBar timeBar = null;
105
106 protected DiagramToolTip toolTip = null;
107
108 protected Accessible accessible = null;
109
110 protected GraphNode toolTipNode;
111
112 protected Lifeline dragAndDrop = null;
113
114 protected int focusedWidget = -1;
115
116 protected float printerZoom = 0;
117
118 protected int printerY = 0;
119
120 protected int printerX = 0;
121
122 protected boolean getDragAndDrop = false;
123
124 protected int dragX = 0;
125 protected int dragY = 0;
126
127 protected boolean reorderMode = false;
128
129 Image collapaseCaretImg = null;
130 Image arrowUpCaretImg = null;
131 Image currentCaretImage = null;
132
133 protected ISDCollapseProvider collapseProvider = null;
134
135 protected Caret insertionCartet = null;
136
137 protected ArrayList<Lifeline[]> reorderList = null;
138
139 protected boolean printing = false;
140 protected Printer printer = null;
141
142 protected boolean shiftSelection = false;
143
144 protected DiagramToolTip scrollToolTip = null;
145
146 public SDWidget(Composite c, int s) {
147 super(c, s | SWT.NO_BACKGROUND, true);
148 setOverviewEnabled(true);
149 selectedNodeList = new ArrayList<GraphNode>();
150 selProvider = new SDWidgetSelectionProvider();
151 SDViewPref.getInstance().addPropertyChangeListener(this);
152 toolTip = new DiagramToolTip(getViewControl());
153 super.addDisposeListener(this);
154
155 scrollToolTip = new DiagramToolTip(c);
156 getVerticalBar().addListener(SWT.MouseUp, new Listener() {
157
158 @Override
159 public void handleEvent(Event event) {
160 scrollToolTip.hideToolTip();
161 }
162
163 });
164 // tooltip on vertical bar causes trouble when scrolling, because
165 // tooltip is constantly updated and shown
166 // getVerticalBar().addSelectionListener(new SelectionListener(){
167 //
168 // public void widgetSelected(SelectionEvent e) {
169 // scrollToolTip.hideToolTip();
170 // double minOcc=0;
171 // boolean needInit=true;
172 // GraphNode m=null;
173 // for (int i=0;i<frame.lifeLinesCount();i++)
174 // {
175 // Lifeline lifeline=frame.getLifeline(i);
176 // BasicExecutionOccurrence exec=frame.getFirstExecution(lifeline);
177 // if (exec instanceof ExecutionOccurrence) {
178 // ExecutionOccurrence occ=(ExecutionOccurrence)exec;
179 // if ((occ.hasTimeInfo())&&(occ.getFirstTime()<minOcc||needInit))
180 // {
181 // needInit=false;
182 // minOcc=occ.getFirstTime();
183 // m=occ;
184 // }
185 // }
186 // }
187 //
188 //
189 // int z=frame.getFirstVisibleSyncMessage();
190 // if (m==null)
191 // {
192 // SyncMessage s=frame.getSyncMessage(z);
193 // if (s.hasTimeInfo())
194 // m=s;
195 // }
196 // if (m==null)
197 // return;
198 //
199 // scrollToolTip.showToolTip(MessageFormat.format(SDMessages._134, //$NON-NLS-1$
200 // new Object[] { formatTimeDate(frame.getSDMinTime()),
201 // formatTimeDate(m),
202 // formatTimeDate(frame.getSDMaxTime())} ));
203 // }
204 //
205 // public void widgetDefaultSelected(SelectionEvent e) {
206 // // scrollToolTip.showToolTip("Toto");
207 // }
208 //
209 // });
210 //
211
212 accessible = getViewControl().getAccessible();
213
214 accessible.addAccessibleListener(new AccessibleAdapter() {
215 @Override
216 public void getName(AccessibleEvent e) {
217 if (e.childID == ACC.CHILDID_SELF) {
218 // e.result = "";
219 }
220 // Case toolTip
221 else if (e.childID == 0) {
222 if (toolTipNode != null) {
223 if (toolTipNode instanceof Lifeline) {
224 Lifeline lifeline = (Lifeline) toolTipNode;
225 e.result = lifeline.getToolTipText();
226 } else {
227 e.result = toolTipNode.getName() + getPostfixForTooltip(true);
228 }
229 }
230 } else {
231 if (getFocusNode() != null) {
232 if (getFocusNode() instanceof Lifeline) {
233 e.result = MessageFormat.format(SDMessages._1, new Object[] { String.valueOf(getFocusNode().getName()) });
234 }
235 if (getFocusNode() instanceof BaseMessage) {
236 BaseMessage mes = (BaseMessage) getFocusNode();
237 if ((mes.getStartLifeline() != null) && (mes.getEndLifeline() != null)) {
238 e.result = MessageFormat.format(
239 SDMessages._2,
240 new Object[] { String.valueOf(mes.getName()), String.valueOf(mes.getStartLifeline().getName()), Integer.valueOf(mes.getStartOccurrence()), String.valueOf(mes.getEndLifeline().getName()),
241 Integer.valueOf(mes.getEndOccurrence()) });
242 } else if ((mes.getStartLifeline() == null) && (mes.getEndLifeline() != null)) {
243 e.result = MessageFormat.format(SDMessages._4, new Object[] { String.valueOf(mes.getName()), String.valueOf(mes.getEndLifeline().getName()), Integer.valueOf(mes.getEndOccurrence()) });
244 } else if ((mes.getStartLifeline() != null) && (mes.getEndLifeline() == null)) {
245 e.result = MessageFormat.format(SDMessages._3, new Object[] { String.valueOf(mes.getName()), String.valueOf(mes.getStartLifeline().getName()), Integer.valueOf(mes.getStartOccurrence()) });
246 }
247 } else if (getFocusNode() instanceof BasicExecutionOccurrence) {
248 BasicExecutionOccurrence exec = (BasicExecutionOccurrence) getFocusNode();
249 e.result = MessageFormat.format(SDMessages._5,
250 new Object[] { String.valueOf(exec.getName()), String.valueOf(exec.getLifeline().getName()), Integer.valueOf(exec.getStartOccurrence()), Integer.valueOf(exec.getEndOccurrence()) });
251 }
252
253 }
254 // e.result =currentGraphNode.getName();
255 }
256 }
257 });
258
259 accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
260 @Override
261 public void getFocus(AccessibleControlEvent e) {
262 if (focusedWidget == -1)
263 e.childID = ACC.CHILDID_SELF;
264 else
265 e.childID = focusedWidget;
266 }
267
268 @Override
269 public void getRole(AccessibleControlEvent e) {
270 switch (e.childID) {
271 case ACC.CHILDID_SELF:
272 e.detail = ACC.ROLE_CLIENT_AREA;
273 break;
274 case 0:
275 e.detail = ACC.ROLE_TOOLTIP;
276 break;
277 case 1:
278 e.detail = ACC.ROLE_LABEL;
279 break;
280 }
281 }
282
283 @Override
284 public void getState(AccessibleControlEvent e) {
285 e.detail = ACC.STATE_FOCUSABLE;
286 if (e.childID == ACC.CHILDID_SELF) {
287 e.detail |= ACC.STATE_FOCUSED;
288 } else {
289 e.detail |= ACC.STATE_SELECTABLE;
290 if (e.childID == focusedWidget)
291 e.detail |= ACC.STATE_FOCUSED | ACC.STATE_SELECTED | ACC.STATE_CHECKED;
292 }
293 }
294 });
295
296 insertionCartet = new Caret((Canvas) getViewControl(), SWT.NONE);
297 insertionCartet.setVisible(false);
298
299 collapaseCaretImg = TmfUiPlugin.getDefault().getImageFromPath(ITmfImageConstants.IMG_UI_ARROW_COLLAPSE_OBJ);
300 arrowUpCaretImg = TmfUiPlugin.getDefault().getImageFromPath(ITmfImageConstants.IMG_UI_ARROW_UP_OBJ);
301
302 reorderList = new ArrayList<Lifeline[]>();
303 getViewControl().addTraverseListener(new TraverseListener() {
304
305 @Override
306 public void keyTraversed(TraverseEvent e) {
307 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS))
308 e.doit = true;
309 }
310
311 });
312
313 addTraverseListener(new TraverseListener() {
314
315 @Override
316 public void keyTraversed(TraverseEvent e) {
317 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS))
318 e.doit = true;
319 }
320
321 });
322
323 getViewControl().addFocusListener(new FocusListener() {
324
325 @Override
326 public void focusGained(FocusEvent e) {
327 // TODO Auto-generated method stub
328 SDViewPref.getInstance().setNoFocusSelection(false);
329 ctrlSelection = false;
330 shiftSelection = false;
331 redraw();
332 }
333
334 @Override
335 public void focusLost(FocusEvent e) {
336 SDViewPref.getInstance().setNoFocusSelection(true);
337 redraw();
338 }
339 });
340 }
341
342 public void setTimeBar(TimeCompressionBar bar) {
343 if (bar != null) {
344 timeBar = bar;
345 timeBar.addTimeCompressionListener(this);
346 }
347 }
348
349 protected void setCollapseProvider(ISDCollapseProvider provider) {
350 collapseProvider = provider;
351 }
352
353 @Override
354 protected void keyPressedEvent(KeyEvent event) {
355 if (!(isFocusControl() || getViewControl().isFocusControl())) {
356 Control[] child = getParent().getChildren();
357 for (int i = 0; i < child.length; i++) {
358 if (child[i].isFocusControl()) {
359 if (!(child[i] instanceof ScrollView)) {
360 getViewControl().setFocus();
361 break;
362 }
363 }
364 }
365 }
366 setFocus(-1);
367
368 if (event.keyCode == SWT.CTRL)
369 ctrlSelection = true;
370 if (event.keyCode == SWT.SHIFT) {
371 shiftSelection = true;
372 prevList = new ArrayList<GraphNode>();
373 prevList.addAll(getSelection());
374 }
375
376 GraphNode prevNode = getFocusNode();
377
378 if (event.keyCode == SWT.ARROW_RIGHT)
379 traverseRight();
380
381 if (event.keyCode == SWT.ARROW_LEFT)
382 traverseLeft();
383
384 if (event.keyCode == SWT.ARROW_DOWN)
385 traverseDown();
386
387 if (event.keyCode == SWT.ARROW_UP)
388 traverseUp();
389
390 if (event.keyCode == SWT.HOME)
391 traverseHome();
392
393 if (event.keyCode == SWT.END)
394 traverseEnd();
395
396 if ((!shiftSelection) && (!ctrlSelection))
397 listStart = currentGraphNode;
398
399 if (event.character == ' ') {
400 performSelection(currentGraphNode);
401 if (!shiftSelection)
402 listStart = currentGraphNode;
403 }
404
405 if ((shiftSelection) && (prevNode != getFocusNode())) {
406 clearSelection();
407 addSelection(prevList);
408 addSelection(frame.getNodeList(listStart, getFocusNode()));
409 if (getFocusNode() instanceof Lifeline)
410 ensureVisible(getFocusNode().getX(), getFocusNode().getY(), getFocusNode().getWidth(), getFocusNode().getHeight(), SWT.CENTER | SWT.VERTICAL, true);
411 else
412 ensureVisible(getFocusNode());
413 } else if ((!ctrlSelection) && (!shiftSelection)) {
414
415 clearSelection();
416 if (getFocusNode() != null) {
417 addSelection(getFocusNode());
418
419 if (getFocusNode() instanceof Lifeline)
420 ensureVisible(getFocusNode().getX(), getFocusNode().getY(), getFocusNode().getWidth(), getFocusNode().getHeight(), SWT.CENTER | SWT.VERTICAL, true);
421 else
422 ensureVisible(getFocusNode());
423 }
424 }
425
426 if (currentGraphNode != null)
427 currentGraphNode.setFocused(true);
428 redraw();
429
430 if ((event.character == ' ') && ((zoomInMode) || (zoomOutMode))) {
431 int cx = Math.round((getContentsX() + getVisibleWidth() / 2) / zoomValue);
432 int cy = Math.round((getContentsY() + getVisibleHeight() / 2) / zoomValue);
433 if (zoomInMode) {
434 if (zoomValue < 64)
435 zoomValue = zoomValue * (float) 1.25;
436 } else
437 zoomValue = zoomValue / (float) 1.25;
438 int x = Math.round(cx * zoomValue - getVisibleWidth() / (float) 2);
439 int y = Math.round(cy * zoomValue - getVisibleHeight() / (float) 2);
440 setContentsPos(x, y);
441 if (timeBar != null)
442 timeBar.setZoom(zoomValue);
443 // redraw also resize the scrollView content
444 redraw();
445 }
446 }
447
448 @Override
449 protected void keyReleasedEvent(KeyEvent event) {
450 setFocus(-1);
451 if (event.keyCode == SWT.CTRL)
452 ctrlSelection = false;
453 if (event.keyCode == SWT.SHIFT)
454 shiftSelection = false;
455 super.keyReleasedEvent(event);
456 setFocus(1);
457 }
458
459 /**
460 * Resize the contents to insure the frame fit into the view
461 *
462 * @param frame the frame which will be drawn in the view
463 */
464 public void resizeContents(Frame frame) {
465 int width = Math.round((frame.getWidth() + 2 * Metrics.FRAME_H_MARGIN) * zoomValue);
466 int height = Math.round((frame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * zoomValue);
467 resizeContents(width, height);
468 }
469
470 protected boolean checkFocusOnChilds(Control childs) {
471 if (childs instanceof Composite) {
472 Control[] child = ((Composite) childs).getChildren();
473 for (int i = 0; i < child.length; i++) {
474 if (child[i].isFocusControl()) {
475 return true;
476 } else
477 checkFocusOnChilds(child[i]);
478 }
479 }
480 return false;
481 }
482
483 @Override
484 public boolean isFocusControl() {
485 Control[] child = getChildren();
486 for (int i = 0; i < child.length; i++) {
487 if (child[i].isFocusControl()) {
488 return true;
489 } else
490 checkFocusOnChilds(child[i]);
491 }
492 return false;
493 }
494
495 /**
496 * The frame to render (the sequence diagram)
497 *
498 * @param theFrame the frame to display
499 */
500 public void setFrame(Frame theFrame, boolean resetPosition) {
501 reorderList.clear();
502 selectedNodeList.clear();
503 selProvider.setSelection(new StructuredSelection());
504 frame = theFrame;
505 if (resetPosition) {
506 setContentsPos(0, 0);
507 resizeContents(frame);
508 redraw();
509 }
510 // prepare the old overview to be reused
511 if (overView != null)
512 overView.dispose();
513 overView = null;
514 resizeContents(frame);
515 }
516
517 /**
518 * Returns the current Frame (the sequence diagram container)
519 *
520 * @return the frame
521 */
522 public Frame getFrame() {
523 return frame;
524 }
525
526 /**
527 * Returns the selection provider for the current sequence diagram
528 *
529 * @return the selection provider
530 */
531 public ISelectionProvider getSelectionProvider() {
532 return selProvider;
533 }
534
535 @Override
536 public boolean setContentsPos(int x, int y) {
537 if (x < 0)
538 x = 0;
539 if (y < 0)
540 y = 0;
541 if (frame == null)
542 return false;
543 if (x + getVisibleWidth() > getContentsWidth())
544 x = getContentsWidth() - getVisibleWidth();
545 if (y + getVisibleHeight() > getContentsHeight())
546 y = getContentsHeight() - getVisibleHeight();
547 int x1 = Math.round(x / zoomValue);
548 int y2 = Math.round(y / zoomValue);
549 int width = Math.round(getVisibleWidth() / zoomValue);
550 int height = Math.round(getVisibleHeight() / zoomValue);
551 frame.updateIndex(x1, y2, width, height);
552
553 if (insertionCartet != null && insertionCartet.isVisible())
554 insertionCartet.setVisible(false);
555
556 return super.setContentsPos(x, y);
557 }
558
559 @Override
560 protected void contentsMouseHover(MouseEvent event) {
561 GraphNode graphNode = null;
562 if (frame != null) {
563 int x = Math.round(event.x / zoomValue);
564 int y = Math.round(event.y / zoomValue);
565 graphNode = frame.getNodeAt(x, y);
566 if ((graphNode != null) && (SDViewPref.getInstance().tooltipEnabled())) {
567 toolTipNode = graphNode;
568 String postfix = getPostfixForTooltip(true);
569 if (graphNode instanceof Lifeline) {
570 Lifeline lifeline = (Lifeline) graphNode;
571 toolTip.showToolTip(lifeline.getToolTipText() + postfix);
572 setFocus(0);
573 } else {
574 toolTip.showToolTip(graphNode.getName() + postfix);
575 setFocus(0);
576 }
577 } else
578 toolTip.hideToolTip();
579 }
580 }
581
582 protected String getPostfixForTooltip(boolean accessible) {
583 String postfix = "";//$NON-NLS-1$
584 // Determine if the tooltip must show the time difference between the current mouse position and
585 // the last selected graphNode
586 if ((currentGraphNode != null) &&
587 (currentGraphNode instanceof ITimeRange) &&
588 (toolTipNode instanceof ITimeRange) &&
589 (currentGraphNode != toolTipNode) &&
590 ((ITimeRange) toolTipNode).hasTimeInfo() &&
591 ((ITimeRange) currentGraphNode).hasTimeInfo()) {
592 postfix = " -> " + currentGraphNode.getName() + "\n" + SDMessages._138 + " "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
593
594 //double delta = ((ITimeRange)toolTipNode).getLastTime()-((ITimeRange)currentGraphNode).getLastTime();
595 ITmfTimestamp firstTime = ((ITimeRange) currentGraphNode).getEndTime();
596 ITmfTimestamp lastTime = ((ITimeRange) toolTipNode).getEndTime();
597 TmfTimestamp delta = (TmfTimestamp) lastTime.getDelta(firstTime);
598 postfix += delta.toString();
599 } else {
600 if ((toolTipNode instanceof ITimeRange) && ((ITimeRange) toolTipNode).hasTimeInfo()) {
601 postfix = "\n";//$NON-NLS-1$
602 ITmfTimestamp firstTime = ((ITimeRange) toolTipNode).getStartTime();
603 ITmfTimestamp lastTime = ((ITimeRange) toolTipNode).getEndTime();
604
605 if (firstTime != null) {
606 if (lastTime != null && firstTime.compareTo(lastTime, true) != 0) {
607 postfix += "start: " + firstTime + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
608 postfix += "end: " + lastTime + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
609 } else {
610 postfix += firstTime.toString();
611 }
612 }
613 else if (lastTime != null) {
614 postfix += lastTime.toString();
615 }
616 }
617 }
618 return postfix;
619 }
620
621 protected void setFocus(int newFocusShape) {
622 focusedWidget = newFocusShape;
623 if (focusedWidget == -1) {
624 getViewControl().getAccessible().setFocus(ACC.CHILDID_SELF);
625 } else {
626 getViewControl().getAccessible().setFocus(focusedWidget);
627 }
628 }
629
630 /** Timer for auto_scroll feature */
631 protected AutoScroll local_auto_scroll_ = null;
632 /** TimerTask for auto_scroll feature !=null when auto scroll is running */
633 protected Timer local_auto_scroll_timer_ = null;
634
635 /** TimerTask for auto scroll feature. */
636 protected static class AutoScroll extends TimerTask {
637 public int dx_, dy_;
638 public SDWidget sv_;
639
640 public AutoScroll(SDWidget _sv, int _dx, int _dy) {
641 sv_ = _sv;
642 dx_ = _dx;
643 dy_ = _dy;
644 }
645
646 @Override
647 public void run() {
648 Display.getDefault().asyncExec(new Runnable() {
649 @Override
650 public void run() {
651 if (sv_.isDisposed())
652 return;
653 sv_.dragX += dx_;
654 sv_.dragY += dy_;
655 sv_.scrollBy(dx_, dy_);
656 }
657 });
658 }
659 }
660
661 @Override
662 protected void contentsMouseMoveEvent(MouseEvent e) {
663 scrollToolTip.hideToolTip();
664 toolTip.hideToolTip();
665 // super.contentsMouseMoveEvent(e);
666 if (!(isFocusControl() || getViewControl().isFocusControl())) {
667 Control[] child = getParent().getChildren();
668 for (int i = 0; i < child.length; i++) {
669 if (child[i].isFocusControl()) {
670 if (!(child[i] instanceof ScrollView)) {
671 getViewControl().setFocus();
672 break;
673 }
674 }
675 }
676 }
677 setFocus(-1);
678
679 if (((e.stateMask & SWT.BUTTON_MASK) != 0) && ((dragAndDrop != null) || getDragAndDrop) && (reorderMode || collapseProvider != null)) {
680 getDragAndDrop = false;
681 if (currentGraphNode instanceof Lifeline)
682 dragAndDrop = (Lifeline) currentGraphNode;
683 if (dragAndDrop != null) {
684 int dx = 0;
685 int dy = 0;
686 if (e.x > getContentsX() + getVisibleWidth()) {
687 dx = e.x - (getContentsX() + getVisibleWidth());
688 } else if (e.x < getContentsX()) {
689 dx = -getContentsX() + e.x;
690 }
691 if (e.y > getContentsY() + getVisibleHeight()) {
692 dy = e.y - (getContentsY() + getVisibleHeight());
693 } else if (e.y < getContentsY()) {
694 dy = -getContentsY() + e.y;
695 }
696 dragX = e.x;
697 dragY = e.y;
698 if (dx != 0 || dy != 0) {
699 if (local_auto_scroll_ == null) {
700 if (local_auto_scroll_timer_ == null) {
701 local_auto_scroll_timer_ = new Timer(true);
702 }
703 local_auto_scroll_ = new AutoScroll(this, dx, dy);
704 local_auto_scroll_timer_.schedule(local_auto_scroll_, 0, 75);
705 } else {
706 local_auto_scroll_.dx_ = dx;
707 local_auto_scroll_.dy_ = dy;
708 }
709 } else if (local_auto_scroll_ != null) {
710 local_auto_scroll_.cancel();
711 local_auto_scroll_ = null;
712 }
713 dragX = Math.round(e.x / zoomValue);
714 dragY = Math.round(e.y / zoomValue);
715 redraw();
716 Lifeline node = frame.getCloserLifeline(dragX);
717 if ((node != null) && (node != dragAndDrop)) {
718 int y = 0;
719 int y1 = 0;
720 int height = Metrics.getLifelineHeaderFontHeigth() + 2 * Metrics.LIFELINE_HEARDER_TEXT_V_MARGIN;
721 int hMargin = Metrics.LIFELINE_VT_MAGIN / 4;
722 int x = node.getX();
723 int width = node.getWidth();
724 if (frame.getVisibleAreaY() < node.getY() + node.getHeight() - height - hMargin)
725 y = contentsToViewY(Math.round((node.getY() + node.getHeight()) * zoomValue));
726 else
727 y = Math.round(height * zoomValue);
728
729 if (frame.getVisibleAreaY() < contentsToViewY(node.getY() - hMargin))
730 y1 = contentsToViewY(Math.round((node.getY() - hMargin) * zoomValue));
731 else
732 y1 = Math.round(height * zoomValue);
733
734 int rx = Math.round(x * zoomValue);
735
736 insertionCartet.setVisible(true);
737 if ((insertionCartet.getImage() != null) && (!insertionCartet.getImage().isDisposed()))
738 insertionCartet.getImage().dispose();
739 if (rx <= e.x && Math.round(rx + (width * zoomValue)) >= e.x) {
740 if (collapseProvider != null) {
741 ImageData data = collapaseCaretImg.getImageData();
742 data = data.scaledTo(Math.round(collapaseCaretImg.getBounds().width * zoomValue), Math.round(collapaseCaretImg.getBounds().height * zoomValue));
743 currentCaretImage = new Image(Display.getCurrent(), data);
744 insertionCartet.setImage(currentCaretImage);
745 insertionCartet.setLocation(contentsToViewX(rx + Math.round((width / (float) 2) * zoomValue)) - currentCaretImage.getBounds().width / 2, y);
746 }
747 } else if (reorderMode) {
748 if (rx > e.x) {
749 if (node.getIndex() > 1 && frame.getLifeline(node.getIndex() - 2) == dragAndDrop)
750 return;
751 ImageData data = arrowUpCaretImg.getImageData();
752 data = data.scaledTo(Math.round(arrowUpCaretImg.getBounds().width * zoomValue), Math.round(arrowUpCaretImg.getBounds().height * zoomValue));
753 currentCaretImage = new Image(Display.getCurrent(), data);
754 insertionCartet.setImage(currentCaretImage);
755 insertionCartet.setLocation(contentsToViewX(Math.round((x - Metrics.LIFELINE_SPACING / 2) * zoomValue)) - currentCaretImage.getBounds().width / 2, y1);
756 } else {
757 if (node.getIndex() < frame.lifeLinesCount() && frame.getLifeline(node.getIndex()) == dragAndDrop)
758 return;
759 ImageData data = arrowUpCaretImg.getImageData();
760 data = data.scaledTo(Math.round(arrowUpCaretImg.getBounds().width * zoomValue), Math.round(arrowUpCaretImg.getBounds().height * zoomValue));
761 currentCaretImage = new Image(Display.getCurrent(), data);
762 insertionCartet.setImage(currentCaretImage);
763 insertionCartet.setLocation(contentsToViewX(Math.round((x + width + Metrics.LIFELINE_SPACING / 2) * zoomValue)) - currentCaretImage.getBounds().width / 2 + 1, y1);
764 }
765 }
766 } else
767 insertionCartet.setVisible(false);
768 }
769 } else
770 super.contentsMouseMoveEvent(e);
771 }
772
773 @Override
774 protected void contentsMouseUpEvent(MouseEvent event) {
775 // Just in case the diagram highlight a time compression region
776 // this region need to be released when clicking everywhere
777 insertionCartet.setVisible(false);
778 if (dragAndDrop != null) {
779 if ((overView != null) && (!overView.isDisposed()))
780 overView.dispose();
781 overView = null;
782 Lifeline node = frame.getCloserLifeline(dragX);
783 if (node != null) {
784 int rx = Math.round(node.getX() * zoomValue);
785 if (rx <= event.x && Math.round(rx + (node.getWidth() * zoomValue)) >= event.x) {
786 if ((collapseProvider != null) && (dragAndDrop != node))
787 collapseProvider.collapseTwoLifelines((Lifeline) dragAndDrop, node);
788 } else if (rx < event.x) {
789 frame.insertLifelineAfter((Lifeline) dragAndDrop, node);
790 if (node.getIndex() < frame.lifeLinesCount()) {
791 Lifeline temp[] = { (Lifeline) dragAndDrop, frame.getLifeline(node.getIndex()) };
792 reorderList.add(temp);
793 } else {
794 Lifeline temp[] = { (Lifeline) dragAndDrop, null };
795 reorderList.add(temp);
796 }
797 } else {
798 frame.insertLifelineBefore((Lifeline) dragAndDrop, node);
799 Lifeline temp[] = { (Lifeline) dragAndDrop, node };
800 reorderList.add(temp);
801 }
802 }
803 }
804 dragAndDrop = null;
805 redraw();
806 if (frame == null) {
807 return;
808 }
809 frame.resetTimeCompression();
810
811 // reset auto scroll if it's engaged
812 if (local_auto_scroll_ != null) {
813 local_auto_scroll_.cancel();
814 local_auto_scroll_ = null;
815 }
816 super.contentsMouseUpEvent(event);
817 }
818
819 @Override
820 protected void contentsMouseDownEvent(MouseEvent event) {
821 if (currentGraphNode != null)
822 currentGraphNode.setFocused(false);
823 // Just in case the diagram highlight a time compression region
824 // this region need to be released when clicking everywhere
825 if (frame == null) {
826 return;
827 }
828 frame.resetTimeCompression();
829
830 if ((event.stateMask & SWT.CTRL) != 0) {
831 ctrlSelection = true;
832 } else
833 ctrlSelection = false;
834
835 if (((zoomInMode) || (zoomOutMode)) && (event.button == 1)) {
836 int cx = Math.round(event.x / zoomValue);
837 int cy = Math.round(event.y / zoomValue);
838 if (zoomInMode) {
839 if (zoomValue < 64)
840 zoomValue = zoomValue * (float) 1.25;
841 } else
842 zoomValue = zoomValue / (float) 1.25;
843 int x = Math.round(cx * zoomValue - getVisibleWidth() / (float) 2);
844 int y = Math.round(cy * zoomValue - getVisibleHeight() / (float) 2);
845 setContentsPos(x, y);
846 if (timeBar != null)
847 timeBar.setZoom(zoomValue);
848 // redraw also resize the scrollView content
849 redraw();
850 } else// if (event.button ==1)
851 {
852 GraphNode node = null;
853 if (frame != null) {
854 int x = Math.round(event.x / zoomValue);
855 int y = Math.round(event.y / zoomValue);
856 node = frame.getNodeAt(x, y);
857
858 if ((event.button == 1) || ((node != null) && !node.isSelected())) {
859 if (!shiftSelection)
860 listStart = node;
861 if (shiftSelection) {
862 clearSelection();
863 addSelection(frame.getNodeList(listStart, node));
864 } else
865 performSelection(node);
866 currentGraphNode = node;
867 if (node != null)
868 node.setFocused(true);
869 }
870 redraw();
871 }
872 }
873 if (dragAndDrop == null)
874 super.contentsMouseDownEvent(event);
875 getDragAndDrop = (event.button == 1);
876
877 }
878
879 /**
880 * Highlight the given GraphNode<br>
881 * The GraphNode is then displayed using the system default selection color
882 *
883 * @param node the GraphNode to highlight
884 */
885 protected void performSelection(GraphNode node) {
886 if ((ctrlSelection) || (shiftSelection)) {
887 if (node != null) {
888 if (selectedNodeList.contains(node)) {
889 removeSelection(node);
890 } else {
891 addSelection(node);
892 }
893 } else
894 return;
895 } else {
896 clearSelection();
897 if (node != null) {
898 addSelection(node);
899 }
900 }
901 }
902
903 public List<GraphNode> getSelection() {
904 return selectedNodeList;
905 }
906
907 public void addSelection(GraphNode node) {
908 if (node == null)
909 return;
910 selectedNodeList.add(node);
911 node.setSelected(true);
912 currentGraphNode = node;
913 StructuredSelection selection = new StructuredSelection(selectedNodeList);
914 selProvider.setSelection(selection);
915 }
916
917 public void addSelection(List<GraphNode> list) {
918 // selectedNodeList.addAll(list);
919 for (int i = 0; i < list.size(); i++) {
920 if (!selectedNodeList.contains(list.get(i))) {
921 selectedNodeList.add(list.get(i));
922 ((GraphNode) list.get(i)).setSelected(true);
923 }
924 }
925 StructuredSelection selection = new StructuredSelection(selectedNodeList);
926 selProvider.setSelection(selection);
927 }
928
929 public void removeSelection(GraphNode node) {
930 selectedNodeList.remove(node);
931 node.setSelected(false);
932 node.setFocused(false);
933 StructuredSelection selection = new StructuredSelection(selectedNodeList);
934 selProvider.setSelection(selection);
935 }
936
937 public void removeSelection(List<GraphNode> list) {
938 selectedNodeList.removeAll(list);
939 for (int i = 0; i < list.size(); i++) {
940 ((GraphNode) list.get(i)).setSelected(false);
941 ((GraphNode) list.get(i)).setFocused(false);
942 }
943 StructuredSelection selection = new StructuredSelection(selectedNodeList);
944 selProvider.setSelection(selection);
945 }
946
947 /**
948 * Clear the list of GraphNodes which must be drawn selected
949 *
950 */
951 public void clearSelection() {
952 for (int i = 0; i < selectedNodeList.size(); i++) {
953 ((GraphNode) selectedNodeList.get(i)).setSelected(false);
954 ((GraphNode) selectedNodeList.get(i)).setFocused(false);
955 }
956 currentGraphNode = null;
957 selectedNodeList.clear();
958 selProvider.setSelection(new StructuredSelection());
959 }
960
961 public void setSite(ViewPart viewSite) {
962 site = viewSite;
963 site.getSite().setSelectionProvider(selProvider);
964 IContextService service = (IContextService) site.getSite().getWorkbenchWindow().getService(IContextService.class);
965 service.activateContext("org.eclipse.linuxtools.tmf.ui.view.uml2sd.context"); //$NON-NLS-1$
966 service.activateContext(IContextIds.CONTEXT_ID_WINDOW);
967 }
968
969 protected Image getDrawBuffer(GC gc) {
970
971 update();
972 Rectangle area = getClientArea();
973 Image dbuffer = null;
974 GC gcim = null;
975
976 try {
977 // if (dbuffer!=null)
978 // dbuffer.dispose();
979 dbuffer = new Image(getDisplay(), area.width, area.height);
980 } catch (Exception e) {
981 System.out.println(e.toString());
982 }
983 gcim = new GC(dbuffer);
984
985 NGC context = new NGC(this, gcim);
986
987 // Set the metrics to use for lifeline text and message text
988 // using the Graphical Context
989 Metrics.setLifelineFontHeight(context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
990 Metrics.setLifelineFontWidth(context.getFontWidth(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
991 Metrics.setLifelineWidth(SDViewPref.getInstance().getLifelineWidth());
992 Metrics.setFrameFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_FRAME_NAME)));
993 Metrics.setLifelineHeaderFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_LIFELINE_HEADER)));
994
995 int syncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS));
996 int syncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS_RET));
997 int asyncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS));
998 int asyncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS_RET));
999
1000 int messageFontHeight = 0;
1001 if (syncMessFontH > syncMessRetFontH)
1002 messageFontHeight = syncMessFontH;
1003 else
1004 messageFontHeight = syncMessRetFontH;
1005 if (messageFontHeight < asyncMessFontH)
1006 messageFontHeight = asyncMessFontH;
1007 if (messageFontHeight < asyncMessRetFontH)
1008 messageFontHeight = asyncMessRetFontH;
1009 Metrics.setMessageFontHeight(messageFontHeight);
1010 context.setFont(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE));
1011
1012 int width = (int) ((frame.getWidth() + 2 * Metrics.FRAME_H_MARGIN) * zoomValue);
1013 int height = (int) ((frame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * zoomValue);
1014 // if (width<area.width)
1015 // width=area.width;
1016 // if (height<area.height)
1017 // height=area.height;
1018 resizeContents(width, height);
1019
1020 context.setBackground(Frame.getUserPref().getBackGroundColor(ISDPreferences.PREF_FRAME));
1021 context.fillRectangle(0, 0, getContentsWidth(), Metrics.FRAME_V_MARGIN);
1022 context.fillRectangle(0, 0, frame.getX(), getContentsHeight());
1023 context.fillRectangle(frame.getX() + frame.getWidth() + 1, 0, getContentsWidth() - (frame.getX() + frame.getWidth() + 1), getContentsHeight());
1024 context.fillRectangle(0, frame.getY() + frame.getHeight() + 1, getContentsWidth(), getContentsHeight() - (frame.getY() + frame.getHeight() + 1));
1025 gcim.setLineWidth(1);
1026
1027 frame.draw(context);
1028 if (dragAndDrop != null) {
1029 Lifeline node = (Lifeline) dragAndDrop;
1030 boolean isSelected = dragAndDrop.isSelected();
1031 boolean hasFocus = dragAndDrop.hasFocus();
1032 node.setSelected(false);
1033 node.setFocused(false);
1034 node.draw(context, dragX, dragY);
1035 node.setSelected(isSelected);
1036 node.setFocused(hasFocus);
1037 }
1038 gcim.dispose();
1039 context.dispose();
1040 return dbuffer;
1041 }
1042
1043 /**
1044 *
1045 * @param gc the context
1046 */
1047 @Override
1048 protected void drawContents(GC gc, int clipx, int clipy, int clipw, int cliph) {
1049 if (frame == null) {
1050 gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
1051 gc.fillRectangle(0, 0, getVisibleWidth(), getVisibleHeight());
1052 gc.dispose();
1053 return;
1054 } else {
1055 Frame.setUserPref(SDViewPref.getInstance());
1056 }
1057
1058 Rectangle area = getClientArea();
1059 Image dbuffer = getDrawBuffer(gc);
1060 int height = Math.round((frame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * zoomValue);
1061
1062 try {
1063 gc.drawImage(dbuffer, 0, 0, area.width, area.height, 0, 0, area.width, area.height);
1064 } catch (Exception e) {
1065 System.out.println(e.getMessage());
1066 }
1067 dbuffer.dispose();
1068 setHScrollBarIncrement(Math.round(SDViewPref.getInstance().getLifelineWidth() / (float) 2 * zoomValue));
1069 setVScrollBarIncrement(Math.round(Metrics.getMessagesSpacing() * zoomValue));
1070 if ((timeBar != null) && (frame.hasTimeInfo())) {
1071 timeBar.resizeContents(9, height + getHorizontalBarHeight());
1072 timeBar.setContentsPos(getContentsX(), getContentsY());
1073 timeBar.redraw();
1074 timeBar.update();
1075 }
1076 float xRatio = getContentsWidth() / (float) getVisibleWidth();
1077 float yRatio = getContentsHeight() / (float) getVisibleHeight();
1078 if (yRatio > xRatio) {
1079 setOverviewSize((int) (getVisibleHeight() * 0.75));
1080 } else {
1081 setOverviewSize((int) (getVisibleWidth() * 0.75));
1082 }
1083 }
1084
1085 /**
1086 * Returns the GraphNode overView the mouse if any
1087 *
1088 * @return the GraphNode
1089 */
1090 public GraphNode getMouseOverNode() {
1091 return currentGraphNode;
1092 }
1093
1094 @Override
1095 public void widgetDefaultSelected(SelectionEvent event) {
1096 }
1097
1098 @Override
1099 public void widgetSelected(SelectionEvent event) {
1100 if (event.widget == zoomIn)
1101 zoomValue = zoomValue * 2;
1102 else if (event.widget == zoomOut)
1103 zoomValue = zoomValue / 2;
1104 else {
1105 // SearchFilterDialog tt = new SearchFilterDialog(null);//display.getActiveShell());
1106 }
1107 redraw();
1108 }
1109
1110 public void setZoomInMode(boolean value) {
1111 if (value)
1112 setZoomOutMode(false);
1113 zoomInMode = value;
1114 }
1115
1116 public void setZoomOutMode(boolean value) {
1117 if (value)
1118 setZoomInMode(false);
1119 zoomOutMode = value;
1120 }
1121
1122 /**
1123 * Moves the Sequence diagram to ensure the given node is visible and draw it selected
1124 *
1125 * @param node the GraphNode to move to
1126 */
1127 public void moveTo(GraphNode node) {
1128 if (node == null)
1129 return;
1130 clearSelection();
1131 addSelection(node);
1132 ensureVisible(node);
1133 }
1134
1135 /**
1136 * Moves the Sequence diagram to ensure the given node is visible
1137 *
1138 * @param node the GraphNode to move to
1139 */
1140 public void ensureVisible(GraphNode node) {
1141 if (node == null)
1142 return;
1143 int x = Math.round(node.getX() * zoomValue);
1144 int y = Math.round(node.getY() * zoomValue);
1145 int width = Math.round(node.getWidth() * zoomValue);
1146 int height = Math.round(node.getHeight() * zoomValue);
1147 if (node instanceof BaseMessage) {
1148 if (height == 0) {
1149 int header = Metrics.LIFELINE_HEARDER_TEXT_V_MARGIN * 2 + Metrics.getLifelineHeaderFontHeigth();
1150 height = -Math.round((Metrics.getMessagesSpacing() + header) * zoomValue);
1151 y = y + Math.round(Metrics.SYNC_INTERNAL_MESSAGE_HEIGHT * zoomValue);
1152 }
1153 }
1154 if (node instanceof BasicExecutionOccurrence) {
1155 width = 1;
1156 height = 1;
1157 }
1158 if (node instanceof Lifeline) {
1159 y = getContentsY();
1160 height = getVisibleHeight();
1161 }
1162 ensureVisible(x, y, width, height, SWT.CENTER, true);
1163 redraw();
1164 }
1165
1166 public float getZoomFactor() {
1167 return zoomValue;
1168 }
1169
1170 /**
1171 * Called when property changed occurs in the preference page. "PREFOK" is fired when the user press the ok or apply
1172 * button
1173 *
1174 * @param e the PropertyChangeEvent
1175 */
1176 @Override
1177 public void propertyChange(PropertyChangeEvent e) {
1178 if (frame != null && !isDisposed()) {
1179 frame.resetTimeCompression();
1180 }
1181 if (e.getProperty().equals("PREFOK")) //$NON-NLS-1$
1182 {
1183 // Prepare the overview to be reused for the new
1184 // settings (especially the colors)
1185 if (overView != null)
1186 overView.dispose();
1187 overView = null;
1188 redraw();
1189 }
1190 }
1191
1192 @Override
1193 public void widgetDisposed(DisposeEvent e) {
1194 if (overView != null)
1195 overView.dispose();
1196 super.removeDisposeListener(this);
1197 if ((currentCaretImage != null) && (!currentCaretImage.isDisposed()))
1198 currentCaretImage.dispose();
1199 if ((arrowUpCaretImg != null) && (!arrowUpCaretImg.isDisposed()))
1200 arrowUpCaretImg.dispose();
1201 if ((collapaseCaretImg != null) && (!collapaseCaretImg.isDisposed()))
1202 collapaseCaretImg.dispose();
1203 SDViewPref.getInstance().removePropertyChangeListener(this);
1204 LoadersManager lm = LoadersManager.getInstance();
1205 if (site != null && site instanceof SDView) {
1206 ((SDView) site).resetProviders();
1207 if (lm != null)
1208 lm.resetLoader(((SDView) site).getViewSite().getId());
1209 }
1210 }
1211
1212 public Image getOverview(Rectangle r) {
1213 float oldzoom = zoomValue;
1214 if ((overView != null) && ((r.width != overView.getBounds().width) || (r.height != overView.getBounds().height))) {
1215 overView.dispose();
1216 overView = null;
1217 }
1218 if (overView == null) {
1219 int backX = getContentsX();
1220 int backY = getContentsY();
1221 setContentsPos(0, 0);
1222 overView = new Image(getDisplay(), r.width, r.height);
1223 GC gcim = new GC(overView);
1224 NGC context = new NGC(this, gcim);
1225 context.setBackground(SDViewPref.getInstance().getBackGroundColor(SDViewPref.PREF_FRAME));
1226 frame.draw(context);
1227 setContentsPos(backX, backY);
1228 gcim.dispose();
1229 context.dispose();
1230 }
1231 zoomValue = oldzoom;
1232 return overView;
1233 }
1234
1235 @Override
1236 protected void drawOverview(GC gc, Rectangle r) {
1237 float oldzoom = zoomValue;
1238 if (getContentsWidth() > getContentsHeight())
1239 zoomValue = (float) r.width / (float) getContentsWidth() * oldzoom;
1240 else
1241 zoomValue = (float) r.height / (float) getContentsHeight() * oldzoom;
1242 if ((overView != null) && ((r.width != overView.getBounds().width) || (r.height != overView.getBounds().height))) {
1243 overView.dispose();
1244 overView = null;
1245 }
1246 if (overView == null) {
1247 int backX = getContentsX();
1248 int backY = getContentsY();
1249 setContentsPos(0, 0);
1250 overView = new Image(getDisplay(), r.width, r.height);
1251 GC gcim = new GC(overView);
1252 NGC context = new NGC(this, gcim);
1253 context.setBackground(SDViewPref.getInstance().getBackGroundColor(SDViewPref.PREF_FRAME));
1254 frame.draw(context);
1255 setContentsPos(backX, backY);
1256 gcim.dispose();
1257 context.dispose();
1258 }
1259 if ((overView != null) && (r.width == overView.getBounds().width) && (r.height == overView.getBounds().height))
1260 gc.drawImage(overView, 0, 0, r.width, r.height, 0, 0, r.width, r.height);
1261
1262 zoomValue = oldzoom;
1263
1264 super.drawOverview(gc, r);
1265 }
1266
1267 @Override
1268 public void deltaSelected(Lifeline lifeline, int startEvent, int nbEvent, IColor color) {
1269 frame.highlightTimeCompression(lifeline, startEvent, nbEvent, color);
1270 ensureVisible(lifeline);
1271 int y1 = lifeline.getY() + lifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * startEvent;
1272 int y2 = lifeline.getY() + lifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * (startEvent + nbEvent);
1273 ensureVisible(lifeline.getX(), y1 - (Metrics.getLifelineHeaderFontHeigth() + +2 * Metrics.LIFELINE_HEARDER_TEXT_V_MARGIN), lifeline.getWidth(), y2 - y1 + 3, SWT.CENTER | SWT.VERTICAL, true);
1274 redraw();
1275 update();
1276 }
1277
1278 public void resetZoomFactor() {
1279 int currentX = Math.round(getContentsX() / zoomValue);
1280 int currentY = Math.round(getContentsY() / zoomValue);
1281 zoomValue = 1;
1282 if (timeBar != null && !timeBar.isDisposed()) {
1283 timeBar.setZoom(zoomValue);
1284 }
1285 redraw();
1286 update();
1287 setContentsPos(currentX, currentY);
1288 }
1289
1290 /**
1291 * Enable or disable the lifeline reodering using Drag and Drop
1292 *
1293 * @param mode - true to enable false otherwise
1294 */
1295 public void setReorderMode(boolean mode) {
1296 reorderMode = mode;
1297 }
1298
1299 /**
1300 * Return the lifelines reorder sequence (using Drag and Drop) if the the reorder mode is turn on. Each ArryList
1301 * element is of type Lifeline[2] with Lifeline[0] inserted before Lifeline[1] in the diagram
1302 *
1303 * @return - the re-odered sequence
1304 */
1305 public ArrayList<Lifeline[]> getLifelineReoderList() {
1306 return reorderList;
1307 }
1308
1309 public void setFocus(GraphNode node) {
1310 if (node == null)
1311 return;
1312 if (currentGraphNode != null)
1313 currentGraphNode.setFocused(false);
1314 currentGraphNode = node;
1315 node.setFocused(true);
1316 ensureVisible(node);
1317 setFocus(0);
1318 }
1319
1320 public GraphNode getFocusNode() {
1321 return currentGraphNode;
1322 }
1323
1324 public void traverseRight() {
1325 Object selectedNode = getFocusNode();
1326 if (selectedNode == null)
1327 traverseLeft();
1328 GraphNode node = null;
1329 if (selectedNode instanceof BaseMessage) {
1330 if (((BaseMessage) selectedNode).getEndLifeline() != null)
1331 node = frame.getCalledMessage((BaseMessage) selectedNode);
1332 }
1333 if (selectedNode instanceof BasicExecutionOccurrence) {
1334 selectedNode = ((BasicExecutionOccurrence) selectedNode).getLifeline();
1335 }
1336 if ((node == null) && (selectedNode instanceof Lifeline)) {
1337 // if (selectedNode instanceof BaseMessage)
1338 // {
1339 // if (((BaseMessage)selectedNode).getStartLifeline()!=null)
1340 // selectedNode = ((BaseMessage)selectedNode).getStartLifeline();
1341 // }
1342 for (int i = 0; i < frame.lifeLinesCount(); i++) {
1343 if ((selectedNode == frame.getLifeline(i)) && (i < frame.lifeLinesCount() - 1)) {
1344 node = frame.getLifeline(i + 1);
1345 break;
1346 }
1347 }
1348 }
1349 if (node != null) {
1350 setFocus(node);
1351 redraw();
1352 }
1353 }
1354
1355 public void traverseLeft() {
1356 Object selectedNode = getFocusNode();
1357 GraphNode node = null;
1358 if (selectedNode instanceof BaseMessage) {
1359 if (((BaseMessage) selectedNode).getStartLifeline() != null)
1360 node = frame.getCallerMessage((BaseMessage) selectedNode);
1361 }
1362 if (selectedNode instanceof BasicExecutionOccurrence) {
1363 selectedNode = ((BasicExecutionOccurrence) selectedNode).getLifeline();
1364 }
1365 if (node == null) {
1366 if (selectedNode instanceof BaseMessage) {
1367 if (((BaseMessage) selectedNode).getEndLifeline() != null)
1368 selectedNode = ((BaseMessage) selectedNode).getEndLifeline();
1369 }
1370 for (int i = 0; i < frame.lifeLinesCount(); i++) {
1371 if ((selectedNode == frame.getLifeline(i)) && (i > 0)) {
1372 node = frame.getLifeline(i - 1);
1373 break;
1374 }
1375 }
1376 if ((frame.lifeLinesCount() > 0) && (node == null))
1377 node = frame.getLifeline(0);
1378 }
1379 if (node != null) {
1380 setFocus(node);
1381 redraw();
1382 }
1383 }
1384
1385 public void traverseUp() {
1386 Object selectedNode = getFocusNode();
1387 if (selectedNode == null)
1388 traverseLeft();
1389 GraphNode node = null;
1390 if (selectedNode instanceof BaseMessage)
1391 node = frame.getPrevLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), (BaseMessage) selectedNode);
1392 else if (selectedNode instanceof Lifeline) {
1393 node = frame.getPrevLifelineMessage((Lifeline) selectedNode, null);
1394 if (!(node instanceof Lifeline))
1395 node = null;
1396 } else if (selectedNode instanceof BasicExecutionOccurrence) {
1397 node = frame.getPrevExecOccurrence((BasicExecutionOccurrence) selectedNode);
1398 if (node == null)
1399 node = ((BasicExecutionOccurrence) selectedNode).getLifeline();
1400 }
1401 if (node == null) {
1402 if (selectedNode instanceof BaseMessage) {
1403 if (((BaseMessage) selectedNode).getStartLifeline() != null)
1404 node = ((BaseMessage) selectedNode).getStartLifeline();
1405 }
1406 }
1407
1408 if (node != null) {
1409 setFocus(node);
1410 redraw();
1411 }
1412 }
1413
1414 public void traverseDown() {
1415 Object selectedNode = getFocusNode();
1416 if (selectedNode == null)
1417 traverseLeft();
1418 GraphNode node;
1419 if (selectedNode instanceof BaseMessage)
1420 node = frame.getNextLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), (BaseMessage) selectedNode);
1421 else if (selectedNode instanceof Lifeline) {
1422 // node = frame.getNextLifelineMessage((Lifeline)selectedNode,null);
1423 node = frame.getFirstExecution((Lifeline) selectedNode);
1424 } else if (selectedNode instanceof BasicExecutionOccurrence) {
1425 node = frame.getNextExecOccurrence((BasicExecutionOccurrence) selectedNode);
1426 } else
1427 return;
1428
1429 if (node != null) {
1430 setFocus(node);
1431 redraw();
1432 }
1433 }
1434
1435 public void traverseHome() {
1436 Object selectedNode = getFocusNode();
1437 if (selectedNode == null)
1438 traverseLeft();
1439 GraphNode node = null;
1440 /*
1441 * if (selectedNode instanceof BaseMessage) { if (((BaseMessage)selectedNode).getStartLifeline()!=null) node =
1442 * ((BaseMessage)selectedNode).getStartLifeline(); }
1443 */
1444 if (selectedNode instanceof BaseMessage) {
1445 if (((BaseMessage) selectedNode).getStartLifeline() != null)
1446 node = frame.getNextLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), null);
1447 else
1448 node = frame.getNextLifelineMessage(((BaseMessage) selectedNode).getEndLifeline(), null);
1449 } else if (selectedNode instanceof Lifeline)
1450 node = frame.getNextLifelineMessage((Lifeline) selectedNode, null);
1451 else if (selectedNode instanceof BasicExecutionOccurrence) {
1452 node = frame.getFirstExecution(((BasicExecutionOccurrence) selectedNode).getLifeline());
1453 } else {
1454 if (frame.lifeLinesCount() > 0) {
1455 Lifeline lifeline = frame.getLifeline(0);
1456 node = frame.getNextLifelineMessage(lifeline, null);
1457 }
1458 }
1459
1460 if (node != null) {
1461 setFocus(node);
1462 redraw();
1463 }
1464 }
1465
1466 public void traverseEnd() {
1467 Object selectedNode = getFocusNode();
1468 if (selectedNode == null)
1469 traverseLeft();
1470 GraphNode node;
1471 if (selectedNode instanceof BaseMessage)
1472 node = frame.getPrevLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), null);
1473 else if (selectedNode instanceof Lifeline)
1474 node = frame.getPrevLifelineMessage((Lifeline) selectedNode, null);
1475 else if (selectedNode instanceof BasicExecutionOccurrence) {
1476 node = frame.getLastExecOccurrence(((BasicExecutionOccurrence) selectedNode).getLifeline());
1477 } else {
1478 if (frame.lifeLinesCount() > 0) {
1479 Lifeline lifeline = frame.getLifeline(0);
1480 node = frame.getPrevLifelineMessage(lifeline, null);
1481 } else
1482 return;
1483 }
1484
1485 if (node != null) {
1486 setFocus(node);
1487 redraw();
1488 }
1489 }
1490
1491 public void printUI(SDPrintDialogUI sdPrinter) {
1492 PrinterData data = sdPrinter.getPrinterData();
1493
1494 if ((data == null) || (frame == null)) {
1495 return;
1496 }
1497
1498 printer = new Printer(data);
1499
1500 String jobName = MessageFormat.format(SDMessages._116, new Object[] { String.valueOf(site.getContentDescription()), String.valueOf(frame.getName()) });
1501 printer.startJob(jobName);
1502
1503 GC gc = new GC(printer);
1504 Frame.setUserPref(SDViewPref.getInstance());
1505
1506 float lastZoom = zoomValue;
1507
1508 Rectangle area = getClientArea();
1509 GC gcim = null;
1510
1511 gcim = gc;
1512 NGC context = new NGC(this, gcim);
1513
1514 // Set the metrics to use for lifeline text and message text
1515 // using the Graphical Context
1516 Metrics.setLifelineFontHeight(context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
1517 Metrics.setLifelineFontWidth(context.getFontWidth(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
1518 Metrics.setLifelineWidth(SDViewPref.getInstance().getLifelineWidth());
1519 Metrics.setFrameFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_FRAME_NAME)));
1520 Metrics.setLifelineHeaderFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_LIFELINE_HEADER)));
1521
1522 int syncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS));
1523 int syncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS_RET));
1524 int asyncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS));
1525 int asyncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS_RET));
1526
1527 int messageFontHeight = 0;
1528 if (syncMessFontH > syncMessRetFontH)
1529 messageFontHeight = syncMessFontH;
1530 else
1531 messageFontHeight = syncMessRetFontH;
1532 if (messageFontHeight < asyncMessFontH)
1533 messageFontHeight = asyncMessFontH;
1534 if (messageFontHeight < asyncMessRetFontH)
1535 messageFontHeight = asyncMessRetFontH;
1536 Metrics.setMessageFontHeight(messageFontHeight);
1537 context.setFont(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE));
1538
1539 int width = Math.round((frame.getWidth() + 2 * Metrics.FRAME_H_MARGIN) * zoomValue);
1540 int height = Math.round((frame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * zoomValue);
1541 if (width < area.width)
1542 width = area.width;
1543 if (height < area.height)
1544 height = area.height;
1545 resizeContents(width, height);
1546
1547 context.setBackground(Frame.getUserPref().getBackGroundColor(ISDPreferences.PREF_FRAME));
1548 context.fillRectangle(0, 0, getContentsWidth(), Metrics.FRAME_V_MARGIN);
1549 context.fillRectangle(0, 0, frame.getX(), getContentsHeight());
1550 context.fillRectangle(frame.getX() + frame.getWidth() + 1, 0, getContentsWidth() - (frame.getX() + frame.getWidth() + 1), getContentsHeight());
1551 context.fillRectangle(0, frame.getY() + frame.getHeight() + 1, getContentsWidth(), getContentsHeight() - (frame.getY() + frame.getHeight() + 1));
1552 gcim.setLineWidth(1);
1553
1554 printer.startPage();
1555 zoomValue = lastZoom;
1556
1557 int restoreX = getContentsX();
1558 int restoreY = getContentsY();
1559
1560 float zh = getContentsHeight();
1561 float zw = getContentsWidth();
1562
1563 zh = sdPrinter.getStepY() * sdPrinter.getZoomFactor();
1564 zw = sdPrinter.getStepX() * sdPrinter.getZoomFactor();
1565
1566 float zoomValueH = printer.getClientArea().height / zh;
1567 float zoomValueW = printer.getClientArea().width / zw;
1568 if (zoomValueH > zoomValueW)
1569 printerZoom = zoomValueH;
1570 else
1571 printerZoom = zoomValueW;
1572
1573 if (sdPrinter.printSelection()) {
1574 int[] pagesList = sdPrinter.getPageList();
1575
1576 for (int pageIndex = 0; pageIndex < pagesList.length; pageIndex++) {
1577 printPage(pagesList[pageIndex], sdPrinter, context);
1578 }
1579 } else if (sdPrinter.printAll()) {
1580 for (int pageIndex = 1; pageIndex <= sdPrinter.maxNumOfPages(); pageIndex++) {
1581 printPage(pageIndex, sdPrinter, context);
1582 }
1583 } else if (sdPrinter.printCurrent()) {
1584 printPage(getContentsX(), getContentsY(), sdPrinter, context, 1);
1585 } else if (sdPrinter.printRange()) {
1586 for (int pageIndex = sdPrinter.getFrom(); pageIndex <= sdPrinter.maxNumOfPages() && pageIndex <= sdPrinter.getTo(); pageIndex++) {
1587 printPage(pageIndex, sdPrinter, context);
1588 }
1589 }
1590
1591 printer.endJob();
1592 printing = false;
1593
1594 gc.dispose();
1595 context.dispose();
1596
1597 zoomValue = lastZoom;
1598 printer.dispose();
1599 setContentsPos(restoreX, restoreY);
1600
1601 }
1602
1603 public void print() {
1604 SDPrintDialog sdPrinter = new SDPrintDialog(this.getShell(), this);
1605 try {
1606 if (sdPrinter.open() != 0)
1607 return;
1608 } catch (Exception e) {
1609
1610 }
1611
1612 printUI(sdPrinter.getDialogUI());
1613 }
1614
1615 public void printPage(int pageNum, SDPrintDialogUI pd, NGC context) {
1616 int j = pageNum / pd.getNbRow();
1617 int i = pageNum % pd.getNbRow();
1618 if (i != 0)
1619 j++;
1620 else
1621 i = pd.getNbRow();
1622
1623 i--;
1624 j--;
1625
1626 i = (int) (i * pd.getStepX());
1627 j = (int) (j * pd.getStepY());
1628
1629 printPage(i, j, pd, context, pageNum);
1630
1631 printer.endPage();
1632 }
1633
1634 public void printPage(int i, int j, SDPrintDialogUI pd, NGC context, int pageNum) {
1635 printing = false;
1636 int pageNumFontZoom = printer.getClientArea().height / getVisibleHeight();
1637 printerX = i;
1638 printerY = j;
1639 setContentsPos(i, j);
1640 update();
1641 printing = true;
1642 float lastZoom = zoomValue;
1643 zoomValue = printerZoom * lastZoom;
1644
1645 frame.draw(context);
1646
1647 zoomValue = pageNumFontZoom;
1648 context.setFont(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE));
1649 String currentPageNum = String.valueOf(pageNum);
1650 int ii = context.textExtent(currentPageNum);
1651 int jj = context.getCurrentFontHeight();
1652 // context.setBackground(ColorImpl.getSystemColor(SWT.COLOR_BLACK));
1653 // context.setForeground(ColorImpl.getSystemColor(SWT.COLOR_WHITE));
1654 zoomValue = printerZoom * lastZoom;
1655 context.drawText(currentPageNum, Math.round(printerX + getVisibleWidth() / printerZoom - ii / printerZoom), Math.round(printerY + getVisibleHeight() / printerZoom - jj / printerZoom), false);
1656 printing = false;
1657 zoomValue = lastZoom;
1658 }
1659
1660 @Override
1661 public int getVisibleWidth() {
1662 if (printing)
1663 return printer.getClientArea().width;
1664 return super.getVisibleWidth();
1665 }
1666
1667 @Override
1668 public int getVisibleHeight() {
1669 if (printing)
1670 return printer.getClientArea().height;
1671 return super.getVisibleHeight();
1672 }
1673
1674 @Override
1675 public int contentsToViewX(int _x) {
1676 if (printing) {
1677 int v = Math.round(printerX * printerZoom);
1678 return _x - v;
1679 }
1680 return _x - getContentsX();
1681 }
1682
1683 @Override
1684 public int contentsToViewY(int _y) {
1685 if (printing) {
1686 int v = Math.round(printerY * printerZoom);
1687 return _y - v;
1688 }
1689 return _y - getContentsY();
1690 }
1691
1692 @Override
1693 public int getContentsX() {
1694 if (printing)
1695 return Math.round(printerX * printerZoom);
1696 return super.getContentsX();
1697
1698 }
1699
1700 @Override
1701 public int getContentsY() {
1702 if (printing)
1703 return Math.round(printerY * printerZoom);
1704 return super.getContentsY();
1705 }
1706
1707 public Printer getPrinter() {
1708 return printer;
1709 }
1710
1711 public boolean isPrinting() {
1712 return printing;
1713 }
1714 }
This page took 0.071946 seconds and 5 git commands to generate.