view src/MPipeline/src/mpipeline/MElement.java @ 27:29276498ebdb database-connection-manager

Remove LTPDARepositoryManager implementation * * * Remove GUI helper
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

import java.awt.Dimension;
import java.awt.Point;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.JPanel;

/**
 * Base abstract class for elements on the canvas.
 * @author hewitson
 */
public abstract class MElement extends JPanel implements Serializable {

    /**
     *
     */
    protected float scaleFactor = 1.0f;
    /**
     *
     */
    protected Dimension mySize = new Dimension(10, 10);
    protected Dimension defaultSize = new Dimension(60, 60);
    /**
     *
     */
    protected String myName = null;
    /**
     * This is the position of the object but the view of the object
     * depends on this and the scale factor.
     */
    protected Point position = new Point(0, 0);
    /**
     *
     */
    protected static int myMinWidth = 30;
    /**
     *
     */
    protected static int myMinHeight = 30;



    /**
     * Scale the element size and position according to the 'scaleFactor'
     */
    public void scaleElement() {
        int nw = Math.round(scaleFactor * mySize.width);
        int nh = Math.round(scaleFactor * mySize.height);
        int nx = Math.round(scaleFactor * position.x);
        int ny = Math.round(scaleFactor * position.y);
        if (nw >= myMinWidth && nh >= myMinHeight){
            setSize(nw, nh);
        }
        setLocation(nx, ny);
    }

    public void setToDefaultSize() {
        mySize.width = defaultSize.width;
        mySize.height = defaultSize.height;
        scaleElement();
    }

    public Dimension getDefaultSize() {
        return defaultSize;
    }

    public void setDefaultSize(Dimension defaultSize) {
        this.defaultSize = defaultSize;
    }

    public static int getMyMinHeight() {
        return myMinHeight;
    }

    public static void setMyMinHeight(int myMinHeight) {
        MElement.myMinHeight = myMinHeight;
    }

    public static int getMyMinWidth() {
        return myMinWidth;
    }

    public static void setMyMinWidth(int myMinWidth) {
        MElement.myMinWidth = myMinWidth;
    }



    /**
     *
     * @param scaleFactor
     */
    public void setScaleFactor(float scaleFactor) {
        this.scaleFactor = scaleFactor;
        scaleElement();
        revalidate();
    }

    /**
     * Move the element to the given x,y coordinates.
     *
     * @param X
     * @param Y
     */
    public void moveTo(int X, int Y) {
        int dx = X - getX();
        int dy = Y - getY();
        translate(dx, dy);
    }

    /**
     * Translate the element by the given amount.
     *
     * @param dx
     * @param dy
     */
    public void translate(int dx, int dy) {
//        MCanvas c = (MCanvas) getParent();
        int nx = getX() + dx;
        int ny = getY() + dy;
        position.x = Math.round(nx / scaleFactor);
        position.y = Math.round(ny / scaleFactor);
        setLocation(nx, ny);
//        c.setNotSaved();
//        revalidate();
    }

    
    /**
     *
     * @return
     */
    public Dimension getMySize() {
        return mySize;
    }

    /**
     *
     * @param mySize
     */
    public void setMySize(Dimension mySize) {
        this.mySize = mySize;
    }

    /**
     *
     * @return
     */
    public Point getPosition() {
        return position;
    }

    /**
     *
     * @param position
     */
    public void setPosition(Point position) {
        this.position = position;
    }

    /**
     *
     * @return
     */
    public String getMyName() {
        return myName;
    }

    /**
     *
     * @param myName
     */
    public void setMyName(String myName) {
        this.myName = Utils.checkName(myName);
    }

    /**
     * Create a copy of an Element.
     *
     * @param b
     * @return
     * @throws java.io.IOException
     * @throws java.lang.ClassNotFoundException
     */
    public static MElement copy(MElement b) throws IOException, ClassNotFoundException {
        // Write the object out to a byte array
        FastByteArrayOutputStream fbos =
                new FastByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(fbos);
        out.writeObject(b);
        out.flush();
        out.close();

        // Retrieve an input stream from the byte array and read
        // a copy of the object back in.
        ObjectInputStream in = new ObjectInputStream(fbos.getInputStream());
        MElement obj = (MElement) in.readObject();
        return obj;

//        // Write the object out to a byte array
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ObjectOutputStream out = new ObjectOutputStream(bos);
//        out.writeObject(b);
//        out.flush();
//        out.close();
//
//        // Make an input stream from the byte array and read
//        // a copy of the object back in.
//        ObjectInputStream in = new ObjectInputStream(
//                new ByteArrayInputStream(bos.toByteArray()));
//        MElement obj = (MElement) in.readObject();
//        return obj;
    }
}