/*
 * Created on 2005-04-05
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2005
 */
package pl.zalewski.mikolaj.symbdev.ui.mmpedit;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.widgets.FormToolkit;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IMMPBitmapContainer;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.MMPBitmapReference;
import pl.zalewski.mikolaj.symbdev.ui.internal.widgets.ChooseDepthDialog;
import pl.zalewski.mikolaj.symbdev.ui.internal.widgets.DirectoryDialog;

/**
 * Allows to manipulate a list of bitmaps in IMMPBitmapContainer
 * 
 * @author mikolaj
 */
public class BitmapContainerWidget extends Composite {

    public BitmapContainerWidget(Composite parent, FormToolkit toolkit, IMMPBitmapContainer containerModel,
            SymbianSDKEntry sdk, IPath currentDir) {
        super(parent, SWT.NONE);
        fSDK=sdk;
        fCurrentDir=currentDir;
        fContainerModel=containerModel;
        toolkit.adapt(this);
        GridLayout layout=new GridLayout(2, false);
        setLayout(layout);
        
        fList=toolkit.createTable(this, SWT.BORDER);
        TableColumn col=new TableColumn(fList, SWT.LEFT);
        col.setText("Filename");
        col.setWidth(130);
        col=new TableColumn(fList, SWT.NONE);
        col.setText("Color Depth");
        col.setWidth(80);
        GridData gd=new GridData(GridData.FILL_HORIZONTAL);
        gd.heightHint=150;
        fList.setLayoutData(gd);
        fList.setHeaderVisible(true);
        fList.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                selectionChanged();
            }			
        });
        
        fButtonsPanel=toolkit.createComposite(this);
        layout=new GridLayout(1, true);
        fButtonsPanel.setLayout(layout);
        fAdd=toolkit.createButton(fButtonsPanel, "Add...", SWT.PUSH);
        fAdd.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                addPressed();
            }			
        });
        gd=new GridData(GridData.FILL_HORIZONTAL);
        fAdd.setLayoutData(gd);
        fRemove=toolkit.createButton(fButtonsPanel, "Remove", SWT.PUSH);
        fRemove.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                removePressed();
            }			
        });
        gd=new GridData(GridData.FILL_HORIZONTAL);
        fRemove.setLayoutData(gd);
        fDepth=toolkit.createButton(fButtonsPanel, "Depth...", SWT.PUSH);
        fDepth.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                depthClicked();
            }
        });
        gd=new GridData(GridData.FILL_HORIZONTAL);
        fDepth.setLayoutData(gd);
        Label l=toolkit.createLabel(fButtonsPanel, " ");
        fUp=toolkit.createButton(fButtonsPanel, "Up", SWT.PUSH);
        fUp.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                upPressed();
            }
        });
        gd=new GridData(GridData.FILL_HORIZONTAL);
        fUp.setLayoutData(gd);
        fDown=toolkit.createButton(fButtonsPanel, "Down", SWT.PUSH);
        fDown.addListener(SWT.Selection, new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                downPressed();
            }
        });
        gd=new GridData(GridData.FILL_HORIZONTAL);
        fDown.setLayoutData(gd);
        
        bitmapsChanged();
        
        fModelListener=new Listener() {
            public void handleEvent(org.eclipse.swt.widgets.Event e) {
                bitmapsChanged();
            }			
        };
        fContainerModel.addChangeListener(fModelListener);
    }
    
    void selectionChanged()
    {
        int index=fList.getSelectionIndex();
        fUp.setEnabled(index>0);
        fDown.setEnabled(index>=0 && index<fList.getItemCount()-1);
        fRemove.setEnabled(index>=0);
        fDepth.setEnabled(index>=0);
    }
    
    void bitmapsChanged()
    {
        if (fList.isDisposed())
            return;
        TableItem[] items=fList.getItems();
        for (int i=0; i<items.length; i++)
            items[i].dispose();
        MMPBitmapReference[] bitmaps=fContainerModel.getBitmaps();
        for (int i=0; i<bitmaps.length; i++)
        {
        	String filename=bitmaps[i].fPath.getFileStringType(fSDK.epocRoot,
        	        fCurrentDir.toString()).getStringName();
        	TableItem ti=new TableItem(fList, SWT.NONE);
        	ti.setText(0, filename);
        	ti.setText(1, bitmaps[i].getDepthName());
        }
        selectionChanged();
    }
    
    protected void addPressed()
    {
        DirectoryDialog dlg=new DirectoryDialog(getShell(), fSDK, fCurrentDir, false);
        if (!dlg.execute())
            return;
        IPath ret=dlg.getResultPath();
        if (!ret.isAbsolute())
            ret=fCurrentDir.append(ret);
        try {
            fContainerModel.addBitmap(ret, MMPBitmapReference.DEPTH_C16);
        } catch (CoreException e) {
            SymbdevPlugin.showError(e, "Error adding bitmap to the MMP file", getShell());
        }
    }
    
    protected void removePressed()
    {
        int pos=fList.getSelectionIndex();
        if (pos<0) return;
        try {
            fContainerModel.removeBitmap(pos);            
        }
        catch (CoreException e) {
            SymbdevPlugin.showError(e, "Error removing bitmap", getShell());
        }
    }
    
    protected void upPressed()
    {
        int pos=fList.getSelectionIndex();
        if (pos<0) return;
        try {
            fContainerModel.moveBitmapUp(pos);            
        }
        catch (CoreException e) {
            SymbdevPlugin.showError(e, "Error moving bitmap up", getShell());
        }        
    }
    
    protected void downPressed()
    {
        int pos=fList.getSelectionIndex();
        if (pos<0) return;
        try {
            fContainerModel.moveBitmapDown(pos);
        }
        catch (CoreException e) {
            SymbdevPlugin.showError(e, "Error moving bitmap down", getShell());
        }
    }
    
    protected void depthClicked() {
        int pos=fList.getSelectionIndex();
        if (pos<0) return;
        ChooseDepthDialog dlg=new ChooseDepthDialog(getShell());
        dlg.setResult(fContainerModel.getBitmaps()[pos].fDepth);
        if (dlg.open()==Dialog.OK) {
            String res=dlg.getResult();
            try {
                fContainerModel.setBitmapDepth(pos, res);
            } catch (CoreException e) {
                SymbdevPlugin.showError(e, "Error while changing depth", getShell());
            }
        }
    }
    
    public void dispose() {
        fContainerModel.removeChangeListener(fModelListener);
        super.dispose();
    }
    
    Button fDown, fUp, fRemove, fAdd, fDepth;
    Composite fButtonsPanel;
    Table fList;
    SymbianSDKEntry fSDK;
    IPath fCurrentDir;
    Listener fModelListener;
    
    IMMPBitmapContainer fContainerModel;    
}
