X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.views%2Fsrc%2Forg%2Fsimantics%2Fviews%2FViewUtils.java;fp=bundles%2Forg.simantics.views%2Fsrc%2Forg%2Fsimantics%2Fviews%2FViewUtils.java;h=f22e5ad836bc53e5f463b722bd83802789827703;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.views/src/org/simantics/views/ViewUtils.java b/bundles/org.simantics.views/src/org/simantics/views/ViewUtils.java new file mode 100644 index 000000000..f22e5ad83 --- /dev/null +++ b/bundles/org.simantics.views/src/org/simantics/views/ViewUtils.java @@ -0,0 +1,294 @@ +package org.simantics.views; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.ui.PlatformUI; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.util.Bean; +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.request.TernaryRead; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.Functions; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.procedure.Listener; +import org.simantics.scl.runtime.function.Function1; +import org.simantics.scl.runtime.function.Function2; +import org.simantics.views.ontology.ViewsResources; + +public class ViewUtils { + + public static class ExtendedMargins { + + public int left, right, top, bottom; + + } + + public static final Binding EXTENDED_MARGINS_BINDING = Bindings.getBindingUnchecked(ExtendedMargins.class); + + public static class GridLayoutBean extends Bean { + + public int numColumns; + public int horizontalSpacing; + public int verticalSpacing; + public int marginLeft; + public int marginRight; + public int marginTop; + public int marginBottom; + + } + + public static class GridDataBean extends Bean { + + public int horizontalSpan; + public boolean grabExcessHorizontalSpace; + public boolean grabExcessVerticalSpace; + public int horizontalAlignment; + public int verticalAlignment; + public int widthHint; + public int heightHint; + + } + + public static class ColumnBean extends Bean { + + public String key; + public String label; + public String alignment; + public int width; + public String tooltip; + public boolean grab; + public int weight; + + } + + + + public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException { + + return processor.sync(new ResourceRead(configuration) { + + @Override + public GridLayoutBean perform(ReadGraph graph) throws DatabaseException { + + ViewsResources VIEW = ViewsResources.getInstance(graph); + + Integer columns = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_columnCount, Bindings.INTEGER); + Integer horizontalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_horizontalSpacing, Bindings.INTEGER); + Integer verticalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_verticalSpacing, Bindings.INTEGER); + ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING); + + GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create(); + layout.numColumns = columns; + layout.horizontalSpacing = horizontalSpacing; + layout.verticalSpacing = verticalSpacing; + layout.marginLeft = extendedMargins.left; + layout.marginRight = extendedMargins.right; + layout.marginTop = extendedMargins.top; + layout.marginBottom = extendedMargins.bottom; + + return layout; + + } + + }); + + } + + public static GridDataBean getGridData(RequestProcessor processor, Resource configuration) throws DatabaseException { + + return processor.sync(new ResourceRead(configuration) { + + @Override + public GridDataBean perform(ReadGraph graph) throws DatabaseException { + + ViewsResources VIEW = ViewsResources.getInstance(graph); + GridDataBean data = new GridDataBean(); + + data.horizontalSpan = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalSpan, Bindings.INTEGER); + data.grabExcessHorizontalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalGrab, Bindings.BOOLEAN); + data.grabExcessVerticalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalGrab, Bindings.BOOLEAN); + data.horizontalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalAlignment, Bindings.INTEGER); + data.verticalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalAlignment, Bindings.INTEGER); + data.widthHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER); + data.heightHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER); + + return data; + + } + + }); + + } + + public static int getStyle(RequestProcessor processor, Resource configuration) throws DatabaseException { + + return processor.sync(new ResourceRead(configuration) { + + @Override + public Integer perform(ReadGraph graph) throws DatabaseException { + + ViewsResources VIEW = ViewsResources.getInstance(graph); + + int result = 0; + for(Resource constant : graph.getObjects(resource, VIEW.Control_Style_HasConstant)) { + int value = graph.getValue(constant, Bindings.INTEGER); + result |= value; + } + + return result; + + } + + }); + + } + + public static ColumnBean getColumn(RequestProcessor processor, Resource configuration) throws DatabaseException { + + return processor.sync(new ResourceRead(configuration) { + + @Override + public ColumnBean perform(ReadGraph graph) throws DatabaseException { + + ViewsResources VIEW = ViewsResources.getInstance(graph); + String key = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasKey, Bindings.STRING); + String label = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasLabel, Bindings.STRING); + String alignment = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasAlignment, Bindings.STRING); + Integer width = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWidth, Bindings.INTEGER); + String tooltip = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasTooltip, Bindings.STRING); + Boolean grab = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasGrab, Bindings.BOOLEAN); + Integer weight = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWeight, Bindings.INTEGER); + + ColumnBean bean = new ColumnBean(); + bean.key = key; + bean.label = label; + bean.alignment = alignment; + bean.width = width; + bean.tooltip = tooltip; + bean.grab = grab; + bean.weight = weight; + + return bean; + + } + + }); + + } + + public static void listen(Resource configuration, Variable context, String relationURI, final Binding binding, final Function1 function) throws DatabaseException { + + Simantics.getSession().async(new TernaryRead (configuration, context, relationURI) { + + @Override + public T perform(ReadGraph graph) throws DatabaseException { + Object value = graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2); + Object result = binding.createDefaultUnchecked(); + try { + binding.readFrom(Bindings.getBinding(binding.type()), value, result); + } catch (BindingException e) { +// e.printStackTrace(); + throw new DatabaseException(e); + } + return (T)result; + } + + }, new Listener() { + + private boolean disposed = false; + + @Override + public void exception(Throwable t) { +// t.printStackTrace(); + } + + @Override + public void execute(T result) { + disposed = function.apply(result); + } + + @Override + public boolean isDisposed() { + return disposed; + } + + }); + + } + + public static void listen(Resource configuration, Variable context, String relationURI, final Function1 function) throws DatabaseException { + + Simantics.getSession().async(new TernaryRead (configuration, context, relationURI) { + + @Override + public T perform(ReadGraph graph) throws DatabaseException { + return graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2); + } + + }, new Listener() { + + private boolean disposed = false; + + @Override + public void exception(Throwable t) { +// t.printStackTrace(); + } + + @Override + public void execute(T result) { + disposed = function.apply(result); + } + + @Override + public boolean isDisposed() { + return disposed; + } + + }); + + } + + public static Function2 getActionFunctionDeprecated(final Resource configuration, final Resource runtime, final String relationURI) throws DatabaseException { + + return new Function2() { + + @Override + public Object apply(final Object selection, final Object event) { + + Simantics.getSession().async(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + + Resource relation = graph.getResource(relationURI); + + final Resource function = graph.getSingleObject(configuration, relation); + + Functions.exec(graph, function, graph, runtime, selection, event); + + } + + }); + + return null; + + } + + }; + + } + + public static void setWorkbenchSelection(ISelection selection) { + ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider(); + provider.setSelection(selection); + } + +}