Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
c8422608 | 2 | * Copyright (c) 2005, 2012 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 |
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 | /** | |
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 | */ | |
33 | public class DiagramToolTip { | |
34 | ||
df0b8ff4 BH |
35 | // ------------------------------------------------------------------------ |
36 | // Attributes | |
37 | // ------------------------------------------------------------------------ | |
73005152 | 38 | /** |
df0b8ff4 | 39 | * The parent control where the tooltip must be drawn. |
73005152 | 40 | */ |
eb63f5ff | 41 | protected Control fParent = null; |
73005152 | 42 | /** |
df0b8ff4 | 43 | * The tooltip shell. |
73005152 | 44 | */ |
eb63f5ff | 45 | protected Shell fToolTipShell = null; |
73005152 | 46 | /** |
df0b8ff4 | 47 | * The tooltip text. |
73005152 | 48 | */ |
eb63f5ff | 49 | protected String fText = null; |
df0b8ff4 BH |
50 | /** |
51 | * The text box. | |
52 | */ | |
eb63f5ff | 53 | protected Text fTextBox = null; |
73005152 | 54 | |
df0b8ff4 BH |
55 | // ------------------------------------------------------------------------ |
56 | // Constructors | |
57 | // ------------------------------------------------------------------------ | |
c8422608 | 58 | |
73005152 BH |
59 | /** |
60 | * Create a new tooltip for the given parent control | |
c8422608 | 61 | * |
eb63f5ff | 62 | * @param parent the parent control. |
73005152 | 63 | */ |
eb63f5ff BH |
64 | public DiagramToolTip(Control parent) { |
65 | fParent = parent; | |
66 | fToolTipShell = new Shell(fParent.getShell(), SWT.MULTI); | |
67 | fToolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
68 | fTextBox = new Text(fToolTipShell, SWT.WRAP | SWT.MULTI); | |
69 | fTextBox.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
73005152 BH |
70 | } |
71 | ||
df0b8ff4 BH |
72 | // ------------------------------------------------------------------------ |
73 | // Methods | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
73005152 BH |
76 | /** |
77 | * Display the tooltip using the given text The tooltip will stay on screen until it is told otherwise | |
c8422608 | 78 | * |
73005152 BH |
79 | * @param value the text to display |
80 | */ | |
81 | public void showToolTip(String value) { | |
df0b8ff4 | 82 | if ((value == null) || (value.equalsIgnoreCase(""))) { //$NON-NLS-1$ |
eb63f5ff | 83 | fToolTipShell.setVisible(false); |
73005152 BH |
84 | return; |
85 | } | |
86 | ||
eb63f5ff BH |
87 | fText = value; |
88 | int w = fToolTipShell.getBounds().width; | |
73005152 BH |
89 | Point hr = Display.getDefault().getCursorLocation(); |
90 | int cursorH = 32; | |
91 | for (int i = 0; i < Display.getDefault().getCursorSizes().length; i++) { | |
df0b8ff4 | 92 | if (Display.getDefault().getCursorSizes()[i].y < cursorH) { |
73005152 | 93 | cursorH = Display.getDefault().getCursorSizes()[i].y; |
df0b8ff4 | 94 | } |
73005152 BH |
95 | } |
96 | if (hr.x + w > Display.getDefault().getBounds().width) { | |
97 | int tempX = (hr.x + w) - Display.getDefault().getBounds().width; | |
df0b8ff4 | 98 | if (tempX > Display.getDefault().getBounds().width) { |
73005152 | 99 | hr.x = 0; |
df0b8ff4 | 100 | } |
73005152 BH |
101 | hr.x = hr.x - tempX; |
102 | } | |
eb63f5ff | 103 | fTextBox.setText(value); |
73005152 | 104 | int charactersPerColumn = 100; |
eb63f5ff | 105 | GC gc = new GC(fTextBox); |
73005152 BH |
106 | FontMetrics fm = gc.getFontMetrics(); |
107 | gc.dispose(); | |
108 | int width = charactersPerColumn * 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 | /** | |
118 | * Hide the tooltip | |
119 | */ | |
120 | public void hideToolTip() { | |
eb63f5ff | 121 | fToolTipShell.setVisible(false); |
73005152 BH |
122 | } |
123 | ||
124 | } |