/**
 * 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.Interface.Menu;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.SwingConstants;

import org.lyra.Lyra;

/**
 * These elements can be used in various circumstances to control volume
 * or the crossfade capability.  In order to provide generality the capability
 * of extracting an instance of JMenu or JPopupMenu is available.
 * <p>
 * @author Martin Foster
 * @version 1.0.1, %Revision, 99%, %LastChangedDate, 17/03/11 8:17PM%
 */
public class VolumeMenu extends JSlider {

	// Serial Requirements
	private static final long serialVersionUID = 1L;

	
	/*
	 * USER INTERFACE ELEMENT
	 */
	private JMenu menuVolume = null;
	private JPopupMenu popupVolume = null;
	private JPanel panelVolume = null;
	
	/*
	 * PACKAGE ELEMENTS
	 */
	private Map<String,String> Lang;
	
	
	/*
	 * CLASS ELEMENTS
	 */
	private Interact Interface;

	
	/**
	 * Base constructor for class.  There is no visual editor class due to
	 * the simplicity of the elements.
	 * <p>
	 * @param lang Language map for language independence.
	 */
	public VolumeMenu(Map<String,String> lang) {
		
		// Initialise with minimum and maximum
		super(0,100);
		
		// Pass up
		Lang = lang;
		
		// Adjust Alignment
		this.setOrientation(
				SwingConstants.VERTICAL);
		
		// Adjust Various Properties
		this.setMajorTickSpacing(25);
		this.setMinorTickSpacing(5);
		this.setPaintTicks(true);
		this.setPaintLabels(true);
		
		// Establish Identifier
		this.setName("CONTROL.VOLUME");
	}
	// End of Constructor Method
	
	
	/**
	 * This method initialises controlVolume.  Displays the volume control
	 * widget for display.
	 * <p>
	 * @return javax.swing.JMenu	
	 */
	public JMenu getMenuVolume() {
		
		// Check for Existence
		if (menuVolume == null) {
			
			// Create Instance
			menuVolume = new JMenu();
			
			// Language Element
			menuVolume.setText(Lang.get("Organizer.Menu.Options.Volume"));
			
			// Identifier
			menuVolume.setName("CONTROL.VOLUME");
			
			// Add in Slider
			menuVolume.add(this);
			
			// Add in Embellishments
			menuVolume.addSeparator();
			menuVolume.add(getPanelVolume());
		}
		
		// Return Instance
		return menuVolume;
	}
	// End of getMenuVolume Method
	
	
	/**
	 * This method initialises controlVolume.  Displays the volume control
	 * widget for display.
	 * <p>
	 * @return javax.swing.JPopupMenu	
	 */
	public JPopupMenu getPopupVolume() {
		
		// Check for Existence
		if (popupVolume == null) {
			
			// Create Instance
			popupVolume = new JPopupMenu();
			
			// Identifier
			popupVolume.setName("COMPT.POPUP");
			
			
			// Add in Slider
			popupVolume.add(this);
			
			// Add in Embellishments
			popupVolume.addSeparator();
			popupVolume.add(getPanelVolume());
			
			
			/*
			 * Interface creation and the associated listeners make up the
			 * expected user interface capability of the display being hidden
			 * when the user exits the JPopupMenu.  We also add in the JSlider
			 * instance since its edges leaves no ability for the later to
			 * detect an exit.
			 */
			
			// Create Instance
			Interface = new Interact();
			
			// Add Listeners
			getPopupVolume().addMouseListener(Interface);
			getSliderVolume().addMouseListener(Interface);
		}
		
		// Return Instance
		return popupVolume;
	}
	// End of getPopupVolume Method
		
	
	/**
	 * This method initialises checkVolume.  Provides a clean method for
	 * having the icon centred at the bottom as would be expected.
	 * <p>
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelVolume() {
		
		// Check for Existence
		if (panelVolume == null) {
			
			// Create Instance
			panelVolume = new JPanel();
			
			// Establish layout
			panelVolume.setLayout(new BoxLayout(panelVolume, BoxLayout.X_AXIS));
			
			// Identifier and Listener
			panelVolume.setName("COMPT.PANEL");
			
			/*
			 * This should create a centred look which will force the icon
			 * directly to the centre which rarely happens otherwise.
			 */
			
			// Spacer
			panelVolume.add(Box.createHorizontalGlue());

			// Embellishment
			panelVolume.add(new JLabel((new ImageIcon(
					Lyra.getIconByMap("Prompt-Volume")))));
			
			// Spacer
			panelVolume.add(Box.createHorizontalGlue());
		}
		
		// Return instance
		return panelVolume;
	}
	// End of getPanelVolume Method
	
	
	/**
	 * This method initialises sliderVolume.  
	 * <p>
	 * @return javax.swing.JSlider
	 */
	private JSlider getSliderVolume() {
		
		/*
		 * This method exists for consistency reasons only.  Since we typically
		 * abstract direct access, this will emulate that.
		 */
		return this;
	}
	// End of getSliderVolume Method
	
	
	/**
	 * Sets the value of the slider to a desired position.
	 * <p>
	 * @param value Integer containing position in slider.
	 */
	public void setState(int value) {
		
		// Adjust the slider value
		this.setValue(value);
	}
	// End of setState Method
	
	
	/**
	 * Gets the value of the slider at its current position.
	 * <p>
	 * @return Integer containing position in slider.
	 */
	public int getState() {
		
		// Pass-through method
		return this.getValue();
	}
	// End of getState Method
	
	
	/**
	 * Provides a codes separation between the user interface elements and the
	 * logic itself.  In this particular case this shall provide the ability to
	 * hide the window when focus is lost or the the ESC key is hit. 
	 * <p>
	 * @author Martin Foster
	 */
	private class Interact implements MouseListener{

		
		/**
		 * Stub constructor class.
		 */
		public Interact() {}
		// End of Constructor Method 
		
		
		/* (non-Javadoc)
		 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
		 */
		public void mouseExited(MouseEvent evt) {
			
			// Initial Location
			Point start = getPopupVolume().getLocationOnScreen();

			// End of actual screen
			Point end   = new Point(
								start.x + getPopupVolume().getWidth(),
								start.y + getPopupVolume().getHeight()
							);
			/*
			 * Here we make sure that any event triggered within these bounds
			 * will be ignored.   As they occur within the menu container
			 * we can care less that they are triggered.
			 */
			if ((evt.getXOnScreen() >= start.getX())
			 && (evt.getXOnScreen() <= end.getX())
			 && (evt.getYOnScreen() >= start.getY())
			 && (evt.getYOnScreen() <= end.getY()))
				return;
			
			/*
			 * We automatically hide the window when the mouse leaves the
			 * window as the user is no longer interested in our usage.
			 */
			getPopupVolume().setVisible(false);
		}
		// End of mouseExited Method
		
		
		/* (non-Javadoc)
		 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
		 */
		public void mouseClicked(MouseEvent evt) {}

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


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

		
		/* (non-Javadoc)
		 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
		 */
		public void mouseReleased(MouseEvent evt) {}
	}
	// End of Interact Class
}
