package echopointng.tabbedpane;

/* 
 * This file is part of the Echo Point Project.  This project is a collection
 * of Components that have extended the Echo Web Application Framework.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import nextapp.echo2.app.Border;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.ImageReference;
import nextapp.echo2.app.Insets;
import echopointng.BorderEx;
import echopointng.ButtonEx;
import echopointng.util.ColorKit;

/**
 * <code>DefaultTabModel</code> holds its content component references in
 * memory (and hence does no lazy loading of tab content)
 * <p>
 * It also does not release tab information unless intructed to do so by the
 * removeTabAt() method.
 * <p>
 * If no font or color information is passed in when a tab is created, then the
 * current colors and fonts of this model will be used later when the
 * getTabTitleAt() method is called.
 */

public class DefaultTabModel extends AbstractTabModel implements Serializable {

	private static final Color XP_ORANGE = ColorKit.makeColor("#FFC73C");
	private static final Color XP_BLUEISH_WHITE = ColorKit.makeColor("#FAFAF9");

	public static final Color 	DEFAULT_SELECTED_BACKGROUND = Color.WHITE;
	public static final Color 	DEFAULT_SELECTED_FOREGROUND = Color.BLACK;
	public static final Font 	DEFAULT_SELECTED_FONT = null;
	public static final Border 	DEFAULT_SELECTED_BORDER;
	
	public static final Color 	DEFAULT_SELECTED_ROLLOVER_BACKGROUND = Color.WHITE;
	public static final Color 	DEFAULT_SELECTED_ROLLOVER_FOREGROUND = Color.BLACK;
	public static final Font 	DEFAULT_SELECTED_ROLLOVER_FONT = null;
	public static final Border 	DEFAULT_SELECTED_ROLLOVER_BORDER;

	public static final Color 	DEFAULT_BACKGROUND = XP_BLUEISH_WHITE;
	public static final Color 	DEFAULT_FOREGROUND = Color.BLACK;
	public static final Font   	DEFAULT_FONT = null;
	public static final Border 	DEFAULT_BORDER;

	public static final Color 	DEFAULT_ROLLOVER_BACKGROUND = XP_BLUEISH_WHITE;
	public static final Color 	DEFAULT_ROLLOVER_FOREGROUND = Color.BLACK;
	public static final Font 	DEFAULT_ROLLOVER_FONT = null;
	public static final Border 	DEFAULT_ROLLOVER_BORDER;
	
	static {
		
		
		DEFAULT_BORDER = new BorderEx(
				new Extent(1), DEFAULT_BACKGROUND, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_BACKGROUND, Border.STYLE_SOLID,
				new Extent(2), DEFAULT_BACKGROUND, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_BACKGROUND, Border.STYLE_SOLID
				); 
		
		DEFAULT_ROLLOVER_BORDER  = new BorderEx(
				new Extent(1), DEFAULT_BACKGROUND, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_BACKGROUND, Border.STYLE_SOLID,
				new Extent(2), XP_ORANGE, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_BACKGROUND, Border.STYLE_SOLID
				); 
		
		DEFAULT_SELECTED_BORDER = new BorderEx(
				new Extent(1), DEFAULT_SELECTED_BACKGROUND, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_SELECTED_BACKGROUND, Border.STYLE_SOLID,
				new Extent(2), XP_ORANGE, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_SELECTED_BACKGROUND, Border.STYLE_SOLID
				); 

		DEFAULT_SELECTED_ROLLOVER_BORDER = new BorderEx(
				new Extent(1), DEFAULT_SELECTED_BACKGROUND, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_SELECTED_BACKGROUND, Border.STYLE_SOLID,
				new Extent(2), XP_ORANGE, Border.STYLE_SOLID,
				new Extent(1), DEFAULT_SELECTED_BACKGROUND, Border.STYLE_SOLID
				); 
		
	}
	
	
	/**
	 * <code>TabEntry</code> contains out tab data at given index
	 */
	private class TabEntry implements Serializable {
		Component tabComponent = null;

		Component tabContent = null;
	}

	private List tabEntryList = new ArrayList();

	private Color background = DEFAULT_BACKGROUND;
	private Color foreground = DEFAULT_FOREGROUND;
	private Font font = DEFAULT_FONT;
	private Border border = DEFAULT_BORDER;

	private Color rolloverBackground = DEFAULT_ROLLOVER_BACKGROUND;
	private Color rolloverForeground = DEFAULT_ROLLOVER_FOREGROUND;
	private Font rolloverFont = DEFAULT_ROLLOVER_FONT;
	private Border rolloverBorder = DEFAULT_ROLLOVER_BORDER;
	
