import java.awt.*; import java.awt.event.*; /** This class contains the button-, command- and macrocommand-objects. It draws the objects to the applet-surface and lets the user move around and connect the objects. */ public class ConstructionPanel extends Panel implements MouseListener, MouseMotionListener { /** defines the number of possible buttons */ private static int MAX_BUTTONS = 5; /** defines the number of possible commands */ private static int MAX_COMMANDS = 5; /** defines the number of possible macro-commands */ private static int MAX_MACROS = 5; /** defines the number of possible connections */ private static int MAX_CONNECTIONS = 10; /** represents one of the two connection-points of a connection */ public class ConnectionNode { /* the object the connection is connected to */ private CP_Item item; /* the port to which the connection is bound */ private int port; public ConnectionNode(CP_Item _item, int _port) { item = _item; port = _port; } } /** a list of button-objects */ private CP_Button buttons[] = new CP_Button[MAX_BUTTONS]; /** a list of command-objects */ private CP_Command commands[] = new CP_Command[MAX_COMMANDS]; /** a list of macrocommand-objects */ private CP_MacroCommand macros[]= new CP_MacroCommand[MAX_MACROS]; /** a list of connection-objects */ private CP_Connection connections[] = new CP_Connection[MAX_CONNECTIONS]; /** the current number of buttons */ private int n_buttons=0; /** the current number of commands */ private int n_commands=0; /** the current number of macros */ private int n_macros=0; /** the currently edited node */ private ConnectionNode node=null; /** help variables */ private int mx, my; /** The constructor. */ public ConstructionPanel() { addMouseListener(this); addMouseMotionListener(this); // mark all connection-objects as not available for (int i=0;ix-w/2) && (mxy-h/2) && (myx+w/2) && (mxy-3) && (myx-w/2) && (mxy-h/2) && (myx-w/2-6)&& (mxy-3) && (myx-w/2) && (mxy-h/2) && (myx-w/2-6) && (mxy-3) && (myx+w/2) && (mxy-18) && (myx+w/2) && (mxy-3) && (myx+w/2) && (mxy+12) && (my If node.port is >0 a port has been selected and we have to draw a connection (that means a line from the selected port to the mouse-pointer. Therefor mx and my will be updated and the panel will be redrawn. */ public void mouseDragged(MouseEvent e) { if (node != null) { if (node.port<0) { node.item.setPosition(e.getX(), e.getY()); } mx = e.getX(); my = e.getY(); repaint(); } } public void mouseMoved(MouseEvent e) { } /** Finds out whether an object was hit or not. */ public void mousePressed(MouseEvent e) { mx = e.getX(); my = e.getY(); node = getConnectionNode(mx, my); if (node != null) if (node.port>=0) { CP_Connection c = null; c = node.item.getConnection(node.port); if (c != null) { c.getSrc().setConnection(node.port,null); c.getDest().setConnection(node.port,null); int i=0; while ((i=0) { ConnectionNode node2 = getConnectionNode(e.getX(),e.getY()); if (node2 != null) { CP_Connection c = null; c = node2.item.getConnection(node2.port); if (c != null) { c.getSrc().setConnection(c.getSrcPort(),null); c.getDest().setConnection(c.getDestPort(),null); if (c.getSrc().getType()==1) { ((CP_Button)c.getSrc()).getButton().setCommand(null); } if (c.getSrc().getType()==3) { ((CP_MacroCommand)c.getSrc()).getMacroCommand().setCommand(c.getSrcPort(),null); } if (c.getDest().getType()==1) { ((CP_Button)c.getDest()).getButton().setCommand(null); } if (c.getDest().getType()==3) { ((CP_MacroCommand)c.getDest()).getMacroCommand().setCommand(c.getDestPort(),null); } int i=0; while ((i=0) g.drawLine(node.item.getPortX(node.port), node.item.getPortY(node.port), mx, my); } } /** Adds a button-object to the panel. With the constructed CP_Button object a reference to the linked button will be saved. */ public void addButton(MyButton _b, int x, int y) { if (n_buttons < MAX_BUTTONS) { buttons[n_buttons] = new CP_Button(_b); buttons[n_buttons].setName(_b.getLabel()); buttons[n_buttons].setPosition(x,y); n_buttons++; } } /** Adds a command-object to the panel. With the constructed CP_Command object a reference to the linked command will be saved. */ public void addCommand(Command _command, String desc, int x, int y) { if (n_commands < MAX_COMMANDS) { commands[n_commands] = new CP_Command(_command); commands[n_commands].setName(desc); commands[n_commands].setPosition(x,y); n_commands++; } } /** Adds a macro-object to the panel. With the constructed CP_MacroCommand object a reference to the linked macro will be saved. */ public void addMacroCommand(MacroCommand _macro, String desc, int x, int y) { if (n_macros < MAX_MACROS) { macros[n_macros] = new CP_MacroCommand(_macro); macros[n_macros].setName(desc); macros[n_macros].setPosition(x,y); n_macros++; } } }