--- /dev/null
+package org.simantics.browsing.ui.model.tooltips;\r
+\r
+import java.util.Map;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.FontMetrics;\r
+import org.eclipse.swt.graphics.GC;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class DescriptionTooltipRule implements TooltipRule {\r
+ \r
+ public static final DescriptionTooltipRule INSTANCE = new DescriptionTooltipRule();\r
+ \r
+ public DescriptionTooltipRule() {\r
+ }\r
+\r
+ @Override\r
+ public Object createTooltip(Object event, Object parentComponent, NodeContext context, Map<Object, Object> auxiliary) {\r
+ Composite parent = (Composite)parentComponent;\r
+ Composite composite = new Composite(parent, SWT.NONE);\r
+ //ScrolledComposite composite = new ScrolledComposite(parent, SWT.NONE);\r
+ \r
+ composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));\r
+ GridLayout layout = new GridLayout(1, false);\r
+ composite.setLayout(layout);\r
+ Text text = new Text(composite, SWT.NONE | SWT.READ_ONLY | SWT.WRAP);\r
+ text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));\r
+ \r
+ String toolTipContent = (String) auxiliary.get("content");\r
+ text.setText(toolTipContent);\r
+ \r
+ GC gc = new GC(text);\r
+ FontMetrics fm = gc.getFontMetrics();\r
+ int width = toolTipContent.length() * fm.getAverageCharWidth();\r
+ gc.dispose();\r
+ GridData gridData = new GridData();\r
+ if (width < 500)\r
+ gridData.widthHint = width;\r
+ else\r
+ gridData.widthHint = 500;\r
+ \r
+ text.setLayoutData(gridData);\r
+ \r
+ return composite;\r
+ }\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return (contentType == Resource.class || contentType == Variable.class);\r
+ }\r
+ \r
+ private static String getToolTipContent(ReadGraph graph, NodeContext nodeContext) throws DatabaseException {\r
+ Object input = nodeContext.getConstant(BuiltinKeys.INPUT);\r
+ String content = null;\r
+ if (input instanceof Variable) {\r
+ Variable var = (Variable) input;\r
+ Resource res = var.getPredicateResource(graph);\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ String description = graph.getPossibleRelatedValue2(res, L0.HasDescription);\r
+ return description;\r
+ } else if (input instanceof Resource) {\r
+ Resource res = (Resource) input;\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ String description = graph.getPossibleRelatedValue2(res, L0.HasDescription);\r
+ return description;\r
+ }\r
+ return content;\r
+ }\r
+\r
+ @Override\r
+ public boolean shouldCreateToolTip(ReadGraph graph , NodeContext context, Map<Object, Object> auxiliary) throws DatabaseException {\r
+ String content = getToolTipContent(graph, context);\r
+ if (content == null || content.isEmpty())\r
+ return false;\r
+ auxiliary.put("content", content);\r
+ return true;\r
+ }\r
+\r
+}\r