FreeFormEdit.java

 package macrolanguage; import java.awt.*; import java.awt.event.*; import macrolanguage.EditToolbar; import macrolanguage.NotAContainerException;

public class FreeFormEdit extends Frame implements KeyListener, WindowListener { /**   * The constructor creates the listeners with the correct layout * for the free form design. **/ public FreeFormEdit {   super;

addWindowListener(this); addKeyListener(this); addMouseListener(mouseManager);

setLayout(new FlowLayout);

// default size. setSize(100,100);

show; }

/**  * Add a component to an x and y location. * This method will be called by the user putting a component on  * the form or an embedded container. This method throws a  * NotAContainerException when the x and y location points at a   * component, since components cannot be placed on other components * in Java (this is untrue with regarding lightweight components). **/ public void add(Component c, int x, int y) throws NotAContainerException {   // Check whether there is a component at the x and y location // and if so throw NotAContainerException. Component temp = getComponentAt(x,y); if(!(temp instanceof Container)) throw (new NotAContainerException("Tried to add a component to something other than a container."));

// Add listeners to events regarding these componenets. // The listening is performed in the MouseManager class to avoid // bloating this class. c.addMouseMotionListener(mouseManager); c.addMouseListener(mouseManager);

// Add the component to the container. ((Container)temp).add(c);

// Set the relative x and y location. c.setLocation(x, y); }

/**  * Set the selection Component to be the selected Component. **/ public void setSelection(Component selection) {   if (this.selection != selection) {     this.selection = selection; repaint; } }

/**  * Places the component comp on the next click on the free form * edit surface. **/ public void placeComponentOnNextClick(Component comp) {   // Change the cursor so it will be evident we are in the middle // of a drop operation. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); mouseManager.placeComponentOnNextClick(comp); }

/**  *  **/  public void removeSelection {   if(selection != this) {     remove(selection); selection = this; repaint; } }

/**  * This method is a part of the keylistener interface used * to listen to keyboard events. **/ public void keyTyped(java.awt.event.KeyEvent ev) { }

/**  * This method is a part of the keylistener interface used * to listen to keyboard events. **/ public void keyPressed(java.awt.event.KeyEvent ev) { }

/**  * This method is a part of the keylistener interface used * to listen to keyboard events. **/ public void keyReleased(java.awt.event.KeyEvent ev) { }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowOpened(java.awt.event.WindowEvent ev) { }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowClosing(java.awt.event.WindowEvent ev) {   System.exit(0); }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowClosed(java.awt.event.WindowEvent ev) { }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowIconified(java.awt.event.WindowEvent ev) { }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowDeiconified(java.awt.event.WindowEvent ev) { }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowActivated(java.awt.event.WindowEvent ev) { }

/**  * This method is a part of the Windowlistener interface used * to listen to window events. **/ public void windowDeactivated(java.awt.event.WindowEvent ev) { }

/**  * The paint method draws the marking around the selection. **/ public void paint(Graphics g)  { super.paint(g);

// If the frame is selected do nothing. if(selection != this) {     // If the selection is not in this container. if(selection.getParent != this) // Draw little squares around the four corners of the selected component. drawSelection(selection,                     selection.getParent.getGraphics); else // Draw little squares around the four corners of the selected component. drawSelection(selection, g); } }

/**  * Draw a thin line around the selected component. **/ private void drawSelection(Component select, Graphics g)  { g.setColor(Color.black);

g.fillRect(select.getLocation.x - SQUARE_SIZE,              select.getLocation.y - SQUARE_SIZE,               select.getSize.width + SQUARE_SIZE,               select.getSize.height + SQUARE_SIZE); }

// The currently selected component. By default this frame. private Component selection = this;

// The MouseManager takes over the full responsibility of manageing the // mouse events for the form designing. private MouseManager mouseManager = new MouseManager(this);

// The default width of the line surrounding selected components. private static final int SQUARE_SIZE = 2; } 