1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\r
14 import java.util.Collections;
\r
15 import java.util.HashMap;
\r
16 import java.util.Map;
\r
18 import org.eclipse.jface.action.IMenuManager;
\r
19 import org.eclipse.jface.viewers.IPostSelectionProvider;
\r
20 import org.eclipse.jface.viewers.ISelection;
\r
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
\r
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
\r
23 import org.eclipse.swt.SWT;
\r
24 import org.eclipse.swt.layout.GridData;
\r
25 import org.eclipse.swt.widgets.Composite;
\r
26 import org.eclipse.swt.widgets.Listener;
\r
27 import org.eclipse.ui.IWorkbenchSite;
\r
28 import org.simantics.Simantics;
\r
29 import org.simantics.browsing.ui.GraphExplorer;
\r
30 import org.simantics.browsing.ui.swt.stubs.BrowsingResource;
\r
31 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
\r
32 import org.simantics.browsing.ui.swt.widgets.ModelBrowser;
\r
33 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
34 import org.simantics.databoard.Bindings;
\r
35 import org.simantics.db.ReadGraph;
\r
36 import org.simantics.db.Resource;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.management.ISessionContext;
\r
39 import org.simantics.db.request.Read;
\r
41 public class ModelledGraphExplorerComposite implements ModelledControl {
\r
43 final private Resource configuration;
\r
45 public ModelledGraphExplorerComposite(Resource configuration) {
\r
46 this.configuration = configuration;
\r
50 public Composite create(Composite parent, IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
\r
52 int style = Simantics.getSession().syncRequest(new Read<Integer>() {
\r
55 public Integer perform(ReadGraph graph) throws DatabaseException {
\r
56 int style = SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI;
\r
57 BrowsingResource br = BrowsingResource.getInstance(graph);
\r
58 Boolean check = graph.getPossibleRelatedValue(configuration, br.GraphExplorerComposite_Check, Bindings.BOOLEAN);
\r
59 if(check != null && check) style |= SWT.CHECK;
\r
66 Map<String, Object> args = Simantics.getSession().syncRequest(new Read<Map<String, Object>>() {
\r
69 public Map<String, Object> perform(ReadGraph graph) throws DatabaseException {
\r
71 Map<String, Object> args = new HashMap<String, Object>();
\r
72 args.put("displaySelectors", Boolean.FALSE);
\r
73 args.put("displayFilter", Boolean.FALSE);
\r
75 BrowsingResource br = BrowsingResource.getInstance(graph);
\r
76 Boolean displayFilter = graph.getPossibleRelatedValue(configuration, br.GraphExplorerComposite_DisplayFilter, Bindings.BOOLEAN);
\r
77 if(displayFilter != null) args.put("displayFilter", displayFilter);
\r
85 String browseContextURI = Simantics.getSession().syncRequest(new Read<String>() {
\r
88 public String perform(ReadGraph graph) throws DatabaseException {
\r
89 BrowsingResource br = BrowsingResource.getInstance(graph);
\r
90 Resource browseContext = graph.getSingleObject(configuration, br.GraphExplorerComposite_BrowseContext);
\r
91 return graph.getURI(browseContext);
\r
96 final GraphExplorerComposite explorer = new ModelBrowser(Collections.singleton(browseContextURI), args, site, parent, support, style) {
\r
98 protected void addListeners(GraphExplorer explorer, IMenuManager menuManager) {
\r
99 // No mouse/key listeners in this explorer.
\r
100 // Prevents unwanted contributions from interfering
\r
101 // with how the control works.
\r
105 GridData gridData = Simantics.getSession().syncRequest(new Read<GridData>() {
\r
108 public GridData perform(ReadGraph graph) throws DatabaseException {
\r
109 BrowsingResource br = BrowsingResource.getInstance(graph);
\r
110 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
\r
111 Integer span = graph.getPossibleRelatedValue(configuration, br.Control_HorizontalSpan, Bindings.INTEGER);
\r
113 data.horizontalSpan = span;
\r
115 Integer preferredWidth = graph.getPossibleRelatedValue(configuration, br.Control_PreferredWidth, Bindings.INTEGER);
\r
116 data.widthHint = preferredWidth != null ? preferredWidth : SWT.DEFAULT;
\r
117 Integer preferredHeight = graph.getPossibleRelatedValue(configuration, br.Control_PreferredHeight, Bindings.INTEGER);
\r
118 data.heightHint = preferredHeight != null ? preferredHeight : SWT.DEFAULT;
\r
124 explorer.setLayoutData(gridData);
\r
126 // explorer.setBrowseContexts(Collections.singleton(browseContextURI));
\r
128 explorer.setInputSource(new InputSourceImpl<Object>() {
\r
131 public Object get(ISessionContext ctx, Object selection) {
\r
132 new Exception(""+selection).printStackTrace();
\r
137 Listener selectionListener = Simantics.getSession().syncRequest(new Read<Listener>() {
\r
140 public Listener perform(ReadGraph graph) throws DatabaseException {
\r
141 BrowsingResource br = BrowsingResource.getInstance(graph);
\r
142 Resource listener = graph.getPossibleObject(configuration, br.GraphExplorerComposite_SelectionListener);
\r
143 if(listener == null) return null;
\r
144 return graph.adapt(listener, Listener.class);
\r
149 if(selectionListener != null) explorer.addListenerToControl(SWT.Selection, selectionListener);
\r
151 final String selectionParameter = Simantics.getSession().syncRequest(new Read<String>() {
\r
154 public String perform(ReadGraph graph) throws DatabaseException {
\r
155 BrowsingResource br = BrowsingResource.getInstance(graph);
\r
156 Resource parameter = graph.getPossibleObject(configuration, br.GraphExplorerComposite_SelectionParameter);
\r
157 if(parameter == null) return null;
\r
158 return graph.getPossibleURI(parameter);
\r
164 if(selectionParameter != null) {
\r
165 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getExplorer().getAdapter(IPostSelectionProvider.class);
\r
166 selectionProvider.addPostSelectionChangedListener(new ISelectionChangedListener() {
\r
169 public void selectionChanged(SelectionChangedEvent event) {
\r
170 ISelection selection = event.getSelection();
\r
171 support.setParameter(selectionParameter, selection);
\r