	private Color selectedBackground = DEFAULT_SELECTED_BACKGROUND;
	private Color selectedForeground = DEFAULT_SELECTED_FOREGROUND;
	private Font selectedFont = DEFAULT_SELECTED_FONT;
	private Border selectedBorder  = DEFAULT_SELECTED_BORDER;

	private Color selectedRolloverBackground = DEFAULT_SELECTED_ROLLOVER_BACKGROUND;
	private Color selectedRolloverForeground = DEFAULT_SELECTED_ROLLOVER_FOREGROUND;
	private Font selectedRolloverFont = DEFAULT_SELECTED_ROLLOVER_FONT;
	private Border selectedRolloverBorder = DEFAULT_SELECTED_ROLLOVER_BORDER;
	
	/**
	 * <code>TabModelButton</code> is a ButtonEx that is linked to the
	 * TabModel and hence is told the tab selection state that it should 
	 * reflect in its colors and fonts etc.. by the TabModel.
	 * <p>
	 * The TabModel will call paintThyself(boolean isSelected) during
	 * the getTabAt() method to allow the ButtonEx to updates
	 * its representation of colors and fonts.
	 * <p>
	 */
	public class TabButton extends ButtonEx {
		
		
		
		/**
		 * Constructs a <code>TabModelButton</code>
		 */
		public TabButton(String text, ImageReference icon) {
			super(text, icon);
			setInsets(new Insets(3));
			setOutsets(new Insets(0));
		}

		/**
		 * Paints the button with the model's current colors
		 * based on whether the tab is selected or not. 
		 *
		 * @param isSelected - is the Tab selected
		 */
		public void paintThyself(boolean isSelected) {
			DefaultTabModel us = DefaultTabModel.this;
			// the ! eQ test prevents uncessary property updates
			// and hence unecessary repaints
			if (isSelected) {
				if (! eQ(getFont(),us.getSelectedFont()))
					setFont(us.getSelectedFont());
				if (! eQ(getForeground(),us.getSelectedForeground()))
					setForeground(us.getSelectedForeground());
				if (! eQ(getBackground(),us.getSelectedBackground()))
					setBackground(us.getSelectedBackground());
				if (! eQ(getBorder(),us.getSelectedBorder()))
					setBorder(us.getSelectedBorder());
				
				if (! eQ(getRolloverBackground(),us.getSelectedRolloverBackground()))
					setRolloverBackground(us.getSelectedRolloverBackground());
				if (! eQ(getRolloverForeground(),us.getSelectedRolloverForeground()))
					setRolloverForeground(us.getSelectedRolloverForeground());
				if (! eQ(getRolloverFont(),us.getSelectedRolloverFont()))
					setRolloverFont(us.getSelectedRolloverFont());
				if (! eQ(getRolloverBorder(),us.getSelectedRolloverBorder()))
					setRolloverBorder(us.getSelectedRolloverBorder());
			} else {
				if (! eQ(getFont(),us.getFont()))
					setFont(us.getFont());
				if (! eQ(getForeground(),us.getForeground()))
					setForeground(us.getForeground());
				if (! eQ(getBackground(),us.getBackground()))
					setBackground(us.getBackground());
				if (! eQ(getBorder(),us.getBorder()))
					setBorder(us.getBorder());
				
				if (! eQ(getRolloverBackground(),us.getRolloverBackground()))
					setRolloverBackground(us.getRolloverBackground());
				if (! eQ(getRolloverForeground(),us.getRolloverForeground()))
					setRolloverForeground(us.getRolloverForeground());
				if (! eQ(getRolloverFont(),us.getRolloverFont()))
					setRolloverFont(us.getRolloverFont());
				if (! eQ(getRolloverBorder(),us.getRolloverBorder()))
					setRolloverBorder(us.getRolloverBorder());
			}
		}
		
		boolean eQ(Object o1, Object o2) {
			if (o1 == o2)
				return true;
			if (o1 != null)
				return o1.equals(o2);
			return false;
		}
	};

	/**
	 * Constructs a <code>DefaultTabModel</code>
	 */
	public DefaultTabModel() {
	};

	/**
	 * @see echopointng.tabbedpane.TabModel#size()
	 */
	public int size() {
		return tabEntryList.size();
	}

	/**
	 * @see echopointng.tabbedpane.TabModel#getTabAt(int, boolean)
	 */
	public Component getTabAt(int index, boolean isSelected) {
		if (index < 0 || index > tabEntryList.size())
			return null;
		TabEntry entry = (TabEntry) tabEntryList.get(index);
		Component tabComponent = entry.tabComponent;
		if (tabComponent instanceof TabButton) {
			((TabButton) tabComponent).paintThyself(isSelected);
		}
		return tabComponent;
	}

	
	/**
	 * @see echopointng.tabbedpane.TabModel#getTabContentAt(int)
	 */
	public Component getTabContentAt(int index) {
		if (index < 0 || index > tabEntryList.size())
			return null;
		TabEntry entry = (TabEntry) tabEntryList.get(index);
		return entry.tabContent;
	}

