Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2007 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
4 | * | |
73005152 BH |
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 | |
73005152 BH |
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 | /** | |
df0b8ff4 | 26 | * <p> |
73005152 BH |
27 | * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse hovers over the |
28 | * sequence diagram | |
df0b8ff4 | 29 | * </p> |
73005152 | 30 | * |
df0b8ff4 | 31 | * @version 1.0 |
73005152 BH |
32 | * @author sveyrier |
33 | */ | |
34 | public class DiagramToolTip { | |
35 | ||
df0b8ff4 BH |
36 | // ------------------------------------------------------------------------ |
37 | // Attributes | |
38 | // ------------------------------------------------------------------------ | |
73005152 | 39 | /** |
df0b8ff4 | 40 | * The parent control where the tooltip must be drawn. |
73005152 | 41 | */ |
eb63f5ff | 42 | protected Control fParent = null; |
73005152 | 43 | /** |
df0b8ff4 | 44 | * The tooltip shell. |
73005152 | 45 | */ |
eb63f5ff | 46 | protected Shell fToolTipShell = null; |
73005152 | 47 | /** |
df0b8ff4 | 48 | * The tooltip text. |
73005152 | 49 | */ |
eb63f5ff | 50 | protected String fText = null; |
df0b8ff4 BH |
51 | /** |
52 | * The text box. | |
53 | */ | |
eb63f5ff | 54 | protected Text fTextBox = null; |
73005152 | 55 | |
df0b8ff4 BH |
56 | // ------------------------------------------------------------------------ |
57 | // Constructors | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
73005152 BH |
60 | /** |
61 | * Create a new tooltip for the given parent control | |
62 | * | |
eb63f5ff | 63 | * @param parent the parent control. |
73005152 | 64 | */ |
eb63f5ff BH |
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)); | |
73005152 BH |
71 | } |
72 | ||
df0b8ff4 BH |
73 | // ------------------------------------------------------------------------ |
74 | // Methods | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
73005152 BH |
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) { | |
df0b8ff4 | 83 | if ((value == null) || (value.equalsIgnoreCase(""))) { //$NON-NLS-1$ |
eb63f5ff | 84 | fToolTipShell.setVisible(false); |
73005152 BH |
85 | return; |
86 | } | |
87 | ||
eb63f5ff BH |
88 | fText = value; |
89 | int w = fToolTipShell.getBounds().width; | |
73005152 BH |
90 | Point hr = Display.getDefault().getCursorLocation(); |
91 | int cursorH = 32; | |
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); |
73005152 | 105 | int charactersPerColumn = 100; |
eb63f5ff | 106 | GC gc = new GC(fTextBox); |
73005152 BH |
107 | FontMetrics fm = gc.getFontMetrics(); |
108 | gc.dispose(); | |
109 | int width = charactersPerColumn * fm.getAverageCharWidth(); | |
eb63f5ff BH |
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); | |
73005152 BH |
115 | |
116 | } | |
117 | ||
118 | /** | |
119 | * Hide the tooltip | |
120 | */ | |
121 | public void hideToolTip() { | |
eb63f5ff | 122 | fToolTipShell.setVisible(false); |
73005152 BH |
123 | } |
124 | ||
125 | } |