package Appletexam;

/**
 * <p>Title: VisioViewer test--Native application</p>
 * <p>Description: Show how to use VisioCom.jar in an Applet</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: Beijing Dawninfo Tech. Co., Ltd.</p>
 * @author Junjie Ruan
 * @version 1.0
 */

import javax.swing.*;
import java.awt.event.*;

public class PopMenu extends JPopupMenu {
  protected static final int POPMENUITEM_UNDO    = 1;
  protected static final int POPMENUITEM_REDO    = 2;
  protected static final int POPMENUITEM_VIEW    = 3;
  protected static final int POPMENUITEM_LAYOUT  = 4;
  protected static final int POPMENUITEM_SAVE    = 5;
  protected static final int POPMENUITEM_CLEAR   = 6;

  private JMenuItem undoPopItem = new JMenuItem();
  private JMenuItem redoPopItem = new JMenuItem();
  private JMenuItem newPopItem = new JMenuItem();
  private JMenuItem layoutPoplItem = new JMenuItem();
  private JMenuItem clearPopItem = new JMenuItem();

  private PaintPanel panel;

  public PopMenu(PaintPanel pane) {
    super();
    panel = pane;

    undoPopItem.setText("undo");
    redoPopItem.setText("redo");
    newPopItem.setText("open");
    layoutPoplItem.setText("layout");
    clearPopItem.setText("clearAll");

    undoPopItem.addMouseListener(new PopMenuItemsAdapter(panel, POPMENUITEM_UNDO));
    redoPopItem.addMouseListener(new PopMenuItemsAdapter(panel, POPMENUITEM_REDO));
    newPopItem.addMouseListener(new PopMenuItemsAdapter(panel, POPMENUITEM_VIEW));
    layoutPoplItem.addMouseListener(new PopMenuItemsAdapter(panel, POPMENUITEM_LAYOUT));
    clearPopItem.addMouseListener(new PopMenuItemsAdapter(panel, POPMENUITEM_CLEAR));

    this.add(undoPopItem);
    this.add(redoPopItem);
    this.addSeparator();
    this.add(newPopItem);
    this.add(layoutPoplItem);
    this.addSeparator();
    this.add(clearPopItem);
  }
}

class PopMenuItemsAdapter extends java.awt.event.MouseAdapter  {
  PaintPanel adaptee;
  int selectedItem;

  PopMenuItemsAdapter(PaintPanel adaptee, int item) {
    this.adaptee = adaptee;
    this.selectedItem = item;
  }

  public void mouseReleased(MouseEvent e) {
    switch (selectedItem) {
      case PopMenu.POPMENUITEM_UNDO:
        adaptee.undo(e);
        break;
      case PopMenu.POPMENUITEM_REDO:
        adaptee.redo(e);
        break;
      case PopMenu.POPMENUITEM_LAYOUT:
        adaptee.layout(e);
        break;
      case PopMenu.POPMENUITEM_CLEAR:
        adaptee.clearAll(e);
        break;
      default:
        break;
    }
  }

}
