public class GridLayout extends AbstractLayout implements Layout.AlignmentHandler, Layout.SpacingHandler, Layout.MarginHandler, LayoutEvents.LayoutClickNotifier
The GridLayout also maintains a cursor for adding components in left-to-right, top-to-bottom order.
Each component in a GridLayout
uses a defined
area
(column1,row1,column2,row2) from the grid. The
components may not overlap with the existing components - if you try to do so
you will get an GridLayout.OverlapsException
. Adding a component with cursor
automatically extends the grid by increasing the grid height.
The grid coordinates, which are specified by a row and column index, always start from 0 for the topmost row and the leftmost column.
Modifier and Type | Class and Description |
---|---|
class |
GridLayout.Area
Defines a rectangular area of cells in a GridLayout.
|
class |
GridLayout.OutOfBoundsException
An
Exception object which is thrown when an area exceeds the
bounds of the grid. |
class |
GridLayout.OverlapsException
Gridlayout does not support laying components on top of each other.
|
Layout.AlignmentHandler, Layout.MarginHandler, Layout.SpacingHandler
HasComponents.ComponentAttachDetachNotifier, HasComponents.ComponentAttachEvent, HasComponents.ComponentAttachListener, HasComponents.ComponentDetachEvent, HasComponents.ComponentDetachListener
Component.ErrorEvent, Component.Event, Component.Focusable, Component.Listener
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
Sizeable.Unit
DESIGN_ATTR_PLAIN_TEXT
SIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS
Constructor and Description |
---|
GridLayout()
Constructs an empty (1x1) grid layout that is extended as needed.
|
GridLayout(int columns,
int rows)
Constructor for a grid of given size (number of columns and rows).
|
GridLayout(int columns,
int rows,
Component... children)
Constructs a GridLayout of given size (number of columns and rows) and
adds the given components in order to the grid.
|
Modifier and Type | Method and Description |
---|---|
void |
addComponent(Component component)
Adds the component into this container to the cursor position.
|
void |
addComponent(Component component,
int column,
int row)
Adds the component to the grid in cells column1,row1 (NortWest corner of
the area.) End coordinates (SouthEast corner of the area) are the same as
column1,row1.
|
void |
addComponent(Component component,
int column1,
int row1,
int column2,
int row2)
Adds a component to the grid in the specified area.
|
void |
addLayoutClickListener(LayoutEvents.LayoutClickListener listener)
Add a click listener to the layout.
|
void |
addListener(LayoutEvents.LayoutClickListener listener)
Deprecated.
As of 7.0, replaced by
#addLayoutClickListener(LayoutClickListener) |
void |
beforeClientResponse(boolean initial)
Called before the shared state and RPC invocations are sent to the
client.
|
float |
getColumnExpandRatio(int columnIndex)
Returns the expand ratio of given column
|
int |
getColumns()
Get the number of columns in the grid.
|
Component |
getComponent(int x,
int y)
Gets the Component at given index.
|
Alignment |
getComponentAlignment(Component childComponent)
Returns the current Alignment of given component.
|
GridLayout.Area |
getComponentArea(Component component)
Returns information about the area where given component is laid in the
GridLayout.
|
int |
getComponentCount()
Gets the number of components contained in the layout.
|
int |
getCursorX()
Gets the current x-position (column) of the cursor.
|
int |
getCursorY()
Gets the current y-position (row) of the cursor.
|
protected Collection<String> |
getCustomAttributes()
Returns a collection of attributes that should not be handled by the
basic implementation of the
readDesign and writeDesign
methods. |
Alignment |
getDefaultComponentAlignment()
Returns the alignment used for new components added to this layout
|
MarginInfo |
getMargin() |
float |
getRowExpandRatio(int rowIndex)
Returns the expand ratio of given row.
|
int |
getRows()
Get the number of rows in the grid.
|
protected GridLayoutState |
getState()
Returns the shared state bean with information to be sent from the server
to the client.
|
protected GridLayoutState |
getState(boolean markAsDirty)
Returns the shared state for this connector.
|
void |
insertRow(int row)
Inserts an empty row at the specified position in the grid.
|
boolean |
isHideEmptyRowsAndColumns()
Checks whether whether empty rows and columns should be considered as
non-existent when rendering or not.
|
boolean |
isSpacing() |
Iterator<Component> |
iterator()
Gets an Iterator for the components contained in the layout.
|
void |
newLine()
Forces the next component to be added at the beginning of the next line.
|
void |
readDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
Reads the component state from the given design.
|
void |
removeAllComponents()
Removes all components from the container.
|
void |
removeComponent(Component component)
Removes the specified component from the layout.
|
void |
removeComponent(int column,
int row)
Removes the component specified by its cell coordinates.
|
void |
removeLayoutClickListener(LayoutEvents.LayoutClickListener listener)
Removes an LayoutClickListener.
|
void |
removeListener(LayoutEvents.LayoutClickListener listener)
Deprecated.
As of 7.0, replaced by
#removeLayoutClickListener(LayoutClickListener) |
void |
removeRow(int row)
Removes a row and all the components in the row.
|
void |
replaceComponent(Component oldComponent,
Component newComponent)
Replaces the component in the container with another one without changing
position.
|
void |
setColumnExpandRatio(int columnIndex,
float ratio)
Sets the expand ratio of given column.
|
void |
setColumns(int columns)
Sets the number of columns in the grid.
|
void |
setComponentAlignment(Component childComponent,
Alignment alignment)
Set alignment for one contained component in this layout.
|
void |
setCursorX(int cursorX)
Sets the current cursor x-position.
|
void |
setCursorY(int cursorY)
Sets the current y-coordinate (row) of the cursor.
|
void |
setDefaultComponentAlignment(Alignment defaultAlignment)
Sets the alignment used for new components added to this layout.
|
void |
setHideEmptyRowsAndColumns(boolean hideEmptyRowsAndColumns)
Sets whether empty rows and columns should be considered as non-existent
when rendering or not.
|
void |
setMargin(boolean enabled)
Enable layout margins.
|
void |
setMargin(MarginInfo marginInfo)
Enable margins for this layout.
|
void |
setRowExpandRatio(int rowIndex,
float ratio)
Sets the expand ratio of given row.
|
void |
setRows(int rows)
Sets the number of rows in the grid.
|
void |
setSpacing(boolean spacing)
Enable spacing between child components within this layout.
|
void |
space()
Moves the cursor forward by one.
|
void |
writeDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
Writes the component state to the given design.
|
readMargin, writeMargin
addComponentAttachListener, addComponentDetachListener, addComponents, addListener, addListener, fireComponentAttachEvent, fireComponentDetachEvent, getComponentIterator, moveComponentsFrom, removeComponentAttachListener, removeComponentDetachListener, removeListener, removeListener, setHeight, setWidth
addContextClickListener, addListener, addShortcutListener, addStyleName, attach, detach, findAncestor, fireComponentErrorEvent, fireComponentEvent, focus, getActionManager, getCaption, getComponentError, getData, getDebugId, getDescription, getErrorMessage, getExplicitImmediateValue, getHeight, getHeightUnits, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getWidth, getWidthUnits, isCaptionAsHtml, isConnectorEnabled, isEnabled, isImmediate, isOrHasAncestor, isReadOnly, isResponsive, isVisible, removeContextClickListener, removeListener, removeShortcutListener, removeStyleName, setCaption, setCaptionAsHtml, setComponentError, setData, setDebugId, setDescription, setEnabled, setHeight, setHeightUndefined, setIcon, setId, setImmediate, setLocale, setParent, setPrimaryStyleName, setReadOnly, setResponsive, setSizeFull, setSizeUndefined, setStyleName, setStyleName, setVisible, setWidth, setWidthUndefined
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addMethodInvocationToQueue, createState, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
addComponents, addListener, addListener, getComponentIterator, moveComponentsFrom, removeListener, removeListener
addListener, addStyleName, attach, getCaption, getDescription, getIcon, getId, getLocale, getParent, getPrimaryStyleName, getStyleName, getUI, isEnabled, isReadOnly, isVisible, removeListener, removeStyleName, setCaption, setEnabled, setIcon, setId, setParent, setPrimaryStyleName, setReadOnly, setStyleName, setVisible
addAttachListener, addDetachListener, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, handleConnectorRequest, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
getConnectorId
getHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setHeightUndefined, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthUndefined
forEach, spliterator
addComponentAttachListener, addComponentDetachListener, removeComponentAttachListener, removeComponentDetachListener
public GridLayout(int columns, int rows)
columns
- Number of columns in the grid.rows
- Number of rows in the grid.public GridLayout()
public GridLayout(int columns, int rows, Component... children)
columns
- Number of columns in the grid.rows
- Number of rows in the grid.children
- Components to add to the grid.AbstractComponentContainer.addComponents(Component...)
protected GridLayoutState getState()
AbstractComponent
getState
in class AbstractLayout
protected GridLayoutState getState(boolean markAsDirty)
AbstractClientConnector
getState
in class AbstractComponent
markAsDirty
- true if the connector should automatically be marked dirty,
false otherwiseAbstractClientConnector.getState()
public void addComponent(Component component, int column1, int row1, int column2, int row2) throws GridLayout.OverlapsException, GridLayout.OutOfBoundsException
Adds a component to the grid in the specified area. The area is defined by specifying the upper left corner (column1, row1) and the lower right corner (column2, row2) of the area. The coordinates are zero-based.
If the area overlaps with any of the existing components already present
in the grid, the operation will fail and an GridLayout.OverlapsException
is
thrown.
component
- the component to be added, not null
.column1
- the column of the upper left corner of the area c
is supposed to occupy. The leftmost column has index 0.row1
- the row of the upper left corner of the area c
is
supposed to occupy. The topmost row has index 0.column2
- the column of the lower right corner of the area
c
is supposed to occupy.row2
- the row of the lower right corner of the area c
is supposed to occupy.GridLayout.OverlapsException
- if the new component overlaps with any of the components
already in the grid.GridLayout.OutOfBoundsException
- if the cells are outside the grid area.public void addComponent(Component component, int column, int row) throws GridLayout.OverlapsException, GridLayout.OutOfBoundsException
component
- the component to be added, not null
.column
- the column index, starting from 0.row
- the row index, starting from 0.GridLayout.OverlapsException
- if the new component overlaps with any of the components
already in the grid.GridLayout.OutOfBoundsException
- if the cell is outside the grid area.public void newLine()
Sets the cursor column to 0 and increments the cursor row by one.
By calling this function you can ensure that no more components are added right of the previous component.
space()
public void space()
newLine()
public void addComponent(Component component)
addComponent
in interface ComponentContainer
addComponent
in class AbstractComponentContainer
component
- the component to be added, not null
.ComponentContainer.addComponent(Component)
public void removeComponent(Component component)
removeComponent
in interface ComponentContainer
removeComponent
in class AbstractComponentContainer
component
- the component to be removed.ComponentContainer.removeComponent(Component)
public void removeComponent(int column, int row)
column
- the component's column, starting from 0.row
- the component's row, starting from 0.public Iterator<Component> iterator()
iterator
in interface HasComponents
iterator
in interface Iterable<Component>
public int getComponentCount()
AbstractComponentContainer.getComponentIterator()
.getComponentCount
in interface ComponentContainer
public void beforeClientResponse(boolean initial)
ClientConnector
beforeClientResponse
in interface ClientConnector
beforeClientResponse
in class AbstractComponent
initial
- true
if the client-side connector will be created
and initialized after this method has been invoked.
false
if there is already an initialized
client-side connector.public Alignment getComponentAlignment(Component childComponent)
Layout.AlignmentHandler
getComponentAlignment
in interface Layout.AlignmentHandler
Alignment
public void setColumns(int columns)
columns
- the new number of columns in the grid.public int getColumns()
public void setRows(int rows)
rows
- the new number of rows in the grid.public int getRows()
public int getCursorX()
The cursor position points the position for the next component that is added without specifying its coordinates (grid cell). When the cursor position is occupied, the next component will be added to first free position after the cursor.
public void setCursorX(int cursorX)
cursorX
- public int getCursorY()
The cursor position points the position for the next component that is added without specifying its coordinates (grid cell). When the cursor position is occupied, the next component will be added to the first free position after the cursor.
public void setCursorY(int cursorY)
cursorY
- the row number, starting from 0 for the topmost row.public void replaceComponent(Component oldComponent, Component newComponent)
ComponentContainer
This method replaces component with another one is such way that the new component overtakes the position of the old component. If the old component is not in the container, the new component is added to the container. If the both component are already in the container, their positions are swapped. Component attach and detach events should be taken care as with add and remove.
replaceComponent
in interface ComponentContainer
oldComponent
- the old component that will be replaced.newComponent
- the new component to be replaced.public void removeAllComponents()
AbstractComponentContainer
removeAllComponents
in interface ComponentContainer
removeAllComponents
in class AbstractComponentContainer
public void setComponentAlignment(Component childComponent, Alignment alignment)
Layout.AlignmentHandler
layout.setComponentAlignment(myComponent, Alignment.TOP_RIGHT);
setComponentAlignment
in interface Layout.AlignmentHandler
childComponent
- the component to align within it's layout cell.alignment
- the Alignment value to be setpublic void setSpacing(boolean spacing)
Layout.SpacingHandler
NOTE: This will only affect the space between
components, not the space around all the components in the layout
(i.e. do not confuse this with the cellspacing attribute of a HTML
Table). Use #setMargin(boolean)
to add space around the
layout.
See the reference manual for more information about CSS rules for defining the amount of spacing to use.
setSpacing
in interface Layout.SpacingHandler
spacing
- true if spacing should be turned on, false if it should be
turned offpublic boolean isSpacing()
isSpacing
in interface Layout.SpacingHandler
public void insertRow(int row)
row
- Index of the row before which the new row will be inserted.
The leftmost row has index 0.public void removeRow(int row)
Components which span over several rows are removed if the selected row is on the first row of such a component.
If the last row is removed then all remaining components will be removed and the grid will be reduced to one row. The cursor will be moved to the upper left cell of the grid.
row
- Index of the row to remove. The leftmost row has index 0.public void setColumnExpandRatio(int columnIndex, float ratio)
The expand ratio defines how excess space is distributed among columns. Excess space means space that is left over from components that are not sized relatively. By default, the excess space is distributed evenly.
Note, that width of this GridLayout needs to be defined (fixed or relative, as opposed to undefined height) for this method to have any effect.
Note that checking for relative width for the child components is done on
the server so you cannot set a child component to have undefined width on
the server and set it to 100%
in CSS. You must set it to
100%
on the server.
columnIndex
- ratio
- #setWidth(float, int)
public float getColumnExpandRatio(int columnIndex)
columnIndex
- setColumnExpandRatio(int, float)
public void setRowExpandRatio(int rowIndex, float ratio)
Expand ratio defines how excess space is distributed among rows. Excess space means the space left over from components that are not sized relatively. By default, the excess space is distributed evenly.
Note, that height of this GridLayout needs to be defined (fixed or relative, as opposed to undefined height) for this method to have any effect.
Note that checking for relative height for the child components is done
on the server so you cannot set a child component to have undefined
height on the server and set it to 100%
in CSS. You must set
it to 100%
on the server.
rowIndex
- The row index, starting from 0 for the topmost row.ratio
- #setHeight(float, int)
public float getRowExpandRatio(int rowIndex)
rowIndex
- The row index, starting from 0 for the topmost row.setRowExpandRatio(int, float)
public Component getComponent(int x, int y)
x
- The column index, starting from 0 for the leftmost column.y
- The row index, starting from 0 for the topmost row.public GridLayout.Area getComponentArea(Component component)
component
- the component whose area information is requested.public void addLayoutClickListener(LayoutEvents.LayoutClickListener listener)
LayoutEvents.LayoutClickNotifier
LayoutEvents.LayoutClickEvent
.
Use #removeListener(LayoutClickListener)
to remove the
listener.addLayoutClickListener
in interface LayoutEvents.LayoutClickNotifier
listener
- The listener to add@Deprecated public void addListener(LayoutEvents.LayoutClickListener listener)
#addLayoutClickListener(LayoutClickListener)
addListener
in interface LayoutEvents.LayoutClickNotifier
public void removeLayoutClickListener(LayoutEvents.LayoutClickListener listener)
LayoutEvents.LayoutClickNotifier
removeLayoutClickListener
in interface LayoutEvents.LayoutClickNotifier
listener
- LayoutClickListener to be removed@Deprecated public void removeListener(LayoutEvents.LayoutClickListener listener)
#removeLayoutClickListener(LayoutClickListener)
removeListener
in interface LayoutEvents.LayoutClickNotifier
public void setMargin(boolean enabled)
Layout.MarginHandler
setMargin
in interface Layout.MarginHandler
enabled
- true if margins should be enabled on all sides, false to
disable all marginspublic void setMargin(MarginInfo marginInfo)
Layout.MarginHandler
NOTE: This will only affect the space around the
components in the layout, not space between the components in the
layout. Use #setSpacing(boolean)
to add space between the
components in the layout.
See the reference manual for more information about CSS rules for defining the size of the margin.
setMargin
in interface Layout.MarginHandler
marginInfo
- MarginInfo object containing the new margins.public MarginInfo getMargin()
getMargin
in interface Layout.MarginHandler
public Alignment getDefaultComponentAlignment()
Layout.AlignmentHandler
getDefaultComponentAlignment
in interface Layout.AlignmentHandler
public void setDefaultComponentAlignment(Alignment defaultAlignment)
Layout.AlignmentHandler
Alignment.TOP_LEFT
.setDefaultComponentAlignment
in interface Layout.AlignmentHandler
defaultAlignment
- The new default alignmentpublic void setHideEmptyRowsAndColumns(boolean hideEmptyRowsAndColumns)
hideEmptyRowsAndColumns
- true to hide empty rows and columns, false to leave them as-ispublic boolean isHideEmptyRowsAndColumns()
setHideEmptyRowsAndColumns(boolean)
public void readDesign(org.jsoup.nodes.Element design, DesignContext designContext)
The component is responsible not only for updating its own state but also for ensuring that its children update their state based on the design.
It is assumed that the component is in its default state when this method is called. Reading should only take into consideration attributes specified in the design and not reset any unspecified attributes to their defaults.
This method must not modify the design.
After reading the design, cursorY is set to point to a row outside of the GridLayout area. CursorX is reset to 0.
readDesign
in interface Component
readDesign
in class AbstractComponent
design
- The element to obtain the state fromdesignContext
- The DesignContext instance used for parsing the designpublic void writeDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Component
The component is responsible not only for writing its own state but also for ensuring that its children write their state to the design.
This method must not modify the component state.
writeDesign
in interface Component
writeDesign
in class AbstractComponent
design
- The element to write the component state to. Any previous
attributes or child nodes are not cleared.designContext
- The DesignContext instance used for writing the designprotected Collection<String> getCustomAttributes()
AbstractComponent
readDesign
and writeDesign
methods. Typically these are handled in a custom way in the overridden
versions of the above methodsgetCustomAttributes
in class AbstractComponent
Copyright © 2019 Vaadin Ltd. All rights reserved.