]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/EditableTextTooltipProvider2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / tooltip / EditableTextTooltipProvider2.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.tooltip;\r
13 \r
14 \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
25 \r
26 public abstract class EditableTextTooltipProvider2 implements FocusableTooltipProvider {\r
27         \r
28         private Shell shell;\r
29         private Text text;\r
30         \r
31         @Override\r
32         public void showTooltip(final IElement element, final int x, final int y) {\r
33                 Display.getDefault().asyncExec(new Runnable() {\r
34                         @Override\r
35                         public void run() {\r
36                                 if (shell != null) {\r
37                                         shell.dispose();\r
38                                         shell = null;\r
39                                 }\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
49                                         @Override\r
50                                         public void keyReleased(KeyEvent e) {\r
51                                                 if (e.keyCode == SWT.CR) {\r
52                                                         setText(element, text.getText());\r
53                                                         shell.dispose();\r
54                                                         shell = null;\r
55                                                 }\r
56                                         }\r
57                                 });\r
58                                 shell.pack();\r
59                                 shell.setVisible(true);\r
60                                 shell.setLocation(x, y-32);\r
61                                 \r
62                         }\r
63                 });\r
64                 \r
65         }\r
66         \r
67         @Override\r
68         public void hideTooltip(IElement element) {\r
69                 if (shell == null)\r
70                         return;\r
71                 Display.getDefault().asyncExec(new Runnable() {\r
72                         @Override\r
73                         public void run() {\r
74                                 shell.dispose();\r
75                                 shell = null;\r
76 \r
77                         }\r
78                 });\r
79         }\r
80         \r
81         public void focus() {\r
82                 Display.getDefault().asyncExec(new Runnable() {\r
83                         @Override\r
84                         public void run() {\r
85                                 if (!shell.isDisposed())\r
86                                         shell.forceFocus();\r
87                         }\r
88                 });\r
89         }\r
90         boolean has_focus = false;\r
91         \r
92         @Override\r
93         public boolean hasFocus() {\r
94                 \r
95                 Display.getDefault().syncExec(new Runnable() {\r
96                         \r
97                         @Override\r
98                         public void run() {\r
99                                 if(shell != null && !shell.isDisposed())\r
100                                         has_focus = shell.isFocusControl() || text.isFocusControl();\r
101                                 \r
102                                 else\r
103                                         has_focus = false;\r
104                                 \r
105                         }\r
106                 });\r
107                 return has_focus;\r
108         }\r
109         \r
110         public abstract String getTooltipText(IElement element);\r
111         \r
112         public abstract void setText(IElement element, String text);\r
113 \r
114 }\r