1 package org.simantics.views;
\r
3 import org.eclipse.jface.viewers.ISelection;
\r
4 import org.eclipse.jface.viewers.ISelectionProvider;
\r
5 import org.eclipse.ui.PlatformUI;
\r
6 import org.simantics.Simantics;
\r
7 import org.simantics.databoard.Bindings;
\r
8 import org.simantics.databoard.binding.Binding;
\r
9 import org.simantics.databoard.binding.error.BindingException;
\r
10 import org.simantics.databoard.util.Bean;
\r
11 import org.simantics.db.ReadGraph;
\r
12 import org.simantics.db.RequestProcessor;
\r
13 import org.simantics.db.Resource;
\r
14 import org.simantics.db.WriteGraph;
\r
15 import org.simantics.db.common.request.ResourceRead;
\r
16 import org.simantics.db.common.request.TernaryRead;
\r
17 import org.simantics.db.common.request.WriteRequest;
\r
18 import org.simantics.db.common.utils.Functions;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.db.layer0.variable.Variable;
\r
21 import org.simantics.db.procedure.Listener;
\r
22 import org.simantics.scl.runtime.function.Function1;
\r
23 import org.simantics.scl.runtime.function.Function2;
\r
24 import org.simantics.views.ontology.ViewsResources;
\r
26 public class ViewUtils {
\r
28 public static class ExtendedMargins {
\r
30 public int left, right, top, bottom;
\r
34 public static final Binding EXTENDED_MARGINS_BINDING = Bindings.getBindingUnchecked(ExtendedMargins.class);
\r
36 public static class LayoutBean extends Bean {
\r
37 public int marginLeft;
\r
38 public int marginRight;
\r
39 public int marginTop;
\r
40 public int marginBottom;
\r
43 public static class GridLayoutBean extends LayoutBean {
\r
45 public int numColumns;
\r
46 public int horizontalSpacing;
\r
47 public int verticalSpacing;
\r
51 public static class RowLayoutBean extends LayoutBean {
\r
54 public boolean center;
\r
55 public boolean fill;
\r
56 public boolean justify;
\r
57 public boolean pack;
\r
58 public boolean wrap;
\r
61 public static class LayoutDataBean extends Bean {}
\r
63 public static class GridDataBean extends LayoutDataBean {
\r
65 public int horizontalSpan;
\r
66 public boolean grabExcessHorizontalSpace;
\r
67 public boolean grabExcessVerticalSpace;
\r
68 public int horizontalAlignment;
\r
69 public int verticalAlignment;
\r
70 public int widthHint;
\r
71 public int heightHint;
\r
75 public static class RowDataBean extends LayoutDataBean {
\r
80 public static class ColumnBean extends Bean {
\r
83 public String label;
\r
84 public String alignment;
\r
86 public String tooltip;
\r
87 public boolean grab;
\r
94 public static GridLayoutBean getLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
\r
96 return processor.sync(new ResourceRead<GridLayoutBean>(configuration) {
\r
99 public GridLayoutBean perform(ReadGraph graph) throws DatabaseException {
\r
101 ViewsResources VIEW = ViewsResources.getInstance(graph);
\r
103 Integer columns = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_columnCount, Bindings.INTEGER);
\r
104 Integer horizontalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_horizontalSpacing, Bindings.INTEGER);
\r
105 Integer verticalSpacing = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_verticalSpacing, Bindings.INTEGER);
\r
106 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
\r
108 GridLayoutBean layout = new GridLayoutBean();//.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
\r
109 layout.numColumns = columns;
\r
110 layout.horizontalSpacing = horizontalSpacing;
\r
111 layout.verticalSpacing = verticalSpacing;
\r
112 layout.marginLeft = extendedMargins.left;
\r
113 layout.marginRight = extendedMargins.right;
\r
114 layout.marginTop = extendedMargins.top;
\r
115 layout.marginBottom = extendedMargins.bottom;
\r
125 public static GridDataBean getGridData(RequestProcessor processor, Resource configuration) throws DatabaseException {
\r
127 return processor.sync(new ResourceRead<GridDataBean>(configuration) {
\r
130 public GridDataBean perform(ReadGraph graph) throws DatabaseException {
\r
132 ViewsResources VIEW = ViewsResources.getInstance(graph);
\r
133 GridDataBean data = new GridDataBean();
\r
135 data.horizontalSpan = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalSpan, Bindings.INTEGER);
\r
136 data.grabExcessHorizontalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalGrab, Bindings.BOOLEAN);
\r
137 data.grabExcessVerticalSpace = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalGrab, Bindings.BOOLEAN);
\r
138 data.horizontalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_horizontalAlignment, Bindings.INTEGER);
\r
139 data.verticalAlignment = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_verticalAlignment, Bindings.INTEGER);
\r
140 data.widthHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
\r
141 data.heightHint = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
\r
151 public static RowLayoutBean getRowLayout(RequestProcessor processor, Resource configuration) throws DatabaseException {
\r
152 return processor.sync(new ResourceRead<RowLayoutBean>(configuration) {
\r
154 public RowLayoutBean perform(ReadGraph graph) throws DatabaseException {
\r
155 ViewsResources VIEW = ViewsResources.getInstance(graph);
\r
156 Integer type = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_type, Bindings.INTEGER);
\r
157 Integer spacing = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_spacing, Bindings.INTEGER);
\r
158 Boolean center = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_center, Bindings.BOOLEAN);
\r
159 Boolean fill = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_fill, Bindings.BOOLEAN);
\r
160 Boolean justify = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_justify, Bindings.BOOLEAN);
\r
161 Boolean pack = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_pack, Bindings.BOOLEAN);
\r
162 Boolean wrap = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_wrap, Bindings.BOOLEAN);
\r
163 ExtendedMargins extendedMargins = graph.getPossibleRelatedValue(resource, VIEW.RowLayout_extendedMargins, EXTENDED_MARGINS_BINDING);
\r
165 RowLayoutBean layout = new RowLayoutBean();
\r
166 layout.type = type;
\r
167 layout.spacing = spacing;
\r
168 layout.center = center;
\r
169 layout.fill = fill;
\r
170 layout.justify = justify;
\r
171 layout.pack = pack;
\r
172 layout.wrap = wrap;
\r
173 layout.marginLeft = extendedMargins.left;
\r
174 layout.marginRight = extendedMargins.right;
\r
175 layout.marginTop = extendedMargins.top;
\r
176 layout.marginBottom = extendedMargins.bottom;
\r
182 public static RowDataBean getRowData(RequestProcessor processor, Resource configuration) throws DatabaseException {
\r
183 return processor.sync(new ResourceRead<RowDataBean>(configuration) {
\r
185 public RowDataBean perform(ReadGraph graph) throws DatabaseException {
\r
186 ViewsResources VIEW = ViewsResources.getInstance(graph);
\r
187 RowDataBean data = new RowDataBean();
\r
188 data.width = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredWidth, Bindings.INTEGER);
\r
189 data.height = graph.getPossibleRelatedValue(resource, VIEW.GridLayout_GridData_preferredHeight, Bindings.INTEGER);
\r
195 public static int getStyle(RequestProcessor processor, Resource configuration) throws DatabaseException {
\r
197 return processor.sync(new ResourceRead<Integer>(configuration) {
\r
200 public Integer perform(ReadGraph graph) throws DatabaseException {
\r
202 ViewsResources VIEW = ViewsResources.getInstance(graph);
\r
205 for(Resource constant : graph.getObjects(resource, VIEW.Control_Style_HasConstant)) {
\r
206 int value = graph.getValue(constant, Bindings.INTEGER);
\r
218 public static ColumnBean getColumn(RequestProcessor processor, Resource configuration) throws DatabaseException {
\r
220 return processor.sync(new ResourceRead<ColumnBean>(configuration) {
\r
223 public ColumnBean perform(ReadGraph graph) throws DatabaseException {
\r
225 ViewsResources VIEW = ViewsResources.getInstance(graph);
\r
226 String key = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasKey, Bindings.STRING);
\r
227 String label = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasLabel, Bindings.STRING);
\r
228 String alignment = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasAlignment, Bindings.STRING);
\r
229 Integer width = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWidth, Bindings.INTEGER);
\r
230 String tooltip = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasTooltip, Bindings.STRING);
\r
231 Boolean grab = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasGrab, Bindings.BOOLEAN);
\r
232 Integer weight = graph.getPossibleRelatedValue(resource, VIEW.Explorer_Column_HasWeight, Bindings.INTEGER);
\r
234 ColumnBean bean = new ColumnBean();
\r
236 bean.label = label;
\r
237 bean.alignment = alignment;
\r
238 bean.width = width;
\r
239 bean.tooltip = tooltip;
\r
241 bean.weight = weight;
\r
251 public static <T> void listen(Resource configuration, Variable context, String relationURI, final Binding binding, final Function1<T, Boolean> function) throws DatabaseException {
\r
253 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
\r
255 @SuppressWarnings("unchecked")
\r
257 public T perform(ReadGraph graph) throws DatabaseException {
\r
258 Object value = graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
\r
259 Object result = binding.createDefaultUnchecked();
\r
261 binding.readFrom(Bindings.getBinding(binding.type()), value, result);
\r
262 } catch (BindingException e) {
\r
263 // e.printStackTrace();
\r
264 throw new DatabaseException(e);
\r
269 }, new Listener<T>() {
\r
271 private boolean disposed = false;
\r
274 public void exception(Throwable t) {
\r
275 // t.printStackTrace();
\r
279 public void execute(T result) {
\r
280 disposed = function.apply(result);
\r
284 public boolean isDisposed() {
\r
292 public static <T> void listen(Resource configuration, Variable context, String relationURI, final Function1<T, Boolean> function) throws DatabaseException {
\r
294 Simantics.getSession().async(new TernaryRead<Resource, Variable, String, T> (configuration, context, relationURI) {
\r
297 public T perform(ReadGraph graph) throws DatabaseException {
\r
298 return graph.getRelatedValue2(parameter, graph.getResource(parameter3), parameter2);
\r
301 }, new Listener<T>() {
\r
303 private boolean disposed = false;
\r
306 public void exception(Throwable t) {
\r
307 // t.printStackTrace();
\r
311 public void execute(T result) {
\r
312 disposed = function.apply(result);
\r
316 public boolean isDisposed() {
\r
324 public static Function2<Object, Object, Object> getActionFunctionDeprecated(final Resource configuration, final Resource runtime, final String relationURI) throws DatabaseException {
\r
326 return new Function2<Object, Object, Object>() {
\r
329 public Object apply(final Object selection, final Object event) {
\r
331 Simantics.getSession().async(new WriteRequest() {
\r
334 public void perform(WriteGraph graph) throws DatabaseException {
\r
336 Resource relation = graph.getResource(relationURI);
\r
338 final Resource function = graph.getSingleObject(configuration, relation);
\r
340 Functions.exec(graph, function, graph, runtime, selection, event);
\r
354 public static void setWorkbenchSelection(ISelection selection) {
\r
355 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();
\r
356 provider.setSelection(selection);
\r