package Appletexam;

import java.awt.Panel;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;

import com.xckx.Visio.Interface.*;
import com.xckx.Visio.Viewer.*;

/**
 * <p>Title: Visio Example--Applet</p>
 * <p>Description: Show how to use VisioCom ia an Applet</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: Beijing Dawninfo Tech. Co., Ltd.</p>
 * @author Junjie Ruan
 * @version 1.0
 */

//!!!the PaintPanel must implements GdiFrame which is the main interface of VisioCom.jar
public class PaintPanel extends Panel implements VisioPanel {
  public static final int SELECT_MOD = 1;
  public static final int DROP_MODE  = 2;
  private int mouseOprMode = SELECT_MOD;
  private PopMenu winPopMenu = null; //!the context-sensitive pop-menu in VisioCom.jar

  VisioApplet mainApplet = null;
  PopupPanel popupPanel = null;      //!a popupPanel which will show and list all documents
  MyViewer viewer = null;

  public PaintPanel(Applet applet) {
    super();
    mainApplet = (VisioApplet)applet;

    viewer = new MyViewer(this);             //!!!instantialize the MyViewer class
                                             //!!!which extends VisioViewer class
                                             //!!!in VisioCom.jar
    viewer.setLanguage("EN");                //!!!set the language:"EN"---English,"ZH"--simplified Chinese
    winPopMenu = new PopMenu(this);          //!instantialize the context-sensitive pop-menu

    setBackground(new Color(255,255,255));
    setForeground(new Color(51,102,153));

    super.setLayout(new BorderLayout());


    this.enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK |
                      AWTEvent.ADJUSTMENT_EVENT_MASK);
    //!adding the VisioViewer object to the mouseEvent listener queue
    this.addMouseListener(viewer);      //!!!very important way to integrate the controlling with VisioCom functions
    this.addMouseMotionListener(viewer);//!!!very important way to integrate the controlling with VisioCom functions
    this.addMouseListener(this);

    this.validate();
  }

  public void paint(Graphics g) {
    Rectangle rec = this.getBounds();
    g.clearRect(rec.x,rec.y,rec.width,rec.height);
    g.drawRect(rec.x,rec.y,rec.x+rec.width-1,rec.y+rec.height-1);

    if (viewer != null)
      viewer.draw((Graphics2D)g);   //!!!invoke the Visioviewer "draw" method to reveal the content of Visio Documents

    triggerPaint(g);
  }

  protected void processMouseMotionEvent(MouseEvent e) {
    Rectangle rec = this.getBounds();
    Point currentMouseCursorPosi = e.getPoint();

    Polygon poly = new Polygon();
    poly.addPoint(rec.width / 2 - 40, 0);
    poly.addPoint(rec.width / 2, 15);
    poly.addPoint(rec.width / 2 + 40, 0);

    if (poly.contains(currentMouseCursorPosi.x,currentMouseCursorPosi.y)) {
      showPopupPanel(true);
    } else {
      showPopupPanel(false);
      viewer.handleMouseEvent(MouseEvent.MOUSE_DRAGGED,e);
    }
  }

  public void showPopupPanel(boolean ifShow) {
    Rectangle rec = this.getBounds();

   if (viewer.getContainer("myContainer") == null)
      return;

    if (popupPanel == null) {
      popupPanel = new PopupPanel(this);
      popupPanel.setBounds(0,0,rec.width,rec.height/4);
      this.add("South",popupPanel);
      popupPanel.setVisible(false);
      this.validate();
    }
    popupPanel.setVisible(ifShow);
    if (ifShow)
      popupPanel.requestFocus();
    else
      this.requestFocus();
  }

//!a mouse cursor sensitive area which trigger the PopupPanel to be shown
  private void triggerPaint(Graphics g) {
    Rectangle rec = this.getBounds();

    Polygon poly = new Polygon();
    poly.addPoint(rec.width / 2 - 40, 0);
    poly.addPoint(rec.width / 2, 15);
    poly.addPoint(rec.width / 2 + 40, 0);
    g.setColor(Color.red);
    g.fillPolygon(poly);
    g.setColor(Color.black);
    g.drawPolygon(poly);

    poly = new Polygon();
    poly.addPoint(rec.width/2-25,0);
    poly.addPoint(rec.width/2,8);
    poly.addPoint(rec.width/2+25,0);
    g.setColor(Color.orange);
    g.fillPolygon(poly);

  }

  public VisioViewer getViewer() {              //!!!a method of GdiFrame interface
    return viewer;
  }

  public Component getPanel() {                 //!!!a method of GdiFrame interface
    return this;
  }

  public Color getPanelBkColor() {             //!!!a method of GdiFrame interface
    return new Color(255,25,255);
  }

  public VisioApplet getApplet() {
    return mainApplet;
  }

  public void undo(MouseEvent e) {
      if (viewer == null)
        return;
      viewer.unDo();                          //!!!undo method of VisioViewer class
  }

  public void redo(MouseEvent e) {
      if (viewer == null)
        return;
      viewer.reDo();                          //!!!redo method of VisioViewer class
  }

  public void layout(MouseEvent e) {
    if (viewer == null)
      return;
    viewer.layoutObj();                       //!!!layoutObj method of VisioViewer class
  }

  public void clearAll(MouseEvent e) {
    if (viewer != null)
      viewer.clearAll();                      //!!!clearAll method of VisioViewer class
  }


  public void setMouseOprMode(int mode) {
    mouseOprMode = mode;
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
    if ((!e.isConsumed()) && (e.getButton() != MouseEvent.BUTTON1)) {
      Point point = e.getPoint();
      winPopMenu.setVisible(true);
      winPopMenu.show(this,point.x,point.y);
      e.consume();
    } else {
      winPopMenu.setVisible(false);
      if (mouseOprMode == DROP_MODE) {
        Point point = e.getPoint();
        Rectangle2D.Float rec = new Rectangle2D.Float((float)point.x, (float)point.y, 50.0f, 50.0f);
        viewer.addVisioObject(popupPanel.getIndex(),rec);    //!!!addDocument method of VisioViewer class
        mouseOprMode = SELECT_MOD;
      }
    }
  }

  public void mouseEntered(MouseEvent e) {
  }
  public void mouseExited(MouseEvent e) {
  }
  void AboutBtn_actionPerformed(ActionEvent e) {
  }
  public void actionPerformed(ActionEvent e) {
  }

}