| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.addons.impl; |
| |
| import javax.vecmath.Matrix4d; |
| import javax.vecmath.Vector3d; |
| |
| import org.eclipse.apogy.addons.AbstractPickLocationToolNode; |
| import org.eclipse.apogy.addons.ApogyAddonsFactory; |
| import org.eclipse.apogy.addons.ApogyAddonsPackage; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.math.ApogyCommonMathFacade; |
| import org.eclipse.apogy.common.math.Tuple3d; |
| import org.eclipse.apogy.common.topology.ApogyCommonTopologyFacade; |
| import org.eclipse.apogy.common.topology.GroupNode; |
| import org.eclipse.apogy.common.topology.Node; |
| import org.eclipse.apogy.common.topology.NodePath; |
| import org.eclipse.apogy.common.topology.ui.NodePresentation; |
| import org.eclipse.apogy.common.topology.ui.NodeSelection; |
| import org.eclipse.apogy.core.topology.ApogyCoreTopologyFacade; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| |
| public class AbstractPickLocationToolCustomImpl extends AbstractPickLocationToolImpl { |
| |
| @Override |
| public void initialise() { |
| super.initialise(); |
| |
| // First, initialize the GeometryPlacementToolNode. |
| AbstractPickLocationToolNode toolNode = ApogyAddonsFactory.eINSTANCE.createAbstractPickLocationToolNode(); |
| { |
| toolNode.setDescription("Node associated with the Abstract Pick Location Tool named <" + getName() + ">"); |
| toolNode.setNodeId("ABSTRACT_PICK_LOCATION_TOOL_" + getName().replaceAll(" ", "_")); |
| } |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__ABSTRACT_PICK_LOCATION_TOOL_NODE, toolNode); |
| |
| // Attaches the tool node to the new to Node. |
| attachPickLocationToolNode(); |
| } |
| |
| @Override |
| public void setVisible(boolean newVisible) { |
| super.setVisible(newVisible); |
| setPickLocationToolNodeVisibility(newVisible); |
| } |
| |
| @Override |
| public void setRootNode(Node newRootNode) { |
| super.setRootNode(newRootNode); |
| |
| initializeNodeAndPosition(); |
| |
| // Update tool node visibility, |
| setPickLocationToolNodeVisibility(isVisible()); |
| } |
| |
| @Override |
| public void variablesInstantiated() { |
| super.variablesInstantiated(); |
| |
| initializeNodeAndPosition(); |
| } |
| |
| @Override |
| public void selectionChanged(NodeSelection nodeSelection) { |
| if (!isDisposed()) { |
| Node node = nodeSelection.getSelectedNode(); |
| |
| // Updates selected node. |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__SELECTED_NODE, node); |
| |
| // Updates TO Node Path |
| Node root = ApogyCoreTopologyFacade.INSTANCE.getApogyTopology().getRootNode(); |
| NodePath nodePath = ApogyCommonTopologyFacade.INSTANCE.createNodePath(root, node); |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__SELECTED_NODE_NODE_PATH, nodePath); |
| |
| // Updates relative position. |
| Tuple3d relativePosition = null; |
| if (nodeSelection.getRelativeIntersectionPoint() != null) { |
| relativePosition = ApogyCommonMathFacade.INSTANCE |
| .createTuple3d(nodeSelection.getRelativeIntersectionPoint()); |
| } |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__SELECTED_RELATIVE_POSITION, |
| relativePosition); |
| |
| // Updates relative normal. |
| Tuple3d relativeNormal = null; |
| if (nodeSelection.getRelativeIntersectionNormal() != null) { |
| relativeNormal = ApogyCommonMathFacade.INSTANCE.createTuple3d( |
| nodeSelection.getRelativeIntersectionNormal().x, |
| nodeSelection.getRelativeIntersectionNormal().y, |
| nodeSelection.getRelativeIntersectionNormal().z); |
| } |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__RELATIVE_INTERSECTION_NORMAL, |
| relativeNormal); |
| |
| // Detach tool node from previous selection. |
| detachPickLocationToolNode(); |
| |
| // Updates the tool node transformation. |
| if (getAbstractPickLocationToolNode() != null) { |
| Matrix4d matrix = new Matrix4d(); |
| matrix.setIdentity(); |
| matrix.setTranslation(new Vector3d(relativePosition.asTuple3d())); |
| |
| // TODO : Update the orientation ? |
| |
| getAbstractPickLocationToolNode().setTransformation(matrix); |
| } |
| |
| // Attaches the tool node to the new selected node. |
| attachPickLocationToolNode(); |
| } |
| } |
| |
| @Override |
| public void dispose() { |
| super.dispose(); |
| |
| if (this.abstractPickLocationToolNode != null) { |
| if (this.abstractPickLocationToolNode.getParent() instanceof GroupNode) { |
| GroupNode parent = (GroupNode) this.abstractPickLocationToolNode.getParent(); |
| parent.getChildren().remove(this.abstractPickLocationToolNode); |
| this.abstractPickLocationToolNode.setParent(null); |
| } |
| } |
| |
| // Clears the tool node. |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__ABSTRACT_PICK_LOCATION_TOOL_NODE, null); |
| } |
| |
| protected void initializeNodeAndPosition() { |
| // Resolve selected node from Node Path. |
| if (getSelectedNodeNodePath() != null) { |
| Node node = ApogyCommonTopologyFacade.INSTANCE.resolveNode(getRootNode(), getSelectedNodeNodePath()); |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogyAddonsPackage.Literals.ABSTRACT_PICK_LOCATION_TOOL__SELECTED_NODE, node); |
| } |
| |
| // Attaches the tool node to the new to Node. |
| attachPickLocationToolNode(); |
| } |
| |
| protected void setPickLocationToolNodeVisibility(boolean visible) { |
| if (getAbstractPickLocationToolNode() != null) { |
| NodePresentation nodePresentation = org.eclipse.apogy.common.topology.ui.Activator |
| .getTopologyPresentationRegistry().getPresentationNode(getAbstractPickLocationToolNode()); |
| if (nodePresentation != null) { |
| // Toggle visibility flag. |
| nodePresentation.setVisible(visible); |
| } |
| } |
| } |
| |
| protected void attachPickLocationToolNode() { |
| if (getAbstractPickLocationToolNode() != null) { |
| // First, detach node from previous node. |
| detachPickLocationToolNode(); |
| |
| if (getSelectedNode() != null) { |
| if (getSelectedNode() instanceof GroupNode) { |
| GroupNode groupNode = (GroupNode) getSelectedNode(); |
| groupNode.getChildren().add(getAbstractPickLocationToolNode()); |
| |
| if (getSelectedRelativePosition() != null) { |
| getAbstractPickLocationToolNode().setPosition(EcoreUtil.copy(getSelectedRelativePosition())); |
| } |
| } else if (getSelectedNode().getParent() instanceof GroupNode) { |
| GroupNode groupNode = (GroupNode) getSelectedNode().getParent(); |
| groupNode.getChildren().add(getAbstractPickLocationToolNode()); |
| |
| if (getSelectedRelativePosition() != null) { |
| getAbstractPickLocationToolNode().setPosition(EcoreUtil.copy(getSelectedRelativePosition())); |
| } |
| } |
| } |
| } |
| } |
| |
| protected void detachPickLocationToolNode() { |
| if (getAbstractPickLocationToolNode() != null |
| && getAbstractPickLocationToolNode().getParent() instanceof GroupNode) { |
| GroupNode parent = (GroupNode) getAbstractPickLocationToolNode().getParent(); |
| parent.getChildren().remove(getAbstractPickLocationToolNode()); |
| } |
| } |
| } // AbstractGeometryPlacementToolImpl |