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