001: package net.sourceforge.squirrel_sql.fw.gui.action;
002:
003: /*
004: * Copyright (C) 2001-2003 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.Dimension;
022: import java.awt.event.ActionEvent;
023: import java.beans.PropertyVetoException;
024:
025: import javax.swing.JDesktopPane;
026: import javax.swing.JInternalFrame;
027:
028: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031:
032: /**
033: * This class will tile all internal frames owned by a
034: * <CODE>JDesktopPane</CODE>.
035: *
036: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
037: */
038: public abstract class TileInternalFramesAction extends BaseAction
039: implements IHasJDesktopPane {
040: /** Internationalized strings for this class. */
041: private static final StringManager s_stringMgr = StringManagerFactory
042: .getStringManager(TileInternalFramesAction.class);
043:
044: /**
045: * The <CODE>JDesktopPane</CODE> that owns the internal frames to be
046: * tiled.
047: */
048: private JDesktopPane _desktop;
049:
050: /**
051: * Default constructor.
052: */
053: public TileInternalFramesAction() {
054: this (null);
055: }
056:
057: /**
058: * Constructor specifying the <CODE>JDesktopPane</CODE> that owns the
059: * internal frames to be tiled.
060: *
061: * @param desktop the <CODE>JDesktopPane</CODE> that owns the
062: * internal frames to be cascaded.
063: */
064: public TileInternalFramesAction(JDesktopPane desktop) {
065: super (s_stringMgr.getString("TileInternalFramesAction.title"));
066: setJDesktopPane(desktop);
067: }
068:
069: /**
070: * Set the <CODE>JDesktopPane</CODE> that owns the internal frames to be
071: * cascaded.
072: *
073: * @param desktop the <CODE>JDesktopPane</CODE> that owns the
074: * internal frames to be cascaded.
075: */
076: public void setJDesktopPane(JDesktopPane value) {
077: _desktop = value;
078: }
079:
080: /**
081: * Tile the internal frames.
082: *
083: * @param evt Specifies the event being proceessed.
084: */
085: public void actionPerformed(ActionEvent evt) {
086: if (_desktop != null) {
087: JInternalFrame[] children = GUIUtils
088: .getNonMinimizedNonToolWindows(_desktop
089: .getAllFrames());
090: final int cells = children.length;
091: if (cells > 0) {
092: final RowColumnCount rcc = getRowColumnCount(cells);
093: final int rows = rcc._rowCount;
094: final int cols = rcc._columnCount;
095: //?? Extract this out into a class like CascadeInternalFramePositioner.
096:
097: final Dimension desktopSize = _desktop.getSize();
098: final int width = desktopSize.width / cols;
099: final int height = desktopSize.height / rows;
100: int xPos = 0;
101: int yPos = 0;
102:
103: for (int y = 0; y < rows; ++y) {
104: for (int x = 0; x < cols; ++x) {
105: final int idx = y + (x * rows);
106: if (idx >= cells) {
107: break;
108: }
109: JInternalFrame frame = children[idx];
110: if (!frame.isClosed()) {
111: if (frame.isIcon()) {
112: try {
113: frame.setIcon(false);
114: } catch (PropertyVetoException ignore) {
115: // Ignore.
116: }
117: } else if (frame.isMaximum()) {
118: try {
119: frame.setMaximum(false);
120: } catch (PropertyVetoException ignore) {
121: // Ignore.
122: }
123: }
124:
125: frame.reshape(xPos, yPos, width, height);
126: xPos += width;
127: }
128: }
129: xPos = 0;
130: yPos += height;
131: }
132: }
133: }
134: }
135:
136: /**
137: * Retrieve the number of rows and columns that the internal frames
138: * should be rearranged into.
139: *
140: * @param internalFrameCount Number of internal frames to be rearranged.
141: */
142: protected abstract RowColumnCount getRowColumnCount(
143: int internalFrameCount);
144:
145: public final static class RowColumnCount {
146: protected final int _rowCount;
147: protected final int _columnCount;
148:
149: public RowColumnCount(int rowCount, int columnCount) {
150: _rowCount = rowCount;
151: _columnCount = columnCount;
152: }
153: }
154: }
|