]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
0bfe1d6546b654fdc59d34f2abb090c191af355e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management in\r
3  * 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  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.trend;\r
13 \r
14 import org.eclipse.jface.layout.GridDataFactory;\r
15 import org.eclipse.jface.layout.GridLayoutFactory;\r
16 import org.eclipse.jface.viewers.ISelectionProvider;\r
17 import org.eclipse.jface.viewers.IStructuredSelection;\r
18 import org.eclipse.swt.SWT;\r
19 import org.eclipse.swt.custom.ScrolledComposite;\r
20 import org.eclipse.swt.events.SelectionAdapter;\r
21 import org.eclipse.swt.events.SelectionEvent;\r
22 import org.eclipse.swt.graphics.Point;\r
23 import org.eclipse.swt.widgets.Composite;\r
24 import org.eclipse.swt.widgets.Control;\r
25 import org.eclipse.swt.widgets.Tree;\r
26 import org.eclipse.ui.IWorkbenchSite;\r
27 import org.simantics.browsing.ui.swt.SingleSelectionInputSource;\r
28 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
29 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
30 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
31 import org.simantics.db.ReadGraph;\r
32 import org.simantics.db.Resource;\r
33 import org.simantics.db.exception.DatabaseException;\r
34 import org.simantics.db.management.ISessionContext;\r
35 import org.simantics.db.request.Read;\r
36 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;\r
37 import org.simantics.jfreechart.chart.properties.xyline.AxisAndVariablesExplorerComposite;\r
38 import org.simantics.jfreechart.chart.properties.xyline.AxisPropertyComposite;\r
39 import org.simantics.jfreechart.chart.properties.xyline.SeriesPropertyComposite;\r
40 import org.simantics.sysdyn.JFreeChartResource;\r
41 import org.simantics.ui.SimanticsUI;\r
42 import org.simantics.utils.datastructures.ArrayMap;\r
43 import org.simantics.utils.ui.AdaptionUtils;\r
44 \r
45 public class SensitivityChartAxisAndVariablesTab  extends LabelPropertyTabContributor {\r
46 \r
47     private GraphExplorerComposite explorer;\r
48     private ScrolledComposite propertyContainer;\r
49     private WidgetSupportImpl additionalSupport;\r
50 \r
51     public SensitivityChartAxisAndVariablesTab() {\r
52         additionalSupport = new WidgetSupportImpl();\r
53     }\r
54 \r
55     @Override\r
56     public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support) {\r
57         Composite composite = new Composite(body, SWT.NONE);\r
58         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
59         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
60 \r
61         // (Ontology-based) GraphExplorer displaying range axis and variables mapped to those axis\r
62         explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys(\r
63                 "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);\r
64         explorer.setBrowseContexts(JFreeChartResource.URIs.ChartAxisAndVariablesBrowseContext);\r
65         explorer.setInputSource(new SingleSelectionInputSource(\r
66                 Resource.class));\r
67         explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning\r
68         explorer.finish();\r
69 \r
70         ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {\r
71             public void widgetSelected(SelectionEvent e) {\r
72                 updateSelection(context);\r
73             }\r
74         });\r
75         GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
76 \r
77         // Scrolled composite for displaying properties of a selection in explorer\r
78         propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);\r
79         GridDataFactory.fillDefaults().span(1, 2).grab(true, true).applyTo(propertyContainer);\r
80         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
81         propertyContainer.setExpandHorizontal(true);\r
82         propertyContainer.setExpandVertical(true);\r
83 \r
84     }\r
85 \r
86     /**\r
87      * Updates the content of propertyContainer  \r
88      * @param context\r
89      */\r
90     private void updateSelection(ISessionContext context) {\r
91         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);\r
92         IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();\r
93         final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
94         if(resource == null)\r
95             return;\r
96 \r
97         // Get the type of the selected node (axis or series)\r
98         String typeUri = null;\r
99         try {\r
100             typeUri = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
101 \r
102                 @Override\r
103                 public String perform(ReadGraph graph) throws DatabaseException {\r
104                     JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
105                     if(graph.isInstanceOf(resource, jfree.Axis))\r
106                         return graph.getURI(jfree.Axis);\r
107                     else if (graph.isInstanceOf(resource, jfree.Series))\r
108                         return graph.getURI(jfree.Series);\r
109                     return null;\r
110                 }\r
111 \r
112             });\r
113         } catch (DatabaseException e) {\r
114             e.printStackTrace();\r
115         }\r
116 \r
117         // Create a PropertyComposite for the selected node\r
118         if(typeUri != null) {\r
119 \r
120             for(Control child : propertyContainer.getChildren()) {\r
121                 child.dispose();\r
122             }\r
123 \r
124             if(typeUri.equals(JFreeChartResource.URIs.Axis)) {\r
125                 AxisPropertyComposite apc = new AxisPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE);\r
126                 propertyContainer.setContent(apc);\r
127                 Point size = apc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
128                 propertyContainer.setMinSize(size);\r
129             } else if(typeUri.equals(JFreeChartResource.URIs.Series)) {\r
130                 SeriesPropertyComposite spc = new SensitivitySeriesPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE);\r
131                 propertyContainer.setContent(spc);\r
132                 Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
133                 propertyContainer.setMinSize(size);\r
134             }\r
135         }\r
136 \r
137         additionalSupport.fireInput(context, selection);\r
138     }\r
139 \r
140 \r
141 }\r