-/*******************************************************************************\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 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.layout.GridData;\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.processeditor.stubs.Equipment;\r
-import org.simantics.proconf.ui.ProConfUI;\r
-import org.simantics.proconf.ui.utils.ResourceAdaptionUtils;\r
-\r
-\r
-/**\r
- * Creates new equipment\r
- * \r
- * @author Marko Luukkainen <Marko.Luukkainen@vtt.fi>\r
- *\r
- */\r
-public class NewEquipmentHandler extends AbstractHandler {\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
- EquipmentDialog dialog = new EquipmentDialog(Display.getDefault().getActiveShell());\r
- if (dialog.open() == EquipmentDialog.CANCEL)\r
- return null;\r
- final String name = dialog.getName();\r
- if (name == null || name.length() == 0)\r
- return null;\r
- ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() {\r
- @Override\r
- public GraphRequestStatus perform(Graph g) throws Exception {\r
- Equipment equipment = Equipment.createDefault(g);\r
- Library l = new Library(g, lib);\r
- l.addStatement(g.getBuiltins().ConsistsOf, equipment);\r
- \r
- // TODO : is this correct (instance & inherits)\r
- equipment.addStatement(ProcessResource.builtins.Inherits, ProcessResource.plant3Dresource.Equipment);\r
-\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
- equipment.addStatement(ProcessResource.plant3Dresource.HasGraphics, model);\r
- equipment.setName(name);\r
- return GraphRequestStatus.transactionComplete();\r
- }\r
- });\r
- \r
- \r
- return null;\r
- }\r
- \r
- private class EquipmentDialog extends Dialog {\r
- \r
- private String name;\r
- \r
- public EquipmentDialog(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
- return composite;\r
- }\r
- \r
- public String getName() {\r
- return name;\r
- }\r
- }\r
-\r
-}\r