package org.simantics.plant3d.actions; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.util.HashSet; import java.util.Set; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.resource.ResourceLocator; import org.eclipse.swt.widgets.Display; import org.simantics.db.Resource; import org.simantics.g3d.math.MathTools; import org.simantics.g3d.scenegraph.NodeMap; import org.simantics.g3d.scenegraph.base.INode; import org.simantics.g3d.vtk.swt.InteractiveVtkComposite; import org.simantics.g3d.vtk.swt.vtkSwtAction; import org.simantics.plant3d.Activator; import org.simantics.plant3d.dialog.ComponentSelectionDialog; import org.simantics.plant3d.gizmo.TerminalSelectionGizmo; 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.ComponentUtils; import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction; import org.simantics.plant3d.utils.Item; import org.simantics.utils.threads.ThreadUtils; import org.simantics.utils.ui.ExceptionUtils; import vtk.vtkProp; public class AddComponentAction extends vtkSwtAction { private P3DRootNode root; private PipelineComponent component; private NodeMap nodeMap; private TerminalSelectionGizmo gizmo; private Set allowed = new HashSet(); private Item toAdd = null; private PositionType insertPosition; @SuppressWarnings("unused") private boolean insertAdjustable; @SuppressWarnings("unused") private boolean lengthAdjustable; private String libUri; private double lengthFactor = 1.0; public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) { super(panel); this.root = root; setText("Add Component"); setImageDescriptor(ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/Component.png").get()); nodeMap = root.getNodeMap(); gizmo = new TerminalSelectionGizmo(panel); this.libUri = libUri; } public void setLengthFactor(double lengthFactor) { this.lengthFactor = lengthFactor; } public void setComponent(PipelineComponent component) { this.component = component; allowed.clear(); if (component instanceof Nozzle) { if (component.getNext() == null && component.getPrevious() == null) { allowed.add(PositionType.NEXT); } } else { if (component.getNext() == null || component.getControlPoint().isVariableLength()) { allowed.add(PositionType.NEXT); } if (component.getPrevious() == null || component.getControlPoint().isVariableLength()) { allowed.add(PositionType.PREVIOUS); } if (component instanceof InlineComponent && component.getControlPoint().isVariableLength()){ allowed.add(PositionType.SPLIT); } } setEnabled(allowed.size() > 0); } private String name; private Double length; private Double angle; private Double rotationAngle; private Double diameter; private Double thickness; private Double turnRadius; @Override public void run() { ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri); // Set list of already reserved component names dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root)); dialog.setLengthFactor(lengthFactor); if (dialog.open() == ComponentSelectionDialog.CANCEL) return; toAdd = dialog.getSelected(); if (toAdd == null) return; this.name = dialog.getName(); this.insertPosition = dialog.getInsertPosition(); this.insertAdjustable = dialog.isInsertAdjustable(); this.lengthAdjustable = dialog.isLenghtAdjustable(); this.length = dialog.getLength(); this.angle = dialog.getAngle(); this.rotationAngle = dialog.getRotationAngle(); this.diameter = dialog.getDiameter(); this.thickness = dialog.getThickness(); this.turnRadius = dialog.getTurnRadius(); allowed = dialog.filterAllowed(); gizmo.setComponent(component, allowed); super.run(); panel.refresh(); } @Override public boolean keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) panel.useDefaultAction(); return true; } public void attach() { if (component == null) return; super.attach(); ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() { public void run() { attachUI(); } }); } public void deattach() { // deactivate(); component = null; nodeMap.commit("Add component"); deattachUI(); super.deattach(); panel.refresh(); } private void attachUI() { //panel.setCursor(activeCursor); gizmo.attach(panel); } private void deattachUI() { //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); gizmo.deattach(); } @Override public boolean mouseMoved(MouseEvent e) { return getDefaultAction().mouseMoved(e); } @Override public boolean mousePressed(MouseEvent e) { return getDefaultAction().mousePressed(e); } @Override public boolean mouseReleased(MouseEvent e) { return getDefaultAction().mouseReleased(e); } @Override public boolean mouseDragged(MouseEvent e) { return getDefaultAction().mouseDragged(e); } public void doInsert(PositionType position) { try { if (position == PositionType.SPLIT && length != null && length > component.getControlPoint().getLength()) { MessageDialog.openError(panel.getComponent().getShell(), "Error", "There is no room for a component of length " + length * lengthFactor + " units"); return; } InsertInstruction inst = new InsertInstruction(); inst.typeUri = toAdd.getUri(); inst.name = name; inst.angle = angle != null ? MathTools.degToRad(angle) : null; inst.diameter = diameter; inst.thickness = thickness; inst.length = length; inst.turnRadius = turnRadius; inst.insertPosition = insertPosition; inst.rotationAngle = rotationAngle; inst.position = position; ComponentUtils.addComponent(root, component, inst); } catch (Exception e) { ExceptionUtils.logAndShowError("Cannot add component", e); } } public boolean mouseClicked(MouseEvent e) { if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) { int type = panel.getPickType(); //panel.setPickType(0); panel.setPickType(5); vtkProp[] picked = panel.pick(e.getX(), e.getY()); panel.setPickType(type); PositionType position = gizmo.getPickedPosition(picked); if (position != null) { doInsert(position); panel.useDefaultAction(); return true; } } return getDefaultAction().mouseClicked(e); } @Override public boolean mouseWheelMoved(MouseWheelEvent e) { return getDefaultAction().mouseWheelMoved(e); } }