	/**
	 * Does nothing as all tabs are kept in memory once added to this
	 * <code>TabModel</code> implementation.
	 * 
	 * @see echopointng.tabbedpane.TabModel#releaseTabAt(int)
	 */
	public void releaseTabAt(int index) {
		// we do nothing
	}

	/**
	 * Called to insert a new tab at the specified index. It will grow the
	 * collection of tabs to fit the new tab.
	 * 
	 * @param index -
	 *            the index to insert at, growing the collection as required
	 * 
	 * @param tabComponent -
	 *            the component to use as the tab for the given
	 *            <code>index</code>
	 * 
	 * @param tabContent -
	 *            the content to use for the given <code>index</code>
	 * @throws IndexOutOfBoundsException
	 *             if the index is out of range (index &lt; 0 || index &gt;
	 *             size()).
	 */
	public void insertTab(int index, Component tabComponent, Component tabContent) {
		TabEntry entry = new TabEntry();
		entry.tabComponent = tabComponent;
		entry.tabContent = tabContent;
		tabEntryList.add(index, entry);
		fireStateChanged();
	}

	/**
	 * Called to insert a new tab at the specified index. It will grow the
	 * collection of tabs to fit the new tab.
	 * <p>
	 * This will use the default colors and fonts defined by 
	 * this <code>TabModel</code>
	 * 
	 * @param index -
	 *            the index to insert at, growing the collection as required
	 * 
	 * @param tabTitle -
	 *            the title of the tab
	 * 
	 * @param icon -
	 *            the icon to use on the tab
	 * 
	 * @param tabContent -
	 *            the content to use for the given <code>index</code>
	 * @throws IndexOutOfBoundsException
	 *             if the index is out of range (index &lt; 0 || index &gt;
	 *             size()).
	 */
	public void insertTab(int index, String tabTitle, ImageReference icon, Component tabContent) {
		TabButton tabButton = new TabButton(tabTitle,icon);
		insertTab(index,tabButton,tabContent);
	}

	/**
	 * Adds a new tab to the end of the collection of tabs.
	 * <p>
	 * This will use the default colors and fonts defined by this
	 * <code>TabModel</code> if no color and font properties are set on the
	 * LabelEx at the time it was passed to the model.
	 * 
	 * @param tabComponent -
	 *            the component to use as the tab for the given
	 *            <code>index</code>
	 * 
	 * @param tabContent -
	 *            the component to use as the tab content.
	 */
	public void addTab(Component tabComponent, Component tabContent) {
		insertTab(tabEntryList.size(), tabComponent, tabContent);
	}

	/**
	 * Adds a new tab with the specified title. This will use the default colors
	 * and fonts defined by this <code>TabModel</code>
	 * 
	 * @param tabTitle -
	 *            the title of the new tab
	 * @param tabContent -
	 *            the component to use as the tab content.
	 */
	public void addTab(String tabTitle, Component tabContent) {
		addTab(tabTitle, null, tabContent);
	}

	/**
	 * Adds a new tab with the specified icon. This will use the default colors
	 * and fonts defined by this <code>TabModel</code>
	 * 
	 * @param icon -
	 *            the icon of rhe new tab
	 * @param tabContent -
	 *            the component to use as the tab content.
	 */
	public void addTab(ImageReference icon, Component tabContent) {
		addTab(null, icon, tabContent);
	}

	/**
	 * Adds a new tab with the specified title and icon. This will use the
	 * default colors and fonts defined by this <code>TabModel</code>
	 * 
	 * @param tabTitle -
	 *            the title of the new tab
	 * @param icon -
	 *            the icon of rhe new tab
	 * @param tabContent -
	 *            the component to use as the tab content.
	 */
	public void addTab(String tabTitle, ImageReference icon, Component tabContent) {
		insertTab(tabEntryList.size(), tabTitle, icon, tabContent);
	}

	/**
	 * Removes the tab details at the specified index
	 * 
	 * @param index -
	 *            the index of the tab details
	 * 
	 * @throws IndexOutOfBoundsException
	 *             if the index is out of range (index &lt; 0 || index &gt;=
	 *             size()).
	 */
	public void removeTabAt(int index) {
		tabEntryList.remove(index);
		fireStateChanged();
	}
	
	/**
	 * @see echopointng.tabbedpane.TabModel#indexOfTab(nextapp.echo2.app.Component)
	 */
	public int indexOfTab(Component tabComponent) {
		for (int i = 0; i < tabEntryList.size(); i++) {
			TabEntry entry = (TabEntry)  tabEntryList.get(i);
			if (entry.tabComponent == tabComponent)
				return i;
		}
		return -1;
	}

