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