From df57dcb3ec91ed3393991d6f890776772d742128 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Mon, 18 Nov 2019 13:27:16 +0200 Subject: [PATCH] Creating equipment with predefined nozzles. gitlab #27 Change-Id: I67d32b141c5c526d0215f5af3fd309d64962f49a (cherry picked from commit 9817ec2aa0d2afada7024d2aba0cd7562de317ed) --- .../plant3d/actions/AddEquipmentAction.java | 30 +++- .../plant3d/actions/AddEquipmentAction2.java | 45 +++++ .../dialog/ComponentSelectionDialog.java | 18 +- .../dialog/EquipmentSelectionDialog.java | 156 ++++++++++++++++++ .../plant3d/editor/Plant3DEditor.java | 7 +- .../plant3d/utils/ComponentUtils.java | 26 ++- 6 files changed, 270 insertions(+), 12 deletions(-) create mode 100644 org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction2.java create mode 100644 org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction.java b/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction.java index ff87c4ea..7efb0f83 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction.java @@ -1,27 +1,47 @@ 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); diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction2.java b/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction2.java new file mode 100644 index 00000000..f232937f --- /dev/null +++ b/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction2.java @@ -0,0 +1,45 @@ +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); + } + } +} diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java index 75b12411..7c124eb0 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java @@ -97,6 +97,18 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange this.libUri = libUri; } + protected List getItems(Class c, String libUri) throws DatabaseException{ + if (InlineComponent.class.equals(c)) { + return P3DUtil.getInlines(libUri); + } else if (TurnComponent.class.equals(c)) { + return P3DUtil.getTurns(libUri); + } else if (EndComponent.class.equals(c)) { + return P3DUtil.getEnds(libUri); + } else { + return null; + } + } + @Override protected Control createDialogArea(Composite parent) { resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent); @@ -123,9 +135,9 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange List turns = null; List inlines = null; try { - ends = P3DUtil.getEnds(libUri); - turns= P3DUtil.getTurns(libUri); - inlines = P3DUtil.getInlines(libUri); + ends = getItems(EndComponent.class, libUri); + turns= getItems(TurnComponent.class, libUri); + inlines = getItems(InlineComponent.class, libUri); } catch (DatabaseException e) { Label label = new Label(composite, SWT.NONE); label.setText("Cannot load pipeline components: " + e.getMessage()); diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java new file mode 100644 index 00000000..99ef9e7c --- /dev/null +++ b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java @@ -0,0 +1,156 @@ +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 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 equipment = null; + List 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; + } + +} diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java index 4059d938..9051d44a 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java @@ -418,9 +418,10 @@ public class Plant3DEditor extends ResourceEditorPart { } try { if (selected.size() == 0) { - for (Item eq : P3DUtil.getEquipments(getLibraryUri())) { - m.add(new AddEquipmentAction(rootNode, eq)); - } + m.add(new AddEquipmentAction(rootNode, getLibraryUri())); +// for (Item eq : P3DUtil.getEquipments(getLibraryUri())) { +// m.add(new AddEquipmentAction(rootNode, eq)); +// } } else if (selected.size() == 1) { IP3DNode node = (IP3DNode)selected.get(0); if (node instanceof Equipment) { diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java index 939d1551..9b6ea189 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java @@ -180,6 +180,25 @@ public class ComponentUtils { 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); @@ -214,7 +233,12 @@ public class ComponentUtils { 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")); -- 2.47.1