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