001: /*
002: * PanelWindowContainer.java - holds dockable windows
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2004 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import org.gjt.sp.jedit.jEdit;
027:
028: import java.awt.CardLayout;
029: import java.awt.Cursor;
030: import java.awt.Dimension;
031: import java.awt.Insets;
032: import java.awt.Point;
033: import java.awt.event.MouseAdapter;
034: import java.awt.event.MouseEvent;
035: import java.awt.event.MouseMotionListener;
036:
037: import javax.swing.JPanel;
038: import javax.swing.border.Border;
039:
040: //}}}
041:
042: /**
043: * @version $Id: DockablePanel.java 8717 2007-01-23 08:08:41Z kpouer $
044: */
045: class DockablePanel extends JPanel {
046: private PanelWindowContainer panel;
047: private DockableWindowManager wm;
048:
049: //{{{ DockablePanel constructor
050: DockablePanel(PanelWindowContainer panel) {
051: super (new CardLayout());
052:
053: this .panel = panel;
054: this .wm = panel.getDockableWindowManager();
055:
056: ResizeMouseHandler resizeMouseHandler = new ResizeMouseHandler();
057: addMouseListener(resizeMouseHandler);
058: addMouseMotionListener(resizeMouseHandler);
059: } //}}}
060:
061: //{{{ getWindowContainer() method
062: PanelWindowContainer getWindowContainer() {
063: return panel;
064: } //}}}
065:
066: //{{{ showDockable() method
067: void showDockable(String name) {
068: ((CardLayout) getLayout()).show(this , name);
069: } //}}}
070:
071: //{{{ getMinimumSize() method
072: public Dimension getMinimumSize() {
073: return new Dimension(0, 0);
074: } //}}}
075:
076: //{{{ getPreferredSize() method
077: public Dimension getPreferredSize() {
078: final String position = panel.getPosition();
079: final int dimension = panel.getDimension();
080:
081: if (panel.getCurrent() == null)
082: return new Dimension(0, 0);
083: else {
084: if (position.equals(DockableWindowManager.TOP)
085: || position.equals(DockableWindowManager.BOTTOM)) {
086: if (dimension <= 0) {
087: int height = super .getPreferredSize().height;
088: panel.setDimension(height);
089: }
090: return new Dimension(0, dimension
091: + PanelWindowContainer.SPLITTER_WIDTH);
092: } else {
093: if (dimension <= 0) {
094: int width = super .getPreferredSize().width;
095: panel.setDimension(width);
096: }
097: return new Dimension(dimension
098: + PanelWindowContainer.SPLITTER_WIDTH, 0);
099: }
100: }
101: } //}}}
102:
103: //{{{ setBounds() method
104: public void setBounds(int x, int y, int width, int height) {
105: final String position = panel.getPosition();
106: final int dimension = panel.getDimension();
107:
108: if (position.equals(DockableWindowManager.TOP)
109: || position.equals(DockableWindowManager.BOTTOM)) {
110: if (dimension != 0
111: && height <= PanelWindowContainer.SPLITTER_WIDTH)
112: panel.show(null);
113: else
114: panel.setDimension(height);
115: } else {
116: if (dimension != 0
117: && width <= PanelWindowContainer.SPLITTER_WIDTH)
118: panel.show(null);
119: else
120: panel.setDimension(width);
121: }
122:
123: super .setBounds(x, y, width, height);
124: } //}}}
125:
126: /** This belong to ResizeMouseHandler but requires to be static. */
127: static Point dragStart;
128:
129: //{{{ ResizeMouseHandler class
130: class ResizeMouseHandler extends MouseAdapter implements
131: MouseMotionListener {
132: /** This is true if the mouse is on the split bar. */
133: boolean canDrag;
134:
135: //{{{ mousePressed() method
136: public void mousePressed(MouseEvent evt) {
137: if (canDrag) {
138: continuousLayout = jEdit
139: .getBooleanProperty("appearance.continuousLayout");
140: wm.setResizePos(panel.getDimension(), panel);
141: dragStart = evt.getPoint();
142: }
143: } //}}}
144:
145: //{{{ mouseReleased() method
146: public void mouseReleased(MouseEvent evt) {
147: if (canDrag) {
148: if (!continuousLayout) {
149: panel.setDimension(wm.resizePos
150: + PanelWindowContainer.SPLITTER_WIDTH);
151: }
152: wm.finishResizing();
153: dragStart = null;
154: wm.revalidate();
155: }
156: } //}}}
157:
158: //{{{ mouseMoved() method
159: public void mouseMoved(MouseEvent evt) {
160: Border border = getBorder();
161: if (border == null) {
162: // collapsed
163: return;
164: }
165:
166: String position = panel.getPosition();
167:
168: Insets insets = border.getBorderInsets(DockablePanel.this );
169: canDrag = false;
170: //{{{ Top...
171: if (position.equals(DockableWindowManager.TOP)) {
172: if (evt.getY() >= getHeight() - insets.bottom)
173: canDrag = true;
174: } //}}}
175: //{{{ Left...
176: else if (position.equals(DockableWindowManager.LEFT)) {
177: if (evt.getX() >= getWidth() - insets.right)
178: canDrag = true;
179: } //}}}
180: //{{{ Bottom...
181: else if (position.equals(DockableWindowManager.BOTTOM)) {
182: if (evt.getY() <= insets.top)
183: canDrag = true;
184: } //}}}
185: //{{{ Right...
186: else if (position.equals(DockableWindowManager.RIGHT)) {
187: if (evt.getX() <= insets.left)
188: canDrag = true;
189: } //}}}
190:
191: if (dragStart == null) {
192: if (canDrag) {
193: wm
194: .setCursor(Cursor
195: .getPredefinedCursor(getAppropriateCursor()));
196: } else {
197: wm
198: .setCursor(Cursor
199: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
200: }
201: }
202: } //}}}
203:
204: //{{{ mouseDragged() method
205: public void mouseDragged(MouseEvent evt) {
206: if (!canDrag)
207: return;
208:
209: if (dragStart == null) // can't happen?
210: return;
211:
212: int dimension = panel.getDimension();
213:
214: String position = panel.getPosition();
215:
216: int newSize = 0;
217: //{{{ Top...
218: if (position.equals(DockableWindowManager.TOP)) {
219: newSize = evt.getY();
220: wm.setResizePos(evt.getY() - dragStart.y + dimension,
221: panel);
222: } //}}}
223: //{{{ Left...
224: else if (position.equals(DockableWindowManager.LEFT)) {
225: newSize = evt.getX();
226: wm.setResizePos(evt.getX() - dragStart.x + dimension,
227: panel);
228: } //}}}
229: //{{{ Bottom...
230: else if (position.equals(DockableWindowManager.BOTTOM)) {
231: newSize = dimension - evt.getY();
232: wm.setResizePos(dimension - evt.getY() + dragStart.y,
233: panel);
234: } //}}}
235: //{{{ Right...
236: else if (position.equals(DockableWindowManager.RIGHT)) {
237: newSize = dimension - evt.getX();
238: wm.setResizePos(dimension - evt.getX() + dragStart.x,
239: panel);
240: } //}}}
241:
242: if (continuousLayout) {
243: panel.setDimension(newSize
244: + PanelWindowContainer.SPLITTER_WIDTH);
245: wm.revalidate();
246: }
247: } //}}}
248:
249: //{{{ mouseExited() method
250: public void mouseExited(MouseEvent evt) {
251: if (dragStart == null) {
252: wm.setCursor(Cursor
253: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
254: }
255: } //}}}
256:
257: //{{{ getCursor() method
258: private int getAppropriateCursor() {
259: String position = panel.getPosition();
260:
261: if (position.equals(DockableWindowManager.TOP))
262: return Cursor.N_RESIZE_CURSOR;
263: else if (position.equals(DockableWindowManager.LEFT))
264: return Cursor.W_RESIZE_CURSOR;
265: else if (position.equals(DockableWindowManager.BOTTOM))
266: return Cursor.S_RESIZE_CURSOR;
267: else if (position.equals(DockableWindowManager.RIGHT))
268: return Cursor.E_RESIZE_CURSOR;
269: else
270: throw new InternalError();
271: } //}}}
272:
273: private boolean continuousLayout;
274: } //}}}
275: }
|