001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import javax.swing.*;
024: import java.awt.event.*;
025: import java.awt.*;
026: import java.util.*;
027:
028: public class PanelLayout implements LayoutManager {
029: private Dimension m_minimumSize;
030: private Hashtable m_componentToLayoutLengthDescriptor = new Hashtable();
031:
032: public PanelLayout() {
033: }
034:
035: public void addLayoutComponent(String name, Component comp) {
036: LayoutLengthDescriptor ld = new LayoutLengthDescriptor(name);
037: m_componentToLayoutLengthDescriptor.put(comp, ld);
038: }
039:
040: public void removeLayoutComponent(Component comp) {
041: m_componentToLayoutLengthDescriptor.remove(comp);
042: }
043:
044: private void calculateMinimumSize(Container parent) {
045: m_minimumSize = new Dimension();
046: for (int i = 0; i < parent.getComponentCount(); i++) {
047: Component element = parent.getComponent(i);
048: if (element.isVisible()) {
049: Dimension eld = element.getPreferredSize();
050: LayoutLengthDescriptor ld = (LayoutLengthDescriptor) m_componentToLayoutLengthDescriptor
051: .get(element);
052: if (ld != null)
053: eld.height = ld.getLength(parent.getHeight());
054:
055: m_minimumSize.height += eld.height;
056: m_minimumSize.width = Math.max(m_minimumSize.width,
057: eld.width);
058: }
059: }
060: }
061:
062: public Dimension preferredLayoutSize(Container parent) {
063: return minimumLayoutSize(parent);
064: }
065:
066: /* Required by LayoutManager. */
067: public Dimension minimumLayoutSize(Container parent) {
068: // if (m_minimumSize == null)
069: calculateMinimumSize(parent);
070:
071: Dimension dim = new Dimension(m_minimumSize);
072:
073: //Always add the container's insets!
074: Insets insets = parent.getInsets();
075: dim.width += insets.left + insets.right;
076: dim.height += insets.top + insets.bottom;
077:
078: return dim;
079: }
080:
081: public void layoutContainer(Container parent) {
082: Insets insets = parent.getInsets();
083: int xoffset = insets.left;
084: int yoffset = insets.top;
085: int maxcwidth = parent.getWidth()
086: - (insets.right + insets.left);
087: int maxcheight = parent.getHeight() - insets.bottom;
088:
089: for (int i = 0; i < parent.getComponentCount(); i++) {
090: Component element = parent.getComponent(i);
091: if (element.isVisible()) {
092: Dimension eld = element.getPreferredSize();
093: LayoutLengthDescriptor ld = (LayoutLengthDescriptor) m_componentToLayoutLengthDescriptor
094: .get(element);
095: if (ld != null)
096: eld.height = ld.getLength(parent.getHeight());
097:
098: if ((eld.height + yoffset) > maxcheight)
099: eld.height = maxcheight - yoffset;
100:
101: element.setBounds(xoffset, yoffset, maxcwidth,
102: eld.height);
103: yoffset += eld.height;
104: }
105: }
106: }
107:
108: // public void layoutContainer(Container parent) {
109: // Insets insets = parent.getInsets();
110: // int maxWidth = parent.getWidth() - (insets.left + insets.right);
111: // int maxHeight = parent.getHeight() - (insets.top + insets.bottom);
112: // int nComps = parent.getComponentCount();
113: // int previousWidth = 0, previousHeight = 0;
114: // int x = 0, y = insets.top;
115: // int rowh = 0, start = 0;
116: // int xFudge = 0, yFudge = 0;
117: // boolean oneColumn = false;
118:
119: // // Go through the components' sizes, if neither
120: // // preferredLayoutSize nor minimumLayoutSize has
121: // // been called.
122: // if (sizeUnknown) {
123: // setSizes(parent);
124: // }
125:
126: // if (maxWidth <= minWidth) {
127: // oneColumn = true;
128: // }
129:
130: // if (maxWidth != preferredWidth) {
131: // xFudge = (maxWidth - preferredWidth)/(nComps - 1);
132: // }
133:
134: // if (maxHeight > preferredHeight) {
135: // yFudge = (maxHeight - preferredHeight)/(nComps - 1);
136: // }
137:
138: // for (int i = 0 ; i < nComps ; i++) {
139: // Component c = parent.getComponent(i);
140: // if (c.isVisible()) {
141: // Dimension d = c.getPreferredSize();
142:
143: // // increase x and y, if appropriate
144: // if (i > 0) {
145: // if (!oneColumn) {
146: // x += previousWidth/2 + xFudge;
147: // }
148: // y += previousHeight + vgap + yFudge;
149: // }
150:
151: // // If x is too large,
152: // if ((!oneColumn) &&
153: // (x + d.width) >
154: // (parent.getWidth() - insets.right)) {
155: // // reduce x to a reasonable number.
156: // x = parent.getWidth()
157: // - insets.bottom - d.width;
158: // }
159:
160: // // If y is too large,
161: // if ((y + d.height)
162: // > (parent.getHeight() - insets.bottom)) {
163: // // do nothing.
164: // // Another choice would be to do what we do to x.
165: // }
166:
167: // // Set the component's size and position.
168: // c.setBounds(x, y, d.width, d.height);
169:
170: // previousWidth = d.width;
171: // previousHeight = d.height;
172: // }
173: // }
174: // }
175:
176: }
|