/*******************************************************************************
* 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 20-Dec-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.pipeline;

import java.io.FileNotFoundException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ColorCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ImageCache;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.CreateMacroButton;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.DeleteButton;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.custombuttons.ExpandButton;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.IDimensionListener;

/**
 * @author Marc
 **/
public class SelectionBox extends AMouseListenerObject implements IProgramGroup, IDimensionListener {

    private static final String SELECTION_TOOL_BAR_IMAGE = "selectiontoolbar.png";
    
    protected Image toolbar;
    protected DeleteButton deleteButton;
    protected ExpandButton expandButton;
    protected CreateMacroButton macroButton;
    
    public SelectionBox() {
        
        super();
        
        try {
        
            toolbar = ImageCache.get(SELECTION_TOOL_BAR_IMAGE);
            
        } catch (FileNotFoundException e) {
            
            System.err.println("File not found: "+SELECTION_TOOL_BAR_IMAGE);
            toolbar = new Image(Display.getDefault(), 1, 1);
            
        }
        
        deleteButton = new DeleteButton(this);
        deleteButton.setPosition(new Point(17, 2));
        deleteButton.setAlignment(true, false);
        addChild(deleteButton);
        
        expandButton = new ExpandButton(this);
        expandButton.setPosition(new Point(29, 2));
        expandButton.setAlignment(true, false);
        addChild(expandButton);
        
        macroButton = new CreateMacroButton(this);
        macroButton.setPosition(new Point(41, 2));
        macroButton.setAlignment(true, false);
        addChild(macroButton);
        
    }
    
    public void drawBefore(GC gc, Rectangle redrawArea) {

        gc.setForeground(ColorCache.get(0, 0, 0));
        gc.setLineStyle(SWT.LINE_DOT);
        gc.drawRectangle(-redrawArea.x, -redrawArea.y, area.width-1, area.height-1);
        
        gc.drawImage(toolbar, area.width-48-redrawArea.x, -redrawArea.y);
            
    }

    public void deleteButtonPressed() {
        
        PipeLineManager.getSingleton().deleteMultiSelect();
        
    }

    public void toggleExpandContract() {

        if (expandButton.isSelected()) {
        
            PipeLineManager.getSingleton().expandMultiSelect();
            expandButton.deselect();
            
        } else {
            
            PipeLineManager.getSingleton().contractMultiSelect();
            expandButton.select();
                        
        }
        
    }
    
    public void createMacro() {
        
        PipeLineManager.getSingleton().createMacro();
        
    }
    
    public boolean capturesMouse() {
        
        return true;
        
    }

    public void objectDimensionsChanged(ADrawableObject object, Rectangle dimensions) {

        PipeLineManager.getSingleton().adjustSelectedArea();
        
    }

}
