Fix static analysis warnings for UML2SD
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / SDWidget.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4 *
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
9 *
10 * Contributors:
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org.eclipse.linuxtools.tmf.ui.views.uml2sd;
15
16 import java.text.MessageFormat;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Timer;
20 import java.util.TimerTask;
21
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.contexts.IContextIds;
25 import org.eclipse.jface.util.IPropertyChangeListener;
26 import org.eclipse.jface.util.PropertyChangeEvent;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.linuxtools.internal.tmf.ui.ITmfImageConstants;
30 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
31 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
32 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
33 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage;
34 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BasicExecutionOccurrence;
35 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Frame;
36 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode;
37 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange;
38 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline;
39 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Metrics;
40 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.SDPrintDialog;
41 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.SDPrintDialogUI;
42 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor;
43 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDCollapseProvider;
44 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.LoadersManager;
45 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences;
46 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref;
47 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
48 import org.eclipse.swt.SWT;
49 import org.eclipse.swt.accessibility.ACC;
50 import org.eclipse.swt.accessibility.Accessible;
51 import org.eclipse.swt.accessibility.AccessibleAdapter;
52 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
53 import org.eclipse.swt.accessibility.AccessibleControlEvent;
54 import org.eclipse.swt.accessibility.AccessibleEvent;
55 import org.eclipse.swt.events.DisposeEvent;
56 import org.eclipse.swt.events.DisposeListener;
57 import org.eclipse.swt.events.FocusEvent;
58 import org.eclipse.swt.events.FocusListener;
59 import org.eclipse.swt.events.KeyEvent;
60 import org.eclipse.swt.events.MouseEvent;
61 import org.eclipse.swt.events.SelectionEvent;
62 import org.eclipse.swt.events.SelectionListener;
63 import org.eclipse.swt.events.TraverseEvent;
64 import org.eclipse.swt.events.TraverseListener;
65 import org.eclipse.swt.graphics.GC;
66 import org.eclipse.swt.graphics.Image;
67 import org.eclipse.swt.graphics.ImageData;
68 import org.eclipse.swt.graphics.Rectangle;
69 import org.eclipse.swt.printing.Printer;
70 import org.eclipse.swt.printing.PrinterData;
71 import org.eclipse.swt.widgets.Canvas;
72 import org.eclipse.swt.widgets.Caret;
73 import org.eclipse.swt.widgets.Composite;
74 import org.eclipse.swt.widgets.Control;
75 import org.eclipse.swt.widgets.Display;
76 import org.eclipse.swt.widgets.Event;
77 import org.eclipse.swt.widgets.Listener;
78 import org.eclipse.swt.widgets.MenuItem;
79 import org.eclipse.ui.contexts.IContextService;
80 import org.eclipse.ui.part.ViewPart;
81
82 /**
83 * <p>
84 * This class implements sequence diagram widget used in the sequence diagram view.
85 * </p>
86 *
87 * @version 1.0
88 * @author sveyrier
89 *
90 */
91 public class SDWidget extends ScrollView implements SelectionListener, IPropertyChangeListener, DisposeListener, ITimeCompressionListener {
92
93 // ------------------------------------------------------------------------
94 // Attributes
95 // ------------------------------------------------------------------------
96
97 /**
98 * The frame to display in the sequence diagram widget.
99 */
100 protected Frame fFrame;
101 /**
102 * The overview image to display.
103 */
104 protected Image fOverView = null;
105 /**
106 * The zoom in menu item.
107 */
108 protected MenuItem fZoomIn = null;
109 /**
110 * The zoom out menu item.
111 */
112 protected MenuItem fZoomOut = null;
113 /**
114 * The sequence diagram selection provider.
115 */
116 protected SDWidgetSelectionProvider fSelProvider = null;
117 /**
118 * The current zoom value.
119 */
120 public float fZoomValue = 1;
121 /**
122 * The current zoomInMode (true for zoom in).
123 */
124 protected boolean fZoomInMode = false;
125 /**
126 * The current zoomOutMode (true for zoom out).
127 */
128 protected boolean fZoomOutMode = false;
129 /**
130 * The current list of selected graph nodes.
131 */
132 protected List<GraphNode> fSelectedNodeList = null;
133 /**
134 * Flag whether ctrl button is selected or not.
135 */
136 protected boolean fCtrlSelection = false;
137 /**
138 * A reference to the view site.
139 */
140 protected ViewPart fSite = null;
141 /**
142 * The current graph node (the last selected one).
143 */
144 public GraphNode fCurrentGraphNode = null;
145 /**
146 * The first graph node in list (multiple selection).
147 */
148 public GraphNode fListStart = null;
149 /**
150 * The previous graph node (multiple selection).
151 */
152 public List<GraphNode> fPrevList = null;
153 /**
154 * The time compression bar.
155 */
156 protected TimeCompressionBar fTimeBar = null;
157 /**
158 * The current diagram tool tip.
159 */
160 protected DiagramToolTip fToolTip = null;
161 /**
162 * The accessible object reference of view control.
163 */
164 protected Accessible fAccessible = null;
165 /**
166 * The current node for the tooltip to display.
167 */
168 protected GraphNode fToolTipNode;
169 /**
170 * The life line to drag and drop.
171 */
172 protected Lifeline fDragAndDrop = null;
173 /**
174 * The number of focused widgets.
175 */
176 protected int fFocusedWidget = -1;
177 /**
178 * The printer zoom.
179 */
180 protected float fPrinterZoom = 0;
181 /**
182 * Y coordinate for printer.
183 */
184 protected int fPrinterY = 0;
185 /**
186 * X coordinate for printer.
187 */
188 protected int fPrinterX = 0;
189 /**
190 * Flag whether drag and drop is enabled or not.
191 */
192 protected boolean fIsDragAndDrop = false;
193 /**
194 * The x coordinate for drag.
195 */
196 protected int fDragX = 0;
197 /**
198 * The y coordinate for drag.
199 */
200 protected int fDragY = 0;
201 /**
202 * The reorder mode.
203 */
204 protected boolean fReorderMode = false;
205 /**
206 * The collapse caret image.
207 */
208 protected Image fCollapaseCaretImg = null;
209 /**
210 * The arrow up caret image.
211 */
212 protected Image fArrowUpCaretImg = null;
213 /**
214 * The current caret image.
215 */
216 protected Image fCurrentCaretImage = null;
217 /**
218 * A sequence diagramm collapse provider (for collapsing graph nodes)
219 */
220 protected ISDCollapseProvider fCollapseProvider = null;
221 /**
222 * The insertion caret.
223 */
224 protected Caret fInsertionCartet = null;
225 /**
226 * The reorder list when in reorder mode.
227 */
228 protected ArrayList<Lifeline[]> fReorderList = null;
229 /**
230 * Flag to specify whether in printing mode or not.
231 */
232 protected boolean fIsPrinting = false;
233 /**
234 * A printer reference.
235 */
236 protected Printer fPrinter = null;
237 /**
238 * Flag whether shift was selected or not.
239 */
240 protected boolean fShiftSelection = false;
241 /**
242 * The scroll tooltip.
243 */
244 protected DiagramToolTip fScrollToolTip = null;
245 /**
246 * Timer for auto_scroll feature
247 */
248 protected AutoScroll fLocalAutoScroll = null;
249 /**
250 * TimerTask for auto_scroll feature !=null when auto scroll is running
251 */
252 protected Timer fLocalAutoScrollTimer = null;
253
254 // ------------------------------------------------------------------------
255 // Constructor
256 // ------------------------------------------------------------------------
257 /**
258 * Constructor for SDWidget.
259 * @param c The parent composite
260 * @param s The style
261 */
262 public SDWidget(Composite c, int s) {
263 super(c, s | SWT.NO_BACKGROUND, true);
264 setOverviewEnabled(true);
265 fSelectedNodeList = new ArrayList<GraphNode>();
266 fSelProvider = new SDWidgetSelectionProvider();
267 SDViewPref.getInstance().addPropertyChangeListener(this);
268 fToolTip = new DiagramToolTip(getViewControl());
269 super.addDisposeListener(this);
270
271 fScrollToolTip = new DiagramToolTip(c);
272 getVerticalBar().addListener(SWT.MouseUp, new Listener() {
273
274 @Override
275 public void handleEvent(Event event) {
276 fScrollToolTip.hideToolTip();
277 }
278
279 });
280 fAccessible = getViewControl().getAccessible();
281
282 fAccessible.addAccessibleListener(new AccessibleAdapter() {
283 /*
284 * (non-Javadoc)
285 * @see org.eclipse.swt.accessibility.AccessibleAdapter#getName(org.eclipse.swt.accessibility.AccessibleEvent)
286 */
287 @Override
288 public void getName(AccessibleEvent e) {
289 // Case toolTip
290 if (e.childID == 0) {
291 if (fToolTipNode != null) {
292 if (fToolTipNode instanceof Lifeline) {
293 Lifeline lifeline = (Lifeline) fToolTipNode;
294 e.result = lifeline.getToolTipText();
295 } else {
296 e.result = fToolTipNode.getName() + getPostfixForTooltip(true);
297 }
298 }
299 } else {
300 if (getFocusNode() != null) {
301 if (getFocusNode() instanceof Lifeline) {
302 e.result = MessageFormat.format(SDMessages._1, new Object[] { String.valueOf(getFocusNode().getName()) });
303 }
304 if (getFocusNode() instanceof BaseMessage) {
305 BaseMessage mes = (BaseMessage) getFocusNode();
306 if ((mes.getStartLifeline() != null) && (mes.getEndLifeline() != null)) {
307 e.result = MessageFormat.format(
308 SDMessages._2,
309 new Object[] { String.valueOf(mes.getName()), String.valueOf(mes.getStartLifeline().getName()), Integer.valueOf(mes.getStartOccurrence()), String.valueOf(mes.getEndLifeline().getName()),
310 Integer.valueOf(mes.getEndOccurrence()) });
311 } else if ((mes.getStartLifeline() == null) && (mes.getEndLifeline() != null)) {
312 e.result = MessageFormat.format(SDMessages._4, new Object[] { String.valueOf(mes.getName()), String.valueOf(mes.getEndLifeline().getName()), Integer.valueOf(mes.getEndOccurrence()) });
313 } else if ((mes.getStartLifeline() != null) && (mes.getEndLifeline() == null)) {
314 e.result = MessageFormat.format(SDMessages._3, new Object[] { String.valueOf(mes.getName()), String.valueOf(mes.getStartLifeline().getName()), Integer.valueOf(mes.getStartOccurrence()) });
315 }
316 } else if (getFocusNode() instanceof BasicExecutionOccurrence) {
317 BasicExecutionOccurrence exec = (BasicExecutionOccurrence) getFocusNode();
318 e.result = MessageFormat.format(SDMessages._5,
319 new Object[] { String.valueOf(exec.getName()), String.valueOf(exec.getLifeline().getName()), Integer.valueOf(exec.getStartOccurrence()), Integer.valueOf(exec.getEndOccurrence()) });
320 }
321
322 }
323 }
324 }
325 });
326
327 fAccessible.addAccessibleControlListener(new AccessibleControlAdapter() {
328 /*
329 * (non-Javadoc)
330 * @see org.eclipse.swt.accessibility.AccessibleControlAdapter#getFocus(org.eclipse.swt.accessibility.AccessibleControlEvent)
331 */
332 @Override
333 public void getFocus(AccessibleControlEvent e) {
334 if (fFocusedWidget == -1) {
335 e.childID = ACC.CHILDID_SELF;
336 } else {
337 e.childID = fFocusedWidget;
338 }
339 }
340
341 /*
342 * (non-Javadoc)
343 * @see org.eclipse.swt.accessibility.AccessibleControlAdapter#getRole(org.eclipse.swt.accessibility.AccessibleControlEvent)
344 */
345 @Override
346 public void getRole(AccessibleControlEvent e) {
347 switch (e.childID) {
348 case ACC.CHILDID_SELF:
349 e.detail = ACC.ROLE_CLIENT_AREA;
350 break;
351 case 0:
352 e.detail = ACC.ROLE_TOOLTIP;
353 break;
354 case 1:
355 e.detail = ACC.ROLE_LABEL;
356 break;
357 default:
358 break;
359 }
360 }
361
362 /*
363 * (non-Javadoc)
364 * @see org.eclipse.swt.accessibility.AccessibleControlAdapter#getState(org.eclipse.swt.accessibility.AccessibleControlEvent)
365 */
366 @Override
367 public void getState(AccessibleControlEvent e) {
368 e.detail = ACC.STATE_FOCUSABLE;
369 if (e.childID == ACC.CHILDID_SELF) {
370 e.detail |= ACC.STATE_FOCUSED;
371 } else {
372 e.detail |= ACC.STATE_SELECTABLE;
373 if (e.childID == fFocusedWidget)
374 e.detail |= ACC.STATE_FOCUSED | ACC.STATE_SELECTED | ACC.STATE_CHECKED;
375 }
376 }
377 });
378
379 fInsertionCartet = new Caret((Canvas) getViewControl(), SWT.NONE);
380 fInsertionCartet.setVisible(false);
381
382 fCollapaseCaretImg = TmfUiPlugin.getDefault().getImageFromPath(ITmfImageConstants.IMG_UI_ARROW_COLLAPSE_OBJ);
383 fArrowUpCaretImg = TmfUiPlugin.getDefault().getImageFromPath(ITmfImageConstants.IMG_UI_ARROW_UP_OBJ);
384
385 fReorderList = new ArrayList<Lifeline[]>();
386 getViewControl().addTraverseListener(new TraverseListener() {
387
388 /*
389 * (non-Javadoc)
390 * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
391 */
392 @Override
393 public void keyTraversed(TraverseEvent e) {
394 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
395 e.doit = true;
396 }
397 }
398
399 });
400
401 addTraverseListener(new TraverseListener() {
402
403 /*
404 * (non-Javadoc)
405 * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
406 */
407 @Override
408 public void keyTraversed(TraverseEvent e) {
409 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
410 e.doit = true;
411 }
412 }
413
414 });
415
416 getViewControl().addFocusListener(new FocusListener() {
417
418 /*
419 * (non-Javadoc)
420 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
421 */
422 @Override
423 public void focusGained(FocusEvent e) {
424 // TODO Auto-generated method stub
425 SDViewPref.getInstance().setNoFocusSelection(false);
426 fCtrlSelection = false;
427 fShiftSelection = false;
428 redraw();
429 }
430
431 /*
432 * (non-Javadoc)
433 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
434 */
435 @Override
436 public void focusLost(FocusEvent e) {
437 SDViewPref.getInstance().setNoFocusSelection(true);
438 redraw();
439 }
440 });
441 }
442
443 // ------------------------------------------------------------------------
444 // Operations
445 // ------------------------------------------------------------------------
446 /**
447 * Sets the time compression bar.
448 *
449 * @param bar The time compression bar to set
450 */
451 public void setTimeBar(TimeCompressionBar bar) {
452 if (bar != null) {
453 fTimeBar = bar;
454 fTimeBar.addTimeCompressionListener(this);
455 }
456 }
457
458 /**
459 * Resize the contents to insure the frame fit into the view
460 *
461 * @param frame the frame which will be drawn in the view
462 */
463 public void resizeContents(Frame frame) {
464 int width = Math.round((frame.getWidth() + 2 * Metrics.FRAME_H_MARGIN) * fZoomValue);
465 int height = Math.round((frame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * fZoomValue);
466 resizeContents(width, height);
467 }
468
469 /**
470 * The frame to render (the sequence diagram)
471 *
472 * @param theFrame the frame to display
473 * @param resetPosition boolean
474 */
475 public void setFrame(Frame theFrame, boolean resetPosition) {
476 fReorderList.clear();
477 fSelectedNodeList.clear();
478 fSelProvider.setSelection(new StructuredSelection());
479 fFrame = theFrame;
480 if (resetPosition) {
481 setContentsPos(0, 0);
482 resizeContents(fFrame);
483 redraw();
484 }
485 // prepare the old overview to be reused
486 if (fOverView != null) {
487 fOverView.dispose();
488 }
489 fOverView = null;
490 resizeContents(fFrame);
491 }
492
493 /**
494 * Returns the current Frame (the sequence diagram container)
495 *
496 * @return the frame
497 */
498 public Frame getFrame() {
499 return fFrame;
500 }
501
502 /**
503 * Returns the selection provider for the current sequence diagram
504 *
505 * @return the selection provider
506 */
507 public ISelectionProvider getSelectionProvider() {
508 return fSelProvider;
509 }
510
511 /**
512 * Returns a list of selected graph nodes.
513 *
514 * @return a list of selected graph nodes.
515 */
516 public List<GraphNode> getSelection() {
517 return fSelectedNodeList;
518 }
519
520 /**
521 * Adds a graph node to the selected nodes list.
522 *
523 * @param node A graph node
524 */
525 public void addSelection(GraphNode node) {
526 if (node == null) {
527 return;
528 }
529 fSelectedNodeList.add(node);
530 node.setSelected(true);
531 fCurrentGraphNode = node;
532 StructuredSelection selection = new StructuredSelection(fSelectedNodeList);
533 fSelProvider.setSelection(selection);
534 }
535
536 /**
537 * Adds a list of node to the selected nodes list.
538 *
539 * @param list of graph nodes
540 */
541 public void addSelection(List<GraphNode> list) {
542 // selectedNodeList.addAll(list);
543 for (int i = 0; i < list.size(); i++) {
544 if (!fSelectedNodeList.contains(list.get(i))) {
545 fSelectedNodeList.add(list.get(i));
546 ((GraphNode) list.get(i)).setSelected(true);
547 }
548 }
549 StructuredSelection selection = new StructuredSelection(fSelectedNodeList);
550 fSelProvider.setSelection(selection);
551 }
552
553 /**
554 * Removes a node from the selected nodes list.
555 *
556 * @param node to remove
557 */
558 public void removeSelection(GraphNode node) {
559 fSelectedNodeList.remove(node);
560 node.setSelected(false);
561 node.setFocused(false);
562 StructuredSelection selection = new StructuredSelection(fSelectedNodeList);
563 fSelProvider.setSelection(selection);
564 }
565
566 /**
567 * Removes a list of graph nodes from the selected nodes list.
568 *
569 * @param list of nodes to remove.
570 */
571 public void removeSelection(List<GraphNode> list) {
572 fSelectedNodeList.removeAll(list);
573 for (int i = 0; i < list.size(); i++) {
574 ((GraphNode) list.get(i)).setSelected(false);
575 ((GraphNode) list.get(i)).setFocused(false);
576 }
577 StructuredSelection selection = new StructuredSelection(fSelectedNodeList);
578 fSelProvider.setSelection(selection);
579 }
580
581 /**
582 * Clear the list of GraphNodes which must be drawn selected.
583 */
584 public void clearSelection() {
585 for (int i = 0; i < fSelectedNodeList.size(); i++) {
586 ((GraphNode) fSelectedNodeList.get(i)).setSelected(false);
587 ((GraphNode) fSelectedNodeList.get(i)).setFocused(false);
588 }
589 fCurrentGraphNode = null;
590 fSelectedNodeList.clear();
591 fSelProvider.setSelection(new StructuredSelection());
592 }
593
594 /**
595 * Sets view part.
596 *
597 * @param viewSite The view part to set
598 */
599 public void setSite(ViewPart viewSite) {
600 fSite = viewSite;
601 fSite.getSite().setSelectionProvider(fSelProvider);
602 IContextService service = (IContextService) fSite.getSite().getWorkbenchWindow().getService(IContextService.class);
603 service.activateContext("org.eclipse.linuxtools.tmf.ui.view.uml2sd.context"); //$NON-NLS-1$
604 service.activateContext(IContextIds.CONTEXT_ID_WINDOW);
605 }
606
607 /**
608 * Returns the GraphNode overView the mouse if any
609 *
610 * @return the current graph node
611 * */
612 public GraphNode getMouseOverNode() {
613 return fCurrentGraphNode;
614 }
615
616 /**
617 * Sets the zoom in mode.
618 *
619 * @param The mode value to set.
620 */
621 public void setZoomInMode(boolean value) {
622 if (value) {
623 setZoomOutMode(false);
624 }
625 fZoomInMode = value;
626 }
627
628 /**
629 * Sets the zoom out mode.
630 *
631 * @param The mode value to set.
632 */
633 public void setZoomOutMode(boolean value) {
634 if (value) {
635 setZoomInMode(false);
636 }
637 fZoomOutMode = value;
638 }
639
640 /**
641 * Moves the Sequence diagram to ensure the given node is visible and draw it selected
642 *
643 * @param node the GraphNode to move to
644 */
645 public void moveTo(GraphNode node) {
646 if (node == null) {
647 return;
648 }
649 clearSelection();
650 addSelection(node);
651 ensureVisible(node);
652 }
653
654 /**
655 * Moves the Sequence diagram to ensure the given node is visible
656 *
657 * @param node the GraphNode to move to
658 */
659 public void ensureVisible(GraphNode node) {
660 if (node == null) {
661 return;
662 }
663 int x = Math.round(node.getX() * fZoomValue);
664 int y = Math.round(node.getY() * fZoomValue);
665 int width = Math.round(node.getWidth() * fZoomValue);
666 int height = Math.round(node.getHeight() * fZoomValue);
667 if ((node instanceof BaseMessage) && (height == 0)) {
668 int header = Metrics.LIFELINE_HEARDER_TEXT_V_MARGIN * 2 + Metrics.getLifelineHeaderFontHeigth();
669 height = -Math.round((Metrics.getMessagesSpacing() + header) * fZoomValue);
670 y = y + Math.round(Metrics.SYNC_INTERNAL_MESSAGE_HEIGHT * fZoomValue);
671 }
672 if (node instanceof BasicExecutionOccurrence) {
673 width = 1;
674 height = 1;
675 }
676 if (node instanceof Lifeline) {
677 y = getContentsY();
678 height = getVisibleHeight();
679 }
680 ensureVisible(x, y, width, height, SWT.CENTER, true);
681 redraw();
682 }
683
684 /**
685 * Returns the current zoom factor.
686 * @return the current zoom factor.
687 */
688 public float getZoomFactor() {
689 return fZoomValue;
690 }
691
692 /**
693 * Returns teh printer reference.
694 *
695 * @return the printer reference
696 */
697 public Printer getPrinter() {
698 return fPrinter;
699 }
700
701 /**
702 * Returns whether the widget is used for printing or not.
703 *
704 * @return whether the widget is used for printing or not
705 */
706 public boolean isPrinting() {
707 return fIsPrinting;
708 }
709
710 /**
711 * Gets the overview image.
712 *
713 * @param rect Rectangle to include overview.
714 * @return the overview image
715 */
716 public Image getOverview(Rectangle rect) {
717 float oldzoom = fZoomValue;
718 if ((fOverView != null) && ((rect.width != fOverView.getBounds().width) || (rect.height != fOverView.getBounds().height))) {
719 fOverView.dispose();
720 fOverView = null;
721 }
722 if (fOverView == null) {
723 int backX = getContentsX();
724 int backY = getContentsY();
725 setContentsPos(0, 0);
726 fOverView = new Image(getDisplay(), rect.width, rect.height);
727 GC gcim = new GC(fOverView);
728 NGC context = new NGC(this, gcim);
729 context.setBackground(SDViewPref.getInstance().getBackGroundColor(SDViewPref.PREF_FRAME));
730 fFrame.draw(context);
731 setContentsPos(backX, backY);
732 gcim.dispose();
733 context.dispose();
734 }
735 fZoomValue = oldzoom;
736 return fOverView;
737 }
738
739 /**
740 * Resets the zoom factor.
741 */
742 public void resetZoomFactor() {
743 int currentX = Math.round(getContentsX() / fZoomValue);
744 int currentY = Math.round(getContentsY() / fZoomValue);
745 fZoomValue = 1;
746 if (fTimeBar != null && !fTimeBar.isDisposed()) {
747 fTimeBar.setZoom(fZoomValue);
748 }
749 redraw();
750 update();
751 setContentsPos(currentX, currentY);
752 }
753
754 /**
755 * Enable or disable the lifeline reodering using Drag and Drop
756 *
757 * @param mode - true to enable false otherwise
758 */
759 public void setReorderMode(boolean mode) {
760 fReorderMode = mode;
761 }
762
763 /**
764 * Return the lifelines reorder sequence (using Drag and Drop) if the the reorder mode is turn on. Each ArryList
765 * element is of type Lifeline[2] with Lifeline[0] inserted before Lifeline[1] in the diagram
766 *
767 * @return - the re-odered sequence
768 */
769 public ArrayList<Lifeline[]> getLifelineReoderList() {
770 return fReorderList;
771 }
772
773 /**
774 * Sets the focus on given graph node (current node).
775 *
776 * @param the graph node to focus on.
777 */
778 public void setFocus(GraphNode node) {
779 if (node == null) {
780 return;
781 }
782 if (fCurrentGraphNode != null) {
783 fCurrentGraphNode.setFocused(false);
784 }
785 fCurrentGraphNode = node;
786 node.setFocused(true);
787 ensureVisible(node);
788 setFocus(0);
789 }
790
791 /**
792 * Returns the graph node focused on.
793 *
794 * @return the current graph node
795 */
796 public GraphNode getFocusNode() {
797 return fCurrentGraphNode;
798 }
799
800 /**
801 * Method to traverse right.
802 */
803 public void traverseRight() {
804 Object selectedNode = getFocusNode();
805 if (selectedNode == null) {
806 traverseLeft();
807 }
808 GraphNode node = null;
809 if ((selectedNode instanceof BaseMessage) && (((BaseMessage) selectedNode).getEndLifeline() != null)) {
810 node = fFrame.getCalledMessage((BaseMessage) selectedNode);
811 }
812 if (selectedNode instanceof BasicExecutionOccurrence) {
813 selectedNode = ((BasicExecutionOccurrence) selectedNode).getLifeline();
814 }
815 if ((node == null) && (selectedNode instanceof Lifeline)) {
816 for (int i = 0; i < fFrame.lifeLinesCount(); i++) {
817 if ((selectedNode == fFrame.getLifeline(i)) && (i < fFrame.lifeLinesCount() - 1)) {
818 node = fFrame.getLifeline(i + 1);
819 break;
820 }
821 }
822 }
823 if (node != null) {
824 setFocus(node);
825 redraw();
826 }
827 }
828
829 /**
830 * Method to traverse left.
831 */
832 public void traverseLeft() {
833 Object selectedNode = getFocusNode();
834 GraphNode node = null;
835 if ((selectedNode instanceof BaseMessage) && (((BaseMessage) selectedNode).getStartLifeline() != null)) {
836 node = fFrame.getCallerMessage((BaseMessage) selectedNode);
837 }
838 if (selectedNode instanceof BasicExecutionOccurrence) {
839 selectedNode = ((BasicExecutionOccurrence) selectedNode).getLifeline();
840 }
841 if (node == null) {
842 if ((selectedNode instanceof BaseMessage) && (((BaseMessage) selectedNode).getEndLifeline() != null)) {
843 selectedNode = ((BaseMessage) selectedNode).getEndLifeline();
844 }
845 for (int i = 0; i < fFrame.lifeLinesCount(); i++) {
846 if ((selectedNode == fFrame.getLifeline(i)) && (i > 0)) {
847 node = fFrame.getLifeline(i - 1);
848 break;
849 }
850 }
851 if ((fFrame.lifeLinesCount() > 0) && (node == null)) {
852 node = fFrame.getLifeline(0);
853 }
854 }
855 if (node != null) {
856 setFocus(node);
857 redraw();
858 }
859 }
860
861 /**
862 * Method to traverse up.
863 */
864 public void traverseUp() {
865 Object selectedNode = getFocusNode();
866 if (selectedNode == null) {
867 traverseLeft();
868 }
869 GraphNode node = null;
870 if (selectedNode instanceof BaseMessage) {
871 node = fFrame.getPrevLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), (BaseMessage) selectedNode);
872 } else if (selectedNode instanceof Lifeline) {
873 node = fFrame.getPrevLifelineMessage((Lifeline) selectedNode, null);
874 if (!(node instanceof Lifeline)) {
875 node = null;
876 }
877 } else if (selectedNode instanceof BasicExecutionOccurrence) {
878 node = fFrame.getPrevExecOccurrence((BasicExecutionOccurrence) selectedNode);
879 if (node == null) {
880 node = ((BasicExecutionOccurrence) selectedNode).getLifeline();
881 }
882 }
883 if ((node == null) && (selectedNode instanceof BaseMessage) && (((BaseMessage) selectedNode).getStartLifeline() != null)) {
884 node = ((BaseMessage) selectedNode).getStartLifeline();
885 }
886
887 if (node != null) {
888 setFocus(node);
889 redraw();
890 }
891 }
892
893 /**
894 * Method to traverse down.
895 */
896 public void traverseDown() {
897 Object selectedNode = getFocusNode();
898 if (selectedNode == null) {
899 traverseLeft();
900 }
901 GraphNode node;
902 if (selectedNode instanceof BaseMessage) {
903 node = fFrame.getNextLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), (BaseMessage) selectedNode);
904 } else if (selectedNode instanceof Lifeline) {
905 // node = frame.getNextLifelineMessage((Lifeline)selectedNode,null);
906 node = fFrame.getFirstExecution((Lifeline) selectedNode);
907 } else if (selectedNode instanceof BasicExecutionOccurrence) {
908 node = fFrame.getNextExecOccurrence((BasicExecutionOccurrence) selectedNode);
909 } else {
910 return;
911 }
912
913 if (node != null) {
914 setFocus(node);
915 redraw();
916 }
917 }
918
919 /**
920 * Method to traverse home.
921 */
922 public void traverseHome() {
923 Object selectedNode = getFocusNode();
924 if (selectedNode == null) {
925 traverseLeft();
926 }
927 GraphNode node = null;
928
929 if (selectedNode instanceof BaseMessage) {
930 if (((BaseMessage) selectedNode).getStartLifeline() != null) {
931 node = fFrame.getNextLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), null);
932 } else {
933 node = fFrame.getNextLifelineMessage(((BaseMessage) selectedNode).getEndLifeline(), null);
934 }
935 } else if (selectedNode instanceof Lifeline) {
936 node = fFrame.getNextLifelineMessage((Lifeline) selectedNode, null);
937 } else if (selectedNode instanceof BasicExecutionOccurrence) {
938 node = fFrame.getFirstExecution(((BasicExecutionOccurrence) selectedNode).getLifeline());
939 } else {
940 if (fFrame.lifeLinesCount() > 0) {
941 Lifeline lifeline = fFrame.getLifeline(0);
942 node = fFrame.getNextLifelineMessage(lifeline, null);
943 }
944 }
945
946 if (node != null) {
947 setFocus(node);
948 redraw();
949 }
950 }
951
952 /**
953 * Method to traverse to the end.
954 */
955 public void traverseEnd() {
956 Object selectedNode = getFocusNode();
957 if (selectedNode == null) {
958 traverseLeft();
959 }
960 GraphNode node;
961 if (selectedNode instanceof BaseMessage) {
962 node = fFrame.getPrevLifelineMessage(((BaseMessage) selectedNode).getStartLifeline(), null);
963 } else if (selectedNode instanceof Lifeline) {
964 node = fFrame.getPrevLifelineMessage((Lifeline) selectedNode, null);
965 } else if (selectedNode instanceof BasicExecutionOccurrence) {
966 node = fFrame.getLastExecOccurrence(((BasicExecutionOccurrence) selectedNode).getLifeline());
967 } else {
968 if (fFrame.lifeLinesCount() > 0) {
969 Lifeline lifeline = fFrame.getLifeline(0);
970 node = fFrame.getPrevLifelineMessage(lifeline, null);
971 } else {
972 return;
973 }
974 }
975
976 if (node != null) {
977 setFocus(node);
978 redraw();
979 }
980 }
981
982 /**
983 * Method to print UI.
984 *
985 * @param sdPrintDialog the sequence diagram printer dialog.
986 */
987 public void printUI(SDPrintDialogUI sdPrintDialog) {
988 PrinterData data = sdPrintDialog.getPrinterData();
989
990 if ((data == null) || (fFrame == null)) {
991 return;
992 }
993
994 fPrinter = new Printer(data);
995
996 String jobName = MessageFormat.format(SDMessages._116, new Object[] { String.valueOf(fSite.getContentDescription()), String.valueOf(fFrame.getName()) });
997 fPrinter.startJob(jobName);
998
999 GC gc = new GC(fPrinter);
1000 Frame.setUserPref(SDViewPref.getInstance());
1001
1002 float lastZoom = fZoomValue;
1003
1004 Rectangle area = getClientArea();
1005 GC gcim = null;
1006
1007 gcim = gc;
1008 NGC context = new NGC(this, gcim);
1009
1010 // Set the metrics to use for lifeline text and message text
1011 // using the Graphical Context
1012 Metrics.setLifelineFontHeight(context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
1013 Metrics.setLifelineFontWidth(context.getFontWidth(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
1014 Metrics.setLifelineWidth(SDViewPref.getInstance().getLifelineWidth());
1015 Metrics.setFrameFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_FRAME_NAME)));
1016 Metrics.setLifelineHeaderFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_LIFELINE_HEADER)));
1017
1018 int syncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS));
1019 int syncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS_RET));
1020 int asyncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS));
1021 int asyncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS_RET));
1022
1023 int messageFontHeight = 0;
1024 if (syncMessFontH > syncMessRetFontH) {
1025 messageFontHeight = syncMessFontH;
1026 } else {
1027 messageFontHeight = syncMessRetFontH;
1028 }
1029 if (messageFontHeight < asyncMessFontH) {
1030 messageFontHeight = asyncMessFontH;
1031 }
1032 if (messageFontHeight < asyncMessRetFontH) {
1033 messageFontHeight = asyncMessRetFontH;
1034 }
1035 Metrics.setMessageFontHeight(messageFontHeight);
1036 context.setFont(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE));
1037
1038 int width = Math.round((fFrame.getWidth() + 2 * Metrics.FRAME_H_MARGIN) * fZoomValue);
1039 int height = Math.round((fFrame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * fZoomValue);
1040 if (width < area.width) {
1041 width = area.width;
1042 }
1043 if (height < area.height) {
1044 height = area.height;
1045 }
1046 resizeContents(width, height);
1047
1048 context.setBackground(Frame.getUserPref().getBackGroundColor(ISDPreferences.PREF_FRAME));
1049 context.fillRectangle(0, 0, getContentsWidth(), Metrics.FRAME_V_MARGIN);
1050 context.fillRectangle(0, 0, fFrame.getX(), getContentsHeight());
1051 context.fillRectangle(fFrame.getX() + fFrame.getWidth() + 1, 0, getContentsWidth() - (fFrame.getX() + fFrame.getWidth() + 1), getContentsHeight());
1052 context.fillRectangle(0, fFrame.getY() + fFrame.getHeight() + 1, getContentsWidth(), getContentsHeight() - (fFrame.getY() + fFrame.getHeight() + 1));
1053 gcim.setLineWidth(1);
1054
1055 fPrinter.startPage();
1056 fZoomValue = lastZoom;
1057
1058 int restoreX = getContentsX();
1059 int restoreY = getContentsY();
1060
1061 float zh = sdPrintDialog.getStepY() * sdPrintDialog.getZoomFactor();
1062 float zw = sdPrintDialog.getStepX() * sdPrintDialog.getZoomFactor();
1063
1064 float zoomValueH = fPrinter.getClientArea().height / zh;
1065 float zoomValueW = fPrinter.getClientArea().width / zw;
1066 if (zoomValueH > zoomValueW) {
1067 fPrinterZoom = zoomValueH;
1068 } else {
1069 fPrinterZoom = zoomValueW;
1070 }
1071
1072 if (sdPrintDialog.printSelection()) {
1073 int[] pagesList = sdPrintDialog.getPageList();
1074
1075 for (int pageIndex = 0; pageIndex < pagesList.length; pageIndex++) {
1076 printPage(pagesList[pageIndex], sdPrintDialog, context);
1077 }
1078 } else if (sdPrintDialog.printAll()) {
1079 for (int pageIndex = 1; pageIndex <= sdPrintDialog.maxNumOfPages(); pageIndex++) {
1080 printPage(pageIndex, sdPrintDialog, context);
1081 }
1082 } else if (sdPrintDialog.printCurrent()) {
1083 printPage(getContentsX(), getContentsY(), sdPrintDialog, context, 1);
1084 } else if (sdPrintDialog.printRange()) {
1085 for (int pageIndex = sdPrintDialog.getFrom(); pageIndex <= sdPrintDialog.maxNumOfPages() && pageIndex <= sdPrintDialog.getTo(); pageIndex++) {
1086 printPage(pageIndex, sdPrintDialog, context);
1087 }
1088 }
1089
1090 fPrinter.endJob();
1091 fIsPrinting = false;
1092
1093 gc.dispose();
1094 context.dispose();
1095
1096 fZoomValue = lastZoom;
1097 fPrinter.dispose();
1098 setContentsPos(restoreX, restoreY);
1099 }
1100
1101 /**
1102 * Method to print.
1103 */
1104 public void print() {
1105 SDPrintDialog sdPrinter = new SDPrintDialog(this.getShell(), this);
1106 try {
1107 if (sdPrinter.open() != 0) {
1108 return;
1109 }
1110 } catch (Exception e) {
1111 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,TmfUiPlugin.PLUGIN_ID, "Error creating image", e)); //$NON-NLS-1$
1112 return;
1113 }
1114 printUI(sdPrinter.getDialogUI());
1115 }
1116
1117 /**
1118 * Method to print a page.
1119 *
1120 * @param pageNum The page number
1121 * @param pd The sequence diagram print dialog
1122 * @param context The graphical context
1123 */
1124 public void printPage(int pageNum, SDPrintDialogUI pd, NGC context) {
1125 int j = pageNum / pd.getNbRow();
1126 int i = pageNum % pd.getNbRow();
1127 if (i != 0) {
1128 j++;
1129 } else {
1130 i = pd.getNbRow();
1131 }
1132
1133 i--;
1134 j--;
1135
1136 i = (int) (i * pd.getStepX());
1137 j = (int) (j * pd.getStepY());
1138
1139 printPage(i, j, pd, context, pageNum);
1140
1141 fPrinter.endPage();
1142 }
1143
1144 /**
1145 * Method to print page ranges.
1146 *
1147 * @param i The start page
1148 * @param j The end page
1149 * @param pd The sequence diagram print dialog
1150 * @param context The graphical context
1151 * @param pageNum int the current page
1152 */
1153 public void printPage(int i, int j, SDPrintDialogUI pd, NGC context, int pageNum) {
1154 fIsPrinting = false;
1155 int pageNumFontZoom = fPrinter.getClientArea().height / getVisibleHeight();
1156 fPrinterX = i;
1157 fPrinterY = j;
1158 setContentsPos(i, j);
1159 update();
1160 fIsPrinting = true;
1161 float lastZoom = fZoomValue;
1162 fZoomValue = fPrinterZoom * lastZoom;
1163
1164 fFrame.draw(context);
1165
1166 fZoomValue = pageNumFontZoom;
1167 context.setFont(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE));
1168 String currentPageNum = String.valueOf(pageNum);
1169 int ii = context.textExtent(currentPageNum);
1170 int jj = context.getCurrentFontHeight();
1171 // context.setBackground(ColorImpl.getSystemColor(SWT.COLOR_BLACK));
1172 // context.setForeground(ColorImpl.getSystemColor(SWT.COLOR_WHITE));
1173 fZoomValue = fPrinterZoom * lastZoom;
1174 context.drawText(currentPageNum, Math.round(fPrinterX + getVisibleWidth() / fPrinterZoom - ii / fPrinterZoom), Math.round(fPrinterY + getVisibleHeight() / fPrinterZoom - jj / fPrinterZoom), false);
1175 fIsPrinting = false;
1176 fZoomValue = lastZoom;
1177 }
1178
1179 /**
1180 * Sets the collapse provider.
1181 *
1182 * @param provider The collapse provider to set
1183 */
1184 protected void setCollapseProvider(ISDCollapseProvider provider) {
1185 fCollapseProvider = provider;
1186 }
1187
1188
1189 /**
1190 * Checks for focus of children.
1191 *
1192 * @param children Control to check
1193 * @return true if child is on focus else false
1194 */
1195 protected boolean checkFocusOnChilds(Control children) {
1196 if (children instanceof Composite) {
1197 Control[] child = ((Composite) children).getChildren();
1198 for (int i = 0; i < child.length; i++) {
1199 if (child[i].isFocusControl()) {
1200 return true;
1201 } else {
1202 checkFocusOnChilds(child[i]);
1203 }
1204 }
1205 }
1206 return false;
1207 }
1208
1209 /**
1210 * A post action for a tooltip (before displaying).
1211 *
1212 * @param accessible true if accessible else false
1213 * @return the tooltip text.
1214 */
1215 protected String getPostfixForTooltip(boolean accessible) {
1216 String postfix = "";//$NON-NLS-1$
1217 // Determine if the tooltip must show the time difference between the current mouse position and
1218 // the last selected graphNode
1219 if ((fCurrentGraphNode != null) &&
1220 (fCurrentGraphNode instanceof ITimeRange) &&
1221 (fToolTipNode instanceof ITimeRange) &&
1222 (fCurrentGraphNode != fToolTipNode) &&
1223 ((ITimeRange) fToolTipNode).hasTimeInfo() &&
1224 ((ITimeRange) fCurrentGraphNode).hasTimeInfo()) {
1225 postfix = " -> " + fCurrentGraphNode.getName() + "\n" + SDMessages._138 + " "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1226
1227 //double delta = ((ITimeRange)toolTipNode).getLastTime()-((ITimeRange)currentGraphNode).getLastTime();
1228 ITmfTimestamp firstTime = ((ITimeRange) fCurrentGraphNode).getEndTime();
1229 ITmfTimestamp lastTime = ((ITimeRange) fToolTipNode).getEndTime();
1230 TmfTimestamp delta = (TmfTimestamp) lastTime.getDelta(firstTime);
1231 postfix += delta.toString();
1232 } else {
1233 if ((fToolTipNode instanceof ITimeRange) && ((ITimeRange) fToolTipNode).hasTimeInfo()) {
1234 postfix = "\n";//$NON-NLS-1$
1235 ITmfTimestamp firstTime = ((ITimeRange) fToolTipNode).getStartTime();
1236 ITmfTimestamp lastTime = ((ITimeRange) fToolTipNode).getEndTime();
1237
1238 if (firstTime != null) {
1239 if (lastTime != null && firstTime.compareTo(lastTime, true) != 0) {
1240 postfix += "start: " + firstTime + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
1241 postfix += "end: " + lastTime + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
1242 } else {
1243 postfix += firstTime.toString();
1244 }
1245 }
1246 else if (lastTime != null) {
1247 postfix += lastTime.toString();
1248 }
1249 }
1250 }
1251 return postfix;
1252 }
1253
1254 /**
1255 * Sets a new focused widget.
1256 *
1257 * @param newFocusShape A new focus shape.
1258 */
1259 protected void setFocus(int newFocusShape) {
1260 fFocusedWidget = newFocusShape;
1261 if (fFocusedWidget == -1) {
1262 getViewControl().getAccessible().setFocus(ACC.CHILDID_SELF);
1263 } else {
1264 getViewControl().getAccessible().setFocus(fFocusedWidget);
1265 }
1266 }
1267
1268 /**
1269 * Highlight the given GraphNode<br>
1270 * The GraphNode is then displayed using the system default selection color
1271 *
1272 * @param node the GraphNode to highlight
1273 */
1274 protected void performSelection(GraphNode node) {
1275 if ((fCtrlSelection) || (fShiftSelection)) {
1276 if (node != null) {
1277 if (fSelectedNodeList.contains(node)) {
1278 removeSelection(node);
1279 } else {
1280 addSelection(node);
1281 }
1282 } else {
1283 return;
1284 }
1285 } else {
1286 clearSelection();
1287 if (node != null) {
1288 addSelection(node);
1289 }
1290 }
1291 }
1292
1293 /**
1294 * Returns a draw buffer image.
1295 *
1296 * @return a Image containing the draw buffer.
1297 */
1298 protected Image getDrawBuffer() {
1299
1300 update();
1301 Rectangle area = getClientArea();
1302 Image dbuffer = null;
1303 GC gcim = null;
1304
1305 try {
1306 dbuffer = new Image(getDisplay(), area.width, area.height);
1307 } catch (Exception e) {
1308 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,TmfUiPlugin.PLUGIN_ID, "Error creating image", e)); //$NON-NLS-1$
1309 }
1310
1311 gcim = new GC(dbuffer);
1312
1313 NGC context = new NGC(this, gcim);
1314
1315 // Set the metrics to use for lifeline text and message text
1316 // using the Graphical Context
1317 Metrics.setLifelineFontHeight(context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
1318 Metrics.setLifelineFontWidth(context.getFontWidth(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE)));
1319 Metrics.setLifelineWidth(SDViewPref.getInstance().getLifelineWidth());
1320 Metrics.setFrameFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_FRAME_NAME)));
1321 Metrics.setLifelineHeaderFontHeight(context.getFontHeight(Frame.getUserPref().getFont(ISDPreferences.PREF_LIFELINE_HEADER)));
1322
1323 int syncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS));
1324 int syncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_SYNC_MESS_RET));
1325 int asyncMessFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS));
1326 int asyncMessRetFontH = context.getFontHeight(SDViewPref.getInstance().getFont(SDViewPref.PREF_ASYNC_MESS_RET));
1327
1328 int messageFontHeight = 0;
1329 if (syncMessFontH > syncMessRetFontH) {
1330 messageFontHeight = syncMessFontH;
1331 } else {
1332 messageFontHeight = syncMessRetFontH;
1333 }
1334 if (messageFontHeight < asyncMessFontH) {
1335 messageFontHeight = asyncMessFontH;
1336 }
1337 if (messageFontHeight < asyncMessRetFontH) {
1338 messageFontHeight = asyncMessRetFontH;
1339 }
1340 Metrics.setMessageFontHeight(messageFontHeight);
1341 context.setFont(SDViewPref.getInstance().getFont(SDViewPref.PREF_LIFELINE));
1342
1343 int width = (int) ((fFrame.getWidth() + 2 * Metrics.FRAME_H_MARGIN) * fZoomValue);
1344 int height = (int) ((fFrame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * fZoomValue);
1345
1346 resizeContents(width, height);
1347
1348 context.setBackground(Frame.getUserPref().getBackGroundColor(ISDPreferences.PREF_FRAME));
1349 context.fillRectangle(0, 0, getContentsWidth(), Metrics.FRAME_V_MARGIN);
1350 context.fillRectangle(0, 0, fFrame.getX(), getContentsHeight());
1351 context.fillRectangle(fFrame.getX() + fFrame.getWidth() + 1, 0, getContentsWidth() - (fFrame.getX() + fFrame.getWidth() + 1), getContentsHeight());
1352 context.fillRectangle(0, fFrame.getY() + fFrame.getHeight() + 1, getContentsWidth(), getContentsHeight() - (fFrame.getY() + fFrame.getHeight() + 1));
1353 gcim.setLineWidth(1);
1354
1355 fFrame.draw(context);
1356 if (fDragAndDrop != null) {
1357 Lifeline node = (Lifeline) fDragAndDrop;
1358 boolean isSelected = fDragAndDrop.isSelected();
1359 boolean hasFocus = fDragAndDrop.hasFocus();
1360 node.setSelected(false);
1361 node.setFocused(false);
1362 node.draw(context, fDragX, fDragY);
1363 node.setSelected(isSelected);
1364 node.setFocused(hasFocus);
1365 }
1366 gcim.dispose();
1367 context.dispose();
1368 return dbuffer;
1369 }
1370
1371 /*
1372 * (non-Javadoc)
1373 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#keyPressedEvent(org.eclipse.swt.events.KeyEvent)
1374 */
1375 @Override
1376 protected void keyPressedEvent(KeyEvent event) {
1377 if (!(isFocusControl() || getViewControl().isFocusControl())) {
1378 Control[] child = getParent().getChildren();
1379 for (int i = 0; i < child.length; i++) {
1380 if ((child[i].isFocusControl())&& (!(child[i] instanceof ScrollView))) {
1381 getViewControl().setFocus();
1382 break;
1383 }
1384 }
1385 }
1386 setFocus(-1);
1387
1388 if (event.keyCode == SWT.CTRL) {
1389 fCtrlSelection = true;
1390 }
1391 if (event.keyCode == SWT.SHIFT) {
1392 fShiftSelection = true;
1393 fPrevList = new ArrayList<GraphNode>();
1394 fPrevList.addAll(getSelection());
1395 }
1396
1397 GraphNode prevNode = getFocusNode();
1398
1399 if (event.keyCode == SWT.ARROW_RIGHT) {
1400 traverseRight();
1401 }
1402
1403 if (event.keyCode == SWT.ARROW_LEFT) {
1404 traverseLeft();
1405 }
1406
1407 if (event.keyCode == SWT.ARROW_DOWN) {
1408 traverseDown();
1409 }
1410
1411 if (event.keyCode == SWT.ARROW_UP) {
1412 traverseUp();
1413 }
1414
1415 if (event.keyCode == SWT.HOME) {
1416 traverseHome();
1417 }
1418
1419 if (event.keyCode == SWT.END) {
1420 traverseEnd();
1421 }
1422
1423 if ((!fShiftSelection) && (!fCtrlSelection)) {
1424 fListStart = fCurrentGraphNode;
1425 }
1426
1427 if (event.character == ' ') {
1428 performSelection(fCurrentGraphNode);
1429 if (!fShiftSelection)
1430 fListStart = fCurrentGraphNode;
1431 }
1432
1433 if ((fShiftSelection) && (prevNode != getFocusNode())) {
1434 clearSelection();
1435 addSelection(fPrevList);
1436 addSelection(fFrame.getNodeList(fListStart, getFocusNode()));
1437 if (getFocusNode() instanceof Lifeline) {
1438 ensureVisible(getFocusNode().getX(), getFocusNode().getY(), getFocusNode().getWidth(), getFocusNode().getHeight(), SWT.CENTER | SWT.VERTICAL, true);
1439 } else {
1440 ensureVisible(getFocusNode());
1441 }
1442 } else if ((!fCtrlSelection) && (!fShiftSelection)) {
1443
1444 clearSelection();
1445 if (getFocusNode() != null) {
1446 addSelection(getFocusNode());
1447
1448 if (getFocusNode() instanceof Lifeline) {
1449 ensureVisible(getFocusNode().getX(), getFocusNode().getY(), getFocusNode().getWidth(), getFocusNode().getHeight(), SWT.CENTER | SWT.VERTICAL, true);
1450 } else {
1451 ensureVisible(getFocusNode());
1452 }
1453 }
1454 }
1455
1456 if (fCurrentGraphNode != null) {
1457 fCurrentGraphNode.setFocused(true);
1458 }
1459 redraw();
1460
1461 if ((event.character == ' ') && ((fZoomInMode) || (fZoomOutMode))) {
1462 int cx = Math.round((getContentsX() + getVisibleWidth() / 2) / fZoomValue);
1463 int cy = Math.round((getContentsY() + getVisibleHeight() / 2) / fZoomValue);
1464 if (fZoomInMode) {
1465 if (fZoomValue < 64) {
1466 fZoomValue = fZoomValue * (float) 1.25;
1467 }
1468 } else {
1469 fZoomValue = fZoomValue / (float) 1.25;
1470 }
1471 int x = Math.round(cx * fZoomValue - getVisibleWidth() / (float) 2);
1472 int y = Math.round(cy * fZoomValue - getVisibleHeight() / (float) 2);
1473 setContentsPos(x, y);
1474 if (fTimeBar != null) {
1475 fTimeBar.setZoom(fZoomValue);
1476 }
1477 // redraw also resize the scrollView content
1478 redraw();
1479 }
1480 }
1481
1482 /*
1483 * (non-Javadoc)
1484 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#keyReleasedEvent(org.eclipse.swt.events.KeyEvent)
1485 */
1486 @Override
1487 protected void keyReleasedEvent(KeyEvent event) {
1488 setFocus(-1);
1489 if (event.keyCode == SWT.CTRL)
1490 fCtrlSelection = false;
1491 if (event.keyCode == SWT.SHIFT)
1492 fShiftSelection = false;
1493 super.keyReleasedEvent(event);
1494 setFocus(1);
1495 }
1496
1497 /*
1498 * (non-Javadoc)
1499 * @see org.eclipse.swt.widgets.Control#isFocusControl()
1500 */
1501 @Override
1502 public boolean isFocusControl() {
1503 Control[] child = getChildren();
1504 for (int i = 0; i < child.length; i++) {
1505 if (child[i].isFocusControl()) {
1506 return true;
1507 } else {
1508 checkFocusOnChilds(child[i]);
1509 }
1510 }
1511 return false;
1512 }
1513
1514 /*
1515 * (non-Javadoc)
1516 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#setContentsPos(int, int)
1517 */
1518 @Override
1519 public boolean setContentsPos(int x, int y) {
1520 int localX = x;
1521 int localY = y;
1522
1523 if (localX < 0) {
1524 localX = 0;
1525 }
1526 if (localY < 0) {
1527 localY = 0;
1528 }
1529 if (fFrame == null) {
1530 return false;
1531 }
1532 if (localX + getVisibleWidth() > getContentsWidth()) {
1533 localX = getContentsWidth() - getVisibleWidth();
1534 }
1535 if (localY + getVisibleHeight() > getContentsHeight()) {
1536 localY = getContentsHeight() - getVisibleHeight();
1537 }
1538 int x1 = Math.round(localX / fZoomValue);
1539 int y2 = Math.round(localY / fZoomValue);
1540 int width = Math.round(getVisibleWidth() / fZoomValue);
1541 int height = Math.round(getVisibleHeight() / fZoomValue);
1542 fFrame.updateIndex(x1, y2, width, height);
1543
1544 if (fInsertionCartet != null && fInsertionCartet.isVisible()) {
1545 fInsertionCartet.setVisible(false);
1546 }
1547
1548 return super.setContentsPos(localX, localY);
1549 }
1550
1551 /*
1552 * (non-Javadoc)
1553 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#contentsMouseHover(org.eclipse.swt.events.MouseEvent)
1554 */
1555 @Override
1556 protected void contentsMouseHover(MouseEvent event) {
1557 GraphNode graphNode = null;
1558 if (fFrame != null) {
1559 int x = Math.round(event.x / fZoomValue);
1560 int y = Math.round(event.y / fZoomValue);
1561 graphNode = fFrame.getNodeAt(x, y);
1562 if ((graphNode != null) && (SDViewPref.getInstance().tooltipEnabled())) {
1563 fToolTipNode = graphNode;
1564 String postfix = getPostfixForTooltip(true);
1565 if (graphNode instanceof Lifeline) {
1566 Lifeline lifeline = (Lifeline) graphNode;
1567 fToolTip.showToolTip(lifeline.getToolTipText() + postfix);
1568 setFocus(0);
1569 } else {
1570 fToolTip.showToolTip(graphNode.getName() + postfix);
1571 setFocus(0);
1572 }
1573 } else {
1574 fToolTip.hideToolTip();
1575 }
1576 }
1577 }
1578
1579 /*
1580 * (non-Javadoc)
1581 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#contentsMouseMoveEvent(org.eclipse.swt.events.MouseEvent)
1582 */
1583 @Override
1584 protected void contentsMouseMoveEvent(MouseEvent e) {
1585 fScrollToolTip.hideToolTip();
1586 fToolTip.hideToolTip();
1587 // super.contentsMouseMoveEvent(e);
1588 if (!(isFocusControl() || getViewControl().isFocusControl())) {
1589 Control[] child = getParent().getChildren();
1590 for (int i = 0; i < child.length; i++) {
1591 if ((child[i].isFocusControl()) && (!(child[i] instanceof ScrollView))) {
1592 getViewControl().setFocus();
1593 break;
1594 }
1595 }
1596 }
1597 setFocus(-1);
1598
1599 if (((e.stateMask & SWT.BUTTON_MASK) != 0) && ((fDragAndDrop != null) || fIsDragAndDrop) && (fReorderMode || fCollapseProvider != null)) {
1600 fIsDragAndDrop = false;
1601 if (fCurrentGraphNode instanceof Lifeline) {
1602 fDragAndDrop = (Lifeline) fCurrentGraphNode;
1603 }
1604 if (fDragAndDrop != null) {
1605 int dx = 0;
1606 int dy = 0;
1607 if (e.x > getContentsX() + getVisibleWidth()) {
1608 dx = e.x - (getContentsX() + getVisibleWidth());
1609 } else if (e.x < getContentsX()) {
1610 dx = -getContentsX() + e.x;
1611 }
1612 if (e.y > getContentsY() + getVisibleHeight()) {
1613 dy = e.y - (getContentsY() + getVisibleHeight());
1614 } else if (e.y < getContentsY()) {
1615 dy = -getContentsY() + e.y;
1616 }
1617 fDragX = e.x;
1618 fDragY = e.y;
1619 if (dx != 0 || dy != 0) {
1620 if (fLocalAutoScroll == null) {
1621 if (fLocalAutoScrollTimer == null) {
1622 fLocalAutoScrollTimer = new Timer(true);
1623 }
1624 fLocalAutoScroll = new AutoScroll(this, dx, dy);
1625 fLocalAutoScrollTimer.schedule(fLocalAutoScroll, 0, 75);
1626 } else {
1627 fLocalAutoScroll.fDeltaX = dx;
1628 fLocalAutoScroll.fDeltaY = dy;
1629 }
1630 } else if (fLocalAutoScroll != null) {
1631 fLocalAutoScroll.cancel();
1632 fLocalAutoScroll = null;
1633 }
1634 fDragX = Math.round(e.x / fZoomValue);
1635 fDragY = Math.round(e.y / fZoomValue);
1636 redraw();
1637 Lifeline node = fFrame.getCloserLifeline(fDragX);
1638 if ((node != null) && (node != fDragAndDrop)) {
1639 int y = 0;
1640 int y1 = 0;
1641 int height = Metrics.getLifelineHeaderFontHeigth() + 2 * Metrics.LIFELINE_HEARDER_TEXT_V_MARGIN;
1642 int hMargin = Metrics.LIFELINE_VT_MAGIN / 4;
1643 int x = node.getX();
1644 int width = node.getWidth();
1645 if (fFrame.getVisibleAreaY() < node.getY() + node.getHeight() - height - hMargin) {
1646 y = contentsToViewY(Math.round((node.getY() + node.getHeight()) * fZoomValue));
1647 } else {
1648 y = Math.round(height * fZoomValue);
1649 }
1650
1651 if (fFrame.getVisibleAreaY() < contentsToViewY(node.getY() - hMargin)) {
1652 y1 = contentsToViewY(Math.round((node.getY() - hMargin) * fZoomValue));
1653 } else {
1654 y1 = Math.round(height * fZoomValue);
1655 }
1656
1657 int rx = Math.round(x * fZoomValue);
1658
1659 fInsertionCartet.setVisible(true);
1660 if ((fInsertionCartet.getImage() != null) && (!fInsertionCartet.getImage().isDisposed())) {
1661 fInsertionCartet.getImage().dispose();
1662 }
1663 if (rx <= e.x && Math.round(rx + (width * fZoomValue)) >= e.x) {
1664 if (fCollapseProvider != null) {
1665 ImageData data = fCollapaseCaretImg.getImageData();
1666 data = data.scaledTo(Math.round(fCollapaseCaretImg.getBounds().width * fZoomValue), Math.round(fCollapaseCaretImg.getBounds().height * fZoomValue));
1667 fCurrentCaretImage = new Image(Display.getCurrent(), data);
1668 fInsertionCartet.setImage(fCurrentCaretImage);
1669 fInsertionCartet.setLocation(contentsToViewX(rx + Math.round((width / (float) 2) * fZoomValue)) - fCurrentCaretImage.getBounds().width / 2, y);
1670 }
1671 } else if (fReorderMode) {
1672 if (rx > e.x) {
1673 if (node.getIndex() > 1 && fFrame.getLifeline(node.getIndex() - 2) == fDragAndDrop) {
1674 return;
1675 }
1676 ImageData data = fArrowUpCaretImg.getImageData();
1677 data = data.scaledTo(Math.round(fArrowUpCaretImg.getBounds().width * fZoomValue), Math.round(fArrowUpCaretImg.getBounds().height * fZoomValue));
1678 fCurrentCaretImage = new Image(Display.getCurrent(), data);
1679 fInsertionCartet.setImage(fCurrentCaretImage);
1680 fInsertionCartet.setLocation(contentsToViewX(Math.round((x - Metrics.LIFELINE_SPACING / 2) * fZoomValue)) - fCurrentCaretImage.getBounds().width / 2, y1);
1681 } else {
1682 if (node.getIndex() < fFrame.lifeLinesCount() && fFrame.getLifeline(node.getIndex()) == fDragAndDrop) {
1683 return;
1684 }
1685 ImageData data = fArrowUpCaretImg.getImageData();
1686 data = data.scaledTo(Math.round(fArrowUpCaretImg.getBounds().width * fZoomValue), Math.round(fArrowUpCaretImg.getBounds().height * fZoomValue));
1687 fCurrentCaretImage = new Image(Display.getCurrent(), data);
1688 fInsertionCartet.setImage(fCurrentCaretImage);
1689 fInsertionCartet.setLocation(contentsToViewX(Math.round((x + width + Metrics.LIFELINE_SPACING / 2) * fZoomValue)) - fCurrentCaretImage.getBounds().width / 2 + 1, y1);
1690 }
1691 }
1692 } else {
1693 fInsertionCartet.setVisible(false);
1694 }
1695 }
1696 } else {
1697 super.contentsMouseMoveEvent(e);
1698 }
1699 }
1700
1701 /*
1702 * (non-Javadoc)
1703 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#contentsMouseUpEvent(org.eclipse.swt.events.MouseEvent)
1704 */
1705 @Override
1706 protected void contentsMouseUpEvent(MouseEvent event) {
1707 // Just in case the diagram highlight a time compression region
1708 // this region need to be released when clicking everywhere
1709 fInsertionCartet.setVisible(false);
1710 if (fDragAndDrop != null) {
1711 if ((fOverView != null) && (!fOverView.isDisposed())) {
1712 fOverView.dispose();
1713 }
1714 fOverView = null;
1715 Lifeline node = fFrame.getCloserLifeline(fDragX);
1716 if (node != null) {
1717 int rx = Math.round(node.getX() * fZoomValue);
1718 if (rx <= event.x && Math.round(rx + (node.getWidth() * fZoomValue)) >= event.x) {
1719 if ((fCollapseProvider != null) && (fDragAndDrop != node)) {
1720 fCollapseProvider.collapseTwoLifelines((Lifeline) fDragAndDrop, node);
1721 }
1722 } else if (rx < event.x) {
1723 fFrame.insertLifelineAfter((Lifeline) fDragAndDrop, node);
1724 if (node.getIndex() < fFrame.lifeLinesCount()) {
1725 Lifeline temp[] = { (Lifeline) fDragAndDrop, fFrame.getLifeline(node.getIndex()) };
1726 fReorderList.add(temp);
1727 } else {
1728 Lifeline temp[] = { (Lifeline) fDragAndDrop, null };
1729 fReorderList.add(temp);
1730 }
1731 } else {
1732 fFrame.insertLifelineBefore((Lifeline) fDragAndDrop, node);
1733 Lifeline temp[] = { (Lifeline) fDragAndDrop, node };
1734 fReorderList.add(temp);
1735 }
1736 }
1737 }
1738 fDragAndDrop = null;
1739 redraw();
1740 if (fFrame == null) {
1741 return;
1742 }
1743 fFrame.resetTimeCompression();
1744
1745 // reset auto scroll if it's engaged
1746 if (fLocalAutoScroll != null) {
1747 fLocalAutoScroll.cancel();
1748 fLocalAutoScroll = null;
1749 }
1750 super.contentsMouseUpEvent(event);
1751 }
1752
1753 /*
1754 * (non-Javadoc)
1755 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#contentsMouseDownEvent(org.eclipse.swt.events.MouseEvent)
1756 */
1757 @Override
1758 protected void contentsMouseDownEvent(MouseEvent event) {
1759 if (fCurrentGraphNode != null) {
1760 fCurrentGraphNode.setFocused(false);
1761 }
1762
1763 // Just in case the diagram highlight a time compression region
1764 // this region need to be released when clicking everywhere
1765 if (fFrame == null) {
1766 return;
1767 }
1768
1769 fFrame.resetTimeCompression();
1770
1771 if ((event.stateMask & SWT.CTRL) != 0) {
1772 fCtrlSelection = true;
1773 } else {
1774 fCtrlSelection = false;
1775 }
1776
1777 if (((fZoomInMode) || (fZoomOutMode)) && (event.button == 1)) {
1778 int cx = Math.round(event.x / fZoomValue);
1779 int cy = Math.round(event.y / fZoomValue);
1780 if (fZoomInMode) {
1781 if (fZoomValue < 64) {
1782 fZoomValue = fZoomValue * (float) 1.25;
1783 }
1784 } else {
1785 fZoomValue = fZoomValue / (float) 1.25;
1786 }
1787 int x = Math.round(cx * fZoomValue - getVisibleWidth() / (float) 2);
1788 int y = Math.round(cy * fZoomValue - getVisibleHeight() / (float) 2);
1789 setContentsPos(x, y);
1790 if (fTimeBar != null) {
1791 fTimeBar.setZoom(fZoomValue);
1792 }
1793 // redraw also resize the scrollView content
1794 redraw();
1795 } else {// if (event.button ==1)
1796 GraphNode node = null;
1797 int x = Math.round(event.x / fZoomValue);
1798 int y = Math.round(event.y / fZoomValue);
1799 node = fFrame.getNodeAt(x, y);
1800
1801 if ((event.button == 1) || ((node != null) && !node.isSelected())) {
1802 if (!fShiftSelection) {
1803 fListStart = node;
1804 }
1805 if (fShiftSelection) {
1806 clearSelection();
1807 addSelection(fFrame.getNodeList(fListStart, node));
1808 } else {
1809 performSelection(node);
1810 }
1811 fCurrentGraphNode = node;
1812 if (node != null) {
1813 node.setFocused(true);
1814 }
1815 }
1816 redraw();
1817 }
1818 if (fDragAndDrop == null) {
1819 super.contentsMouseDownEvent(event);
1820 }
1821 fIsDragAndDrop = (event.button == 1);
1822
1823 }
1824
1825 /**
1826 * TimerTask for auto scroll feature.
1827 */
1828 protected static class AutoScroll extends TimerTask {
1829 /**
1830 * Field delta x.
1831 */
1832 public int fDeltaX;
1833 /**
1834 * Field delta y.
1835 */
1836 public int fDeltaY;
1837 /**
1838 * Field sequence diagram reference.
1839 */
1840 public SDWidget fSdWidget;
1841
1842 /**
1843 * Constructor for AutoScroll.
1844 * @param sv sequence diagram widget reference
1845 * @param dx delta x
1846 * @param dy delta y
1847 */
1848 public AutoScroll(SDWidget sv, int dx, int dy) {
1849 fSdWidget = sv;
1850 fDeltaX = dx;
1851 fDeltaY = dy;
1852 }
1853
1854 /*
1855 * (non-Javadoc)
1856 * @see java.util.TimerTask#run()
1857 */
1858 @Override
1859 public void run() {
1860 Display.getDefault().asyncExec(new Runnable() {
1861 @Override
1862 public void run() {
1863 if (fSdWidget.isDisposed())
1864 return;
1865 fSdWidget.fDragX += fDeltaX;
1866 fSdWidget.fDragY += fDeltaY;
1867 fSdWidget.scrollBy(fDeltaX, fDeltaY);
1868 }
1869 });
1870 }
1871 }
1872
1873 /*
1874 * (non-Javadoc)
1875 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#drawContents(org.eclipse.swt.graphics.GC, int, int, int, int)
1876 */
1877 @Override
1878 protected void drawContents(GC gc, int clipx, int clipy, int clipw, int cliph) {
1879 if (fFrame == null) {
1880 gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
1881 gc.fillRectangle(0, 0, getVisibleWidth(), getVisibleHeight());
1882 gc.dispose();
1883 return;
1884 } else {
1885 Frame.setUserPref(SDViewPref.getInstance());
1886 }
1887
1888 Rectangle area = getClientArea();
1889 Image dbuffer = getDrawBuffer();
1890 int height = Math.round((fFrame.getHeight() + 2 * Metrics.FRAME_V_MARGIN) * fZoomValue);
1891
1892 try {
1893 gc.drawImage(dbuffer, 0, 0, area.width, area.height, 0, 0, area.width, area.height);
1894 } catch (Exception e) {
1895 TmfUiPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,TmfUiPlugin.PLUGIN_ID, "Error drawin content", e)); //$NON-NLS-1$
1896 }
1897 dbuffer.dispose();
1898 setHScrollBarIncrement(Math.round(SDViewPref.getInstance().getLifelineWidth() / (float) 2 * fZoomValue));
1899 setVScrollBarIncrement(Math.round(Metrics.getMessagesSpacing() * fZoomValue));
1900 if ((fTimeBar != null) && (fFrame.hasTimeInfo())) {
1901 fTimeBar.resizeContents(9, height + getHorizontalBarHeight());
1902 fTimeBar.setContentsPos(getContentsX(), getContentsY());
1903 fTimeBar.redraw();
1904 fTimeBar.update();
1905 }
1906 float xRatio = getContentsWidth() / (float) getVisibleWidth();
1907 float yRatio = getContentsHeight() / (float) getVisibleHeight();
1908 if (yRatio > xRatio) {
1909 setOverviewSize((int) (getVisibleHeight() * 0.75));
1910 } else {
1911 setOverviewSize((int) (getVisibleWidth() * 0.75));
1912 }
1913 }
1914
1915 /*
1916 * (non-Javadoc)
1917 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
1918 */
1919 @Override
1920 public void widgetDefaultSelected(SelectionEvent event) {
1921 }
1922
1923 /*
1924 * (non-Javadoc)
1925 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
1926 */
1927 @Override
1928 public void widgetSelected(SelectionEvent event) {
1929 if (event.widget == fZoomIn) {
1930 fZoomValue = fZoomValue * 2;
1931 } else if (event.widget == fZoomOut) {
1932 fZoomValue = fZoomValue / 2;
1933 }
1934 redraw();
1935 }
1936
1937 /*
1938 * Called when property changed occurs in the preference page. "PREFOK" is fired when the user press the ok or apply button
1939 *
1940 * (non-Javadoc)
1941 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
1942 */
1943 @Override
1944 public void propertyChange(PropertyChangeEvent e) {
1945 if (fFrame != null && !isDisposed()) {
1946 fFrame.resetTimeCompression();
1947 }
1948 if (e.getProperty().equals("PREFOK")) //$NON-NLS-1$
1949 {
1950 // Prepare the overview to be reused for the new
1951 // settings (especially the colors)
1952 if (fOverView != null)
1953 fOverView.dispose();
1954 fOverView = null;
1955 redraw();
1956 }
1957 }
1958
1959 /*
1960 * (non-Javadoc)
1961 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
1962 */
1963 @Override
1964 public void widgetDisposed(DisposeEvent e) {
1965 if (fOverView != null) {
1966 fOverView.dispose();
1967 }
1968 super.removeDisposeListener(this);
1969 if ((fCurrentCaretImage != null) && (!fCurrentCaretImage.isDisposed())) {
1970 fCurrentCaretImage.dispose();
1971 }
1972 if ((fArrowUpCaretImg != null) && (!fArrowUpCaretImg.isDisposed())) {
1973 fArrowUpCaretImg.dispose();
1974 }
1975 if ((fCollapaseCaretImg != null) && (!fCollapaseCaretImg.isDisposed())) {
1976 fCollapaseCaretImg.dispose();
1977 }
1978 SDViewPref.getInstance().removePropertyChangeListener(this);
1979 LoadersManager lm = LoadersManager.getInstance();
1980 if (fSite instanceof SDView) {
1981 ((SDView) fSite).resetProviders();
1982 if (lm != null) {
1983 lm.resetLoader(((SDView) fSite).getViewSite().getId());
1984 }
1985 }
1986 }
1987
1988 /*
1989 * (non-Javadoc)
1990 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#drawOverview(org.eclipse.swt.graphics.GC, org.eclipse.swt.graphics.Rectangle)
1991 */
1992 @Override
1993 protected void drawOverview(GC gc, Rectangle r) {
1994 float oldzoom = fZoomValue;
1995 if (getContentsWidth() > getContentsHeight()) {
1996 fZoomValue = (float) r.width / (float) getContentsWidth() * oldzoom;
1997 } else {
1998 fZoomValue = (float) r.height / (float) getContentsHeight() * oldzoom;
1999 }
2000 if ((fOverView != null) && ((r.width != fOverView.getBounds().width) || (r.height != fOverView.getBounds().height))) {
2001 fOverView.dispose();
2002 fOverView = null;
2003 }
2004 if (fOverView == null) {
2005 int backX = getContentsX();
2006 int backY = getContentsY();
2007 setContentsPos(0, 0);
2008 fOverView = new Image(getDisplay(), r.width, r.height);
2009 GC gcim = new GC(fOverView);
2010 NGC context = new NGC(this, gcim);
2011 context.setBackground(SDViewPref.getInstance().getBackGroundColor(SDViewPref.PREF_FRAME));
2012 fFrame.draw(context);
2013 setContentsPos(backX, backY);
2014 gcim.dispose();
2015 context.dispose();
2016 }
2017 if ((fOverView != null) && (r.width == fOverView.getBounds().width) && (r.height == fOverView.getBounds().height)) {
2018 gc.drawImage(fOverView, 0, 0, r.width, r.height, 0, 0, r.width, r.height);
2019 }
2020
2021 fZoomValue = oldzoom;
2022
2023 super.drawOverview(gc, r);
2024 }
2025
2026 /*
2027 * (non-Javadoc)
2028 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ITimeCompressionListener#deltaSelected(org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline, int, int, org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor)
2029 */
2030 @Override
2031 public void deltaSelected(Lifeline lifeline, int startEvent, int nbEvent, IColor color) {
2032 fFrame.highlightTimeCompression(lifeline, startEvent, nbEvent, color);
2033 ensureVisible(lifeline);
2034 int y1 = lifeline.getY() + lifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * startEvent;
2035 int y2 = lifeline.getY() + lifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * (startEvent + nbEvent);
2036 ensureVisible(lifeline.getX(), y1 - (Metrics.getLifelineHeaderFontHeigth() + +2 * Metrics.LIFELINE_HEARDER_TEXT_V_MARGIN), lifeline.getWidth(), y2 - y1 + 3, SWT.CENTER | SWT.VERTICAL, true);
2037 redraw();
2038 update();
2039 }
2040
2041 /*
2042 * (non-Javadoc)
2043 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getVisibleWidth()
2044 */
2045 @Override
2046 public int getVisibleWidth() {
2047 if (fIsPrinting) {
2048 return fPrinter.getClientArea().width;
2049 }
2050 return super.getVisibleWidth();
2051 }
2052
2053 /*
2054 * (non-Javadoc)
2055 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getVisibleHeight()
2056 */
2057 @Override
2058 public int getVisibleHeight() {
2059 if (fIsPrinting) {
2060 return fPrinter.getClientArea().height;
2061 }
2062 return super.getVisibleHeight();
2063 }
2064
2065 /*
2066 * (non-Javadoc)
2067 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#contentsToViewX(int)
2068 */
2069 @Override
2070 public int contentsToViewX(int x) {
2071 if (fIsPrinting) {
2072 int v = Math.round(fPrinterX * fPrinterZoom);
2073 return x - v;
2074 }
2075 return x - getContentsX();
2076 }
2077
2078 /*
2079 * (non-Javadoc)
2080 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#contentsToViewY(int)
2081 */
2082 @Override
2083 public int contentsToViewY(int y) {
2084 if (fIsPrinting) {
2085 int v = Math.round(fPrinterY * fPrinterZoom);
2086 return y - v;
2087 }
2088 return y - getContentsY();
2089 }
2090
2091 /*
2092 * (non-Javadoc)
2093 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsX()
2094 */
2095 @Override
2096 public int getContentsX() {
2097 if (fIsPrinting) {
2098 return Math.round(fPrinterX * fPrinterZoom);
2099 }
2100 return super.getContentsX();
2101
2102 }
2103
2104 /*
2105 * (non-Javadoc)
2106 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.ScrollView#getContentsY()
2107 */
2108 @Override
2109 public int getContentsY() {
2110 if (fIsPrinting) {
2111 return Math.round(fPrinterY * fPrinterZoom);
2112 }
2113 return super.getContentsY();
2114 }
2115 }
This page took 0.075796 seconds and 6 git commands to generate.