]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledGraphExplorerComposite.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ModelledGraphExplorerComposite.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
40
41 public class ModelledGraphExplorerComposite implements ModelledControl {
42
43         final private Resource configuration;
44         
45         public ModelledGraphExplorerComposite(Resource configuration) {
46                 this.configuration = configuration;
47         }
48         
49         @Override
50         public Composite create(Composite parent, IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {
51
52                 int style = Simantics.getSession().syncRequest(new Read<Integer>() {
53
54                         @Override
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;
60                                 return style;
61                                 
62                         }
63                         
64                 });
65                 
66                 Map<String, Object> args = Simantics.getSession().syncRequest(new Read<Map<String, Object>>() {
67
68                         @Override
69                         public Map<String, Object> perform(ReadGraph graph) throws DatabaseException {
70                                 
71                         Map<String, Object> args = new HashMap<String, Object>();
72                         args.put("displaySelectors", Boolean.FALSE);
73                         args.put("displayFilter", Boolean.FALSE);
74                         
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);
78                         
79                         return args;
80                         
81                         }
82                         
83                 });
84
85                 String browseContextURI = Simantics.getSession().syncRequest(new Read<String>() {
86
87                         @Override
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);
92                         }
93                         
94                 });
95                 
96         final GraphExplorerComposite explorer = new ModelBrowser(Collections.singleton(browseContextURI), args, site, parent, support, style) {
97             @Override
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.
102             }
103         };
104         
105         GridData gridData = Simantics.getSession().syncRequest(new Read<GridData>() {
106
107             @Override
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);
112                 if(span != null) {
113                         data.horizontalSpan = span;
114                 }
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;
119                 return data;
120             }
121
122         });
123         
124         explorer.setLayoutData(gridData);
125         
126 //        explorer.setBrowseContexts(Collections.singleton(browseContextURI));
127         
128         explorer.setInputSource(new InputSourceImpl<Object>() {
129
130                         @Override
131                         public Object get(ISessionContext ctx, Object selection) {
132                                 new Exception(""+selection).printStackTrace();
133                                 return selection;
134                         }
135                 });
136         
137                 Listener selectionListener = Simantics.getSession().syncRequest(new Read<Listener>() {
138
139                         @Override
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);
145                         }
146                         
147                 });
148                 
149                 if(selectionListener != null) explorer.addListenerToControl(SWT.Selection, selectionListener);
150                 
151                 final String selectionParameter = Simantics.getSession().syncRequest(new Read<String>() {
152
153                         @Override
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);
159                         }
160                         
161                 });
162                 
163                 
164                 if(selectionParameter != null) {
165                         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getExplorer().getAdapter(IPostSelectionProvider.class);
166                         selectionProvider.addPostSelectionChangedListener(new ISelectionChangedListener() {
167                                 
168                                 @Override
169                                 public void selectionChanged(SelectionChangedEvent event) {
170                                         ISelection selection = event.getSelection();
171                                         support.setParameter(selectionParameter, selection);
172                                 }
173                                 
174                         });
175                 }
176                 
177         explorer.finish();
178         
179         return explorer;
180                 
181         }
182         
183 }