package org.simantics.plant3d.actions;
import org.eclipse.jface.action.Action;
+import org.eclipse.swt.widgets.Display;
+import org.simantics.plant3d.dialog.EquipmentSelectionDialog;
import org.simantics.plant3d.scenegraph.Equipment;
import org.simantics.plant3d.scenegraph.P3DRootNode;
import org.simantics.plant3d.utils.ComponentUtils;
import org.simantics.plant3d.utils.Item;
import org.simantics.utils.ui.ExceptionUtils;
+/**
+ * Action that allows user to choose added Equipment with a dialog.
+ *
+ * @author luukkainen
+ *
+ */
public class AddEquipmentAction extends Action {
P3DRootNode root;
- private Item item;
- public AddEquipmentAction(P3DRootNode root, Item item) {
+ private String libUri;
+
+ public AddEquipmentAction(P3DRootNode root, String libUri) {
this.root = root;
- this.item = item;
- setText("Add " + item.getName());
+ this.libUri = libUri;
+ setText("Add equipment");
}
@Override
public void run() {
+ EquipmentSelectionDialog dialog = new EquipmentSelectionDialog(Display.getCurrent().getActiveShell(), root);
+ if (dialog.open() == EquipmentSelectionDialog.CANCEL)
+ return;
+ Item item = dialog.getSelected();
+ if (item == null)
+ return;
+ Item selectedNozzle = dialog.getSelectedNozzle();
try {
- Equipment equipment = ComponentUtils.createEquipment(root, item);
+ Equipment equipment = null;
+ if (selectedNozzle == null)
+ equipment = ComponentUtils.createEquipment(root, item);
+ else
+ equipment = ComponentUtils.createEquipmentWithNozzles(root, item, selectedNozzle);
root.getNodeMap().commit("Add equipment " + equipment.getName());
} catch (Exception e) {
ExceptionUtils.logAndShowError("Cannot create equipment",e);
--- /dev/null
+package org.simantics.plant3d.actions;
+
+import org.eclipse.jface.action.Action;
+import org.simantics.plant3d.scenegraph.Equipment;
+import org.simantics.plant3d.scenegraph.P3DRootNode;
+import org.simantics.plant3d.utils.ComponentUtils;
+import org.simantics.plant3d.utils.Item;
+import org.simantics.utils.ui.ExceptionUtils;
+
+/**
+ * Action that creates given Equipment.
+ *
+ * @author luukkainen
+ *
+ */
+public class AddEquipmentAction2 extends Action {
+
+ P3DRootNode root;
+ private Item item;
+ private Item nozzleItem;
+
+ public AddEquipmentAction2(P3DRootNode root, Item item) {
+ this(root, item, null);
+ }
+
+ public AddEquipmentAction2(P3DRootNode root, Item item, Item nozzleItem) {
+ this.root = root;
+ this.item = item;
+ setText("Add " + item.getName());
+ }
+
+ @Override
+ public void run() {
+ try {
+ Equipment equipment = null;
+ if (nozzleItem == null)
+ equipment = ComponentUtils.createEquipment(root, item);
+ else
+ equipment = ComponentUtils.createEquipmentWithNozzles(root, item, nozzleItem);
+ root.getNodeMap().commit("Add equipment " + equipment.getName());
+ } catch (Exception e) {
+ ExceptionUtils.logAndShowError("Cannot create equipment",e);
+ }
+ }
+}
--- /dev/null
+package org.simantics.plant3d.dialog;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.jface.resource.ResourceManager;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ListViewer;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.ExpandBar;
+import org.eclipse.swt.widgets.ExpandItem;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.plant3d.ontology.Plant3D;
+import org.simantics.plant3d.scenegraph.Equipment;
+import org.simantics.plant3d.scenegraph.InlineComponent;
+import org.simantics.plant3d.scenegraph.Nozzle;
+import org.simantics.plant3d.scenegraph.P3DRootNode;
+import org.simantics.plant3d.scenegraph.PipelineComponent;
+import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
+import org.simantics.plant3d.utils.Item;
+import org.simantics.plant3d.utils.P3DUtil;
+import org.simantics.utils.ui.ExceptionUtils;
+
+public class EquipmentSelectionDialog extends Dialog implements ISelectionChangedListener{
+
+ private ResourceManager resourceManager;
+ private String libUri;
+ private P3DRootNode root;
+
+ private Item selected;
+ private Item selectedNozzle;
+
+ ListViewer equipmentViewer;
+ ListViewer nozzleViewer;
+
+ public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root) {
+ this(parentShell,root,Plant3D.URIs.Builtin);
+ }
+
+ public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root, String libUri){
+ super(parentShell);
+ this.root = root;
+ this.libUri = libUri;
+ }
+
+ protected List<Item> getItems(Class c, String libUri) throws DatabaseException{
+ if (Equipment.class.equals(c)) {
+ return P3DUtil.getEquipments(libUri);
+ } else if (Nozzle.class.equals(c)) {
+ return P3DUtil.getNozzles(libUri);
+ }
+ return null;
+ }
+
+ @Override
+ protected Control createDialogArea(Composite parent) {
+ resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
+
+ Composite composite = new Composite(parent, SWT.NONE);
+ GridLayout layout = new GridLayout(2,false);
+ 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);
+ composite.setLayoutData(new GridData(GridData.FILL_BOTH));
+ applyDialogFont(composite);
+
+ List<Item> equipment = null;
+ List<Item> nozzles = null;
+ try {
+ equipment = getItems(Equipment.class, libUri);
+ nozzles = getItems(Nozzle.class, libUri);
+ } catch (DatabaseException e) {
+ Label label = new Label(composite, SWT.NONE);
+ label.setText("Cannot load equipment data: " + e.getMessage());
+ ExceptionUtils.logError(e);
+ return composite;
+ }
+ ExpandBar expandBar = new ExpandBar(composite, SWT.NONE);
+
+ ExpandItem equipmentItem = new ExpandItem(expandBar, SWT.NONE);
+
+ equipmentItem.setText("Equipment");
+ equipmentViewer = new ListViewer(expandBar);
+ equipmentViewer.setLabelProvider(new ComponentLabelProvider());
+ equipmentViewer.setContentProvider(new ComponentContentProvider());
+
+ ExpandItem nozzleItem = new ExpandItem(expandBar, SWT.NONE);
+
+ nozzleItem.setText("Nozzles");
+ nozzleViewer = new ListViewer(expandBar);
+ nozzleViewer.setLabelProvider(new ComponentLabelProvider());
+ nozzleViewer.setContentProvider(new ComponentContentProvider());
+
+ equipmentItem.setControl(equipmentViewer.getList());
+ nozzleItem.setControl(nozzleViewer.getList());
+
+ equipmentViewer.setInput(equipment);
+ nozzleViewer.setInput(nozzles);
+
+ equipmentItem.setHeight(equipmentViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
+ nozzleItem.setHeight(nozzleViewer.getList().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
+
+ equipmentViewer.addSelectionChangedListener(this);
+ nozzleViewer.addSelectionChangedListener(this);
+
+ GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(2, 1).applyTo(expandBar);
+ GridDataFactory.fillDefaults().minSize(500, 500).hint(500, 500).applyTo(composite);
+
+ return composite;
+ }
+
+ @Override
+ public void selectionChanged(SelectionChangedEvent event) {
+ IStructuredSelection sel = (IStructuredSelection)event.getSelection();
+ Item i = (Item)sel.getFirstElement();
+ if (i != null) {
+ if (event.getSource() == equipmentViewer) {
+ selected = i;
+ } else if (event.getSource() == nozzleViewer) {
+ selectedNozzle = i;
+ }
+
+ } else {
+ if (event.getSource() == equipmentViewer) {
+ selected = i;
+ } else if (event.getSource() == nozzleViewer) {
+ selectedNozzle = i;
+ }
+ }
+ }
+
+ public Item getSelected() {
+ return selected;
+ }
+
+ public Item getSelectedNozzle() {
+ return selectedNozzle;
+ }
+
+}
return equipment;
}
+ public static Equipment createEquipmentWithNozzles(P3DRootNode root, String typeURI, String nozzleTypeUri) throws Exception {
+ GeometryProvider provider = providers.get(typeURI);
+ if (provider == null) {
+ load(typeURI);
+ provider = providers.get(typeURI);
+ }
+ Equipment equipment = root.createEquipment();
+ equipment.setType(typeURI);
+ equipment.setGeometry(provider);
+ root.addChild(equipment);
+
+ for (int i = 0; i < equipment.numberOfFixedNozzles(); i++) {
+ createNozzle(root, equipment, new Item(nozzleTypeUri, "Nozzle"));
+
+ }
+
+ return equipment;
+ }
+
public static InlineComponent createStraight(P3DRootNode root) throws Exception{
InlineComponent component = root.createInline();
component.setType(Plant3D.URIs.Builtin_Straight);
return equipment;
}
-
+ public static Equipment createEquipmentWithNozzles(P3DRootNode root, Item equipmentType, Item nozzleType) throws Exception {
+ Equipment equipment = createEquipmentWithNozzles(root, equipmentType.getUri(), nozzleType.getUri());
+ String n = root.getUniqueName(equipmentType.getName());
+ equipment.setName(n);
+ return equipment;
+ }
public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));