-/*******************************************************************************\r
- * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.processeditor.handlers;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.jface.dialogs.Dialog;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.ModifyEvent;\r
-import org.eclipse.swt.events.ModifyListener;\r
-import org.eclipse.swt.events.SelectionAdapter;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.widgets.Button;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.simantics.db.Graph;\r
-import org.simantics.db.GraphRequestAdapter;\r
-import org.simantics.db.GraphRequestStatus;\r
-import org.simantics.db.Resource;\r
-import org.simantics.layer0.stubs.Library;\r
-import org.simantics.layer0.utils.EntityFactory;\r
-import org.simantics.layer0.utils.IEntity;\r
-import org.simantics.layer0.utils.instantiation.InstanceFactory;\r
-import org.simantics.processeditor.ProcessResource;\r
-import org.simantics.proconf.ui.ProConfUI;\r
-import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;\r
-\r
-\r
-\r
-/**\r
- * Handler that creates new pipeline components.\r
- * \r
- * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
- *\r
- */\r
-public class NewComponentHandler extends AbstractHandler {\r
- \r
- private Map<Resource, String> nameMap;\r
- \r
- @Override\r
- public Object execute(ExecutionEvent event) throws ExecutionException {\r
- ISelection s = HandlerUtil.getCurrentSelectionChecked(event);\r
- IStructuredSelection ss = (IStructuredSelection) s;\r
- if (ss.size() != 1)\r
- return null;\r
- final Resource lib = ResourceAdaptionUtils.toSingleResource(ss);\r
- \r
- \r
- \r
- if (nameMap == null) {\r
- ProConfUI.getSession().syncRead(new GraphRequestAdapter(){\r
- @Override\r
- public GraphRequestStatus perform(Graph g) throws Exception {\r
- nameMap = new HashMap<Resource, String>();\r
- for (Resource r : getComponentTypes()) {\r
- IEntity e = EntityFactory.create(g,r);\r
- nameMap.put(r, e.getName());\r
- for (Resource r2 : getComponentOptions(r)) {\r
- IEntity e2 = EntityFactory.create(g,r2);\r
- nameMap.put(r2, e2.getName());\r
- }\r
- }\r
- return GraphRequestStatus.transactionComplete();\r
- }\r
- });\r
- }\r
- \r
- ComponentTypeDialog dialog = new ComponentTypeDialog(Display.getDefault().getActiveShell());\r
- if (dialog.open() == ComponentTypeDialog.CANCEL)\r
- return null;\r
- final List<Resource> types = dialog.getSelection();\r
- final String name = dialog.getName();\r
- \r
- if (types.size() == 0 || name == null || name.length() == 0)\r
- return null;\r
- \r
- ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
- @Override\r
- public GraphRequestStatus perform(Graph g) throws Exception {\r
- // instantiate component\r
- IEntity instance = EntityFactory.create(g, InstanceFactory.instantiate(g, types));\r
- Library l = new Library(g, lib);\r
- l.addStatement(g.getBuiltins().ConsistsOf, instance);\r
- instance.setName(name);\r
- \r
- // TODO : is this correct (instance & inherits)\r
- for (Resource type : types) {\r
- instance.addStatement(ProcessResource.builtins.Inherits, type);\r
- }\r
- \r
- // instantiate control point(s)\r
- List<Resource> cpTypes = getControlPointTypes(types);\r
- boolean isDual = (cpTypes.contains(ProcessResource.plant3Dresource.SizeChangeControlPoint) || \r
- cpTypes.contains(ProcessResource.plant3Dresource.OffsettingPoint));\r
- IEntity cp = EntityFactory.create(g, InstanceFactory.instantiate(g, cpTypes));\r
- instance.addStatement(ProcessResource.plant3Dresource.HasControlPoint, cp);\r
- if (isDual) {\r
- IEntity subCP = EntityFactory.create(g, InstanceFactory.instantiate(g, ProcessResource.plant3Dresource.DualSubControlPoint));\r
- cp.addStatement(ProcessResource.plant3Dresource.HasSubPoint, subCP);\r
- }\r
- // instantiate model\r
- Resource modelType = g.getResourceByURI("http://www.vtt.fi/Simantics/CSG/1.0/Types#CSGModel");\r
- IEntity model = EntityFactory.create(g, InstanceFactory.instantiate(g, modelType));\r
- instance.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);\r
- \r
- return GraphRequestStatus.transactionComplete();\r
- }\r
- });\r
- \r
- \r
- return null;\r
- }\r
- \r
- /**\r
- * Returns all possible types for a pipeline component\r
- * @return\r
- */\r
- private List<Resource> getComponentTypes() {\r
- List<Resource> list = new ArrayList<Resource>();\r
- list.add(ProcessResource.plant3Dresource.FixedLengthInlineComponent);\r
- list.add(ProcessResource.plant3Dresource.VariableLengthInlineComponent);\r
- list.add(ProcessResource.plant3Dresource.VariableAngleTurnComponent);\r
- list.add(ProcessResource.plant3Dresource.FixedAngleTurnComponent);\r
- list.add(ProcessResource.plant3Dresource.EndComponent);\r
- list.add(ProcessResource.plant3Dresource.Nozzle);\r
- \r
- return list;\r
- }\r
- \r
- /**\r
- * Returns optional types for a component type\r
- * @param type\r
- * @return\r
- */\r
- private List<Resource> getComponentOptions(Resource type) {\r
- List<Resource> list = new ArrayList<Resource>();\r
- if (type.equals(ProcessResource.plant3Dresource.FixedLengthInlineComponent)) {\r
- list.add(ProcessResource.plant3Dresource.SizeChangeComponent);\r
- list.add(ProcessResource.plant3Dresource.OffsetComponent);\r
- }\r
- return list;\r
- }\r
- \r
- /**\r
- * Returns control point type(s) for given control point type. \r
- * \r
- * @param types\r
- * @return\r
- */\r
- private List<Resource> getControlPointTypes(List<Resource> types) {\r
- Resource primaryType = types.get(0);\r
- List<Resource> cpTypes = new ArrayList<Resource>();\r
- if (primaryType == ProcessResource.plant3Dresource.FixedLengthInlineComponent) {\r
- if (types.size() == 1) {\r
- cpTypes.add(ProcessResource.plant3Dresource.FixedLengthControlPoint);\r
- } else {\r
- if (types.contains(ProcessResource.plant3Dresource.SizeChangeComponent)) {\r
- cpTypes.add(ProcessResource.plant3Dresource.SizeChangeControlPoint);\r
- }\r
- if (types.contains(ProcessResource.plant3Dresource.OffsetComponent)) {\r
- cpTypes.add(ProcessResource.plant3Dresource.OffsettingPoint);\r
- }\r
- if (cpTypes.size() == 0) {\r
- throw new RuntimeException("Cannot find control point type for " + primaryType);\r
- }\r
- }\r
- \r
- } else if (primaryType == ProcessResource.plant3Dresource.VariableLengthInlineComponent) {\r
- cpTypes.add(ProcessResource.plant3Dresource.VariableLengthControlPoint);\r
- } else if (primaryType == ProcessResource.plant3Dresource.FixedAngleTurnComponent) {\r
- cpTypes.add(ProcessResource.plant3Dresource.FixedAngleTurnControlPoint);\r
- } else if (primaryType == ProcessResource.plant3Dresource.VariableAngleTurnComponent) {\r
- cpTypes.add(ProcessResource.plant3Dresource.VariableAngleTurnControlPoint);\r
- } else if (primaryType == ProcessResource.plant3Dresource.EndComponent) {\r
- cpTypes.add(ProcessResource.plant3Dresource.EndComponentControlPoint);\r
- } else if (primaryType == ProcessResource.plant3Dresource.Nozzle) {\r
- cpTypes.add(ProcessResource.plant3Dresource.NozzleControlPoint);\r
- } else {\r
- throw new RuntimeException("Cannot find control point type for " + primaryType);\r
- }\r
- \r
- return cpTypes;\r
- }\r
- \r
- \r
- private class ComponentTypeDialog extends Dialog {\r
- \r
- List<Resource> selected = new ArrayList<Resource>();\r
- String name;\r
- \r
- public ComponentTypeDialog(Shell shell) {\r
- super(shell);\r
- }\r
- \r
- @Override\r
- protected Control createDialogArea(Composite parent) {\r
- Composite composite = (Composite) super.createDialogArea(parent);\r
- Label label = new Label(composite,SWT.NONE);\r
- label.setText("Name:");\r
- Text text = new Text(composite,SWT.SINGLE|SWT.BORDER);\r
- text.addModifyListener(new ModifyListener() {\r
- @Override\r
- public void modifyText(ModifyEvent e) {\r
- name = ((Text)e.widget).getText();\r
- }\r
- });\r
- GridData data = new GridData();\r
- data.grabExcessHorizontalSpace = true;\r
- data.horizontalAlignment = SWT.FILL;\r
- text.setLayoutData(data);\r
- label = new Label(composite,SWT.NONE);\r
- label.setText("Type:");\r
- for (Resource r : getComponentTypes()) {\r
- final Button b = new Button(composite,SWT.RADIO);\r
- b.setText(nameMap.get(r));\r
- b.setData(r);\r
- b.addSelectionListener(new SelectionAdapter() {\r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- Button button = (Button)e.widget;\r
- Resource res = (Resource)button.getData();\r
- if (button.getSelection()) {\r
- selected.add(0,res);\r
- } else {\r
- selected.remove(res);\r
- }\r
- }\r
- });\r
- data = new GridData();\r
- b.setLayoutData(data);\r
- for (Resource r2 : getComponentOptions(r)) {\r
- final Button b2 = new Button(composite,SWT.CHECK);\r
- b2.setText(nameMap.get(r2));\r
- b2.setData(r2);\r
- b.addSelectionListener(new SelectionAdapter() {\r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- b2.setEnabled(b.getSelection());\r
- Resource res = (Resource)b2.getData();\r
- if (!b.getSelection()) {\r
- selected.remove(res);\r
- } else if (b2.getSelection()) {\r
- selected.add(res);\r
- }\r
- }\r
- });\r
- b2.addSelectionListener(new SelectionAdapter() {\r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- Button button = (Button)e.widget;\r
- Resource res = (Resource)button.getData();\r
- if (button.getSelection()) {\r
- selected.add(res);\r
- } else {\r
- selected.remove(res);\r
- }\r
- }\r
- });\r
- b2.setEnabled(false);\r
- data = new GridData();\r
- data.horizontalIndent = convertWidthInCharsToPixels(2);\r
- b2.setLayoutData(data);\r
- }\r
- }\r
- \r
- return composite;\r
- }\r
- \r
- List<Resource> getSelection() {\r
- return selected;\r
- }\r
- \r
- public String getName() {\r
- return name;\r
- }\r
- }\r
-\r
-}\r