/**
 * Copyright (c) 2011, Martin Foster <martin@ethereal-realms.org>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Ethereal-Realms.org nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.lyra.Listener;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * A basic listener which will be used with the Microsoft Windows based resize
 * icon.   This will permit a resize outside of the standard border elements.
 * <p>
 * @author Martin Foster
 * @version 1.0.4, %Revision, 99%, %LastChangedDate, 17/03/11 8:17PM%
 */
public class WindowSizeListener implements MouseListener, MouseMotionListener {

	// Windowing element
    Window myWindow;
    
    // Starting position and size
    Point startDrag;
    Dimension startSize;
   
    // Cursor change over
    Cursor oldCursor;
    

    /**
     * Basic constructor for the class.  Will take in the the element being
     * resized in order to control the operation.
     * <p>
     * @param window Window instance to control.
     */
    public WindowSizeListener (Window window) {
    	
    	// Straight forward passage
        this.myWindow = window;
    }
    // End of Constructor Method
    
    
    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void mouseClicked(MouseEvent e) {}

    
    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
     */
    public void mouseEntered(MouseEvent e) {
    	
    	// Maintain the old cursor
    	oldCursor = myWindow.getCursor();
    	
    	// Set the cursor for a resize event
    	myWindow.setCursor(
    		new Cursor(Cursor.SE_RESIZE_CURSOR)
    	  );
    }
    // End of mouseEntered Method
    
    
    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
     */
    public void mouseExited(MouseEvent e) {
    	
    	// Safety
    	if (oldCursor != null) {
    		
    		// Assign the old cursor
    		myWindow.setCursor(oldCursor);
    	}
    }
    // End of mouseExited Method
    
    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     */
    public void mousePressed(MouseEvent e) {
    	
    	/*
    	 * What we do here is fetch the initial values in order to provide a
    	 * baseline for how the behaviour will take place.   We cannot resize
    	 * if we do not know what we were originally.
    	 */
        startDrag = e.getLocationOnScreen();
        startSize = myWindow.getSize();
    }
    // End of mousePressed Method

    
    /* (non-Javadoc)
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    public void mouseReleased(MouseEvent e) {}

    
    /* (non-Javadoc)
     * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
     */
    public void mouseDragged(MouseEvent e) {

    	/*
    	 * Here we get the current point, calculate an offset and add it to
    	 * the size of the window.  This will effectively create the resizing
    	 * effect that is normally found.  However it is important to know
    	 * that the redraw will show all contents which is not always the case
    	 * with the default install.
    	 */
    	
    	// Get the current location of the screen
        Point current = e.getLocationOnScreen();
        
        // Calculate the difference since the last time we calculated
        Point offset = new Point(
            (int)current.getX()-(int)startDrag.getX(),
            (int)current.getY()-(int)startDrag.getY());

        // Adjust the size
        myWindow.setSize(
        	(int)(startSize.getWidth() + offset.getX()),
        	(int)(startSize.getHeight() + offset.getY())
         );

    }
    // End of mouseDragged Method
    
    
    /* (non-Javadoc)
     * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
     */
    public void mouseMoved(MouseEvent e) {}
}
