diff options
Diffstat (limited to 'src/com/itmill/toolkit/ui/GridLayout.java')
-rw-r--r-- | src/com/itmill/toolkit/ui/GridLayout.java | 328 |
1 files changed, 198 insertions, 130 deletions
diff --git a/src/com/itmill/toolkit/ui/GridLayout.java b/src/com/itmill/toolkit/ui/GridLayout.java index 3324db74a8..3e2b7521ec 100644 --- a/src/com/itmill/toolkit/ui/GridLayout.java +++ b/src/com/itmill/toolkit/ui/GridLayout.java @@ -36,15 +36,20 @@ import java.util.LinkedList; import com.itmill.toolkit.terminal.PaintException; import com.itmill.toolkit.terminal.PaintTarget; -/** <p>A container that consists of components with certain coordinates on a +/** + * <p> + * A container that consists of components with certain coordinates on a * grid. It also maintains cursor for adding component in left to right, - * top to bottom order.</p> + * top to bottom order. + * </p> * - * <p>Each component in a <code>GridLayout</code> uses a certain + * <p> + * Each component in a <code>GridLayout</code> uses a certain * {@link GridLayout.Area area} (x1,y1,x2,y2) from the grid. One should not * add components that would overlap with the existing components because in * such case an {@link OverlapsException} is thrown. Adding component with - * cursor automatically extends the grid by increasing the grid height.</p> + * cursor automatically extends the grid by increasing the grid height. + * </p> * * @author IT Mill Ltd. * @version @VERSION@ @@ -52,65 +57,82 @@ import com.itmill.toolkit.terminal.PaintTarget; */ public class GridLayout extends AbstractComponentContainer implements Layout { - /** Initial grid x size */ + /** + * Initial grid x size. + */ private int width = 0; - /** Initial grid y size */ + /** + * Initial grid y size. + */ private int height = 0; - /** Cursor X position: this is where the next component with + /** + * Cursor X position: this is where the next component with * unspecified x,y is inserted */ private int cursorX = 0; - /** Cursor Y position: this is where the next component with + /** + * Cursor Y position: this is where the next component with * unspecified x,y is inserted */ private int cursorY = 0; - /** Contains all items that are placed on the grid. + /** + * Contains all items that are placed on the grid. * These are components with grid area definition. */ private LinkedList areas = new LinkedList(); - /** Mapping from components to threir respective areas. */ + /** + * Mapping from components to threir respective areas. + */ private LinkedList components = new LinkedList(); - /** Constructor for grid of given size. + /** + * Constructor for grid of given size. * Note that grid's final size depends on the items that are added into the grid. * Grid grows if you add components outside the grid's area. - * @param width Width of the grid. - * @param height Height of the grid. + * @param width the Width of the grid. + * @param height the Height of the grid. */ public GridLayout(int width, int height) { setWidth(width); setHeight(height); } - /** Constructs an empty grid layout that is extended as needed. */ + /** + * Constructs an empty grid layout that is extended as needed. + */ public GridLayout() { this(1, 1); } - /** <p>Adds a component with a specified area to the grid. The area the + /** + * <p> + * Adds a component with a specified area to the grid. The area the * new component should take is defined by specifying the upper left - * corner (x1, y1) and the lower right corner (x2, y2) of the area.</p> + * corner (x1, y1) and the lower right corner (x2, y2) of the area. + * </p> * - * <p>If the new component overlaps with any of the existing components + * <p> + * If the new component overlaps with any of the existing components * already present in the grid the operation will fail and an - * {@link OverlapsException} is thrown.</p> + * {@link OverlapsException} is thrown. + * </p> * - * @param c The component to be added. - * @param x1 The X-coordinate of the upper left corner of the area - * <code>c</code> is supposed to occupy - * @param y1 The Y-coordinate of the upper left corner of the area - * <code>c</code> is supposed to occupy - * @param x2 The X-coordinate of the lower right corner of the area - * <code>c</code> is supposed to occupy - * @param y2 The Y-coordinate of the lower right corner of the area - * <code>c</code> is supposed to occupy + * @param c the component to be added. + * @param x1 the X-coordinate of the upper left corner of the area + * <code>c</code> is supposed to occupy. + * @param y1 the Y-coordinate of the upper left corner of the area + * <code>c</code> is supposed to occupy. + * @param x2 the X-coordinate of the lower right corner of the area + * <code>c</code> is supposed to occupy. + * @param y2 the Y-coordinate of the lower right corner of the area + * <code>c</code> is supposed to occupy. * @throws OverlapsException if the new component overlaps with any - * of the components already in the grid + * of the components already in the grid. * @throws OutOfBoundsException if the coordinates are outside of the * grid area. */ @@ -125,23 +147,23 @@ public class GridLayout extends AbstractComponentContainer implements Layout { if (component == null) throw new NullPointerException("Component must not be null"); - // Check that the component does not already exist in the container + // Checks that the component does not already exist in the container if (components.contains(component)) throw new IllegalArgumentException("Component is already in the container"); - // Create area + // Creates the area Area area = new Area(component, x1, y1, x2, y2); - // Check the validity of the coordinates + // Checks the validity of the coordinates if (x2 < x1 || y2 < y2) throw new IllegalArgumentException("Illegal coordinates for the component"); if (x1 < 0 || y1 < 0 || x2 >= width || y2 >= height) throw new OutOfBoundsException(area); - // Check that newItem does not overlap with existing items + // Checks that newItem does not overlap with existing items checkExistingOverlaps(area); - // Insert the component to right place at the list + // Inserts the component to right place at the list // Respect top-down, left-right ordering component.setParent(this); Iterator i = areas.iterator(); @@ -166,12 +188,13 @@ public class GridLayout extends AbstractComponentContainer implements Layout { requestRepaint(); } - /** Tests if the given area overlaps with any of the items already on + /** + * Tests if the given area overlaps with any of the items already on * the grid. * - * @param area Area to be checked for overlapping + * @param area the Area to be checked for overlapping. * @throws OverlapsException if <code>area</code> overlaps with - * any existing area + * any existing area. */ private void checkExistingOverlaps(Area area) throws OverlapsException { for (Iterator i = areas.iterator(); i.hasNext();) { @@ -183,20 +206,22 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } } - /** Add component into this container to coordinates x1,y1 (NortWest corner of the area.) + /** + * Adds the component into this container to coordinates x1,y1 (NortWest corner of the area.) * End coordinates (SouthEast corner of the area) are the same as x1,y1. Component width * and height is 1. - * @param c The component to be added. - * @param x X-coordinate - * @param y Y-coordinate + * @param c the component to be added. + * @param x the X-coordinate. + * @param y the Y-coordinate. */ public void addComponent(Component c, int x, int y) { this.addComponent(c, x, y, x, y); } - /** Force the next component to be added to the beginning of the next line. - * By calling this function user can ensure that no more components are - * added to the right of the previous component. + /** + * Force the next component to be added to the beginning of the next line. + * By calling this function user can ensure that no more components are + * added to the right of the previous component. * * @see #space() */ @@ -205,7 +230,8 @@ public class GridLayout extends AbstractComponentContainer implements Layout { cursorY++; } - /** Move cursor forwards by one. If the cursor goes out of the right grid border, + /** + * Moves the cursor forwards by one. If the cursor goes out of the right grid border, * move it to next line. * * @see #newLine() @@ -218,15 +244,16 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } } - /** Add a component into this container to the cursor position. + /** + * Adds the component into this container to the cursor position. * If the cursor position is already occupied, the cursor is * moved forwards to find free position. If the cursor goes out * from the bottom of the grid, the grid is automaticly extended. - * @param c The component to be added. + * @param c the component to be added. */ public void addComponent(Component component) { - // Find first available place from the grid + // Finds first available place from the grid Area area; boolean done = false; while (!done) @@ -238,17 +265,18 @@ public class GridLayout extends AbstractComponentContainer implements Layout { space(); } - // Extend the grid if needed + // Extends the grid if needed width = cursorX >= width ? cursorX + 1 : width; height = cursorY >= height ? cursorY + 1 : height; addComponent(component, cursorX, cursorY); } - /** Removes the given component from this + /** + * Removes the given component from this * container. * - * @param c The component to be removed. + * @param c the component to be removed. */ public void removeComponent(Component component) { @@ -272,15 +300,16 @@ public class GridLayout extends AbstractComponentContainer implements Layout { requestRepaint(); } - /** Removes a component specified with it's top-left corner coordinates + /** + * Removes the component specified with it's top-left corner coordinates * from this grid. * - * @param x Component's top-left corner's X-coordinate - * @param y Component's top-left corner's Y-coordinate + * @param x the Component's top-left corner's X-coordinate. + * @param y the Component's top-left corner's Y-coordinate. */ public void removeComponent(int x, int y) { - // Find area + // Finds the area for (Iterator i = areas.iterator(); i.hasNext();) { Area area = (Area) i.next(); if (area.getX1() == x && area.getY1() == y) { @@ -290,19 +319,21 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } } - /** Gets an Iterator to the component container contents. Using the + /** + * Gets an Iterator to the component container contents. Using the * Iterator it's possible to step through the contents of the container. * - * @return Iterator of the components inside the container. + * @return the Iterator of the components inside the container. */ public Iterator getComponentIterator() { return Collections.unmodifiableCollection(components).iterator(); } - /** Paints the contents of this component. + /** + * Paints the contents of this component. * - * @param event PaintEvent. - * @throws PaintException The paint operation failed. + * @param target the Paint Event. + * @throws PaintException if the paint operation failed. */ public void paintContent(PaintTarget target) throws PaintException { @@ -315,20 +346,20 @@ public class GridLayout extends AbstractComponentContainer implements Layout { // Current item to be processed (fetch first item) Area area = areaiterator.hasNext() ? (Area) areaiterator.next() : null; - // Collect rowspan related information here + // Collects rowspan related information here HashMap cellUsed = new HashMap(); // Empty cell collector int emptyCells = 0; - // Iterate every applicable row + // Iterates every applicable row for (int cury = 0; cury < height; cury++) { target.startTag("gr"); - // Iterate every applicable column + // Iterates every applicable column for (int curx = 0; curx < width; curx++) { - // Check if current item is located at curx,cury + // Checks if current item is located at curx,cury if (area != null && (area.y1 == cury) && (area.x1 == curx)) { // First check if empty cell needs to be rendered @@ -368,7 +399,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { area = null; } - // Update cellUsed if rowspan needed + // Updates the cellUsed if rowspan needed if (rows > 1) { int spannedx = curx; for (int j = 1; j <= cols; j++) { @@ -379,14 +410,14 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } } - // Skip current item's spanned columns + // Skips the current item's spanned columns if (cols > 1) { curx += cols - 1; } } else { - // Check against cellUsed, render space or ignore cell + // Checks against cellUsed, render space or ignore cell if (cellUsed.containsKey(new Integer(curx))) { // Current column contains already an item, @@ -415,7 +446,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { // empty cell is needed emptyCells++; - // Remove cellUsed key as it has become obsolete + // Removes the cellUsed key as it has become obsolete cellUsed.remove(new Integer(curx)); } } else { @@ -425,11 +456,11 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } } - } // iterate every column + } // iterates every column // Last column handled of current row - // Check if empty cell needs to be rendered + // Checks if empty cell needs to be rendered if (emptyCells > 0) { target.startTag("gc"); target.addAttribute("x", width - emptyCells); @@ -443,54 +474,70 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } target.endTag("gr"); - } // iterate every row + } // iterates every row // Last row handled } - /** Gets the components UIDL tag. + /** + * Gets the components UIDL tag. * - * @return Component UIDL tag as string. + * @return the Component UIDL tag as string. * @see com.itmill.toolkit.ui.AbstractComponent#getTag() */ public String getTag() { return "gridlayout"; } - /** This class defines an area on a grid. An Area is defined by the + /** + * This class defines an area on a grid. An Area is defined by the * coordinates of its upper left corner (x1,y1) and lower right corner - * (x2,y2) + * (x2,y2). + * * @author IT Mill Ltd. * @version @VERSION@ * @since 3.0 */ public class Area { - /** X-coordinate of the upper left corner of the area */ + /** + * X-coordinate of the upper left corner of the area. + */ private int x1; - /** Y-coordinate of the upper left corner of the area */ + /** + * Y-coordinate of the upper left corner of the area. + */ private int y1; - /** X-coordinate of the lower right corner of the area */ + /** + * X-coordinate of the lower right corner of the area. + */ private int x2; - /** Y-coordinate of the lower right corner of the area */ + /** + * Y-coordinate of the lower right corner of the area. + */ private int y2; - /** Component painted on the area */ + /** + * Component painted on the area. + */ private Component component; - /** <p>Construct a new area on a grid. - * - * @param x1 The X-coordinate of the upper left corner of the area - * <code>c</code> is supposed to occupy - * @param y1 The Y-coordinate of the upper left corner of the area - * <code>c</code> is supposed to occupy - * @param x2 The X-coordinate of the lower right corner of the area - * <code>c</code> is supposed to occupy - * @param y2 The Y-coordinate of the lower right corner of the area - * <code>c</code> is supposed to occupy + /** + * <p> + * Construct a new area on a grid. + * </p> + * @param component the component connected to the area. + * @param x1 the X-coordinate of the upper left corner of the area + * <code>c</code> is supposed to occupy. + * @param y1 the Y-coordinate of the upper left corner of the area + * <code>c</code> is supposed to occupy. + * @param x2 the X-coordinate of the lower right corner of the area + * <code>c</code> is supposed to occupy. + * @param y2 the Y-coordinate of the lower right corner of the area + * <code>c</code> is supposed to occupy. * @throws OverlapsException if the new component overlaps with any * of the components already in the grid */ @@ -502,12 +549,13 @@ public class GridLayout extends AbstractComponentContainer implements Layout { this.component = component; } - /** Tests if the given Area overlaps with an another Area. + /** + * Tests if the given Area overlaps with an another Area. * - * @param other Another Area that's to be tested for overlap with - * this area + * @param other the Another Area that's to be tested for overlap with + * this area. * @return <code>true</code> if <code>other</code> overlaps with - * this area, <code>false</code> if it doesn't + * this area, <code>false</code> if it doesn't. */ public boolean overlaps(Area other) { return x1 <= other.getX2() @@ -517,42 +565,45 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } - /** Returns the component connected to the area. - * @return Component + /** + * Gets the component connected to the area. + * @return the Component. */ public Component getComponent() { return component; } - /** Sets the component connected to the area. + /** + * Sets the component connected to the area. * * <p>This function only sets the value in the datastructure and does not - * send any events or set parents</p> + * send any events or set parents.</p> * - * @param newComponent The new connected overriding the existing one + * @param newComponent the new connected overriding the existing one. */ protected void setComponent(Component newComponent) { component= newComponent; } - /** Returns the top-left corner x-coordinate. - * @return int + /** + * Gets the top-left corner x-coordinate. + * @return the top-left corner of x-coordinate. */ public int getX1() { return x1; } /** - * Returns the bottom-right corner x-coordinate. - * @return int + * Gets the bottom-right corner x-coordinate. + * @return the x-coordinate. */ public int getX2() { return x2; } /** - * Returns the top-left corner y-coordinate. - * @return int + * Gets the top-left corner y-coordinate. + * @return the y-coordinate. */ public int getY1() { return y1; @@ -560,7 +611,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { /** * Returns the bottom-right corner y-coordinate. - * @return int + * @return the y-coordinate. */ public int getY2() { return y2; @@ -568,8 +619,10 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } - /** An <code>Exception</code> object which is thrown when two Items + /** + * An <code>Exception</code> object which is thrown when two Items * occupy the same space on a grid. + * * @author IT Mill Ltd. * @version @VERSION@ * @since 3.0 @@ -583,21 +636,27 @@ public class GridLayout extends AbstractComponentContainer implements Layout { private Area existingArea; - /** Constructs an <code>OverlapsException</code>. - * @param msg the detail message. + /** + * Constructs an <code>OverlapsException</code>. + * @param existingArea */ public OverlapsException(Area existingArea) { this.existingArea = existingArea; } - /** Get the area */ + /** + * Gets the area . + * @return the existing area. + */ public Area getArea() { return existingArea; } } - /** An <code>Exception</code> object which is thrown when an area exceeds the + /** + * An <code>Exception</code> object which is thrown when an area exceeds the * bounds of the grid. + * * @author IT Mill Ltd. * @version @VERSION@ * @since 3.0 @@ -611,26 +670,29 @@ public class GridLayout extends AbstractComponentContainer implements Layout { private Area areaOutOfBounds; - /** Constructs an <code>OoutOfBoundsException</code> with the specified + /** + * Constructs an <code>OoutOfBoundsException</code> with the specified * detail message. * - * @param msg the detail message. + * @param areaOutOfBounds */ public OutOfBoundsException(Area areaOutOfBounds) { this.areaOutOfBounds = areaOutOfBounds; } - /** Get the area that is out of bounds */ + /** + * Gets the area that is out of bounds. + * @return the area out of Bound. + */ public Area getArea() { return areaOutOfBounds; } } - /** Set the width of the grid. The width can not be reduced if there are + /** + * Sets the width of the grid. The width can not be reduced if there are * any areas that would be outside of the shrunk grid. - * @param width New width of the grid. - * @throws OutOfBoundsException if the one of the areas would exceed the - * bounds of the grid after the modification of the grid size. + * @param width the New width of the grid. */ public void setWidth(int width) { @@ -642,7 +704,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { if (this.width == width) return; - // Check for overlaps + // Checks for overlaps if (this.width > width) for (Iterator i = areas.iterator(); i.hasNext();) { Area area = (Area) i.next(); @@ -655,16 +717,18 @@ public class GridLayout extends AbstractComponentContainer implements Layout { requestRepaint(); } - /** Get the width of the grids. - * @return The width of the grid + /** + * Get the width of the grids. + * @return the width of the grid. */ public final int getWidth() { return this.width; } - /** Set the height of the grid. The width can not be reduced if there are + /** + * Sets the height of the grid. The width can not be reduced if there are * any areas that would be outside of the shrunk grid. - * @param Height of the grid + * @param height the height of the grid. */ public void setHeight(int height) { @@ -676,7 +740,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { if (this.height == height) return; - // Check for overlaps + // Checks for overlaps if (this.height > height) for (Iterator i = areas.iterator(); i.hasNext();) { Area area = (Area) i.next(); @@ -689,30 +753,33 @@ public class GridLayout extends AbstractComponentContainer implements Layout { requestRepaint(); } - /** Get the height of the grid. - * @return int - how many cells high the grid is + /** + * Gets the height of the grid. + * @return int - how many cells high the grid is. */ public final int getHeight() { return this.height; } - /** Get the current cursor x-position. + /** + * Gets the current cursor x-position. * The cursor position points the position for the next component * that is added without specifying its coordinates. When the * cursor position is occupied, the next component will be added * to first free position after the cursor. - * @return Cursor x-coordinate. + * @return the Cursor x-coordinate. */ public int getCursorX() { return cursorX; } - /** Get the current cursor y-position. + /** + * Gets the current cursor y-position. * The cursor position points the position for the next component * that is added without specifying its coordinates. When the * cursor position is occupied, the next component will be added * to first free position after the cursor. - * @return Cursor y-coordinate. + * @return the Cursor y-coordinate. */ public int getCursorY() { return cursorY; @@ -723,7 +790,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { Component oldComponent, Component newComponent) { - // Get the locations + // Gets the locations Area oldLocation = null; Area newLocation = null; for (Iterator i=areas.iterator(); i.hasNext();) { @@ -746,6 +813,7 @@ public class GridLayout extends AbstractComponentContainer implements Layout { } /* + * Removes all components from this container. * @see com.itmill.toolkit.ui.ComponentContainer#removeAllComponents() */ public void removeAllComponents() { |