/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * 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 org.simantics.processeditor.dialogs; import java.util.ArrayList; import java.util.List; 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.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.Shell; import org.eclipse.swt.widgets.Text; 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.processeditor.ProcessResource; import org.simantics.processeditor.animations.ResourcePipeAnimationController; import org.simantics.processeditor.common.PathUtils; import org.simantics.processeditor.stubs.PipeRun; import org.simantics.proconf.browsing.GraphExplorer; import org.simantics.proconf.g3d.animation.AnimationSystem; import org.simantics.proconf.g3d.base.JmeRenderingComponent; import org.simantics.proconf.g3d.tools.OEPathSelectionListener; import org.simantics.utils.ui.ErrorLogger; public class ConfigurePipelineAnimationDialog extends Dialog { Session session; java.util.List nodes; //private Control viewpointControl1; private GraphExplorer oe1; //private Control viewpointControl2; private GraphExplorer oe2; JmeRenderingComponent component; private Button applyAnimationButton; Resource sampleInstance; Resource sampleSource1; Resource sampleSource2; java.util.List samplePath1; java.util.List samplePath2; AnimationSystem animationSystem; Button scaleButton1; Text minText1; Text maxText1; Button scaleButton2; Text minText2; Text maxText2; public ConfigurePipelineAnimationDialog(Shell parentShell, Session session,java.util.List nodes, JmeRenderingComponent component, AnimationSystem animationSystem) { super(parentShell); this.session = session; this.nodes = nodes; this.component = component; this.animationSystem = animationSystem; int shellStyle = getShellStyle(); setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); } @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("Configure pipeline animations"); } 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("Color Change"); 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()); label = new Label(composite, SWT.WRAP); label.setText("Particle velocity"); label.setLayoutData(data); label.setFont(parent.getFont()); sampleInstance = nodes.get(0).getResource(); // oe1 = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId())); // viewpointControl1 = oe1.getControl(composite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE); // // oe2 = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId())); // viewpointControl2 = oe2.getControl(composite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE); // // Control c = oe1.getControl(composite, 1, OntologyExplorer.OntologyTree, SWT.SINGLE|SWT.BORDER); // c.setLayoutData(new GridData(GridData.FILL_BOTH)); // // c = oe2.getControl(composite, 1, OntologyExplorer.OntologyTree, SWT.SINGLE|SWT.BORDER); // c.setLayoutData(new GridData(GridData.FILL_BOTH)); // // oe1.init(null, ViewpointUtils.getModelledHandler(oe1.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false); // oe1.setSelectionScheme(new SourceSelectionScheme(oe1)); // // oe2.init(null, ViewpointUtils.getModelledHandler(oe1.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false); // oe2.setSelectionScheme(new SourceSelectionScheme(oe2)); oe1 = new GraphExplorer(composite,SWT.SINGLE); oe1.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); oe2 = new GraphExplorer(composite,SWT.SINGLE); oe2.getTree().setLayoutData(new GridData(GridData.FILL_BOTH)); oe1.getViewer().addPostSelectionChangedListener(new OEPathListener(oe1)); oe2.getViewer().addPostSelectionChangedListener(new OEPathListener(oe2)); Composite buttonComposite = new Composite(composite,SWT.NONE); buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,1,1)); buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL)); scaleButton1 = new Button(buttonComposite,SWT.CHECK); scaleButton1.setText("Scaling"); scaleButton1.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (scaleButton1.getSelection()) { minText1.setEnabled(true); maxText1.setEnabled(true); } else { minText1.setEnabled(false); maxText1.setEnabled(false); } } }); minText1 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER); minText1.setText("0.0"); maxText1 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER); maxText1.setText("1.0"); minText1.setEnabled(false); maxText1.setEnabled(false); buttonComposite = new Composite(composite,SWT.NONE); buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,1,1)); buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL)); scaleButton2 = new Button(buttonComposite,SWT.CHECK); scaleButton2.setText("Scaling"); scaleButton2.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (scaleButton2.getSelection()) { minText2.setEnabled(true); maxText2.setEnabled(true); } else { minText2.setEnabled(false); maxText2.setEnabled(false); } } }); minText2 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER); minText2.setText("0.0"); maxText2 = new Text(buttonComposite,SWT.SINGLE|SWT.BORDER); maxText2.setText("1.0"); minText2.setEnabled(false); maxText2.setEnabled(false); applyAnimationButton = new Button(composite,SWT.PUSH); applyAnimationButton.setText("Apply animation"); applyAnimationButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { session.syncRead(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { applyAnimation(g); return GraphRequestStatus.transactionComplete(); } }); } }); applyAnimationButton.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,2,1)); return composite; } private class OEPathListener extends OEPathSelectionListener { GraphExplorer oe; public OEPathListener(GraphExplorer oe) { this.oe = oe; } protected void pathSelectionUpdated(List paths) { if (paths.size() == 0) { //selectSource(null); } else { selectSource(oe,paths.get(0)); } } } private void selectSource(GraphExplorer oe,TraversalPath path) { Graph graph = null; IEntity t = EntityFactory.create(graph,path.getResource()); if (!t.isInstanceOf(ProcessResource.builtins.Double)) return; if (oe == oe1) { sampleSource1 = path.getResource(); samplePath1 = new ArrayList(); PathUtils.createPath(samplePath1, path,sampleInstance,sampleSource1,oe); } else if (oe == oe2) { sampleSource2 = path.getResource(); samplePath2 = new ArrayList(); PathUtils.createPath(samplePath2, path,sampleInstance,sampleSource2,oe); } } private void applyAnimation(Graph graph) { if (sampleInstance == null || sampleSource1 == null || sampleSource2 == null || samplePath1 == null|| samplePath2 == null) { throw new RuntimeException("Missing required selections"); //return; } java.util.List instances1 = new ArrayList(); java.util.List instances2 = new ArrayList(); java.util.List sources1 = new ArrayList(); java.util.List sources2 = new ArrayList(); browseOthers(instances1, sources1,samplePath1); browseOthers(instances2, sources2,samplePath2); instances1.add(sampleInstance); instances2.add(sampleInstance); // instances1 == instances2 sources1.add(sampleSource1); sources2.add(sampleSource2); for (int i = 0; i < instances1.size(); i++) { double cMin = 0.0; double cMax = 1.0; double vMin = 0.0; double vMax = 0.0; if (scaleButton1.getSelection()) { cMin = Double.parseDouble(minText1.getText()); cMax = Double.parseDouble(maxText1.getText()); } if (scaleButton2.getSelection()) { vMin = Double.parseDouble(minText2.getText()); vMax = Double.parseDouble(maxText2.getText()); } ResourcePipeAnimationController c = new ResourcePipeAnimationController(component, new PipeRun(graph,instances1.get(i)),sources1.get(i),cMin,cMax,sources2.get(i),vMin,vMax); animationSystem.add(c); } } private void browseOthers(java.util.List instances,java.util.List sources,java.util.List samplePath) { Graph graph = null; java.util.List possibleInstances = new ArrayList(); for (Resource n : nodes) { possibleInstances.add(n); } for (Resource instance : possibleInstances) { IEntity source = PathUtils.findSimilar(samplePath,EntityFactory.create(graph,instance)); if (source != null) { instances.add(instance); sources.add(source.getResource()); } else { ErrorLogger.getDefault().logWarning("Cannot find animation source for " + instance, null); } } } }