]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
c414bf94bfbeaa01fb5fa421480b20ba191e1a79
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2013, 2014 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.AdjustableTab;\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 AdjustableTab {\r
46 \r
47     private GraphExplorerComposite explorer;\r
48     private ScrolledComposite propertyContainer;\r
49     private WidgetSupportImpl additionalSupport;\r
50 \r
51     public SensitivityChartAxisAndVariablesTab(Object id) {\r
52         super(id);\r
53         additionalSupport = new WidgetSupportImpl();\r
54     }\r
55 \r
56     /**\r
57      * Updates the content of propertyContainer  \r
58      * @param context\r
59      */\r
60     private void updateSelection(ISessionContext context) {\r
61         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);\r
62         IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();\r
63         final Resource resource = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
64         if(resource == null)\r
65             return;\r
66 \r
67         // Get the type of the selected node (axis or series)\r
68         String typeUri = null;\r
69         try {\r
70             typeUri = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
71 \r
72                 @Override\r
73                 public String perform(ReadGraph graph) throws DatabaseException {\r
74                     JFreeChartResource jfree = JFreeChartResource.getInstance(graph);\r
75                     if(graph.isInstanceOf(resource, jfree.Axis))\r
76                         return graph.getURI(jfree.Axis);\r
77                     else if (graph.isInstanceOf(resource, jfree.Series))\r
78                         return graph.getURI(jfree.Series);\r
79                     return null;\r
80                 }\r
81 \r
82             });\r
83         } catch (DatabaseException e) {\r
84             e.printStackTrace();\r
85         }\r
86 \r
87         // Create a PropertyComposite for the selected node\r
88         if(typeUri != null) {\r
89 \r
90             for(Control child : propertyContainer.getChildren()) {\r
91                 child.dispose();\r
92             }\r
93 \r
94             if(typeUri.equals(JFreeChartResource.URIs.Axis)) {\r
95                 AxisPropertyComposite apc = new AxisPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE, isVertical());\r
96                 propertyContainer.setContent(apc);\r
97                 Point size = apc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
98                 propertyContainer.setMinSize(size);\r
99             } else if(typeUri.equals(JFreeChartResource.URIs.Series)) {\r
100                 SeriesPropertyComposite spc = new SensitivitySeriesPropertyComposite(propertyContainer, context, additionalSupport, SWT.NONE);\r
101                 propertyContainer.setContent(spc);\r
102                 Point size = spc.computeSize(SWT.DEFAULT, SWT.DEFAULT);\r
103                 propertyContainer.setMinSize(size);\r
104             }\r
105         }\r
106 \r
107         additionalSupport.fireInput(context, selection);\r
108     }\r
109 \r
110         @Override\r
111         protected void createAndAddControls(Composite body, IWorkbenchSite site,\r
112                         final ISessionContext context, WidgetSupport support) {\r
113                 composite = new Composite(body, SWT.NONE);\r
114 \r
115         // (Ontology-based) GraphExplorer displaying range axis and variables mapped to those axis\r
116         explorer = new AxisAndVariablesExplorerComposite(ArrayMap.keys(\r
117                 "displaySelectors", "displayFilter").values(false, false), site, composite, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);\r
118         explorer.setBrowseContexts(JFreeChartResource.URIs.ChartAxisAndVariablesBrowseContext);\r
119         explorer.setInputSource(new SingleSelectionInputSource(\r
120                 Resource.class));\r
121         explorer.getExplorer().setAutoExpandLevel(2); // Expand everything in the beginning\r
122         explorer.finish();\r
123 \r
124         ((Tree)explorer.getExplorerControl()).addSelectionListener(new SelectionAdapter() {\r
125             public void widgetSelected(SelectionEvent e) {\r
126                 updateSelection(context);\r
127             }\r
128         });\r
129 \r
130         // Scrolled composite for displaying properties of a selection in explorer\r
131         propertyContainer = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);\r
132         propertyContainer.setExpandHorizontal(true);\r
133         propertyContainer.setExpandVertical(true);\r
134 \r
135         }\r
136 \r
137         @Override\r
138         protected void createControlLayoutVertical() {\r
139         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
140         GridLayoutFactory.fillDefaults().numColumns(1).margins(3, 3).applyTo(composite);\r
141 \r
142         GridDataFactory.fillDefaults().hint(220, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
143 \r
144         // Scrolled composite for displaying properties of a selection in explorer\r
145         GridDataFactory.fillDefaults().span(1, 1).hint(SWT.DEFAULT, 210).grab(true, false).applyTo(propertyContainer);\r
146         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
147         }\r
148 \r
149         @Override\r
150         protected void createControlLayoutHorizontal(boolean wideScreen) {\r
151         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
152         GridLayoutFactory.fillDefaults().numColumns(2).margins(3, 3).applyTo(composite);\r
153 \r
154         GridDataFactory.fillDefaults().hint(250, SWT.DEFAULT).grab(false, true).applyTo(explorer);\r
155 \r
156         // Scrolled composite for displaying properties of a selection in explorer\r
157         GridDataFactory.fillDefaults().span(1, 2).hint(SWT.DEFAULT, SWT.DEFAULT).grab(true, true).applyTo(propertyContainer);\r
158         GridLayoutFactory.fillDefaults().applyTo(propertyContainer);\r
159         }\r
160 \r
161 \r
162 }\r