Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2a416e9c02601d179690200f9bfc134d4a6b73d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*******************************************************************************
 * Copyright (c) 2010 THALES GLOBAL SERVICES.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.editor.tabbar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ArmEvent;
import org.eclipse.swt.events.ArmListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.PlatformUI;

/**
 * A listener which listens when a menu item is armed to display a tooltip.
 * 
 * @author mchauvin
 */
public class MenuContributionItemArmListener implements ArmListener {

    private Control control;

    private Shell currentTooltip;

    /**
     * Create a new instance.
     * 
     * @param control
     *            the control
     */
    public MenuContributionItemArmListener(Control control) {
        this.control = control;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.swt.events.ArmListener#widgetArmed(org.eclipse.swt.events.ArmEvent)
     */
    public void widgetArmed(final ArmEvent e) {

        Widget widget = e.widget;
        if (widget instanceof MenuItem) {
            Object data = ((MenuItem) widget).getData(AbstractMenuContributionItem.TOOLTIP);
            if (data instanceof String) {
                createTooltip((String) data);
            }
        }
    }

    private void createTooltip(String tooltipText) {

        if (currentTooltip != null && !currentTooltip.isDisposed()) {
            currentTooltip.dispose();
        }
        Label label;
        final Shell tip = new Shell(control.getShell(), SWT.ON_TOP | SWT.TOOL);
        tip.setLayout(new FillLayout());
        label = new Label(tip, SWT.NONE);

        Display display = PlatformUI.getWorkbench().getDisplay();

        label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
        label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
        label.setText(tooltipText);

        Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        Point pt = PlatformUI.getWorkbench().getDisplay().getCursorLocation();
        int offset = 15;
        tip.setBounds(pt.x + offset, pt.y - offset, size.x, size.y);
        tip.setVisible(true);
        control.getDisplay().timerExec(3000, new Runnable() {
            public void run() {
                tip.dispose();
            }
        });
        currentTooltip = tip;
    }
}

Back to the top