1 package org.simantics.views;
3 import org.eclipse.jface.viewers.ISelection;
4 import org.eclipse.jface.viewers.ISelectionProvider;
5 import org.eclipse.ui.PlatformUI;
6 import org.simantics.Simantics;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.util.Bean;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.RequestProcessor;
13 import org.simantics.db.Resource;
14 import org.simantics.db.WriteGraph;
15 import org.simantics.db.common.request.ResourceRead;
16 import org.simantics.db.common.request.TernaryRead;
17 import org.simantics.db.common.request.WriteRequest;
18 import org.simantics.db.common.utils.Functions;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.db.procedure.Listener;
22 import org.simantics.scl.runtime.function.Function1;
23 import org.simantics.scl.runtime.function.Function2;
24 import org.simantics.views.ontology.ViewsResources;
26 public class ViewUtils {
28 public static class ExtendedMargins {
30 public int left, right, top, bottom;
34 public static final Binding EXTENDED_MARGINS_BINDING = Bindings.getBindingUnchecked(ExtendedMargins.class);
36 public static class LayoutBean extends Bean {
37 public int marginLeft;
38 public int marginRight;
40 public int marginBottom;
43 public static class GridLayoutBean extends LayoutBean {
45 public int numColumns;
46 public int horizontalSpacing;
47 public int verticalSpacing;
51 public static class RowLayoutBean extends LayoutBean {
54 public boolean center;
56 public boolean justify;
61 public static class LayoutDataBean extends Bean {}
63 public static class GridDataBean extends LayoutDataBean {
65 public int horizontalSpan;
66 public boolean grabExcessHorizontalSpace;
67 public boolean grabExcessVerticalSpace;
68 public int horizontalAlignment;
69 public int verticalAlignment;
71 public int heightHint;
75 public static class RowDataBean extends LayoutDataBean {
80 public static class ColumnBean extends Bean {
84 public String alignment;
86 public String tooltip;
92 private static void readMargins(ExtendedMargins extendedMargins, LayoutBean layout) {
93 if (extendedMargins != null) {
94 layout.marginLeft = extendedMargins.left;
95 layout.marginRight = extendedMargins.right;
96 layout.marginTop = extendedMargins.top;
97 layout.marginBottom = extendedMargins.bottom;
99 layout.marginLeft = 3;
100 layout.marginRight = 3;
101 layout.marginTop = 3;
102 layout.marginBottom = 3;
106 public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
108 return processor.sync(new ResourceRead<GridLayoutBean>(configuration) {
111 public GridLayoutBean perform(ReadGraph graph) throws DatabaseException {
113 ViewsResources VIEW = ViewsResources.getInstance(graph);
115 Integer columns = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_columnCount, Bindings.INTEGER);
116 Integer horizontalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_horizontalSpacing, Bindings.INTEGER);
117 Integer verticalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_verticalSpacing, Bindings.INTEGER);
118 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
120 GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
121 layout.numColumns = columns != null ? columns : 1;
122 layout.horizontalSpacing = horizontalSpacing != null ? horizontalSpacing : 5;
123 layout.verticalSpacing = verticalSpacing != null ? verticalSpacing : 5;
124 readMargins(extendedMargins, layout);
134 public static GridDataBean getGridData(RequestProcessor processor, Resource configuration) throws DatabaseException {
136 return processor.sync(new ResourceRead<GridDataBean>(configuration) {
139 public GridDataBean perform(ReadGraph graph) throws DatabaseException {
141 ViewsResources VIEW = ViewsResources.getInstance(graph);
142 GridDataBean data = new GridDataBean();
144 data.horizontalSpan = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalSpan, Bindings.INTEGER);
145 data.grabExcessHorizontalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalGrab, Bindings.BOOLEAN);
146 data.grabExcessVerticalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalGrab, Bindings.BOOLEAN);
147 data.horizontalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalAlignment, Bindings.INTEGER);
148 data.verticalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalAlignment, Bindings.INTEGER);
149 data.widthHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
150 data.heightHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
160 public static RowLayoutBean getRowLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
161 return processor.sync(new ResourceRead<RowLayoutBean>(configuration) {
163 public RowLayoutBean perform(ReadGraph graph) throws DatabaseException {
164 ViewsResources VIEW = ViewsResources.getInstance(graph);
165 Integer type = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_type, Bindings.INTEGER);
166 Integer spacing = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_spacing, Bindings.INTEGER);
167 Boolean center = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_center, Bindings.BOOLEAN);
168 Boolean fill = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_fill, Bindings.BOOLEAN);
169 Boolean justify = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_justify, Bindings.BOOLEAN);
170 Boolean pack = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_pack, Bindings.BOOLEAN);
171 Boolean wrap = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_wrap, Bindings.BOOLEAN);
172 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
174 RowLayoutBean layout = new RowLayoutBean();
175 layout.type = type != null ? type : 256;
176 layout.spacing = spacing != null ? spacing : 3;
177 layout.center = center != null ? center : false;
178 layout.fill = fill != null ? fill : false;
179 layout.justify = justify != null ? justify : false;
180 layout.pack = pack != null ? pack : true;
181 layout.wrap = wrap != null ? wrap : true;
182 readMargins(extendedMargins, layout);
188 public static RowDataBean getRowData(RequestProcessor processor, Resource configuration) throws DatabaseException {
189 return processor.sync(new ResourceRead<RowDataBean>(configuration) {
191 public RowDataBean perform(ReadGraph graph) throws DatabaseException {
192 ViewsResources VIEW = ViewsResources.getInstance(graph);
193 RowDataBean data = new RowDataBean();
194 Integer width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
195 Integer height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
196 data.width = width != null ? width : -1;
197 data.height = height != null ? height : -1;
203 public static int getStyle(RequestProcessor processor, Resource configuration) throws DatabaseException {
205 return processor.sync(new ResourceRead<Integer>(configuration) {
208 public Integer perform(ReadGraph graph) throws DatabaseException {
210 ViewsResources VIEW = ViewsResources.getInstance(graph);
213 for(Resource constant : graph.getObjects(resource, VIEW.Control_Style_HasConstant)) {
214 int value = graph.getValue(constant, Bindings.INTEGER);
226 public static ColumnBean getColumn(RequestProcessor processor, Resource configuration) throws DatabaseException {
228 return processor.sync(new ResourceRead<ColumnBean>(configuration) {
231 public ColumnBean perform(ReadGraph graph) throws DatabaseException {
233 ViewsResources VIEW = ViewsResources.getInstance(graph);
234 String key = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasKey, Bindings.STRING);
235 String label = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasLabel, Bindings.STRING);
236 String alignment = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasAlignment, Bindings.STRING);
237 Integer width = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWidth, Bindings.INTEGER);
238 String tooltip = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasTooltip, Bindings.STRING);
239 Boolean grab = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasGrab, Bindings.BOOLEAN);
240 Integer weight = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWeight, Bindings.INTEGER);
242 ColumnBean bean = new ColumnBean();
245 bean.alignment = alignment;
246 bean.width = width != null ? width : -1;
247 bean.tooltip = tooltip;
248 bean.grab = grab != null ? grab : false;
249 bean.weight = weight != null ? weight : 0;
259 public static <T> void listen(Resource configuration, Variable context, String relationURI, final Binding binding, final Function1<T, Boolean> function) throws DatabaseException {
261 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
263 @SuppressWarnings("unchecked")
265 public T perform(ReadGraph graph) throws DatabaseException {
266 Object value = graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
267 Object result = binding.createDefaultUnchecked();
269 binding.readFrom(Bindings.getBinding(binding.type()), value, result);
270 } catch (BindingException e) {
271 // e.printStackTrace();
272 throw new DatabaseException(e);
277 }, new Listener<T>() {
279 private boolean disposed = false;
282 public void exception(Throwable t) {
283 // t.printStackTrace();
287 public void execute(T result) {
288 disposed = function.apply(result);
292 public boolean isDisposed() {
300 public static <T> void listen(Resource configuration, Variable context, String relationURI, final Function1<T, Boolean> function) throws DatabaseException {
302 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
305 public T perform(ReadGraph graph) throws DatabaseException {
306 return graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
309 }, new Listener<T>() {
311 private boolean disposed = false;
314 public void exception(Throwable t) {
315 // t.printStackTrace();
319 public void execute(T result) {
320 disposed = function.apply(result);
324 public boolean isDisposed() {
332 public static Function2<Object, Object, Object> getActionFunctionDeprecated(final Resource configuration, final Resource runtime, final String relationURI) throws DatabaseException {
334 return new Function2<Object, Object, Object>() {
337 public Object apply(final Object selection, final Object event) {
339 Simantics.getSession().async(new WriteRequest() {
342 public void perform(WriteGraph graph) throws DatabaseException {
344 Resource relation = graph.getResource(relationURI);
346 final Resource function = graph.getSingleObject(configuration, relation);
348 Functions.exec(graph, function, graph, runtime, selection, event);
362 public static void setWorkbenchSelection(ISelection selection) {
363 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();
364 provider.setSelection(selection);