1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.tooltip;
\r
15 import org.eclipse.swt.SWT;
\r
16 import org.eclipse.swt.events.KeyAdapter;
\r
17 import org.eclipse.swt.events.KeyEvent;
\r
18 import org.eclipse.swt.layout.FillLayout;
\r
19 import org.eclipse.swt.widgets.Composite;
\r
20 import org.eclipse.swt.widgets.Display;
\r
21 import org.eclipse.swt.widgets.Label;
\r
22 import org.eclipse.swt.widgets.Shell;
\r
23 import org.eclipse.swt.widgets.Text;
\r
24 import org.simantics.g2d.element.IElement;
\r
26 public abstract class EditableTextTooltipProvider2 implements FocusableTooltipProvider {
\r
28 private Shell shell;
\r
32 public void showTooltip(final IElement element, final int x, final int y) {
\r
33 Display.getDefault().asyncExec(new Runnable() {
\r
36 if (shell != null) {
\r
40 shell = new Shell(Display.getCurrent().getActiveShell(),SWT.NO_TRIM);
\r
41 shell.setLayout(new FillLayout());
\r
42 Composite composite = new Composite(shell, SWT.BORDER);
\r
43 composite.setLayout(new FillLayout(SWT.VERTICAL));
\r
44 Label label = new Label(composite,SWT.NONE);
\r
45 label.setText("Press 'F9' to focus");
\r
46 text = new Text(composite, SWT.SINGLE);
\r
47 text.setText(getTooltipText(element));
\r
48 text.addKeyListener(new KeyAdapter() {
\r
50 public void keyReleased(KeyEvent e) {
\r
51 if (e.keyCode == SWT.CR) {
\r
52 setText(element, text.getText());
\r
59 shell.setVisible(true);
\r
60 shell.setLocation(x, y-32);
\r
68 public void hideTooltip(IElement element) {
\r
71 Display.getDefault().asyncExec(new Runnable() {
\r
81 public void focus() {
\r
82 Display.getDefault().asyncExec(new Runnable() {
\r
85 if (!shell.isDisposed())
\r
90 boolean has_focus = false;
\r
93 public boolean hasFocus() {
\r
95 Display.getDefault().syncExec(new Runnable() {
\r
99 if(shell != null && !shell.isDisposed())
\r
100 has_focus = shell.isFocusControl() || text.isFocusControl();
\r
110 public abstract String getTooltipText(IElement element);
\r
112 public abstract void setText(IElement element, String text);
\r