tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / DiagramToolTip.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.FontMetrics;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.swt.widgets.Text;
23
24 /**
25 * <p>
26 * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse hovers over the
27 * sequence diagram
28 * </p>
29 *
30 * @version 1.0
31 * @author sveyrier
32 */
33 public class DiagramToolTip {
34
35 // ------------------------------------------------------------------------
36 // Constants
37 // ------------------------------------------------------------------------
38 private static final int CHARACTERS_PER_COLUMN = 100;
39 private static final int DEFAULT_CURSOR_HEIGHT = 32;
40
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
44 /**
45 * The parent control where the tooltip must be drawn.
46 */
47 private Control fParent = null;
48 /**
49 * The tooltip shell.
50 */
51 private Shell fToolTipShell = null;
52 /**
53 * The text box.
54 */
55 private Text fTextBox = null;
56
57 // ------------------------------------------------------------------------
58 // Constructors
59 // ------------------------------------------------------------------------
60
61 /**
62 * Create a new tooltip for the given parent control
63 *
64 * @param parent the parent control.
65 */
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));
72 }
73
74 // ------------------------------------------------------------------------
75 // Methods
76 // ------------------------------------------------------------------------
77
78 /**
79 * Display the tooltip using the given text The tooltip will stay on screen until it is told otherwise
80 *
81 * @param value the text to display
82 */
83 public void showToolTip(String value) {
84 if ((value == null) || (value.equalsIgnoreCase(""))) { //$NON-NLS-1$
85 fToolTipShell.setVisible(false);
86 return;
87 }
88
89 int w = fToolTipShell.getBounds().width;
90 Point hr = Display.getDefault().getCursorLocation();
91 int cursorH = DEFAULT_CURSOR_HEIGHT;
92 for (int i = 0; i < Display.getDefault().getCursorSizes().length; i++) {
93 if (Display.getDefault().getCursorSizes()[i].y < cursorH) {
94 cursorH = Display.getDefault().getCursorSizes()[i].y;
95 }
96 }
97 if (hr.x + w > Display.getDefault().getBounds().width) {
98 int tempX = (hr.x + w) - Display.getDefault().getBounds().width;
99 if (tempX > Display.getDefault().getBounds().width) {
100 hr.x = 0;
101 }
102 hr.x = hr.x - tempX;
103 }
104 fTextBox.setText(value);
105 GC gc = new GC(fTextBox);
106 FontMetrics fm = gc.getFontMetrics();
107 gc.dispose();
108 int width = CHARACTERS_PER_COLUMN * fm.getAverageCharWidth();
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);
114 }
115
116 /**
117 * Hide the tooltip
118 */
119 public void hideToolTip() {
120 fToolTipShell.setVisible(false);
121 }
122
123 /**
124 * @return parent control
125 * @since 2.0
126 */
127 protected Control getParent() {
128 return fParent;
129 }
130
131 }
This page took 0.03939 seconds and 5 git commands to generate.