/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package fi.vtt.simantics.processeditor.dialogs; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.TreeMap; import java.util.Map.Entry; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.layer0.utils.EntityFactory; import org.simantics.layer0.utils.IEntity; import org.simantics.layer0.utils.viewpoints.TraversalPath; import org.simantics.proconf.browsing.GraphExplorer; import org.simantics.proconf.g3d.stubs.G3DModel; import org.simantics.proconf.g3d.tools.OEPathSelectionListener; import fi.vtt.simantics.processeditor.ProcessResource; import fi.vtt.simantics.processeditor.common.PathUtils; import fi.vtt.simantics.processeditor.monitors.PathContainer; public class ConfigureMonitorDialog extends Dialog { Session session; List typeList; java.util.List nodes; private Composite oeLabelComposite; //private Control viewpointControl; private Composite oeComposite; private GraphExplorer oe; private Button applyAnimationButton; Resource selectedType; Resource sampleInstance; java.util.List sampleSource; java.util.List> samplePath; public ConfigureMonitorDialog(Shell parentShell, Session session, java.util.List nodes) { super(parentShell); this.nodes = nodes; this.session = session; int shellStyle = getShellStyle(); setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); } @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("Configure monitors"); } protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); GridLayout layout = new GridLayout(2,true); layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); composite.setLayout(layout); Label label = new Label(composite, SWT.WRAP); label.setText("Types"); GridData data = new GridData(GridData.CENTER,GridData.FILL,true,false,1,1); data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); label.setLayoutData(data); label.setFont(parent.getFont()); oeLabelComposite = new Composite(composite,SWT.NONE); oeLabelComposite.setLayoutData(data); oeLabelComposite.setLayout(new FillLayout(SWT.HORIZONTAL)); //label = new Label(composite, SWT.WRAP); label = new Label(oeLabelComposite, SWT.WRAP); label.setText("Monitor source"); // label.setLayoutData(data); label.setFont(parent.getFont()); typeList = new List(composite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY | SWT.V_SCROLL); typeList.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); oeComposite = new Composite(composite,SWT.BORDER); oeComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); layout = new GridLayout(); layout.marginWidth = 0; layout.marginHeight = 0; oeComposite.setLayout(layout); session.asyncRead(new GraphRequestAdapter() { TreeMap sorter; @Override public GraphRequestStatus perform(Graph g) throws Exception { Collection types = getTypes(g,nodes); sorter = new TreeMap(); for (Resource type : types) { IEntity t = EntityFactory.create(g, type); String key = t.getName(); if (key.equals("")) key = "ERROR (" + type.getResourceId() + ")"; sorter.put(key, type); } return GraphRequestStatus.transactionComplete(); } @Override public void requestCompleted(GraphRequestStatus status) { getShell().getDisplay().asyncExec(new Runnable() { @Override public void run() { for (Entry e : sorter.entrySet()) { typeList.add(e.getKey()); typeList.setData(e.getKey(), e.getValue()); } } }); } }); typeList.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { session.asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { String key = typeList.getItem(typeList.getSelectionIndex()); selectType(EntityFactory.create(g,(Resource) typeList.getData(key))); return GraphRequestStatus.transactionComplete(); } }); } public void widgetDefaultSelected(SelectionEvent e) { } }); Composite buttonComposite = new Composite(composite,SWT.NONE); buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,4,1)); buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL)); applyAnimationButton = new Button(buttonComposite,SWT.PUSH); applyAnimationButton.setText("Apply monitor configuration"); applyAnimationButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { applyAnimation(); } }); return composite; } /** * Updates type selection: * Finds all GraphicsModels that are connected to instances of selected type with HasGraphics-relation * and lists them in modelList. * * @param resource */ private void selectType(IEntity resource) { selectedType = resource.getResource(); TreeMap sorter = new TreeMap(); Collection models = resource.getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics); for (IEntity model : models) { String key = model.getName(); if (key.equals("")) key = "ERROR (" + model.getResource().getResourceId() + ")"; sorter.put(key, new G3DModel(model)); } if (oe != null) { oe.dispose(); } sampleInstance = null; sampleSource = null; for (Resource n : nodes) { IEntity t = EntityFactory.create(resource.getGraph(),n); if (t.isInstanceOf(resource.getResource())) { sampleInstance = n; break; } } if (sampleInstance != null) { // oe = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId())); // if(viewpointControl != null && !viewpointControl.isDisposed()) { // viewpointControl.dispose(); // } // viewpointControl = oe.getControl(oeLabelComposite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE); // Control c = oe.getControl(oeComposite, 1, OntologyExplorer.OntologyTree, SWT.MULTI); // c.setLayoutData(new GridData(GridData.FILL_BOTH)); // oe.init(null, ViewpointUtils.getModelledHandler(oe.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false); // oe.setSelectionScheme(new SourceSelectionScheme(oe)); // oeComposite.getParent().layout(true,true); oe = new GraphExplorer(oeComposite,SWT.MULTI); oe.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); oe.getViewer().addPostSelectionChangedListener(new OEPathSelectionListener(){ @Override protected void pathSelectionUpdated(java.util.List paths) { if (paths.size() == 0) { //selectSource(null); } else { final java.util.List ps = paths; session.asyncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { selectSource(g,ps); return GraphRequestStatus.transactionComplete(); } }); } } }); } } private void selectSource(Graph graph, java.util.List resources) { if (resources == null) { sampleSource = null; } for (TraversalPath p : resources) { IEntity t = EntityFactory.create(graph, p.getResource()); if (!t.isInstanceOf(ProcessResource.builtins.Double)) { sampleSource = null; return; } } sampleSource = new ArrayList(); for (TraversalPath p : resources) sampleSource.add(p.getResource()); samplePath = new ArrayList>(); for (int i = 0; i < sampleSource.size(); i++) { TraversalPath tpath = resources.get(i); java.util.List path = new ArrayList(); PathUtils.createPath(path, tpath,sampleInstance,sampleSource.get(i),oe); samplePath.add(path); } } private void applyAnimation() { if (sampleInstance == null || sampleSource == null || samplePath == null ) { throw new RuntimeException("Missing required selections"); //return; } PathContainer.getInstance().clearPaths(selectedType); for (java.util.List s : samplePath) { PathContainer.getInstance().addPath(selectedType, s); } } private Collection getTypes(Graph g, java.util.List instances) { Set types = new HashSet(); for (Resource r : instances) { IEntity t = EntityFactory.create(g,r); Collection tTypes = t.getTypes(); for (IEntity type : tTypes) types.add(type.getResource()); } return types; } }