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;
94 public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
96 return processor.sync(new ResourceRead<GridLayoutBean>(configuration) {
99 public GridLayoutBean perform(ReadGraph graph) throws DatabaseException {
101 ViewsResources VIEW = ViewsResources.getInstance(graph);
103 Integer columns = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_columnCount, Bindings.INTEGER);
104 Integer horizontalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_horizontalSpacing, Bindings.INTEGER);
105 Integer verticalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_verticalSpacing, Bindings.INTEGER);
106 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
108 GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
109 layout.numColumns = columns;
110 layout.horizontalSpacing = horizontalSpacing;
111 layout.verticalSpacing = verticalSpacing;
112 layout.marginLeft = extendedMargins.left;
113 layout.marginRight = extendedMargins.right;
114 layout.marginTop = extendedMargins.top;
115 layout.marginBottom = extendedMargins.bottom;
125 public static GridDataBean getGridData(RequestProcessor processor, Resource configuration) throws DatabaseException {
127 return processor.sync(new ResourceRead<GridDataBean>(configuration) {
130 public GridDataBean perform(ReadGraph graph) throws DatabaseException {
132 ViewsResources VIEW = ViewsResources.getInstance(graph);
133 GridDataBean data = new GridDataBean();
135 data.horizontalSpan = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalSpan, Bindings.INTEGER);
136 data.grabExcessHorizontalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalGrab, Bindings.BOOLEAN);
137 data.grabExcessVerticalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalGrab, Bindings.BOOLEAN);
138 data.horizontalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalAlignment, Bindings.INTEGER);
139 data.verticalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalAlignment, Bindings.INTEGER);
140 data.widthHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
141 data.heightHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
151 public static RowLayoutBean getRowLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
152 return processor.sync(new ResourceRead<RowLayoutBean>(configuration) {
154 public RowLayoutBean perform(ReadGraph graph) throws DatabaseException {
155 ViewsResources VIEW = ViewsResources.getInstance(graph);
156 Integer type = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_type, Bindings.INTEGER);
157 Integer spacing = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_spacing, Bindings.INTEGER);
158 Boolean center = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_center, Bindings.BOOLEAN);
159 Boolean fill = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_fill, Bindings.BOOLEAN);
160 Boolean justify = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_justify, Bindings.BOOLEAN);
161 Boolean pack = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_pack, Bindings.BOOLEAN);
162 Boolean wrap = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_wrap, Bindings.BOOLEAN);
163 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
165 RowLayoutBean layout = new RowLayoutBean();
167 layout.spacing = spacing;
168 layout.center = center;
170 layout.justify = justify;
173 layout.marginLeft = extendedMargins.left;
174 layout.marginRight = extendedMargins.right;
175 layout.marginTop = extendedMargins.top;
176 layout.marginBottom = extendedMargins.bottom;
182 public static RowDataBean getRowData(RequestProcessor processor, Resource configuration) throws DatabaseException {
183 return processor.sync(new ResourceRead<RowDataBean>(configuration) {
185 public RowDataBean perform(ReadGraph graph) throws DatabaseException {
186 ViewsResources VIEW = ViewsResources.getInstance(graph);
187 RowDataBean data = new RowDataBean();
188 data.width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
189 data.height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
195 public static int getStyle(RequestProcessor processor, Resource configuration) throws DatabaseException {
197 return processor.sync(new ResourceRead<Integer>(configuration) {
200 public Integer perform(ReadGraph graph) throws DatabaseException {
202 ViewsResources VIEW = ViewsResources.getInstance(graph);
205 for(Resource constant : graph.getObjects(resource, VIEW.Control_Style_HasConstant)) {
206 int value = graph.getValue(constant, Bindings.INTEGER);
218 public static ColumnBean getColumn(RequestProcessor processor, Resource configuration) throws DatabaseException {
220 return processor.sync(new ResourceRead<ColumnBean>(configuration) {
223 public ColumnBean perform(ReadGraph graph) throws DatabaseException {
225 ViewsResources VIEW = ViewsResources.getInstance(graph);
226 String key = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasKey, Bindings.STRING);
227 String label = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasLabel, Bindings.STRING);
228 String alignment = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasAlignment, Bindings.STRING);
229 Integer width = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWidth, Bindings.INTEGER);
230 String tooltip = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasTooltip, Bindings.STRING);
231 Boolean grab = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasGrab, Bindings.BOOLEAN);
232 Integer weight = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWeight, Bindings.INTEGER);
234 ColumnBean bean = new ColumnBean();
237 bean.alignment = alignment;
239 bean.tooltip = tooltip;
241 bean.weight = weight;
251 public static <T> void listen(Resource configuration, Variable context, String relationURI, final Binding binding, final Function1<T, Boolean> function) throws DatabaseException {
253 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
255 @SuppressWarnings("unchecked")
257 public T perform(ReadGraph graph) throws DatabaseException {
258 Object value = graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
259 Object result = binding.createDefaultUnchecked();
261 binding.readFrom(Bindings.getBinding(binding.type()), value, result);
262 } catch (BindingException e) {
263 // e.printStackTrace();
264 throw new DatabaseException(e);
269 }, new Listener<T>() {
271 private boolean disposed = false;
274 public void exception(Throwable t) {
275 // t.printStackTrace();
279 public void execute(T result) {
280 disposed = function.apply(result);
284 public boolean isDisposed() {
292 public static <T> void listen(Resource configuration, Variable context, String relationURI, final Function1<T, Boolean> function) throws DatabaseException {
294 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
297 public T perform(ReadGraph graph) throws DatabaseException {
298 return graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
301 }, new Listener<T>() {
303 private boolean disposed = false;
306 public void exception(Throwable t) {
307 // t.printStackTrace();
311 public void execute(T result) {
312 disposed = function.apply(result);
316 public boolean isDisposed() {
324 public static Function2<Object, Object, Object> getActionFunctionDeprecated(final Resource configuration, final Resource runtime, final String relationURI) throws DatabaseException {
326 return new Function2<Object, Object, Object>() {
329 public Object apply(final Object selection, final Object event) {
331 Simantics.getSession().async(new WriteRequest() {
334 public void perform(WriteGraph graph) throws DatabaseException {
336 Resource relation = graph.getResource(relationURI);
338 final Resource function = graph.getSingleObject(configuration, relation);
340 Functions.exec(graph, function, graph, runtime, selection, event);
354 public static void setWorkbenchSelection(ISelection selection) {
355 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();
356 provider.setSelection(selection);