1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.dialogs;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Collection;
\r
15 import java.util.HashSet;
\r
16 import java.util.Set;
\r
17 import java.util.TreeMap;
\r
18 import java.util.Map.Entry;
\r
20 import org.eclipse.jface.dialogs.Dialog;
\r
21 import org.eclipse.jface.dialogs.IDialogConstants;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.events.SelectionAdapter;
\r
24 import org.eclipse.swt.events.SelectionEvent;
\r
25 import org.eclipse.swt.events.SelectionListener;
\r
26 import org.eclipse.swt.layout.FillLayout;
\r
27 import org.eclipse.swt.layout.GridData;
\r
28 import org.eclipse.swt.layout.GridLayout;
\r
29 import org.eclipse.swt.widgets.Button;
\r
30 import org.eclipse.swt.widgets.Composite;
\r
31 import org.eclipse.swt.widgets.Control;
\r
32 import org.eclipse.swt.widgets.Label;
\r
33 import org.eclipse.swt.widgets.List;
\r
34 import org.eclipse.swt.widgets.Shell;
\r
35 import org.simantics.db.Graph;
\r
36 import org.simantics.db.GraphRequestAdapter;
\r
37 import org.simantics.db.GraphRequestStatus;
\r
38 import org.simantics.db.Resource;
\r
39 import org.simantics.db.Session;
\r
40 import org.simantics.layer0.utils.EntityFactory;
\r
41 import org.simantics.layer0.utils.IEntity;
\r
42 import org.simantics.layer0.utils.viewpoints.TraversalPath;
\r
43 import org.simantics.processeditor.ProcessResource;
\r
44 import org.simantics.processeditor.common.PathUtils;
\r
45 import org.simantics.processeditor.monitors.PathContainer;
\r
46 import org.simantics.proconf.browsing.GraphExplorer;
\r
47 import org.simantics.proconf.g3d.stubs.G3DModel;
\r
48 import org.simantics.proconf.g3d.tools.OEPathSelectionListener;
\r
51 public class ConfigureMonitorDialog extends Dialog {
\r
54 java.util.List<Resource> nodes;
\r
57 private Composite oeLabelComposite;
\r
58 //private Control viewpointControl;
\r
59 private Composite oeComposite;
\r
60 private GraphExplorer oe;
\r
62 private Button applyAnimationButton;
\r
64 Resource selectedType;
\r
65 Resource sampleInstance;
\r
66 java.util.List<Resource> sampleSource;
\r
67 java.util.List<java.util.List<Resource>> samplePath;
\r
70 public ConfigureMonitorDialog(Shell parentShell, Session session, java.util.List<Resource> nodes) {
\r
73 this.session = session;
\r
74 int shellStyle = getShellStyle();
\r
75 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
\r
79 protected void configureShell(Shell newShell) {
\r
80 super.configureShell(newShell);
\r
81 newShell.setText("Configure monitors");
\r
85 protected Control createDialogArea(Composite parent) {
\r
86 Composite composite = (Composite) super.createDialogArea(parent);
\r
87 GridLayout layout = new GridLayout(2,true);
\r
88 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
\r
89 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
\r
90 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
\r
91 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
\r
92 composite.setLayout(layout);
\r
94 Label label = new Label(composite, SWT.WRAP);
\r
95 label.setText("Types");
\r
96 GridData data = new GridData(GridData.CENTER,GridData.FILL,true,false,1,1);
\r
97 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
\r
98 label.setLayoutData(data);
\r
99 label.setFont(parent.getFont());
\r
101 oeLabelComposite = new Composite(composite,SWT.NONE);
\r
102 oeLabelComposite.setLayoutData(data);
\r
103 oeLabelComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
\r
104 //label = new Label(composite, SWT.WRAP);
\r
105 label = new Label(oeLabelComposite, SWT.WRAP);
\r
106 label.setText("Monitor source");
\r
107 // label.setLayoutData(data);
\r
108 label.setFont(parent.getFont());
\r
110 typeList = new List(composite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY | SWT.V_SCROLL);
\r
111 typeList.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
\r
113 oeComposite = new Composite(composite,SWT.BORDER);
\r
114 oeComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
\r
115 layout = new GridLayout();
\r
116 layout.marginWidth = 0;
\r
117 layout.marginHeight = 0;
\r
118 oeComposite.setLayout(layout);
\r
119 session.asyncRead(new GraphRequestAdapter() {
\r
120 TreeMap<String, Resource> sorter;
\r
122 public GraphRequestStatus perform(Graph g) throws Exception {
\r
123 Collection<Resource> types = getTypes(g,nodes);
\r
125 sorter = new TreeMap<String, Resource>();
\r
126 for (Resource type : types) {
\r
127 IEntity t = EntityFactory.create(g, type);
\r
128 String key = t.getName();
\r
129 if (key.equals("")) key = "ERROR (" + type.getResourceId() + ")";
\r
130 sorter.put(key, type);
\r
134 return GraphRequestStatus.transactionComplete();
\r
138 public void requestCompleted(GraphRequestStatus status) {
\r
139 getShell().getDisplay().asyncExec(new Runnable() {
\r
141 public void run() {
\r
142 for (Entry<String, Resource> e : sorter.entrySet()) {
\r
143 typeList.add(e.getKey());
\r
144 typeList.setData(e.getKey(), e.getValue());
\r
151 typeList.addSelectionListener(new SelectionListener() {
\r
152 public void widgetSelected(SelectionEvent e) {
\r
153 session.asyncRead(new GraphRequestAdapter() {
\r
155 public GraphRequestStatus perform(Graph g) throws Exception {
\r
156 String key = typeList.getItem(typeList.getSelectionIndex());
\r
157 selectType(EntityFactory.create(g,(Resource) typeList.getData(key)));
\r
158 return GraphRequestStatus.transactionComplete();
\r
163 public void widgetDefaultSelected(SelectionEvent e) {
\r
169 Composite buttonComposite = new Composite(composite,SWT.NONE);
\r
170 buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,4,1));
\r
171 buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
\r
174 applyAnimationButton = new Button(buttonComposite,SWT.PUSH);
\r
175 applyAnimationButton.setText("Apply monitor configuration");
\r
176 applyAnimationButton.addSelectionListener(new SelectionAdapter() {
\r
178 public void widgetSelected(SelectionEvent e) {
\r
186 * Updates type selection:
\r
187 * Finds all GraphicsModels that are connected to instances of selected type with HasGraphics-relation
\r
188 * and lists them in modelList.
\r
192 private void selectType(IEntity resource) {
\r
193 selectedType = resource.getResource();
\r
195 TreeMap<String, G3DModel> sorter = new TreeMap<String, G3DModel>();
\r
197 Collection<IEntity> models = resource.getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics);
\r
198 for (IEntity model : models) {
\r
200 String key = model.getName();
\r
201 if (key.equals("")) key = "ERROR (" + model.getResource().getResourceId() + ")";
\r
202 sorter.put(key, new G3DModel(model));
\r
208 sampleInstance = null;
\r
209 sampleSource = null;
\r
210 for (Resource n : nodes) {
\r
211 IEntity t = EntityFactory.create(resource.getGraph(),n);
\r
212 if (t.isInstanceOf(resource.getResource())) {
\r
213 sampleInstance = n;
\r
217 if (sampleInstance != null) {
\r
218 // oe = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId()));
\r
219 // if(viewpointControl != null && !viewpointControl.isDisposed()) {
\r
220 // viewpointControl.dispose();
\r
222 // viewpointControl = oe.getControl(oeLabelComposite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE);
\r
223 // Control c = oe.getControl(oeComposite, 1, OntologyExplorer.OntologyTree, SWT.MULTI);
\r
224 // c.setLayoutData(new GridData(GridData.FILL_BOTH));
\r
225 // oe.init(null, ViewpointUtils.getModelledHandler(oe.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false);
\r
226 // oe.setSelectionScheme(new SourceSelectionScheme(oe));
\r
227 // oeComposite.getParent().layout(true,true);
\r
228 oe = new GraphExplorer(oeComposite,SWT.MULTI);
\r
229 oe.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
\r
230 oe.getViewer().addPostSelectionChangedListener(new OEPathSelectionListener(){
\r
232 protected void pathSelectionUpdated(java.util.List<TraversalPath> paths) {
\r
233 if (paths.size() == 0) {
\r
234 //selectSource(null);
\r
236 final java.util.List<TraversalPath> ps = paths;
\r
237 session.asyncRead(new GraphRequestAdapter() {
\r
239 public GraphRequestStatus perform(Graph g) throws Exception {
\r
240 selectSource(g,ps);
\r
241 return GraphRequestStatus.transactionComplete();
\r
255 private void selectSource(Graph graph, java.util.List<TraversalPath> resources) {
\r
256 if (resources == null) {
\r
257 sampleSource = null;
\r
259 for (TraversalPath p : resources) {
\r
260 IEntity t = EntityFactory.create(graph, p.getResource());
\r
261 if (!t.isInstanceOf(ProcessResource.builtins.Double)) {
\r
262 sampleSource = null;
\r
266 sampleSource = new ArrayList<Resource>();
\r
267 for (TraversalPath p : resources)
\r
268 sampleSource.add(p.getResource());
\r
270 samplePath = new ArrayList<java.util.List<Resource>>();
\r
271 for (int i = 0; i < sampleSource.size(); i++) {
\r
272 TraversalPath tpath = resources.get(i);
\r
273 java.util.List<Resource> path = new ArrayList<Resource>();
\r
274 PathUtils.createPath(path, tpath,sampleInstance,sampleSource.get(i),oe);
\r
275 samplePath.add(path);
\r
280 private void applyAnimation() {
\r
282 if (sampleInstance == null || sampleSource == null || samplePath == null ) {
\r
283 throw new RuntimeException("Missing required selections");
\r
287 PathContainer.getInstance().clearPaths(selectedType);
\r
288 for (java.util.List<Resource> s : samplePath) {
\r
289 PathContainer.getInstance().addPath(selectedType, s);
\r
297 private Collection<Resource> getTypes(Graph g, java.util.List<Resource> instances) {
\r
298 Set<Resource> types = new HashSet<Resource>();
\r
299 for (Resource r : instances) {
\r
300 IEntity t = EntityFactory.create(g,r);
\r
301 Collection<IEntity> tTypes = t.getTypes();
\r
302 for (IEntity type : tTypes)
\r
303 types.add(type.getResource());
\r