Commit | Line | Data |
---|---|---|
73005152 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2005, 2007, 2011 IBM Corporation and others. | |
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 | * $Id: DiagramToolTip.java,v 1.3 2007/01/02 21:11:39 ewchan Exp $ | |
8 | * | |
9 | * Contributors: | |
10 | * IBM - Initial API and implementation | |
11 | * Bernd Hufmann - Updated for TMF | |
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 | * This class is used to reproduce the same tooltip behavior on Windows and Linux when the mouse hovers over the | |
26 | * sequence diagram | |
27 | * | |
28 | * @author sveyrier | |
29 | */ | |
30 | public class DiagramToolTip { | |
31 | ||
32 | /** | |
33 | * The parent control where the tooltip must be drawn | |
34 | */ | |
35 | protected Control parent = null; | |
36 | /** | |
37 | * The tooltip shell | |
38 | */ | |
39 | protected Shell toolTipShell = null; | |
40 | /** | |
41 | * The tooltip text | |
42 | */ | |
43 | protected String text = null; | |
44 | protected Text textBox = null; | |
45 | ||
46 | // added for defect 141750 in order to allow proper text wrapping of the toolTip | |
47 | // E. Dancy | |
48 | ||
49 | /** | |
50 | * Create a new tooltip for the given parent control | |
51 | * | |
52 | * @param _parent the parent control | |
53 | */ | |
54 | public DiagramToolTip(Control _parent) { | |
55 | parent = _parent; | |
56 | toolTipShell = new Shell(parent.getShell(), SWT.MULTI); | |
57 | toolTipShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
58 | textBox = new Text(toolTipShell, SWT.WRAP | SWT.MULTI); | |
59 | textBox.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Display the tooltip using the given text The tooltip will stay on screen until it is told otherwise | |
64 | * | |
65 | * @param value the text to display | |
66 | */ | |
67 | public void showToolTip(String value) { | |
68 | if ((value == null) || (value.equalsIgnoreCase("")))//$NON-NLS-1$ | |
69 | { | |
70 | toolTipShell.setVisible(false); | |
71 | return; | |
72 | } | |
73 | ||
74 | text = value; | |
75 | int w = toolTipShell.getBounds().width; | |
76 | Point hr = Display.getDefault().getCursorLocation(); | |
77 | int cursorH = 32; | |
78 | for (int i = 0; i < Display.getDefault().getCursorSizes().length; i++) { | |
79 | if (Display.getDefault().getCursorSizes()[i].y < cursorH) | |
80 | cursorH = Display.getDefault().getCursorSizes()[i].y; | |
81 | } | |
82 | if (hr.x + w > Display.getDefault().getBounds().width) { | |
83 | int tempX = (hr.x + w) - Display.getDefault().getBounds().width; | |
84 | if (tempX > Display.getDefault().getBounds().width) | |
85 | hr.x = 0; | |
86 | hr.x = hr.x - tempX; | |
87 | } | |
88 | textBox.setText(value); | |
89 | int charactersPerColumn = 100; | |
90 | GC gc = new GC(textBox); | |
91 | FontMetrics fm = gc.getFontMetrics(); | |
92 | gc.dispose(); | |
93 | int width = charactersPerColumn * fm.getAverageCharWidth(); | |
94 | textBox.setSize(textBox.computeSize(width, textBox.getLineCount() * textBox.getLineHeight())); | |
95 | toolTipShell.setLocation(hr.x, hr.y + cursorH); | |
96 | toolTipShell.setSize(textBox.getSize()); | |
97 | textBox.setVisible(true); | |
98 | toolTipShell.setVisible(true); | |
99 | ||
100 | } | |
101 | ||
102 | /** | |
103 | * Hide the tooltip | |
104 | */ | |
105 | public void hideToolTip() { | |
106 | toolTipShell.setVisible(false); | |
107 | } | |
108 | ||
109 | } |