Mercurial > hg > ltpda
view src/MPipeline/src/mpipeline/MNode.java @ 46:ca0b8d4dcdb6 database-connection-manager
Fix
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Tue, 06 Dec 2011 19:07:27 +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.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; 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 java.util.ArrayList; import java.util.Iterator; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * Create a Node as an extension of an MElement. * * Nodes can have multiple pipes associated with them, but a * node can only be associated with a single port. If the port * is an input port, then the node should only be associated with * one pipe. * * @author hewitson */ public class MNode extends MElement { private ArrayList<MPipe> pipes = new ArrayList<MPipe>(); private Point center = new Point(); private int defaultRadius = 4; private int radius = defaultRadius; private Color color = Color.darkGray; private MPort myport = null; /** * Get the center coordinates of this node. * @return */ public Point getCenter() { return center; } /** * Set the center of the node. * @param center */ public void setCenter(Point center) { this.center = center; } /** * Get the node color. * @return */ public Color getColor() { return color; } /** * Set the node color * @param color */ public void setColor(Color color) { this.color = color; } /** * Get the port associated with this node. * @return */ public MPort getMyport() { return myport; } /** * Set the port associated with this node. * @param myport */ public void setMyport(MPort myport) { this.myport = myport; } /** * Create a copy of the node. * @param b * @return * @throws java.io.IOException * @throws java.lang.ClassNotFoundException */ public static MNode copy(MNode b) throws IOException, ClassNotFoundException { // 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())); MNode obj = (MNode) in.readObject(); return obj; } /** * Create a node associated with the given port. * @param p */ public MNode(MPort p) { myport = p; center.x = p.getParent().getX() + p.getX() + p.getWidth() / 2; center.y = p.getParent().getY() + p.getY() + p.getHeight() / 2; setupNode(); } /** * Create a node at the given point. * @param p */ public MNode(Point p) { center = p; setupNode(); } /** * Create a node at the given x,y coords * @param x * @param y */ public MNode(int x, int y) { center.x = x; center.y = y; setupNode(); } /** * Empty constructor. */ public MNode() { center.x = 0; center.y = 0; } /** * Return the list of pipes associated with this node. * @return */ public ArrayList<MPipe> getPipes() { return pipes; } /** * Clear the port of this node. */ public void clearPort() { myport = null; } /** * Get the port associated with this node. * @return */ public MPort getPort() { return myport; } /** * Get the radius of this node. * @return */ public int getRadius() { return radius; } /** * Move this node by dx,dy. * @param dx * @param dy */ @Override public void translate(int dx, int dy) { center.x += dx; center.y += dy; setupBounds(); // move my pipe Iterator it = pipes.iterator(); while (it.hasNext()) { MPipe p = (MPipe) it.next(); p.updatePipe(); } repaint(); } /** * Move this node to x,y. * @param x * @param y */ @Override public void moveTo(int x, int y) { center.x = x+radius; center.y = y+radius; setupBounds(); // move my pipe Iterator it = pipes.iterator(); while (it.hasNext()) { MPipe p = (MPipe) it.next(); p.updatePipe(); } repaint(); } /** * Add a pipe to this node. * * @param aThis */ public void addPipe(MPipe aThis) { pipes.add(aThis); } /** * Delete this node and any connected pipe */ public void delete() { MCanvas c = (MCanvas) getParent(); c.deletePipes(pipes); } /** * Attach this node to the XML DOM. * @param doc * @param node */ public void attachToDom(Document doc, Element node) { // start new connection for each pipe going from this node Element nodenode = doc.createElement("node"); // add each pipe Iterator pit = pipes.iterator(); while (pit.hasNext()) { MPipe mp = (MPipe) pit.next(); mp.attachToDom(doc, nodenode); } // attach to the DOM node.appendChild(nodenode); } /** * Return the requested pipe. * @param i * @return */ public MPipe getPipes(int i) { return pipes.get(i); } void zoomIn(float zoomStep) { radius = (int)Math.ceil(radius*zoomStep); setupBounds(); } void zoomOut(float zoomStep) { radius = (int)Math.floor(radius/zoomStep); setupBounds(); } /** * Set up this node. */ private void setupNode() { setupBounds(); this.setOpaque(false); this.setVisible(true); // this.addMouseMotionListener(new NodeMouseEvents()); } @Override public void scaleElement(){ radius = Math.round(scaleFactor*defaultRadius); setupBounds(); } /** * Set the bounds of this node. */ private void setupBounds() { this.setBounds(center.x - radius, center.y - radius, 2 * radius, 2 * radius); // System.out.println("Setting node bounds to " + getBounds().toString()); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(color); g.fillOval(0, 0, 2 * radius, 2 * radius); } /** * */ public void display(){ // dump some info String header = "------- NODE --------"; String footer = "-"; while (footer.length() < header.length()) { footer += "-"; } System.out.println(header); System.out.println(" parent block: " + getPort().getParent().toString()); System.out.println(" num pipes: " + pipes.size()); System.out.println(footer); } /** * Handle mouse events on the node. */ public class NodeMouseEvents implements Serializable, MouseListener, MouseMotionListener { /** * Create a new MBlockCoreMouseEvents listener. */ public NodeMouseEvents() { } public void mouseClicked(MouseEvent me) { System.out.println("Click on node: " + toString()); /* Pass to canvas */ Point pt = me.getPoint(); // the point that was clicked MPipe mp = (MPipe) getParent(); // get canvas MCanvas c = (MCanvas) mp.getParent(); int newX = getX() + pt.x; // transform to canvas coords int newY = getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_CLICKED, me.getWhen(), 0, newX, newY, me.getClickCount(), false, me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } public void mouseEntered(MouseEvent me) { color = Color.GRAY; repaint(); } public void mouseExited(MouseEvent me) { color = Color.red; repaint(); } public void mousePressed(MouseEvent me) { /* Pass to canvas */ Point pt = me.getPoint(); // the point that was clicked MPipe mp = (MPipe) getParent(); // get canvas MCanvas c = (MCanvas) mp.getParent(); int newX = getX() + pt.x; // transform to canvas coords int newY = getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, me.getWhen(), 0, newX, newY, me.getClickCount(), false, me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } public void mouseReleased(MouseEvent me) { /* Pass to canvas */ Point pt = me.getPoint(); // the point that was clicked MPipe mp = (MPipe) getParent(); // get canvas MCanvas c = (MCanvas) mp.getParent(); int newX = getX() + pt.x; // transform to canvas coords int newY = getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_RELEASED, me.getWhen(), 0, newX, newY, me.getClickCount(), false, me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } public void mouseDragged(MouseEvent me) { // pass this through to canvas underneath Point pt = me.getPoint(); MPipe mp = (MPipe) getParent(); // get canvas MCanvas c = (MCanvas) mp.getParent(); int newX = getX() + pt.x; // transform to canvas coords int newY = getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_DRAGGED, me.getWhen(), 0, newX, newY, me.getClickCount(), false, me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } public void mouseMoved(MouseEvent me) { /* Pass to canvas */ Point pt = me.getPoint(); // the point that was clicked MPipe mp = (MPipe) getParent(); // get canvas MCanvas c = (MCanvas) mp.getParent(); int newX = getX() + pt.x; // transform to canvas coords int newY = getY() + pt.y; // transform to canvas coords // create a new mouse event... MouseEvent transformed = new MouseEvent(c, MouseEvent.MOUSE_MOVED, me.getWhen(), 0, newX, newY, me.getClickCount(), false, me.getButton()); // ... and pass it to the canvas c.dispatchEvent(transformed); } } }