]> gerrit.simantics Code Review - simantics/platform.git/blob
7bef8e22c127bf00946f7c35f92c8c3f48bcb403
[simantics/platform.git] /
1 package org.simantics.browsing.ui.model.tooltips;
2
3 import java.util.Map;
4
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;
19
20 public class DescriptionTooltipRule implements TooltipRule {
21     
22     public static final DescriptionTooltipRule INSTANCE = new DescriptionTooltipRule();
23     
24     public DescriptionTooltipRule() {
25     }
26
27     @Override
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);
32         
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));
38         
39         String toolTipContent = (String) auxiliary.get("content");
40         text.setText(toolTipContent);
41         
42         GC gc = new GC(text);
43         FontMetrics fm = gc.getFontMetrics();
44         int width = toolTipContent.length() * fm.getAverageCharWidth();
45         gc.dispose();
46         GridData gridData = new GridData();
47         if (width < 500)
48             gridData.widthHint = width;
49         else
50             gridData.widthHint = 500;
51         
52         text.setLayoutData(gridData);
53         
54         return composite;
55     }
56
57     @Override
58     public boolean isCompatible(Class<?> contentType) {
59         return (contentType == Resource.class || contentType == Variable.class);
60     }
61     
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);
70             return description;
71         } else if (input instanceof Resource) {
72             Resource res = (Resource) input;
73
74             Layer0 L0 = Layer0.getInstance(graph);
75             String description = graph.getPossibleRelatedValue2(res, L0.HasDescription);
76             return description;
77         }
78         return content;
79     }
80
81     @Override
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())
85             return false;
86         auxiliary.put("content", content);
87         return true;
88     }
89
90 }