home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-13 | 5.9 KB | 183 lines |
- /*
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
- import java.awt.*;
- import java.util.Vector;
-
- public class DiagonalLayout implements LayoutManager {
-
- private int vgap;
- private int minWidth = 0, minHeight = 0;
- private int preferredWidth = 0, preferredHeight = 0;
- private boolean sizeUnknown = true;
-
- public DiagonalLayout() {
- this(5);
- }
-
- public DiagonalLayout(int v) {
- vgap = v;
- }
-
- /* Required by LayoutManager. */
- public void addLayoutComponent(String name, Component comp) {
- }
-
- /* Required by LayoutManager. */
- public void removeLayoutComponent(Component comp) {
- }
-
- private void setSizes(Container parent) {
- int nComps = parent.countComponents();
- Dimension d = null;
-
- //Reset preferred/minimum width and height.
- preferredWidth = 0;
- preferredHeight = 0;
- minWidth = 0;
- minHeight = 0;
-
- for (int i = 0; i < nComps; i++) {
- Component c = parent.getComponent(i);
- if (c.isVisible()) {
- d = c.preferredSize();
-
- if (i > 0) {
- preferredWidth += d.width/2;
- preferredHeight += vgap;
- } else {
- preferredWidth = d.width;
- }
- preferredHeight += d.height;
-
- minWidth = Math.max(c.minimumSize().width, minWidth);
- minHeight = preferredHeight;
- }
- }
- }
-
-
- /* Required by LayoutManager. */
- public Dimension preferredLayoutSize(Container parent) {
- Dimension dim = new Dimension(0, 0);
- int nComps = parent.countComponents();
-
- setSizes(parent);
-
- //Always add the container's insets!
- Insets insets = parent.insets();
- dim.width = preferredWidth + insets.left + insets.right;
- dim.height = preferredHeight + insets.top + insets.bottom;
-
- sizeUnknown = false;
-
- return dim;
- }
-
- /* Required by LayoutManager. */
- public Dimension minimumLayoutSize(Container parent) {
- Dimension dim = new Dimension(0, 0);
- int nComps = parent.countComponents();
-
- //Always add the container's insets!
- Insets insets = parent.insets();
- dim.width = minWidth + insets.left + insets.right;
- dim.height = minHeight + insets.top + insets.bottom;
-
- sizeUnknown = false;
-
- return dim;
- }
-
- /* Required by LayoutManager. */
- /* This is called when the panel is first displayed,
- * and every time its size changes.
- * Note: You CAN'T assume preferredLayoutSize() or minimumLayoutSize()
- * will be called -- in the case of applets, at least, they probably
- * won't be. */
- public void layoutContainer(Container parent) {
- Insets insets = parent.insets();
- int maxWidth = parent.size().width
- - (insets.left + insets.right);
- int maxHeight = parent.size().height
- - (insets.top + insets.bottom);
- int nComps = parent.countComponents();
- int previousWidth = 0, previousHeight = 0;
- int x = 0, y = insets.top;
- int rowh = 0, start = 0;
- int xFudge = 0, yFudge = 0;
- boolean oneColumn = false;
-
- // Go through the components' sizes, if neither preferredLayoutSize()
- // nor minimumLayoutSize() has been called.
- if (sizeUnknown) {
- setSizes(parent);
- }
-
- if (maxWidth <= minWidth) {
- oneColumn = true;
- }
-
- if (maxWidth != preferredWidth) {
- xFudge = (maxWidth - preferredWidth)/(nComps - 1);
- }
-
- if (maxHeight > preferredHeight) {
- yFudge = (maxHeight - preferredHeight)/(nComps - 1);
- }
-
- for (int i = 0 ; i < nComps ; i++) {
- Component c = parent.getComponent(i);
- if (c.isVisible()) {
- Dimension d = c.preferredSize();
-
- // increase x and y, if appropriate
- if (i > 0) {
- if (!oneColumn) {
- //x += previousWidth - d.width/2 + xFudge;
- x += previousWidth/2 + xFudge;
- }
- y += previousHeight + vgap + yFudge;
- }
-
- // If x is too large, ...
- if ((!oneColumn) &&
- (x + d.width) > (parent.size().width - insets.right)) {
- // ... reduce x to a reasonable number.
- x = parent.size().width - insets.bottom - d.width;
- }
-
- // If y is too large, ...
- if ((y + d.height) > (parent.size().height - insets.bottom)) {
- // ... do nothing.
- // Another choice would be to do what we do to x.
- }
-
- // Set the component's size and position.
- c.reshape(x, y, d.width, d.height);
-
- previousWidth = d.width;
- previousHeight = d.height;
- }
- }
- }
-
- public String toString() {
- String str = "";
- return getClass().getName() + "[vgap=" + vgap + str + "]";
- }
- }
-