001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ResizablePanel.java,v 1.19 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui.panel;
024:
025: import net.infonode.gui.CursorManager;
026: import net.infonode.util.Direction;
027:
028: import javax.swing.*;
029: import java.awt.*;
030: import java.awt.event.MouseAdapter;
031: import java.awt.event.MouseEvent;
032: import java.awt.event.MouseMotionAdapter;
033:
034: /**
035: * @author $Author: jesper $
036: * @version $Revision: 1.19 $
037: */
038: public class ResizablePanel extends BaseContainer {
039: private Direction direction;
040: private int resizeWidth = 4;
041: private boolean cursorChanged;
042: private int offset = -1;
043: private boolean mouseInside;
044: private boolean heavyWeight = true;
045: private boolean continuousLayout = false;
046: private Component dragIndicator;
047: private JComponent layeredPane;
048: private JComponent innerArea;
049: private Dimension lastSize;
050: private int dragIndicatorThickness = 4;
051: private Component comp;
052:
053: public ResizablePanel(Direction _direction) {
054: this (false, _direction, null);
055: }
056:
057: public ResizablePanel(boolean useHeavyWeightDragIndicator,
058: Direction _direction, Component mouseListenComponent) {
059: super (new BorderLayout());
060: this .heavyWeight = useHeavyWeightDragIndicator;
061: this .direction = _direction;
062:
063: if (heavyWeight) {
064: dragIndicator = new Canvas();
065: } else {
066: dragIndicator = new BaseContainer();
067: }
068: setDragIndicatorColor(null);
069: if (mouseListenComponent == null)
070: mouseListenComponent = this ;
071:
072: mouseListenComponent.addMouseListener(new MouseAdapter() {
073: public void mouseExited(MouseEvent e) {
074: if (offset == -1)
075: resetCursor();
076:
077: mouseInside = false;
078: }
079:
080: public void mouseEntered(MouseEvent e) {
081: mouseInside = true;
082: }
083:
084: public void mousePressed(MouseEvent e) {
085: //if (MouseEventCoalesceManager.getInstance().isPressedAllowed(e)) {
086: if (!continuousLayout && layeredPane != null) {
087: if (layeredPane instanceof JLayeredPane)
088: layeredPane.add(dragIndicator,
089: JLayeredPane.DRAG_LAYER);
090: else
091: layeredPane.add(dragIndicator, 0);
092:
093: layeredPane.repaint();
094: updateDragIndicator(e);
095: }
096: if (cursorChanged) {
097: offset = direction == Direction.LEFT ? e.getPoint().x
098: : direction == Direction.RIGHT ? getWidth()
099: - e.getPoint().x
100: : direction == Direction.UP ? e
101: .getPoint().y : getHeight()
102: - e.getPoint().y;
103: }
104: //}
105: }
106:
107: public void mouseReleased(MouseEvent e) {
108: //if (MouseEventCoalesceManager.getInstance().isReleasedAllowed(e)) {
109: if (!continuousLayout && layeredPane != null) {
110: layeredPane.remove(dragIndicator);
111: layeredPane.repaint();
112: }
113: offset = -1;
114: checkCursor(e.getPoint());
115:
116: if (!continuousLayout && lastSize != null) {
117: setPreferredSize(lastSize);
118: revalidate();
119: }
120:
121: lastSize = null;
122: //}
123: }
124: });
125:
126: mouseListenComponent
127: .addMouseMotionListener(new MouseMotionAdapter() {
128: public void mouseMoved(MouseEvent e) {
129: checkCursor(e.getPoint());
130: }
131:
132: public void mouseDragged(MouseEvent e) {
133: if (offset != -1) {// && MouseEventCoalesceManager.getInstance().isDraggedAllowed(e)) {
134: int size = direction.isHorizontal() ? (direction == Direction.LEFT ? getWidth()
135: - e.getPoint().x + offset
136: : e.getPoint().x + offset)
137: : (direction == Direction.UP ? getHeight()
138: - e.getPoint().y + offset
139: : e.getPoint().y + offset);
140: lastSize = getBoundedSize(size);
141:
142: if (continuousLayout) {
143: setPreferredSize(lastSize);
144: revalidate();
145: } else {
146: updateDragIndicator(e);
147: }
148: }
149: }
150: });
151: }
152:
153: public void setComponent(Component c) {
154: if (comp != null)
155: remove(comp);
156:
157: if (c != null) {
158: add(c, BorderLayout.CENTER);
159: //c.repaint();
160: revalidate();
161: }
162:
163: comp = c;
164: }
165:
166: public void setDragIndicatorColor(Color color) {
167: dragIndicator.setBackground(color == null ? Color.DARK_GRAY
168: : color);
169: }
170:
171: public void setLayeredPane(JComponent layeredPane) {
172: this .layeredPane = layeredPane;
173: if (innerArea == null)
174: innerArea = layeredPane;
175: }
176:
177: public void setInnerArea(JComponent innerArea) {
178: if (innerArea == null)
179: innerArea = layeredPane;
180: else
181: this .innerArea = innerArea;
182: }
183:
184: public boolean isContinuousLayout() {
185: return continuousLayout;
186: }
187:
188: public void setContinuousLayout(boolean continuousLayout) {
189: this .continuousLayout = continuousLayout;
190: }
191:
192: public Dimension getPreferredSize() {
193: Dimension d = super .getPreferredSize();
194: return getBoundedSize(direction.isHorizontal() ? d.width
195: : d.height);
196: }
197:
198: private void updateDragIndicator(MouseEvent e) {
199: if (layeredPane != null) {
200: Point p = SwingUtilities.convertPoint((Component) e
201: .getSource(), e.getPoint(), layeredPane);
202: Point p2 = SwingUtilities.convertPoint(this .getParent(),
203: getLocation(), layeredPane);
204: Dimension size = innerArea.getSize();
205: Dimension minimumSize = getMinimumSize();
206: Point offset = SwingUtilities.convertPoint(innerArea, 0, 0,
207: layeredPane);
208:
209: if (direction.isHorizontal()) {
210: int x = 0;
211: if (direction == Direction.LEFT)
212: x = Math.min(Math.max(offset.x, p.x), offset.x
213: + size.width - minimumSize.width);
214: else
215: x = Math.min(Math.max(offset.x + minimumSize.width,
216: p.x), offset.x + size.width)
217: - dragIndicatorThickness;
218:
219: dragIndicator.setBounds(x, p2.y,
220: dragIndicatorThickness, getHeight());
221: } else {
222: int y = 0;
223: if (direction == Direction.UP)
224: y = Math.min(Math.max(offset.y, p.y), offset.y
225: + size.height - minimumSize.height);
226: else
227: y = Math.min(Math.max(
228: offset.y + minimumSize.height, p.y),
229: offset.y + size.height)
230: - dragIndicatorThickness;
231:
232: dragIndicator.setBounds(p2.x, y, getWidth(),
233: dragIndicatorThickness);
234: }
235: }
236: }
237:
238: private Dimension getBoundedSize(int size) {
239: if (direction.isHorizontal()) {
240: return new Dimension(Math.max(getMinimumSize().width, Math
241: .min(size, getMaximumSize().width)), 0);
242: } else {
243: return new Dimension(0, Math.max(getMinimumSize().height,
244: Math.min(size, getMaximumSize().height)));
245: }
246: }
247:
248: public void setResizeWidth(int width) {
249: this .resizeWidth = width;
250: }
251:
252: public int getResizeWidth() {
253: return resizeWidth;
254: }
255:
256: private void checkCursor(Point point) {
257: if (offset != -1)
258: return;
259:
260: int dist = direction == Direction.UP ? point.y
261: : direction == Direction.DOWN ? getHeight() - point.y
262: : direction == Direction.LEFT ? point.x
263: : getWidth() - point.x;
264:
265: if (dist >= 0 && dist < resizeWidth && mouseInside) {
266: if (!cursorChanged) {
267: cursorChanged = true;
268: CursorManager
269: .setGlobalCursor(
270: getRootPane(),
271: new Cursor(
272: direction == Direction.LEFT ? Cursor.W_RESIZE_CURSOR
273: : direction == Direction.RIGHT ? Cursor.E_RESIZE_CURSOR
274: : direction == Direction.UP ? Cursor.N_RESIZE_CURSOR
275: : Cursor.S_RESIZE_CURSOR));
276: }
277: } else
278: resetCursor();
279: }
280:
281: private void resetCursor() {
282: CursorManager.resetGlobalCursor(getRootPane());
283: cursorChanged = false;
284: }
285:
286: public Direction getDirection() {
287: return direction;
288: }
289:
290: public void setVisible(boolean aFlag) {
291: super.setVisible(aFlag);
292: }
293: }
|