package org.simantics.processeditor.handlers; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Resource; import org.simantics.layer0.stubs.Library; import org.simantics.processeditor.stubs.Plant; import org.simantics.proconf.ui.ProConfUI; import org.simantics.proconf.ui.utils.ResourceAdaptionUtils; public class NewPlantHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection s = HandlerUtil.getCurrentSelectionChecked(event); IStructuredSelection ss = (IStructuredSelection) s; if (ss.size() != 1) return null; final Resource lib = ResourceAdaptionUtils.toSingleResource(ss); PlantDialog dialog = new PlantDialog(Display.getDefault().getActiveShell()); if (dialog.open() == PlantDialog.CANCEL) return null; final String name = dialog.getName(); if (name == null || name.length() == 0) return null; ProConfUI.getSession().asyncWrite(new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { Plant model = Plant.createDefault(g); model.setName(name); Library l = new Library(g, lib); l.addStatement(g.getBuiltins().ConsistsOf, model); return GraphRequestStatus.transactionComplete(); } }); return null; } private class PlantDialog extends Dialog { private String name; public PlantDialog(Shell shell) { super(shell); } @Override protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); Label label = new Label(composite,SWT.NONE); label.setText("Name:"); Text text = new Text(composite,SWT.SINGLE|SWT.BORDER); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { name = ((Text)e.widget).getText(); } }); GridData data = new GridData(); data.grabExcessHorizontalSpace = true; data.horizontalAlignment = SWT.FILL; text.setLayoutData(data); return composite; } public String getName() { return name; } } }