/**
 * 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.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.4, %Revision, 147%, %LastChangedDate, 06/11/11 4:57PM%
 */
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;

	
	/**
	 * 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());
		}
		
		// 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(
					getClass().getResource(
							Lyra.getIconByMap("Prompt-Volume"))))));
			
			// Spacer
			panelVolume.add(Box.createHorizontalGlue());
		}
		
		// Return instance
		return panelVolume;
	}
	// End of getPanelVolume 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
	
}
