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