]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Creating equipment with predefined nozzles. 53/3553/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Mon, 18 Nov 2019 11:27:16 +0000 (13:27 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Mon, 18 Nov 2019 11:32:03 +0000 (11:32 +0000)
gitlab #27

Change-Id: I67d32b141c5c526d0215f5af3fd309d64962f49a
(cherry picked from commit 9817ec2aa0d2afada7024d2aba0cd7562de317ed)

org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction.java
org.simantics.plant3d/src/org/simantics/plant3d/actions/AddEquipmentAction2.java [new file with mode: 0644]
org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java
org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java [new file with mode: 0644]
org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java
org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java

index ff87c4ea2da4acf894b118d85bb8225c5e7abf5f..7efb0f83f2cc3892d135d5c1f424692223e6ec6c 100644 (file)
@@ -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 (file)
index 0000000..f232937
--- /dev/null
@@ -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);
+               }
+       }
+}
index 75b124119830199167eb827c1618b666a44b8161..7c124eb0ecb269076d4979bc1155305befc385dd 100644 (file)
@@ -97,6 +97,18 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange
         this.libUri = libUri;
     }
        
+        protected List<Item> 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<Item> turns = null;
                List<Item> 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 (file)
index 0000000..99ef9e7
--- /dev/null
@@ -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<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;
+    }
+
+}
index 4059d938f0d939f754dd0204190cf1452703d487..9051d44a55f0c79aa92e00078d19bf61296af3d0 100644 (file)
@@ -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) {
index 939d1551a5343e20526b9db50e0ad87039a43dc8..9b6ea1895507f359c671c54df8b7ff4e4f65cb50 100644 (file)
@@ -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"));