	/**
	 * @return the background color to be used on non selected tabs
	 */
	public Color getBackground() {
		return background;
	}

	/**
	 * Sets the background color to be used on non selected tabs
	 */
	public void setBackground(Color background) {
		this.background = background;
	}

	/**
	 * @return the font to be used on non selected tabs
	 */
	public Font getFont() {
		return font;
	}

	/**
	 * Sets the font to be used on non selected tabs
	 */
	public void setFont(Font font) {
		this.font = font;
	}

	/**
	 * @return the foreground color to be used on non selected tabs
	 */
	public Color getForeground() {
		return foreground;
	}

	/**
	 * Sets the foreground color to be used on non selected tabs
	 */
	public void setForeground(Color foreground) {
		this.foreground = foreground;
	}

	/**
	 * @return the background color to be used on selected tabs
	 */
	public Color getSelectedBackground() {
		return selectedBackground;
	}

	/**
	 * Sets the background color to be used on selected tabs
	 */
	public void setSelectedBackground(Color selectedBackground) {
		this.selectedBackground = selectedBackground;
	}

	/**
	 * @return the font to be used on selected tabs
	 */
	public Font getSelectedFont() {
		return selectedFont;
	}

	/**
	 * Sets the font to be used on selected tabs
	 */
	public void setSelectedFont(Font selectedFont) {
		this.selectedFont = selectedFont;
	}

	/**
	 * @return the foreground color to be used on selected tabs
	 */
	public Color getSelectedForeground() {
		return selectedForeground;
	}

	/**
	 * Sets the foreground color to be used on selected tabs
	 */
	public void setSelectedForeground(Color selectedForeground) {
		this.selectedForeground = selectedForeground;
	}
	
	/**
	 * @return the background color to use on tab rollover
	 */
	public Color getRolloverBackground() {
		return rolloverBackground;
	}
	/**
	 * Sets the background color to use on tab rollover
	 */
	public void setRolloverBackground(Color rolloverBackground) {
		this.rolloverBackground = rolloverBackground;
	}
	/**
	 * @return the font to use on tab rollover
	 */
	public Font getRolloverFont() {
		return rolloverFont;
	}
	/**
	 * Sets the font to use on tab rollover
	 */
	public void setRolloverFont(Font rolloverFont) {
		this.rolloverFont = rolloverFont;
	}
	/**
	 * @return the foreground color to use on tab rollover
	 */
	public Color getRolloverForeground() {
		return rolloverForeground;
	}
	/**
	 * Set the foreground color to use on tab rollover
	 */
	public void setRolloverForeground(Color rolloverForeground) {
		this.rolloverForeground = rolloverForeground;
	}
	/**
	 * @return the background color to use on rollover of selected tabs 
	 */
	public Color getSelectedRolloverBackground() {
		return selectedRolloverBackground;
	}
	/**
	 * Sets the background color to use on rollover of selected tabs 
	 */
	public void setSelectedRolloverBackground(Color selectedRolloverBackground) {
		this.selectedRolloverBackground = selectedRolloverBackground;
	}
	/**
	 * @return the font to use on rollover of selected tabs 
	 */
	public Font getSelectedRolloverFont() {
		return selectedRolloverFont;
	}
	/**
	 * Sets the font to use on rollover of selected tabs 
	 */
	public void setSelectedRolloverFont(Font selectedRolloverFont) {
		this.selectedRolloverFont = selectedRolloverFont;
	}
	/**
	 * @return the foreground color to use on rollover of selected tabs 
	 */
	public Color getSelectedRolloverForeground() {
		return selectedRolloverForeground;
	}
	/**
	 * Sets the foreground color to use on rollover of selected tabs 
	 */
	public void setSelectedRolloverForeground(Color selectedRolloverForeground) {
		this.selectedRolloverForeground = selectedRolloverForeground;
	}
	
	
	public Border getBorder() {
		return border;
	}
	public void setBorder(Border border) {
		this.border = border;
	}
	public Border getRolloverBorder() {
		return rolloverBorder;
	}
	public void setRolloverBorder(Border rolloverBorder) {
		this.rolloverBorder = rolloverBorder;
	}
	public Border getSelectedBorder() {
		return selectedBorder;
	}
	public void setSelectedBorder(Border selectedBorder) {
		this.selectedBorder = selectedBorder;
	}
	public Border getSelectedRolloverBorder() {
		return selectedRolloverBorder;
	}
	public void setSelectedRolloverBorder(Border selectedRolloverBorder) {
		this.selectedRolloverBorder = selectedRolloverBorder;
	}
}
