Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.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
f26e290b
BH
35 // ------------------------------------------------------------------------
36 // Constants
37 // ------------------------------------------------------------------------
38 private static final int CHARACTERS_PER_COLUMN = 100;
39 private static final int DEFAULT_CURSOR_HEIGHT = 32;
40
df0b8ff4
BH
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
73005152 44 /**
df0b8ff4 45 * The parent control where the tooltip must be drawn.
73005152 46 */
cab6c8ff 47 private Control fParent = null;
73005152 48 /**
df0b8ff4 49 * The tooltip shell.
73005152 50 */
cab6c8ff 51 private Shell fToolTipShell = null;
df0b8ff4
BH
52 /**
53 * The text box.
54 */
cab6c8ff 55 private Text fTextBox = null;
73005152 56
df0b8ff4
BH
57 // ------------------------------------------------------------------------
58 // Constructors
59 // ------------------------------------------------------------------------
c8422608 60
73005152
BH
61 /**
62 * Create a new tooltip for the given parent control
c8422608 63 *
eb63f5ff 64 * @param parent the parent control.
73005152 65 */
eb63f5ff
BH
66 public DiagramToolTip(Control parent) {
67 fParent = parent;
68 fToolTipShell = new Shell(fParent.getShell(), SWT.MULTI);
69 fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
70 fTextBox = new Text(fToolTipShell, SWT.WRAP | SWT.MULTI);
71 fTextBox.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
73005152
BH
72 }
73
df0b8ff4
BH
74 // ------------------------------------------------------------------------
75 // Methods
76 // ------------------------------------------------------------------------
77
73005152
BH
78 /**
79 * Display the tooltip using the given text The tooltip will stay on screen until it is told otherwise
c8422608 80 *
73005152
BH
81 * @param value the text to display
82 */
83 public void showToolTip(String value) {
df0b8ff4 84 if ((value == null) || (value.equalsIgnoreCase(""))) { //$NON-NLS-1$
eb63f5ff 85 fToolTipShell.setVisible(false);
73005152
BH
86 return;
87 }
88
eb63f5ff 89 int w = fToolTipShell.getBounds().width;
73005152 90 Point hr = Display.getDefault().getCursorLocation();
f26e290b 91 int cursorH = DEFAULT_CURSOR_HEIGHT;
73005152 92 for (int i = 0; i < Display.getDefault().getCursorSizes().length; i++) {
df0b8ff4 93 if (Display.getDefault().getCursorSizes()[i].y < cursorH) {
73005152 94 cursorH = Display.getDefault().getCursorSizes()[i].y;
df0b8ff4 95 }
73005152
BH
96 }
97 if (hr.x + w > Display.getDefault().getBounds().width) {
98 int tempX = (hr.x + w) - Display.getDefault().getBounds().width;
df0b8ff4 99 if (tempX > Display.getDefault().getBounds().width) {
73005152 100 hr.x = 0;
df0b8ff4 101 }
73005152
BH
102 hr.x = hr.x - tempX;
103 }
eb63f5ff 104 fTextBox.setText(value);
eb63f5ff 105 GC gc = new GC(fTextBox);
73005152
BH
106 FontMetrics fm = gc.getFontMetrics();
107 gc.dispose();
f26e290b 108 int width = CHARACTERS_PER_COLUMN * fm.getAverageCharWidth();
eb63f5ff
BH
109 fTextBox.setSize(fTextBox.computeSize(width, fTextBox.getLineCount() * fTextBox.getLineHeight()));
110 fToolTipShell.setLocation(hr.x, hr.y + cursorH);
111 fToolTipShell.setSize(fTextBox.getSize());
112 fTextBox.setVisible(true);
113 fToolTipShell.setVisible(true);
73005152
BH
114 }
115
116 /**
117 * Hide the tooltip
118 */
119 public void hideToolTip() {
eb63f5ff 120 fToolTipShell.setVisible(false);
73005152
BH
121 }
122
cab6c8ff
BH
123 /**
124 * @return parent control
125 * @since 2.0
126 */
127 protected Control getParent() {
128 return fParent;
129 }
130
73005152 131}
This page took 0.072652 seconds and 5 git commands to generate.