tmf: API clean-up of sequence diagram framework
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / DiagramToolTip.java
CommitLineData
73005152 1/**********************************************************************
cab6c8ff 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
c8422608
AM
7 *
8 * Contributors:
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 org.eclipse.swt.SWT;
16import org.eclipse.swt.graphics.FontMetrics;
17import org.eclipse.swt.graphics.GC;
18import org.eclipse.swt.graphics.Point;
19import org.eclipse.swt.widgets.Control;
20import org.eclipse.swt.widgets.Display;
21import org.eclipse.swt.widgets.Shell;
22import org.eclipse.swt.widgets.Text;
23
24/**
df0b8ff4 25 * <p>
73005152
BH
26 * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse hovers over the
27 * sequence diagram
df0b8ff4 28 * </p>
c8422608 29 *
df0b8ff4 30 * @version 1.0
73005152
BH
31 * @author sveyrier
32 */
33public class DiagramToolTip {
34
df0b8ff4
BH
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
73005152 38 /**
df0b8ff4 39 * The parent control where the tooltip must be drawn.
73005152 40 */
cab6c8ff 41 private Control fParent = null;
73005152 42 /**
df0b8ff4 43 * The tooltip shell.
73005152 44 */
cab6c8ff 45 private Shell fToolTipShell = null;
df0b8ff4
BH
46 /**
47 * The text box.
48 */
cab6c8ff 49 private Text fTextBox = null;
73005152 50
df0b8ff4
BH
51 // ------------------------------------------------------------------------
52 // Constructors
53 // ------------------------------------------------------------------------
c8422608 54
73005152
BH
55 /**
56 * Create a new tooltip for the given parent control
c8422608 57 *
eb63f5ff 58 * @param parent the parent control.
73005152 59 */
eb63f5ff
BH
60 public DiagramToolTip(Control parent) {
61 fParent = parent;
62 fToolTipShell = new Shell(fParent.getShell(), SWT.MULTI);
63 fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
64 fTextBox = new Text(fToolTipShell, SWT.WRAP | SWT.MULTI);
65 fTextBox.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
73005152
BH
66 }
67
df0b8ff4
BH
68 // ------------------------------------------------------------------------
69 // Methods
70 // ------------------------------------------------------------------------
71
73005152
BH
72 /**
73 * Display the tooltip using the given text The tooltip will stay on screen until it is told otherwise
c8422608 74 *
73005152
BH
75 * @param value the text to display
76 */
77 public void showToolTip(String value) {
df0b8ff4 78 if ((value == null) || (value.equalsIgnoreCase(""))) { //$NON-NLS-1$
eb63f5ff 79 fToolTipShell.setVisible(false);
73005152
BH
80 return;
81 }
82
eb63f5ff 83 int w = fToolTipShell.getBounds().width;
73005152
BH
84 Point hr = Display.getDefault().getCursorLocation();
85 int cursorH = 32;
86 for (int i = 0; i < Display.getDefault().getCursorSizes().length; i++) {
df0b8ff4 87 if (Display.getDefault().getCursorSizes()[i].y < cursorH) {
73005152 88 cursorH = Display.getDefault().getCursorSizes()[i].y;
df0b8ff4 89 }
73005152
BH
90 }
91 if (hr.x + w > Display.getDefault().getBounds().width) {
92 int tempX = (hr.x + w) - Display.getDefault().getBounds().width;
df0b8ff4 93 if (tempX > Display.getDefault().getBounds().width) {
73005152 94 hr.x = 0;
df0b8ff4 95 }
73005152
BH
96 hr.x = hr.x - tempX;
97 }
eb63f5ff 98 fTextBox.setText(value);
73005152 99 int charactersPerColumn = 100;
eb63f5ff 100 GC gc = new GC(fTextBox);
73005152
BH
101 FontMetrics fm = gc.getFontMetrics();
102 gc.dispose();
103 int width = charactersPerColumn * fm.getAverageCharWidth();
eb63f5ff
BH
104 fTextBox.setSize(fTextBox.computeSize(width, fTextBox.getLineCount() * fTextBox.getLineHeight()));
105 fToolTipShell.setLocation(hr.x, hr.y + cursorH);
106 fToolTipShell.setSize(fTextBox.getSize());
107 fTextBox.setVisible(true);
108 fToolTipShell.setVisible(true);
73005152
BH
109 }
110
111 /**
112 * Hide the tooltip
113 */
114 public void hideToolTip() {
eb63f5ff 115 fToolTipShell.setVisible(false);
73005152
BH
116 }
117
cab6c8ff
BH
118 /**
119 * @return parent control
120 * @since 2.0
121 */
122 protected Control getParent() {
123 return fParent;
124 }
125
73005152 126}
This page took 0.04145 seconds and 5 git commands to generate.