import java.awt.*;
import java.util.Date;

/**
 * Class OperationList
 * Gestion de la liste des operations
 * @version        0.1 Feb 1997 
 * @author         Ebele Nicolas, Mallet Frederic
 * Date : 29/03/97  9:38
 */
public class OperationList extends MyList {
  private int insertPos = 0;

  public OperationList() {
    Init.println(ClassName, "constructeur");
    ClassName = "OperationList";
    Initialise();
  }

/**
 * Recupere les parametres de configuration
 */
  public void Initialise() {
    Init.println(ClassName, "Init");
    String param;

    param = (String)Init.get(ClassName, "Layout", "LINE_SIZE");
    LINE_SIZE = param==null?LINE_SIZE:Integer.parseInt(param);

    param = (String)Init.get(ClassName, "Layout", "Info");
    Info = param==null?Info:param;

// Recuperation des couleurs
    getColor();
  }

/**
 * ajoute un operation a la liste des operations
 * @param from url source
 * @param to url destination
 * @param op Operation a effectuer (PUT ou GET)
 */
  public void addOperation(String from, String to, String op) {
    Init.println(ClassName, "from:"+from+" to:"+to);
    addItem(op.toUpperCase()+" "+from+" "+to);
  }

/**
 * @return l'operation actuellement selectionne, null si aucun
 */ 
  public String getSelectedItem() {
    return list.getSelectedItem();
  }

/**
 * Cherche la place a laquelle doit etre insere l'operation dont
 * la Date est at. (Les operations sont triees par ordre chronologique
 * d'execution.
 * @param at Date de l'operation a inserer
 * @return position a laquelle on peut inserer l'operation
 */
  public int searchPlace(Date at) {
    for (int i=0; i<countItems(); i++) {
      String item=list.getItem(i);
      if (item.startsWith(Info)) {
        Init.println(ClassName, "lue:"+item.substring(3));
        Date date = new Date(item.substring(3));
        if (date.getTime()>at.getTime())
          return (insertPos=i);
        if (date.getTime() == at.getTime()) {
          insertPos = i+1;
          return -1;
        }
      }
    }
    return (insertPos=countItems());
  }

/**
 * @return position de l'operation selectionnee, -1 si aucune
 */
  public int getSelectedIndex() {
    return list.getSelectedIndex();
  }

/**
 * Ajoute une operation a la position insertPos dans la liste
 * @param item texte de l'item a ajouter
 */
  public void addItem(String item) {
    addItem(item, insertPos++);
  }

/**
 * Efface l'operation a la position index
 * @param index Position de l'index a effacer
 */
  public void delItem(int index) {
    list.delItem(index);
    list.deselect(list.getSelectedIndex());
    if (list.getItem(index-1).startsWith("AT") && ((index<countItems() &&
        list.getItem(index).startsWith("AT")) || index == countItems()))
      list.delItem(index-1);	 
  }

/**
 * @param index position de l'operation a renvoyer
 * @return l'operation a la position index
 */
  public String getItem(int index) {
    return list.getItem(index);
  }

/**
 * Change la liste affichee
 * @param l nouvelle liste
 * @return ancienne liste
 */
  public List swapList(List l) {
    List tmp = list;
    list = l;
    repaint();
    return tmp;
  }

  public void update(Graphics g) {
    Init.println(ClassName, "paint");
    Dimension tmp = size();
    int height = tmp.height, larg;
    max_it = (tmp.height-4)/LINE_SIZE; // Nombre max d'items affichable

    int view=sb.getValue();
//Nettoie la zone autour de la ScrollBar
    g.setColor(BackGround);
    g.fillRect(tmp.width-25, 2, 8, height-4);
// Mise en place de la scrollbar
    larg = drawScrollbar(view, height, tmp.width);

// Dessin du cadre
    drawBorder(g, larg, height);

// Dessine le texte
    g.setColor(TextColor);
    boolean sel;
    String item;
    int ind;
    Font font = g.getFont();
    font = new Font(font.getName(), font.getStyle(), LINE_SIZE-8);
    g.setFont(font);
    FontMetrics fm = g.getFontMetrics(font);
    int i;
    for (i=view; (i-view)<max_it && i<list.countItems(); i++) {
      sel = list.isSelected(i);
      g.setColor(sel?SelBackGround:BackGround);
      g.fillRect(8, (i-view)*LINE_SIZE+2, larg-16, LINE_SIZE-3);
      item = list.getItem(i);
      if (sel)
        g.setColor(SelTextColor);
      else if (item.startsWith(Info))
        g.setColor(Color.blue);
      else if (item.endsWith("->failed"))
        g.setColor(Color.red);
      else if (item.endsWith("->executed"))
        g.setColor(new Color(0, 128, 0));
      else	
        g.setColor(TextColor);
      g.drawString(adapted(item, fm, larg-15), 10, (i-view+1)*LINE_SIZE-5);
    }
// Efface si la liste n'est pas pleine
    g.setColor(BackGround);
    for (; (i-view)<max_it; i++)
      g.fillRect(2, (i-view)*LINE_SIZE+2, larg-5, LINE_SIZE-3);
  }
}
