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