| /******************************************************************************* |
| * 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, |
| * Sebastien Gemme - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.topology.ui.impl; |
| |
| import java.lang.ref.WeakReference; |
| |
| import org.eclipse.apogy.common.math.ApogyCommonMathFactory; |
| import org.eclipse.apogy.common.math.Tuple3d; |
| import org.eclipse.apogy.common.topology.ui.Activator; |
| import org.eclipse.apogy.common.topology.ui.ApogyCommonTopologyUIPackage; |
| import org.eclipse.apogy.common.topology.ui.NodePresentation; |
| import org.eclipse.apogy.common.topology.ui.SceneObject; |
| import org.eclipse.emf.common.notify.Adapter; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.jface.util.IPropertyChangeListener; |
| import org.eclipse.jface.util.PropertyChangeEvent; |
| |
| public class NodePresentationCustomImpl extends NodePresentationImpl { |
| |
| private Adapter adapter; |
| private final NodePresentationRef ref; |
| |
| protected NodePresentationCustomImpl() { |
| super(); |
| this.eAdapters().add(getAdapter()); |
| |
| // Register a listener to the preference store |
| this.ref = new NodePresentationRef(this); |
| Activator.getDefault().getPreferenceStore().addPropertyChangeListener(this.ref); |
| } |
| |
| public Tuple3d basicGetCentroid() { |
| Tuple3d tuple = ApogyCommonMathFactory.eINSTANCE.createTuple3d(); |
| tuple.setX(0.0); |
| tuple.setY(0.0); |
| tuple.setZ(0.0); |
| |
| return tuple; |
| } |
| |
| public Tuple3d basicGetMin() { |
| Tuple3d tuple = null; |
| |
| if (getSceneObject() != null && getSceneObject().getMin() != null) { |
| tuple = ApogyCommonMathFactory.eINSTANCE.createTuple3d(); |
| |
| javax.vecmath.Tuple3d min = getSceneObject().getMin(); |
| |
| tuple.setX(min.x); |
| tuple.setY(min.y); |
| tuple.setZ(min.z); |
| } |
| |
| return tuple; |
| } |
| |
| public Tuple3d basicGetMax() { |
| Tuple3d tuple = null; |
| |
| if (getSceneObject() != null && getSceneObject().getMax() != null) { |
| tuple = ApogyCommonMathFactory.eINSTANCE.createTuple3d(); |
| javax.vecmath.Tuple3d max = getSceneObject().getMax(); |
| |
| tuple.setX(max.x); |
| tuple.setY(max.y); |
| tuple.setZ(max.z); |
| } |
| |
| return tuple; |
| } |
| |
| public double getXRange() { |
| double range = 0.0; |
| if (getMin() != null && getMax() != null) { |
| range = Math.abs(getMin().getX() - getMax().getX()); |
| } |
| return range; |
| } |
| |
| public double getYRange() { |
| double range = 0.0; |
| if (getMin() != null && getMax() != null) { |
| range = Math.abs(getMin().getY() - getMax().getY()); |
| } |
| return range; |
| } |
| |
| public double getZRange() { |
| double range = 0.0; |
| if (getMin() != null && getMax() != null) { |
| range = Math.abs(getMin().getZ() - getMax().getZ()); |
| } |
| return range; |
| } |
| |
| @Override |
| public void setSceneObject(SceneObject newSceneObject) { |
| |
| // why are two NodePresentation created for each node ? |
| |
| boolean updateRequired = (newSceneObject != this.sceneObject); |
| |
| super.setSceneObject(newSceneObject); |
| |
| // Initialize the scene object just set if required. |
| if (updateRequired) { |
| initialSceneObject(); |
| } |
| } |
| |
| protected void initialSceneObject() { |
| // Applies the current values of the preferences. |
| applyPreferences(); |
| |
| this.sceneObject.setVisible(isVisible()); |
| this.sceneObject.setColor(getColor()); |
| this.sceneObject.setIDVisible(isIdVisible()); |
| this.sceneObject.setSelected(isSelected()); |
| } |
| |
| protected void applyPreferences() { |
| } |
| |
| protected void updateSceneObject(Notification notification) { |
| if (notification.getNotifier() instanceof NodePresentation) { |
| int featureId = notification.getFeatureID(NodePresentation.class); |
| |
| switch (featureId) { |
| case ApogyCommonTopologyUIPackage.NODE_PRESENTATION__COLOR: |
| this.sceneObject.setColor(getColor()); |
| break; |
| |
| case ApogyCommonTopologyUIPackage.NODE_PRESENTATION__VISIBLE: |
| this.sceneObject.setVisible(isVisible()); |
| break; |
| |
| case ApogyCommonTopologyUIPackage.NODE_PRESENTATION__SHADOW_MODE: |
| this.sceneObject.setShadowMode(getShadowMode()); |
| break; |
| |
| case ApogyCommonTopologyUIPackage.NODE_PRESENTATION__ID_VISIBLE: |
| this.sceneObject.setIDVisible(isIdVisible()); |
| break; |
| |
| case ApogyCommonTopologyUIPackage.NODE_PRESENTATION__SELECTED: |
| this.sceneObject.setSelected(isSelected()); |
| break; |
| |
| default: |
| break; |
| } |
| } |
| } |
| |
| private Adapter getAdapter() { |
| if (this.adapter == null) { |
| this.adapter = new AdapterImpl() { |
| @Override |
| public void notifyChanged(Notification notification) { |
| if (NodePresentationCustomImpl.this.sceneObject != null) { |
| updateSceneObject(notification); |
| } |
| } |
| }; |
| } |
| return this.adapter; |
| } |
| |
| /** |
| * This class has been created to solve a memory leak problem. |
| * |
| * @author sgemme |
| * |
| */ |
| private class NodePresentationRef extends WeakReference<NodePresentationCustomImpl> |
| implements IPropertyChangeListener { |
| |
| public NodePresentationRef(NodePresentationCustomImpl nodePresentation) { |
| super(nodePresentation); |
| } |
| |
| @Override |
| public void propertyChange(PropertyChangeEvent event) { |
| if (get() != null) { |
| get().applyPreferences(); |
| } else { |
| Activator.getDefault().getPreferenceStore().removePropertyChangeListener(this); |
| } |
| } |
| } |
| |
| } // NodePresentationImpl |