view java/src/uk/ac/qmul/eecs/ccmi/pdsupport/PdConnection.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
line wrap: on
line source
/*  
 accessPD - An accessible PD patches editor
  
 Copyright (C) 2014  Fiore Martin

 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.pdsupport;

import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.InputStream;

import uk.ac.qmul.eecs.ccmi.gui.Edge;
import uk.ac.qmul.eecs.ccmi.gui.GraphElement;
import uk.ac.qmul.eecs.ccmi.gui.LineStyle;
import uk.ac.qmul.eecs.ccmi.gui.Node;
import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;

public class PdConnection extends Edge implements PdElement{
	private static final long serialVersionUID = 1L;
	private static InputStream sound;
	private static String FROM = "from";
	private static String TO = "to";
	
	static{
		sound = PdConnection.class.getResourceAsStream("audio/PdConnection.mp3");
		SoundFactory.getInstance().loadSound(sound);
	}
	
	public PdConnection(){
		super("Connection", new String[] {FROM, TO}, 2, 2, LineStyle.Solid);
	}


	@Override
	public void draw(Graphics2D g2) {
        /* use this edge stroke */
        Stroke oldStroke = g2.getStroke();
        g2.setStroke(getStyle().getStroke());
        /* straight line  */

        if(points.isEmpty()){
                /* just one line from one node to the other */
                Line2D line = getSegment(getNodeAt(0),getNodeAt(1));
                g2.draw(line);
        }else{ 
                /* the edge has been bended into more lines.   * 
                 * for every inner point neighbour draw a line *
                 * and draw the inner point itself             */
                 for(InnerPoint p : points){
                         for(GraphElement ge : p.getNeighbours()){
                                 g2.draw(getSegment(p,ge)); 
                         }
                         p.draw(g2);
                 }

        }

        /* restore old stroke of g2 */
        g2.setStroke(oldStroke);
	}

	@Override
	public Rectangle2D getBounds() {
        Rectangle2D bounds = (Rectangle2D)getNodeAt(0).getBounds();

        for(int i=1; i< getNodesNum(); i++)
                bounds.add(getNodeAt(i).getBounds());
        
        for(InnerPoint p : points){
                bounds.add(p.getBounds());
        }

        return bounds;
	}

	@Override
	public InputStream getSound() {
		return sound;
	}


	@Override
	public String toPdFile() {
		
		Node n0 = getNodeAt(0);
		Node n1 = getNodeAt(1);
		
		PdElement elemFrom =  (PdElement) ( FROM.equals(this.getEndDescription(n0)) ? n0 : n1);
		PdElement elemTo =  (PdElement) ( TO.equals(this.getEndDescription(n0)) ? n0 : n1);
		
		return getChunckType() + 
				" connect " + 
				elemFrom.getOrderNumber()+ 
				" 0 " +
				elemTo.getOrderNumber() +  
				" 0"
				;
	}


	@Override
	public String getChunckType() {
		return "#X";
	}
	
	@Override
	public int getOrderNumber(){
		throw new UnsupportedOperationException();
	}
	
	public Object clone(){
		
		return new PdConnection();
	}

}