1 package org.simantics.browsing.ui.model.tooltips;
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.graphics.FontMetrics;
7 import org.eclipse.swt.graphics.GC;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Text;
12 import org.simantics.browsing.ui.BuiltinKeys;
13 import org.simantics.browsing.ui.NodeContext;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.layer0.Layer0;
20 public class DescriptionTooltipRule implements TooltipRule {
22 public static final DescriptionTooltipRule INSTANCE = new DescriptionTooltipRule();
24 public DescriptionTooltipRule() {
28 public Object createTooltip(Object event, Object parentComponent, NodeContext context, Map<Object, Object> auxiliary) {
29 Composite parent = (Composite)parentComponent;
30 Composite composite = new Composite(parent, SWT.NONE);
31 //ScrolledComposite composite = new ScrolledComposite(parent, SWT.NONE);
33 composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
34 GridLayout layout = new GridLayout(1, false);
35 composite.setLayout(layout);
36 Text text = new Text(composite, SWT.NONE | SWT.READ_ONLY | SWT.WRAP);
37 text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
39 String toolTipContent = (String) auxiliary.get("content");
40 text.setText(toolTipContent);
43 FontMetrics fm = gc.getFontMetrics();
44 int width = toolTipContent.length() * fm.getAverageCharWidth();
46 GridData gridData = new GridData();
48 gridData.widthHint = width;
50 gridData.widthHint = 500;
52 text.setLayoutData(gridData);
58 public boolean isCompatible(Class<?> contentType) {
59 return (contentType == Resource.class || contentType == Variable.class);
62 private static String getToolTipContent(ReadGraph graph, NodeContext nodeContext) throws DatabaseException {
63 Object input = nodeContext.getConstant(BuiltinKeys.INPUT);
64 String content = null;
65 if (input instanceof Variable) {
66 Variable var = (Variable) input;
67 Resource res = var.getPredicateResource(graph);
68 Layer0 L0 = Layer0.getInstance(graph);
69 String description = graph.getPossibleRelatedValue2(res, L0.HasDescription);
71 } else if (input instanceof Resource) {
72 Resource res = (Resource) input;
74 Layer0 L0 = Layer0.getInstance(graph);
75 String description = graph.getPossibleRelatedValue2(res, L0.HasDescription);
82 public boolean shouldCreateToolTip(ReadGraph graph , NodeContext context, Map<Object, Object> auxiliary) throws DatabaseException {
83 String content = getToolTipContent(graph, context);
84 if (content == null || content.isEmpty())
86 auxiliary.put("content", content);