package nxm.sys.libg;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import nxm.sys.inc.MessageHandler;
import nxm.sys.lib.Parser;

/**
  A class to display a block of text.

  @author Jeff Schoen

*/
public class GList extends GWidget implements ActionListener {

  /** comma separated list of GList flags */
  public static final String flagsList = GWidget.flagsList+","+MJText.flagsList;

  /** Multiple flag */
  public static final int MULT =     0x100;
  /** FIFO flag */
  public static final int FIFO =     0x200;
  /** TREE flag */
  public static final int TREE =     0x400;
  /** Columns? flag*/
  public static final int COLS =     0x800;
  /** No Select flag*/
  public static final int NOSEL =   0x1000;
  /** No Highlight flag*/
  public static final int NOHIL =   0x2000;
  /** MOMENT flag*/
  public static final int MOMENT =  0x4000;
  /** INPUT flag*/
  public static final int INPUT =   0x8000;
  /** Color flag*/
  public static final int COLOR =  0x10000;
  /** Banner flag*/
  public static final int BANNER = 0x20000;

  /** text area of the list */
  public MJText list;
  int selected=-1;

  /**
     Instantiate a new List, initialize the parameters, and open the graphics panel.
     @param parent the parent object
     @param name the name of this List
     @param items the comma separated list of text items to be displayed
     @param aw the width in characters
     @param ah the height in lines
     @param flags the widget flags
     @param mh the MessageHandler
   */
  public GList (Object parent, String name, String items, int aw, int ah,
                int flags, MessageHandler mh) {
    super("GList",parent,name,flags,mh);
    open (name, aw, ah, items);
    addTo (parent);
  }

  /** GList constructor
   *  @param parent the parent object
   *  @param name the name of this List
   *  @param items the String array of text items to be displayed
   *  @param aw the width in characters
   *  @param ah the height in lines
   *  @param flags flags the widget flags
   *  @param mh mh the MessageHandler
   */
  public GList (Object parent, String name, String[] items, int aw, int ah,
                int flags, MessageHandler mh) {
    super("GList",parent,name,flags,mh);
    open (name, aw, ah, null);
    addItems(items);
    addTo (parent);
  }

  /** Open the graphics window for this List with the given name.
      This List will be sized according to the given width and height,
      and will display the given text.
      @param name the name of this List
      @param aw the width
      @param ah the height
      @param text the text to be displayed
   */
  public void open (String name, int aw, int ah, String text) {
    open(); // get text sizes
    super.open (name,aw*tw,ah*th+ta);
    setupList(aw,ah,text);
  }

  /** Setup the MJText object
      @param aw The width
      @param ah The height
      @param text The text to display
   */
  public void setupList (int aw, int ah, String text) {
    list = new MJText (256,aw,ah,12,theme,(flags>>8));
    list.setMWindow(this);
    if (text!=null) {
      Parser p = new Parser(text);
      while(p.more()) list.add(p.next());
    }
    list.addActionListener(this);
    panel.add(list);
  }

  @Override
  public void resize (int mode) {
    super.resize(mode);
  }

  @Override
  public void setBounds (int x, int y, int w, int h) {
    super.setBounds(x,y,w,h);
    pw = pos.w-bw-bw;
    ph = pos.h-titleh;
    if (list!=null)    list.setBounds(bw,bw+titleh,pw,ph);
  }

  @Override
  public void setValue (String value) {
    list.add(value);
  }

  @Override
  public Object getValue() {
    return list.getSelectedItem();
  }

  /** Get selected items
   *  @return the selected items */
  public String[] getValues() {
    return list.getSelectedItems();
  }

  @Override
  public void setTheme (Theme theme) {
    super.setTheme(theme);
    list.setTheme(theme);
  }

  /**
   * Add an item to the list
   * @param item to be added
   */
  public void addItem (String item) {
    list.add(item);
  }

  /**
   * Add multiple items to the list
   * @param items the String array of items to be added
   */
  public void addItems (String[] items) {
    for (int i=0; i<items.length; i++) {
      list.add(items[i]);
    }
  }

  /**
   * Remove all items from the list
   */
  public void removeItems() {
    list.removeAll();
  }

  public void actionPerformed (ActionEvent e) {
    String action = e.getActionCommand();
    int item = e.getModifiers();
    if (action.equals("SELECT") && item>=0 && !is(NOMSG)) {
      String text;
      text = list.getLine(item);
      sendWidgetMessage (label,item,text);
    }
  }

  @Override
  public boolean isStretchable() {
    return true;
  }

}
