PageRenderTime 93ms CodeModel.GetById 72ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/DockablePanel.java

#
Java | 286 lines | 211 code | 30 blank | 45 comment | 46 complexity | c7164e7495c4a98f487f9c539c305847 MD5 | raw file
  1/*
  2 * PanelWindowContainer.java - holds dockable windows
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2004 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.border.*;
 27import javax.swing.plaf.metal.*;
 28import javax.swing.*;
 29import java.awt.event.*;
 30import java.awt.font.*;
 31import java.awt.geom.AffineTransform;
 32import java.awt.*;
 33import java.util.*;
 34import org.gjt.sp.jedit.*;
 35//}}}
 36
 37class DockablePanel extends JPanel
 38{
 39	private PanelWindowContainer panel;
 40	private DockableWindowManager wm;
 41
 42	//{{{ DockablePanel constructor
 43	DockablePanel(PanelWindowContainer panel)
 44	{
 45		super(new CardLayout());
 46
 47		this.panel = panel;
 48		this.wm = panel.getDockableWindowManager();
 49
 50		ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler();
 51		addMouseListener(resizeMouseHandler);
 52		addMouseMotionListener(resizeMouseHandler);
 53	} //}}}
 54
 55	//{{{ getWindowContainer() method
 56	PanelWindowContainer getWindowContainer()
 57	{
 58		return panel;
 59	} //}}}
 60
 61	//{{{ showDockable() method
 62	void showDockable(String name)
 63	{
 64		((CardLayout)getLayout()).show(this,name);
 65	} //}}}
 66
 67	//{{{ getMinimumSize() method
 68	public Dimension getMinimumSize()
 69	{
 70		return new Dimension(0,0);
 71	} //}}}
 72
 73	//{{{ getPreferredSize() method
 74	public Dimension getPreferredSize()
 75	{
 76		final String position = panel.getPosition();
 77		final int dimension = panel.getDimension();
 78
 79		if(panel.getCurrent() == null)
 80			return new Dimension(0,0);
 81		else
 82		{
 83			if(position.equals(DockableWindowManager.TOP)
 84				|| position.equals(DockableWindowManager.BOTTOM))
 85			{
 86				if(dimension <= 0)
 87				{
 88					int height = super.getPreferredSize().height;
 89					panel.setDimension(height);
 90				}
 91				return new Dimension(0,
 92					dimension + PanelWindowContainer
 93					.SPLITTER_WIDTH);
 94			}
 95			else
 96			{
 97				if(dimension <= 0)
 98				{
 99					int width = super.getPreferredSize().width;
100					panel.setDimension(width);
101				}
102				return new Dimension(dimension +
103					PanelWindowContainer.SPLITTER_WIDTH,
104					0);
105			}
106		}
107	} //}}}
108
109	//{{{ setBounds() method
110	public void setBounds(int x, int y, int width, int height)
111	{
112		final String position = panel.getPosition();
113		final int dimension = panel.getDimension();
114
115		if(position.equals(DockableWindowManager.TOP) ||
116			position.equals(DockableWindowManager.BOTTOM))
117		{
118			if(dimension != 0 && height <= PanelWindowContainer.SPLITTER_WIDTH)
119				panel.show(null);
120			else
121				panel.setDimension(height);
122		}
123		else
124		{
125			if(dimension != 0 && width <= PanelWindowContainer.SPLITTER_WIDTH)
126				panel.show(null);
127			else
128				panel.setDimension(width);
129		}
130
131		super.setBounds(x,y,width,height);
132	} //}}}
133
134	//{{{ ResizeMouseHandler class
135	class ResizeMouseHandler extends MouseAdapter implements MouseMotionListener
136	{
137		boolean canDrag;
138		Point dragStart;
139
140		//{{{ mousePressed() method
141		public void mousePressed(MouseEvent evt)
142		{
143			if(canDrag)
144			{
145				wm.setResizePos(panel.getDimension(),panel);
146				dragStart = evt.getPoint();
147			}
148		} //}}}
149
150		//{{{ mouseReleased() method
151		public void mouseReleased(MouseEvent evt)
152		{
153			if(canDrag)
154			{
155				panel.setDimension(wm.resizePos
156					+ PanelWindowContainer
157					.SPLITTER_WIDTH);
158				wm.finishResizing();
159				dragStart = null;
160				wm.revalidate();
161			}
162		} //}}}
163
164		//{{{ mouseMoved() method
165		public void mouseMoved(MouseEvent evt)
166		{
167			Border border = getBorder();
168			if(border == null)
169			{
170				// collapsed
171				return;
172			}
173
174			String position = panel.getPosition();
175
176			Insets insets = border.getBorderInsets(DockablePanel.this);
177			canDrag = false;
178			//{{{ Top...
179			if(position.equals(DockableWindowManager.TOP))
180			{
181				if(evt.getY() >= getHeight() - insets.bottom)
182					canDrag = true;
183			} //}}}
184			//{{{ Left...
185			else if(position.equals(DockableWindowManager.LEFT))
186			{
187				if(evt.getX() >= getWidth() - insets.right)
188					canDrag = true;
189			} //}}}
190			//{{{ Bottom...
191			else if(position.equals(DockableWindowManager.BOTTOM))
192			{
193				if(evt.getY() <= insets.top)
194					canDrag = true;
195			} //}}}
196			//{{{ Right...
197			else if(position.equals(DockableWindowManager.RIGHT))
198			{
199				if(evt.getX() <= insets.left)
200					canDrag = true;
201			} //}}}
202
203			if(canDrag)
204			{
205				wm.setCursor(Cursor.getPredefinedCursor(
206					getAppropriateCursor()));
207			}
208			else
209			{
210				wm.setCursor(Cursor.getPredefinedCursor(
211					Cursor.DEFAULT_CURSOR));
212			}
213		} //}}}
214
215		//{{{ mouseDragged() method
216		public void mouseDragged(MouseEvent evt)
217		{
218			if(!canDrag)
219				return;
220
221			if(dragStart == null) // can't happen?
222				return;
223
224			wm.setCursor(Cursor.getPredefinedCursor(
225				getAppropriateCursor()));
226
227			int dimension = panel.getDimension();
228
229			String position = panel.getPosition();
230
231			//{{{ Top...
232			if(position.equals(DockableWindowManager.TOP))
233			{
234				wm.setResizePos(
235					evt.getY() - dragStart.y
236					+ dimension,
237					panel);
238			} //}}}
239			//{{{ Left...
240			else if(position.equals(DockableWindowManager.LEFT))
241			{
242				wm.setResizePos(evt.getX() - dragStart.x
243					+ dimension,
244					panel);
245			} //}}}
246			//{{{ Bottom...
247			else if(position.equals(DockableWindowManager.BOTTOM))
248			{
249				wm.setResizePos(dimension - evt.getY()
250					+ dragStart.y,
251					panel);
252			} //}}}
253			//{{{ Right...
254			else if(position.equals(DockableWindowManager.RIGHT))
255			{
256				wm.setResizePos(dimension - evt.getX()
257					+ dragStart.x,
258					panel);
259			} //}}}
260		} //}}}
261
262		//{{{ mouseExited() method
263		public void mouseExited(MouseEvent evt)
264		{
265			wm.setCursor(Cursor.getPredefinedCursor(
266				Cursor.DEFAULT_CURSOR));
267		} //}}}
268
269		//{{{ getCursor() method
270		private int getAppropriateCursor()
271		{
272			String position = panel.getPosition();
273
274			if(position.equals(DockableWindowManager.TOP))
275				return Cursor.N_RESIZE_CURSOR;
276			else if(position.equals(DockableWindowManager.LEFT))
277				return Cursor.W_RESIZE_CURSOR;
278			else if(position.equals(DockableWindowManager.BOTTOM))
279				return Cursor.S_RESIZE_CURSOR;
280			else if(position.equals(DockableWindowManager.RIGHT))
281				return Cursor.E_RESIZE_CURSOR;
282			else
283				throw new InternalError();
284		} //}}}
285	} //}}}
286}