/*
 * PanelWindowContainer.java - holds dockable windows
 * :tabSize=4:indentSize=4:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 2000, 2004 Slava Pestov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.gjt.sp.jedit.gui;

//{{{ Imports
import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.Border;

import static org.gjt.sp.jedit.gui.DockableWindowManager.*;
import static org.gjt.sp.jedit.gui.DockableWindowManager.TOP;
//}}}

/** A panel that holds dockable windows.
 *
 * @version $Id: DockablePanel.java 25313 2020-05-04 22:18:59Z kpouer $
 */
class DockablePanel extends JPanel
{
	private final PanelWindowContainer panel;
	private final DockableWindowManagerImpl wm;

	//{{{ DockablePanel constructor
	DockablePanel(PanelWindowContainer panel)
	{
		super(new CardLayout());

		this.panel = panel;
		wm = panel.getDockableWindowManager();

		ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler();
		addMouseListener(resizeMouseHandler);
		addMouseMotionListener(resizeMouseHandler);
	} //}}}

	//{{{ getWindowContainer() method
	PanelWindowContainer getWindowContainer()
	{
		return panel;
	} //}}}

	//{{{ showDockable() method
	void showDockable(String name)
	{
		((CardLayout)getLayout()).show(this,name);
	} //}}}

	//{{{ getMinimumSize() method
	@Override
	public Dimension getMinimumSize()
	{
		return new Dimension(0,0);
	} //}}}

	//{{{ getPreferredSize() method
	@Override
	public Dimension getPreferredSize()
	{
		final String position = panel.getPosition();
		final int dimension = panel.getDimension();

		if(panel.getCurrent() == null)
			return new Dimension(0,0);
		else
		{
			if(position.equals(TOP)
				|| position.equals(BOTTOM))
			{
				if(dimension <= 0)
				{
					int height = super.getPreferredSize().height;
					panel.setDimension(height);
				}
				return new Dimension(0,
					dimension + PanelWindowContainer
					.SPLITTER_WIDTH);
			}
			else
			{
				if(dimension <= 0)
				{
					int width = super.getPreferredSize().width;
					panel.setDimension(width);
				}
				return new Dimension(dimension +
					PanelWindowContainer.SPLITTER_WIDTH,
					0);
			}
		}
	} //}}}

	//{{{ setBounds() method
	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		final String position = panel.getPosition();
		final int dimension = panel.getDimension();

		if(position.equals(TOP) ||
			position.equals(BOTTOM))
		{
			if(dimension != 0 && height <= PanelWindowContainer.SPLITTER_WIDTH)
				panel.show((DockableWindowManagerImpl.Entry) null);
			else
				panel.setDimension(height);
		}
		else
		{
			if(dimension != 0 && width <= PanelWindowContainer.SPLITTER_WIDTH)
				panel.show((DockableWindowManagerImpl.Entry) null);
			else
				panel.setDimension(width);
		}

		super.setBounds(x,y,width,height);
	} //}}}

	/** This belong to ResizeMouseHandler but requires to be static. */
	static Point dragStart;

	//{{{ ResizeMouseHandler class
	private class ResizeMouseHandler extends MouseAdapter
	{
		/** This is true if the mouse is on the split bar. */
		boolean canDrag;

		//{{{ mousePressed() method
		@Override
		public void mousePressed(MouseEvent evt)
		{
			if(canDrag)
			{
				continuousLayout = UIManager.getBoolean("SplitPane.continuousLayout");
				wm.setResizePos(panel.getDimension(),panel);
				dragStart = evt.getPoint();
			}
		} //}}}

		//{{{ mouseReleased() method
		@Override
		public void mouseReleased(MouseEvent evt)
		{
			if(canDrag)
			{
				if (!continuousLayout)
				{
					panel.setDimension(wm.resizePos
							   + PanelWindowContainer
						.SPLITTER_WIDTH);
				}
				wm.finishResizing();
				dragStart = null;
				wm.revalidate();
			}
		} //}}}

		//{{{ mouseMoved() method
		@Override
		public void mouseMoved(MouseEvent evt)
		{
			Border border = getBorder();
			if(border == null)
			{
				// collapsed
				return;
			}

			String position = panel.getPosition();

			Insets insets = border.getBorderInsets(DockablePanel.this);
			canDrag = false;
			//{{{ Top...
			switch (position)
			{
				case TOP:
					if (evt.getY() >= getHeight() - insets.bottom)
						canDrag = true;
					break;
				case LEFT:
					if (evt.getX() >= getWidth() - insets.right)
						canDrag = true;
					break;
				case BOTTOM:
					if (evt.getY() <= insets.top)
						canDrag = true;
					break;
				case RIGHT:
					if (evt.getX() <= insets.left)
						canDrag = true;
					break;
			}

			if (dragStart == null)
			{
				if(canDrag)
				{
					wm.setCursor(Cursor.getPredefinedCursor(
						getAppropriateCursor()));
				}
				else
				{
					wm.setCursor(Cursor.getPredefinedCursor(
						Cursor.DEFAULT_CURSOR));
				}
			}
		} //}}}

		//{{{ mouseDragged() method
		@Override
		public void mouseDragged(MouseEvent evt)
		{
			if(!canDrag)
				return;

			if(dragStart == null) // can't happen?
				return;

			int dimension = panel.getDimension();

			String position = panel.getPosition();

			int newSize = 0;
			//{{{ Top...
			switch (position)
			{
				case TOP:
					newSize = evt.getY();
					wm.setResizePos(evt.getY() - dragStart.y + dimension, panel);
					break;
				case LEFT:
					newSize = evt.getX();
					wm.setResizePos(evt.getX() - dragStart.x + dimension, panel);
					break;
				case BOTTOM:
					newSize = dimension - evt.getY();
					wm.setResizePos(dimension - evt.getY() + dragStart.y, panel);
					break;
				case RIGHT:
					newSize = dimension - evt.getX();
					wm.setResizePos(dimension - evt.getX() + dragStart.x, panel);
					break;
			}

			if (continuousLayout)
			{
				panel.setDimension(newSize + PanelWindowContainer.SPLITTER_WIDTH);
				wm.revalidate();
			}
		} //}}}

		//{{{ mouseExited() method
		@Override
		public void mouseExited(MouseEvent evt)
		{
			if (dragStart == null)
			{
				wm.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}
		} //}}}

		//{{{ getCursor() method
		private int getAppropriateCursor()
		{
			String position = panel.getPosition();

			switch (position)
			{
				case TOP:
					return Cursor.N_RESIZE_CURSOR;
				case LEFT:
					return Cursor.W_RESIZE_CURSOR;
				case BOTTOM:
					return Cursor.S_RESIZE_CURSOR;
				case RIGHT:
					return Cursor.E_RESIZE_CURSOR;
				default:
					throw new InternalError();
			}
		} //}}}

		private boolean continuousLayout;
	} //}}}
}
