]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/EditableTextTooltipProvider2.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / tooltip / EditableTextTooltipProvider2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.tooltip;
13
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.KeyAdapter;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.layout.FillLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24 import org.simantics.g2d.element.IElement;
25
26 public abstract class EditableTextTooltipProvider2 implements FocusableTooltipProvider {
27         
28         private Shell shell;
29         private Text text;
30         
31         @Override
32         public void showTooltip(final IElement element, final int x, final int y) {
33                 Display.getDefault().asyncExec(new Runnable() {
34                         @Override
35                         public void run() {
36                                 if (shell != null) {
37                                         shell.dispose();
38                                         shell = null;
39                                 }
40                                 shell = new Shell(Display.getCurrent().getActiveShell(),SWT.NO_TRIM);
41                                 shell.setLayout(new FillLayout());
42                                 Composite composite = new Composite(shell, SWT.BORDER);
43                                 composite.setLayout(new FillLayout(SWT.VERTICAL));
44                                 Label label = new Label(composite,SWT.NONE);
45                                 label.setText("Press 'F9' to focus");
46                                 text = new Text(composite, SWT.SINGLE);
47                                 text.setText(getTooltipText(element));
48                                 text.addKeyListener(new KeyAdapter() {
49                                         @Override
50                                         public void keyReleased(KeyEvent e) {
51                                                 if (e.keyCode == SWT.CR) {
52                                                         setText(element, text.getText());
53                                                         shell.dispose();
54                                                         shell = null;
55                                                 }
56                                         }
57                                 });
58                                 shell.pack();
59                                 shell.setVisible(true);
60                                 shell.setLocation(x, y-32);
61                                 
62                         }
63                 });
64                 
65         }
66         
67         @Override
68         public void hideTooltip(IElement element) {
69                 if (shell == null)
70                         return;
71                 Display.getDefault().asyncExec(new Runnable() {
72                         @Override
73                         public void run() {
74                                 shell.dispose();
75                                 shell = null;
76
77                         }
78                 });
79         }
80         
81         public void focus() {
82                 Display.getDefault().asyncExec(new Runnable() {
83                         @Override
84                         public void run() {
85                                 if (!shell.isDisposed())
86                                         shell.forceFocus();
87                         }
88                 });
89         }
90         boolean has_focus = false;
91         
92         @Override
93         public boolean hasFocus() {
94                 
95                 Display.getDefault().syncExec(new Runnable() {
96                         
97                         @Override
98                         public void run() {
99                                 if(shell != null && !shell.isDisposed())
100                                         has_focus = shell.isFocusControl() || text.isFocusControl();
101                                 
102                                 else
103                                         has_focus = false;
104                                 
105                         }
106                 });
107                 return has_focus;
108         }
109         
110         public abstract String getTooltipText(IElement element);
111         
112         public abstract void setText(IElement element, String text);
113
114 }