view java/src/uk/ac/qmul/eecs/ccmi/gui/HapticTrigger.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.gui;

import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionEvent;
import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionListener;
import uk.ac.qmul.eecs.ccmi.diagrammodel.ElementChangedEvent;
import uk.ac.qmul.eecs.ccmi.haptics.HapticsFactory;

/**
 * A CollectionListener that updates the haptic scene upon insertion deletion and 
 * movement of nodes and edges in the diagram.
 * 
 *
 */
public class HapticTrigger implements CollectionListener {

	@Override
	public void elementInserted(CollectionEvent evt) {
		DiagramEventSource source = (DiagramEventSource)evt.getSource(); 
		if(evt.getDiagramElement() instanceof Node){
			Node n = (Node)evt.getDiagramElement();
			HapticsFactory.getInstance().addNode(n.getBounds().getCenterX(), n.getBounds().getCenterY(), System.identityHashCode(n),source.getDiagramName());
		}else{//edge
			Edge e = (Edge)evt.getDiagramElement();
			Edge.PointRepresentation pr = e.getPointRepresentation();
			HapticsFactory.getInstance().addEdge(System.identityHashCode(e),pr.xs,pr.ys,pr.adjMatrix,pr.nodeStart,e.getStipplePattern(),e.getNameLine(),source.getDiagramName());
		}
	}

	@Override
	public void elementTakenOut(CollectionEvent evt) {
		DiagramEventSource source = (DiagramEventSource)evt.getSource(); 
		if(evt.getDiagramElement() instanceof Node){
			Node n = (Node)evt.getDiagramElement();
			HapticsFactory.getInstance().removeNode(System.identityHashCode(n),source.getDiagramName());
		}else{//edge
			Edge e = (Edge)evt.getDiagramElement();
			HapticsFactory.getInstance().removeEdge(System.identityHashCode(e),source.getDiagramName());
		}
	}

	@Override
	public void elementChanged(ElementChangedEvent evt) {
		DiagramEventSource source = (DiagramEventSource)evt.getSource(); 
		if("stop_move".equals(evt.getChangeType()) || "remove_node".equals(evt.getChangeType())){
			if(evt.getDiagramElement() instanceof Edge){
				Edge e = (Edge)evt.getDiagramElement();
				Edge.PointRepresentation pr = e.getPointRepresentation();
				HapticsFactory.getInstance().updateEdge(
						System.identityHashCode(e), 
						pr.xs, 
						pr.ys,
						pr.adjMatrix,
						pr.nodeStart, 
						e.getNameLine(),
						source.getDiagramName());
			}else{
				Node n = (Node)evt.getDiagramElement();		
				HapticsFactory.getInstance().moveNode(
						n.getBounds().getCenterX(),
						n.getBounds().getCenterY(),
						System.identityHashCode(n),
						source.getDiagramName()
				);
			}
		}
	}
	   
}