]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/EditableTextTooltipProvider2.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/EditableTextTooltipProvider2.java
new file mode 100644 (file)
index 0000000..3b68fac
--- /dev/null
@@ -0,0 +1,114 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.tooltip;\r
+\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.g2d.element.IElement;\r
+\r
+public abstract class EditableTextTooltipProvider2 implements FocusableTooltipProvider {\r
+       \r
+       private Shell shell;\r
+       private Text text;\r
+       \r
+       @Override\r
+       public void showTooltip(final IElement element, final int x, final int y) {\r
+               Display.getDefault().asyncExec(new Runnable() {\r
+                       @Override\r
+                       public void run() {\r
+                               if (shell != null) {\r
+                                       shell.dispose();\r
+                                       shell = null;\r
+                               }\r
+                               shell = new Shell(Display.getCurrent().getActiveShell(),SWT.NO_TRIM);\r
+                               shell.setLayout(new FillLayout());\r
+                               Composite composite = new Composite(shell, SWT.BORDER);\r
+                               composite.setLayout(new FillLayout(SWT.VERTICAL));\r
+                               Label label = new Label(composite,SWT.NONE);\r
+                               label.setText("Press 'F9' to focus");\r
+                               text = new Text(composite, SWT.SINGLE);\r
+                               text.setText(getTooltipText(element));\r
+                               text.addKeyListener(new KeyAdapter() {\r
+                                       @Override\r
+                                       public void keyReleased(KeyEvent e) {\r
+                                               if (e.keyCode == SWT.CR) {\r
+                                                       setText(element, text.getText());\r
+                                                       shell.dispose();\r
+                                                       shell = null;\r
+                                               }\r
+                                       }\r
+                               });\r
+                               shell.pack();\r
+                               shell.setVisible(true);\r
+                               shell.setLocation(x, y-32);\r
+                               \r
+                       }\r
+               });\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public void hideTooltip(IElement element) {\r
+               if (shell == null)\r
+                       return;\r
+               Display.getDefault().asyncExec(new Runnable() {\r
+                       @Override\r
+                       public void run() {\r
+                               shell.dispose();\r
+                               shell = null;\r
+\r
+                       }\r
+               });\r
+       }\r
+       \r
+       public void focus() {\r
+               Display.getDefault().asyncExec(new Runnable() {\r
+                       @Override\r
+                       public void run() {\r
+                               if (!shell.isDisposed())\r
+                                       shell.forceFocus();\r
+                       }\r
+               });\r
+       }\r
+       boolean has_focus = false;\r
+       \r
+       @Override\r
+       public boolean hasFocus() {\r
+               \r
+               Display.getDefault().syncExec(new Runnable() {\r
+                       \r
+                       @Override\r
+                       public void run() {\r
+                               if(shell != null && !shell.isDisposed())\r
+                                       has_focus = shell.isFocusControl() || text.isFocusControl();\r
+                               \r
+                               else\r
+                                       has_focus = false;\r
+                               \r
+                       }\r
+               });\r
+               return has_focus;\r
+       }\r
+       \r
+       public abstract String getTooltipText(IElement element);\r
+       \r
+       public abstract void setText(IElement element, String text);\r
+\r
+}\r