EditToolbar.java

From EDM2
Jump to: navigation, search
package macrolanguage;
import java.awt.*;
import java.awt.event.*;
import macrolanguage.*;

/**
 * The class EditToolbar supplies a toolbar for adding and manipulating
 * objects on the FreeFormEdit.
 * EditToolbar is simply a user interface class which delegates all of
 * the functionality to the free form editing.
**/
public class EditToolbar extends Window implements ActionListener
{
  /**
   * The constructor sets the UI to the correct appearance.
  **/
  public EditToolbar(FreeFormEdit form)
  {
    super(form);

    // The FreeFormEdit instance owning this toolbar.
    this.form = form;

    // The default layout currently. This is subject to
    // profound change.
    setLayout(new FlowLayout());

    // Adding the components to the toolbar.
    add(classNameEntry);
    add(addClassButton);
    add(deleteSelectedButton);
    add(exitButton);

    // Listening to the component events.
    addClassButton.addActionListener(this);
    deleteSelectedButton.addActionListener(this);
    exitButton.addActionListener(this);

    // Set the size of the window to fit the Components in it.
    pack();

    // Set the FreeFormEdit to be right under this window.
    form.setLocation(getLocation().x , getLocation().y + getSize().height);

    // Show the window.
    show();
  }

  public void actionPerformed(java.awt.event.ActionEvent ev)
  {
    // If the aa button is pressed.
    if(ev.getSource() == addClassButton)
    {
      // Find the class coresponding to the string in
      // the text entry and create an instance of it.
      Object temp = loader.loadObject(classNameEntry.getText());

      // If the instance is not invalid then enter add
      // a new component mode with this Component.
      if(temp == null)
      {
        System.out.println("Class loading error.");
        return;
      }
      if (temp instanceof Component)
      {
        form.placeComponentOnNextClick((Component)temp);
        System.out.println("Successfully loaded " + temp.toString());
      }
      else
      {
        System.out.println("You can only place subclasses of the class Component on the freeform edit.");
      }
    }

    // If the delete button is pressed then delete the
    // selected Component.
    if(ev.getSource() == deleteSelectedButton)
    {
      form.removeSelection();
    }

    // If the exit button is pressed then ...
    if(ev.getSource() == exitButton)
    {
      System.exit(0);
    }
  }

  // Used to load new instances of components automaticaly.
  private MacroExtension loader = new MacroExtension();

  // The owner FreeFormEdit.
  private FreeFormEdit form;

  // The entry field in which you select the name of the class
  // to add to the free form edit.
  private TextField classNameEntry = new TextField("java.awt.Button");

  // Add the class whos name appears in the classNameEntry field.
  private Button addClassButton = new Button("Add class");

  // Delete the selected Component.
  private Button deleteSelectedButton = new Button("Delete selected");

  // Exit the application.
  private Button exitButton = new Button("Exit");
}