package Appletexam;

import java.awt.Panel;
import java.awt.*;
import java.awt.event.*;

/**
 * <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
 */

//!!!a penel to show and list the documents on the container,
//!!!user can select one of documents to display on the Paintpanel
public class PopupPanel extends Panel {
  private PaintPanel parent;
  private int cols = 0, rows = 0, tots = 0, currentIndex;
  private int gridWid, gridHig;
  private Color fgColor   = new Color(51,102,153);
  private Color bgColor   = new Color(0xF2,0xD7,0xD7);
  private String fontStr = "ËÎÌå,plain,12";
  private String containerName = "myContainer";

  public PopupPanel(PaintPanel panel) {
    super();
    parent = panel;

    setForeground(new Color(51,102,153));
    setBackground(new Color(250,243,220));

    currentIndex = -1;
    this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  }

  public void paint(Graphics g) {
    int i,j,i1,j1,sepScale = 20, sepLength;;
    Rectangle rec = this.getBounds();
    Rectangle rec1;

    layoutObj();     //!!!lays out the shapes on the panel

    g.clearRect(rec.x,rec.y,rec.width,rec.height);
    g.setColor(Color.BLUE);
    g.drawRect(rec.x,rec.y,rec.x+rec.width-1,rec.y+rec.height-1);

    if ((tots > 0) && (gridWid > 0)) {   //drow a selection frame around the current shape
      if (currentIndex >= 0) {
        rec.width = rec.width * 9 / 10;
        rec.height = rec.height * 9 / 10;
        sepLength = Math.min(gridWid, gridHig) / sepScale;

        i = currentIndex;
        i1 = (int) (i / cols);
        j1 = i - i1 * cols;
        rec1 = new Rectangle(rec.width/20+gridWid*j1-sepLength,rec.height /20+
                             gridHig*i1-sepLength/2, gridWid,
                             gridHig+sepLength);
        g.setColor(Color.blue);
        g.drawRect(rec1.x, rec1.y, rec1.width, rec1.height);
      }
      //!!!call the draw method of VisioViewer class in Visiocom.jar
      //!!!to show and list all documents in the named container,i.e.,
      //!!!the container named "myContainer"
      parent.getViewer().draw(parent.getViewer().getContainer(containerName),(Graphics2D)g);
   }
  }

  private void layoutObj() {
//!!!lays out the shapes of documents on the panel, and return the integer of rows, columns,
//!!!grid width and height each shape occupied, in a Rectangle object
    Rectangle rec = parent.getViewer().layoutObj(this,parent.getViewer().getContainer(containerName));
    if (rec == null)
      return;

    setColumn(rec.x);
    setTotals(rec.y);
    gridWid = rec.width;
    gridHig = rec.height;

  }

  private void setColumn(int i) {
    cols = i;
  }

  private void setTotals(int i) {
    tots = i;
    rows = (int)Math.round((double)tots/(double)cols);
  }

  public int getIndex() {
    return currentIndex;
  }

//!!!draw the selection indicator fame
  private void cursorPaint(int last,int cur) {
    int i,j,i1,j1,sepScale = 20, sepLength;;
    Rectangle rec = this.getBounds();
    Rectangle rec1;

    if ((tots > 0) && (gridWid > 0)) {
      Graphics g = this.getGraphics();
      rec.width  = rec.width *9 / 10;
      rec.height = rec.height *9 / 10;
      sepLength = Math.min(gridWid, gridHig) / sepScale;
      for (j=0; j<2; j++) {
        if (j == 0)
          i = last;
        else
          i = cur;
        if (i < 0)
          continue;
        i1 = (int)(i / cols);
        j1 = i - i1*cols;
        rec1 = new Rectangle(rec.width/20+gridWid*j1-sepLength,rec.height /20+
                             gridHig*i1-sepLength/2, gridWid,
                             gridHig+sepLength);
        if (j == 0)
          g.setColor(bgColor);
        else
          g.setColor(Color.blue);
        g.drawRect(rec1.x,rec1.y,rec1.width,rec1.height);
      }
    }

  }

  protected void processMouseEvent(MouseEvent e) {
    int i,j,i1,j1,sepScale = 20, sepLength;
    int lastIndex;
    Rectangle rec = this.getBounds();
    Rectangle rec1;

    rec.width  = rec.width *9 / 10;
    rec.height = rec.height *9 / 10;
    sepLength = Math.min(gridWid, gridHig) / sepScale;

    if (e.getID() == MouseEvent.MOUSE_CLICKED) {
      lastIndex = currentIndex;
      for (i=0; i<tots; i++) {
        i1 = (int)(i / cols);
        j1 = i - i1*cols;
        rec1 = new Rectangle(rec.width/20+gridWid*j1-sepLength,rec.height /20+
                             gridHig*i1-sepLength, gridWid,
                             gridHig+sepLength);
        if (rec1.contains(e.getPoint())) {
            currentIndex = i;
            break;
        }
      }
      if(lastIndex != currentIndex) {
        cursorPaint(lastIndex, currentIndex);
      }
/*
      if (currentIndex >= 3)
        i = parent.getViewer().getLabelIndex(parent.getViewer().getContainer(containerName),
                                       currentIndex);
      else {
        if (currentIndex == -1)
          i = currentIndex;
        else
          i = (currentIndex + 1) * ( -1);
      }
*/
      parent.setMouseOprMode(PaintPanel.DROP_MODE);
    }

  }

  public void update(Graphics g) {
    paint(g);
  }
}
