/*
Tiamat - a role-playing game framework
LayerListPanel.java - panel to list Layers
 
 Copyright  (C) 2004 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.tools;

import java.lang.Object;
import java.lang.String;
import java.lang.StringBuffer;
import com.huguesjohnson.tiamat.graphics.Layer;
import com.huguesjohnson.tiamat.tools.LayerDialog;
import com.huguesjohnson.ui.ListPanel;

/** LayerListPanel - panel to list Layers
 * @author Hugues Johnson
 */
public class LayerListPanel extends ListPanel{

	/** Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=-6455818747576229277L;

	//internal class to represent items in the list
    private class LayerListItem{
        private Layer layer;
        private int order;
        LayerListItem(Layer layer,int order){ this.layer=layer; this.order=order; }
        Layer getLayer(){ return(this.layer); }
        int getOrder(){ return(this.order); }
        public String toString(){
            StringBuffer tostring=new StringBuffer("["+this.order+"] - ");
            switch(this.layer.getType()){
                case Layer.TYPE_DIALOG: { tostring.append("Dialog"); break; }
                case Layer.TYPE_SPRITE: { tostring.append("Sprite"); break; }
                case Layer.TYPE_MAP: { tostring.append("Map - "); tostring.append(this.layer.getMapPath()); break; }
            }
            return(tostring.toString());
	}
    }    
    
    /** Creates a new instance of LayerListPanel 
     */
    public LayerListPanel(){
        super("Layers");
    }

    /* begin overrides */
    
    /** event that fires when add button is clicked
     */
    public void addAction(){ this.addLayer(); }
    
    /** event that fires when edit button is clicked
     */
    public void editAction(){ this.editLayer(); }    

    /* end overrides */

    /** add a new Layer 
     */
    @SuppressWarnings("serial")
	private void addLayer(){
        LayerDialog layerDialog=new LayerDialog(null){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    addLayer(getLayer());
                }
                dispose();
            }
        };
        layerDialog.setVisible(true);
    }
    
    /** add a new Layer
     * @param layer the Layer to add
     */
    public void addLayer(Layer layer){
        this.addListItem(new LayerListItem(layer,this.getCount()));
    }

    /** edit the selected Layer 
     */    
    @SuppressWarnings("serial")
	private void editLayer(){
        LayerListItem listItem=(LayerListItem)this.getSelectedListItem();
        LayerDialog layerDialog=new LayerDialog(null,listItem.getLayer()){
            public void closeDialog(boolean cancel){
                if(!cancel){
                    updateSelectedLayer(getLayer());
                }
                dispose();
            }
        };
        layerDialog.setVisible(true);        
    }
    
    /** update the selected layer
     * @param layer the Layer to replace the selected Layer with
     */
    public void updateSelectedLayer(Layer layer){
        int order=((LayerListItem)this.getSelectedListItem()).getOrder();
        this.updateSelectedListItem(new LayerListItem(layer,order));
    }
    
    /** return an array of all Layers in the panel
     * @return array of all Layers in the panel
     */
    public Layer[] getAllLayers(){
        Object[] objects=this.getAllListItems();
        int length=objects.length;
        Layer[] layers=new Layer[length];
        for(int index=0;index<length;index++){
            layers[index]=((LayerListItem)objects[index]).getLayer();
        }
        return(layers);
    }
}