PageRenderTime 49ms CodeModel.GetById 23ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/DockablePanel.java

#
Java | 317 lines | 233 code | 34 blank | 50 comment | 52 complexity | b2f142e3867a7c559f565c737437e770 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 org.gjt.sp.jedit.jEdit;
 27
 28import java.awt.CardLayout;
 29import java.awt.Cursor;
 30import java.awt.Dimension;
 31import java.awt.Insets;
 32import java.awt.Point;
 33import java.awt.event.MouseAdapter;
 34import java.awt.event.MouseEvent;
 35import java.awt.event.MouseMotionListener;
 36
 37import javax.swing.JPanel;
 38import javax.swing.border.Border;
 39//}}}
 40
 41/**
 42 * @version $Id: DockablePanel.java 13259 2008-08-10 20:54:46Z shlomy $
 43 */
 44class DockablePanel extends JPanel
 45{
 46	private PanelWindowContainer panel;
 47	private DockableWindowManagerImpl wm;
 48
 49	//{{{ DockablePanel constructor
 50	DockablePanel(PanelWindowContainer panel)
 51	{
 52		super(new CardLayout());
 53
 54		this.panel = panel;
 55		this.wm = panel.getDockableWindowManager();
 56
 57		ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler();
 58		addMouseListener(resizeMouseHandler);
 59		addMouseMotionListener(resizeMouseHandler);
 60	} //}}}
 61
 62	//{{{ getWindowContainer() method
 63	PanelWindowContainer getWindowContainer()
 64	{
 65		return panel;
 66	} //}}}
 67
 68	//{{{ showDockable() method
 69	void showDockable(String name)
 70	{
 71		((CardLayout)getLayout()).show(this,name);
 72	} //}}}
 73
 74	//{{{ getMinimumSize() method
 75	public Dimension getMinimumSize()
 76	{
 77		return new Dimension(0,0);
 78	} //}}}
 79
 80	//{{{ getPreferredSize() method
 81	public Dimension getPreferredSize()
 82	{
 83		final String position = panel.getPosition();
 84		final int dimension = panel.getDimension();
 85
 86		if(panel.getCurrent() == null)
 87			return new Dimension(0,0);
 88		else
 89		{
 90			if(position.equals(DockableWindowManager.TOP)
 91				|| position.equals(DockableWindowManager.BOTTOM))
 92			{
 93				if(dimension <= 0)
 94				{
 95					int height = super.getPreferredSize().height;
 96					panel.setDimension(height);
 97				}
 98				return new Dimension(0,
 99					dimension + PanelWindowContainer
100					.SPLITTER_WIDTH);
101			}
102			else
103			{
104				if(dimension <= 0)
105				{
106					int width = super.getPreferredSize().width;
107					panel.setDimension(width);
108				}
109				return new Dimension(dimension +
110					PanelWindowContainer.SPLITTER_WIDTH,
111					0);
112			}
113		}
114	} //}}}
115
116	//{{{ setBounds() method
117	public void setBounds(int x, int y, int width, int height)
118	{
119		final String position = panel.getPosition();
120		final int dimension = panel.getDimension();
121
122		if(position.equals(DockableWindowManager.TOP) ||
123			position.equals(DockableWindowManager.BOTTOM))
124		{
125			if(dimension != 0 && height <= PanelWindowContainer.SPLITTER_WIDTH)
126				panel.show((DockableWindowManagerImpl.Entry) null);
127			else
128				panel.setDimension(height);
129		}
130		else
131		{
132			if(dimension != 0 && width <= PanelWindowContainer.SPLITTER_WIDTH)
133				panel.show((DockableWindowManagerImpl.Entry) null);
134			else
135				panel.setDimension(width);
136		}
137
138		super.setBounds(x,y,width,height);
139	} //}}}
140
141	/** This belong to ResizeMouseHandler but requires to be static. */
142	static Point dragStart;
143	
144	//{{{ ResizeMouseHandler class
145	class ResizeMouseHandler extends MouseAdapter implements MouseMotionListener
146	{
147		/** This is true if the mouse is on the split bar. */
148		boolean canDrag;
149
150		//{{{ mousePressed() method
151		public void mousePressed(MouseEvent evt)
152		{
153			if(canDrag)
154			{
155				continuousLayout = jEdit.getBooleanProperty("appearance.continuousLayout");
156				wm.setResizePos(panel.getDimension(),panel);
157				dragStart = evt.getPoint();
158			}
159		} //}}}
160
161		//{{{ mouseReleased() method
162		public void mouseReleased(MouseEvent evt)
163		{
164			if(canDrag)
165			{
166				if (!continuousLayout)
167				{
168					panel.setDimension(wm.resizePos
169							   + PanelWindowContainer
170						.SPLITTER_WIDTH);
171				}
172				wm.finishResizing();
173				dragStart = null;
174				wm.revalidate();
175			}
176		} //}}}
177
178		//{{{ mouseMoved() method
179		public void mouseMoved(MouseEvent evt)
180		{
181			Border border = getBorder();
182			if(border == null)
183			{
184				// collapsed
185				return;
186			}
187
188			String position = panel.getPosition();
189
190			Insets insets = border.getBorderInsets(DockablePanel.this);
191			canDrag = false;
192			//{{{ Top...
193			if(position.equals(DockableWindowManager.TOP))
194			{
195				if(evt.getY() >= getHeight() - insets.bottom)
196					canDrag = true;
197			} //}}}
198			//{{{ Left...
199			else if(position.equals(DockableWindowManager.LEFT))
200			{
201				if(evt.getX() >= getWidth() - insets.right)
202					canDrag = true;
203			} //}}}
204			//{{{ Bottom...
205			else if(position.equals(DockableWindowManager.BOTTOM))
206			{
207				if(evt.getY() <= insets.top)
208					canDrag = true;
209			} //}}}
210			//{{{ Right...
211			else if(position.equals(DockableWindowManager.RIGHT))
212			{
213				if(evt.getX() <= insets.left)
214					canDrag = true;
215			} //}}}
216
217			if (dragStart == null)
218			{
219				if(canDrag)
220				{
221					wm.setCursor(Cursor.getPredefinedCursor(
222						getAppropriateCursor()));
223				}
224				else
225				{
226					wm.setCursor(Cursor.getPredefinedCursor(
227						Cursor.DEFAULT_CURSOR));
228				}
229			}
230		} //}}}
231
232		//{{{ mouseDragged() method
233		public void mouseDragged(MouseEvent evt)
234		{
235			if(!canDrag)
236				return;
237
238			if(dragStart == null) // can't happen?
239				return;
240
241			int dimension = panel.getDimension();
242
243			String position = panel.getPosition();
244
245			int newSize = 0;
246			//{{{ Top...
247			if(position.equals(DockableWindowManager.TOP))
248			{
249				newSize = evt.getY();
250				wm.setResizePos(
251					evt.getY() - dragStart.y
252					+ dimension,
253					panel);
254			} //}}}
255			//{{{ Left...
256			else if(position.equals(DockableWindowManager.LEFT))
257			{
258				newSize = evt.getX();
259				wm.setResizePos(evt.getX() - dragStart.x
260					+ dimension,
261					panel);
262			} //}}}
263			//{{{ Bottom...
264			else if(position.equals(DockableWindowManager.BOTTOM))
265			{
266				newSize = dimension - evt.getY();
267				wm.setResizePos(dimension - evt.getY()
268					+ dragStart.y,
269					panel);
270			} //}}}
271			//{{{ Right...
272			else if(position.equals(DockableWindowManager.RIGHT))
273			{
274				newSize = dimension - evt.getX();
275				wm.setResizePos(dimension - evt.getX()
276					+ dragStart.x,
277					panel);
278			} //}}}
279
280			if (continuousLayout)
281			{
282				panel.setDimension(newSize
283						   + PanelWindowContainer.SPLITTER_WIDTH);
284				wm.revalidate();
285			}
286		} //}}}
287
288		//{{{ mouseExited() method
289		public void mouseExited(MouseEvent evt)
290		{
291			if (dragStart == null)
292			{
293				wm.setCursor(Cursor.getPredefinedCursor(
294					Cursor.DEFAULT_CURSOR));
295			}
296		} //}}}
297
298		//{{{ getCursor() method
299		private int getAppropriateCursor()
300		{
301			String position = panel.getPosition();
302
303			if(position.equals(DockableWindowManager.TOP))
304				return Cursor.N_RESIZE_CURSOR;
305			else if(position.equals(DockableWindowManager.LEFT))
306				return Cursor.W_RESIZE_CURSOR;
307			else if(position.equals(DockableWindowManager.BOTTOM))
308				return Cursor.S_RESIZE_CURSOR;
309			else if(position.equals(DockableWindowManager.RIGHT))
310				return Cursor.E_RESIZE_CURSOR;
311			else
312				throw new InternalError();
313		} //}}}
314
315		private boolean continuousLayout;
316	} //}}}
317}