001: package net.sourceforge.squirrel_sql.client.gui;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.Dimension;
023: import java.awt.Graphics;
024: import java.awt.event.ComponentEvent;
025: import java.awt.event.ComponentListener;
026:
027: import javax.swing.JDesktopPane;
028: import javax.swing.JInternalFrame;
029:
030: //
031: public class ScrollableDesktopPane extends JDesktopPane {
032: // TODO: make serializable safe
033: private MyComponentListener _listener = new MyComponentListener();
034:
035: /**
036: * Default ctor.
037: */
038: public ScrollableDesktopPane() {
039: super ();
040: }
041:
042: protected void paintComponent(Graphics g) {
043: setPreferredSize(getRequiredSize());
044: super .paintComponent(g);
045: }
046:
047: public void remove(Component comp) {
048: if (comp != null) {
049: comp.removeComponentListener(_listener);
050: }
051: super .remove(comp);
052: revalidate();
053: repaint();
054: }
055:
056: protected void addImpl(Component comp, Object constraints, int index) {
057: if (comp != null) {
058: comp.addComponentListener(_listener);
059: revalidate();
060: }
061: super .addImpl(comp, constraints, index);
062: }
063:
064: /**
065: * Calculate the required size of this desktop pane so that
066: * all visible intenal frames will be fully shown.
067: *
068: * @return <TT>Dimension</TT> required size.
069: */
070: public Dimension getRequiredSize() {
071: JInternalFrame[] frames = getAllFrames();
072: int maxX = 0;
073: int maxY = 0;
074: for (int i = 0; i < frames.length; ++i) {
075: if (frames[i].isVisible()) {
076: JInternalFrame frame = frames[i];
077: int x = frame.getX() + frame.getWidth();
078: if (x > maxX) {
079: maxX = x;
080: }
081: int y = frame.getY() + frame.getHeight();
082: if (y > maxY) {
083: maxY = y;
084: }
085: }
086: }
087: return new Dimension(maxX, maxY);
088: }
089:
090: private final class MyComponentListener implements
091: ComponentListener {
092: public void componentHidden(ComponentEvent evt) {
093: ScrollableDesktopPane.this .revalidate();
094: }
095:
096: public void componentMoved(ComponentEvent evt) {
097: ScrollableDesktopPane.this .revalidate();
098: }
099:
100: public void componentResized(ComponentEvent evt) {
101: ScrollableDesktopPane.this .revalidate();
102: }
103:
104: public void componentShown(ComponentEvent evt) {
105: ScrollableDesktopPane.this.revalidate();
106: }
107: }
108: }
|