/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 23-Dec-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.widgets.dropdownbox;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.caches.FontCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.Utils;
import uk.ac.ic.doc.kevlar.gui.internal.keyboard.KeyboardManager;
import uk.ac.ic.doc.kevlar.gui.internal.keyboard.states.DropDownBoxState;
import uk.ac.ic.doc.kevlar.gui.internal.popup.PopUpManager;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.IDeselectListener;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.listbox.IListSelectionListener;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.listbox.ListBox;

/**
 * @author Marc
 **/
public class DropDownBox extends AMouseListenerObject implements IListSelectionListener {
 
	private static final String EDIT_BOX_SKIN = "editbox";
	private static final String TRANSPARENT_EDIT_BOX_SKIN = "transparenteditbox";
    
    protected DropDownButton button;
    protected boolean selected;
    protected Font font;
    protected int current;
    protected ArrayList<String> options;
    protected ListBox dropDownList;
    protected IDeselectListener listener;
    protected boolean dropDownVisible;
    
    public DropDownBox() {
        
        super();
        
        selected = false;
        current = 0;
        options = new ArrayList<String>();
        listener = null;
        dropDownVisible = false;
        
        button = new DropDownButton(this);
        button.setPosition(new Point(15, 4));
        button.setAlignment(true, false);
        addChild(button);
        
        font = FontCache.get("Courier New", 10, SWT.NORMAL);
        
        dropDownList = new ListBox();
        dropDownList.setSelectionListener(this);
        dropDownList.setDrawBackground(false);
        addChild(dropDownList);
        
    }
    
    public void setDeselectListener(IDeselectListener l) {
        
        listener = l;
        
    }
    
    public void setCurrent(int current) {
        
        if (current >= 0 && current < options.size()) {
        
            this.current = current;
            
        }
        
    }
    
    public String getCurrent() {
        
        if (current >= 0 && current < options.size()) {
            
            return options.get(current);
            
        } else {
            
            return "";
            
        }
        
    }
    
    public void clearOptions() {
        
        options.clear();
        current = -1;
        
    }
    
    public void addOption(String option) {
        
        options.add(option);
        
    }
    
    public void moveUp() {
        
        current--;
        
        if (current < 0) {
            
            current = options.size()-1;
            
        }
        
        redraw();
        
    }
    
    public void moveDown() {
        
        current++;
        
        if (current >= options.size()) {
            
            current = 0;
            
        }
        
        redraw();
        
    }
    
    public void select() {
        
        selected = true;
        redraw();
        
    }
    
    public void deselect() {
        
        if (dropDownVisible) {
            
            setSize(new Point(area.width, area.height-100));
            dropDownVisible = false;            
            
        }
        
        selected = false;
        redraw();
        
        if (listener != null) {
            
            listener.onDeselect(this);
            
        }
        
    }
    
    public void drawBefore(GC gc, Rectangle redrawArea) {
        
        if (selected) {
            
            Utils.drawBox(gc, new Rectangle(0-redrawArea.x, 0-redrawArea.y, getSize().x, getSize().y), EDIT_BOX_SKIN);
            
        } else {
            
            Utils.drawBox(gc, new Rectangle(0-redrawArea.x, 0-redrawArea.y, getSize().x, getSize().y), TRANSPARENT_EDIT_BOX_SKIN);
            
        }
        
        if (current >= 0 && current < options.size()) {
        
            gc.setFont(font);
            Utils.drawText(gc, options.get(current), new Rectangle(7-redrawArea.x, -redrawArea.y, area.width-27, 18), Utils.ALIGN_LEFT);
            
        }
        
    }
    
    public void dropDown() {
        
        if (!selected) {
            
            KeyboardManager.getSingleton().setState(new DropDownBoxState(this));
            
        }
        
        if (!dropDownVisible) {
        
            dropDownList.setData(options);
            dropDownList.setPosition(new Point(5, getSize().y));
            dropDownList.setSize(new Point(getSize().x-7, 95));
            setSize(new Point(area.width, area.height+100));
            dropDownVisible = true;
            
        }
//        PopUpManager.getSingleton().showPopUp(dropDownList);
        
    }

    public void entrySelected(Object entry) {
        
//        PopUpManager.getSingleton().hidePopUp(dropDownList);
        
        if (dropDownVisible) {
        
            setSize(new Point(area.width, area.height-100));
            current = options.indexOf(entry);
            dropDownVisible = false;
            redraw();
            
        }
        
    }
    
    public void mousePress(Point position, int button) {
        
        super.mousePress(position, button);
        
        if (!selected) {
        
            KeyboardManager.getSingleton().setState(new DropDownBoxState(this));
            
        }
        
    }
    
    public boolean capturesMouse() {
        
        return true;
        
    }
    
    public void hide() {
        
        super.hide();
        
        if (selected) {
        
            KeyboardManager.getSingleton().gotoHistoryState();
            
        }
        
    }
    
}
