From 1327596eb8ed9c97abcd4477156d9a960544c336 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 8 Aug 2008 10:40:43 +0000 Subject: [PATCH] Fix for #124 Insert & remove row for GridLayout svn changeset:5159/svn branch:trunk --- .../ITMILL/themes/tests-tickets/styles.css | 9 +- .../terminal/gwt/client/ui/IGridLayout.java | 15 +++ .../toolkit/tests/tickets/Ticket124.java | 91 ++++++++++++++++++ src/com/itmill/toolkit/ui/GridLayout.java | 96 +++++++++++++++++-- 4 files changed, 204 insertions(+), 7 deletions(-) create mode 100644 src/com/itmill/toolkit/tests/tickets/Ticket124.java diff --git a/WebContent/ITMILL/themes/tests-tickets/styles.css b/WebContent/ITMILL/themes/tests-tickets/styles.css index 1f20f4589e..2fd070513b 100644 --- a/WebContent/ITMILL/themes/tests-tickets/styles.css +++ b/WebContent/ITMILL/themes/tests-tickets/styles.css @@ -97,4 +97,11 @@ { width: 100px; height: 50px; - } \ No newline at end of file + } +/*****************************************************************************/ +/* Ticket 124 */ +/*****************************************************************************/ + +.i-gridlayout-border td { + border: 1px solid black; +} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IGridLayout.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IGridLayout.java index f0593e734f..d62f630000 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IGridLayout.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IGridLayout.java @@ -121,8 +121,23 @@ public class IGridLayout extends SimplePanel implements Paintable, Container, for (final Iterator iterator = iterator(); iterator.hasNext();) { oldWidgetWrappers.add(iterator.next()); } + + /* Clear() removes all widgets but leaves the tr and td tags */ clear(); + boolean structuralChange = uidl + .getBooleanAttribute("structuralChange"); + + /* + * If a row has been inserted or removed at the middle of the table + * we need to remove all old tr and td tags. + */ + if (structuralChange) { + while (getRowCount() > 0) { + removeRow(0); + } + } + final int[] alignments = uidl.getIntArrayAttribute("alignments"); int alignmentIndex = 0; diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket124.java b/src/com/itmill/toolkit/tests/tickets/Ticket124.java new file mode 100644 index 0000000000..3c7843d835 --- /dev/null +++ b/src/com/itmill/toolkit/tests/tickets/Ticket124.java @@ -0,0 +1,91 @@ +package com.itmill.toolkit.tests.tickets; + +import com.itmill.toolkit.Application; +import com.itmill.toolkit.ui.Button; +import com.itmill.toolkit.ui.GridLayout; +import com.itmill.toolkit.ui.Label; +import com.itmill.toolkit.ui.TextField; +import com.itmill.toolkit.ui.Window; +import com.itmill.toolkit.ui.Button.ClickEvent; +import com.itmill.toolkit.ui.Button.ClickListener; + +public class Ticket124 extends Application { + + private TextField tf; + private GridLayout gl; + + public void init() { + Window w = new Window("#124: Insert & remove row for GridLayout"); + setMainWindow(w); + setTheme("tests-tickets"); + // gl = new GridLayout(4, 4); + gl = new GridLayout(2, 2); + + tf = new TextField("Row nr"); + Button insert = new Button("Insert row", new ClickListener() { + + public void buttonClick(ClickEvent event) { + insertRow(); + + } + }); + Button delete = new Button("Delete row", new ClickListener() { + + public void buttonClick(ClickEvent event) { + deleteRow(); + + } + }); + +// gl.addComponent(new Label("0-0"), 0, 0); +// gl.addComponent(new Label("0-1"), 1, 0); + gl.addComponent(new Label("1-0"), 1, 0); + gl.addComponent(new Label("1-1"), 1, 1); + gl.addComponent(new Label("0,0-1,0"), 0, 0, 1, 0); + gl.addComponent(new Label("2,0-3,0"), 2, 0, 3, 0); + Label l = new + Label("Large cell 0,1-2,2
yadayada
lorem ipsum"); + l.setContentMode(Label.CONTENT_XHTML); + gl.addComponent(l, 0, 1, 2, 2); + gl.addComponent(new Label("3-1"), 3, 1); + gl.addComponent(new Label("3,2-3,3"), 3, 2, 3, 3); + gl.addComponent(tf, 0, 3); + gl.addComponent(insert, 1, 3); + gl.addComponent(delete, 2, 3); + + gl.setStyleName("border"); + w.addComponent(gl); + } + + protected void deleteRow() { + int pos = Integer.parseInt(tf.getValue().toString()); + gl.removeRow(pos); + + } + + protected void clearRow() { + int pos = Integer.parseInt(tf.getValue().toString()); + for (int col = 0; col < gl.getColumns(); col++) { + try { + gl.removeComponent(col, pos); + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + + protected void insertRow() { + int pos = Integer.parseInt(tf.getValue().toString()); + gl.insertRow(pos); + try { + TextField t = new TextField("", + "Newly added row"); + t.setWidth("100%"); + gl.addComponent(t, 0, pos, 3, pos); + } catch (Exception e) { + // TODO: handle exception + } + } + +} diff --git a/src/com/itmill/toolkit/ui/GridLayout.java b/src/com/itmill/toolkit/ui/GridLayout.java index b0e78ea044..65ad77b613 100644 --- a/src/com/itmill/toolkit/ui/GridLayout.java +++ b/src/com/itmill/toolkit/ui/GridLayout.java @@ -81,6 +81,11 @@ public class GridLayout extends AbstractLayout implements private static final int ALIGNMENT_DEFAULT = ALIGNMENT_TOP + ALIGNMENT_LEFT; + /** + * Has there been rows inserted or deleted in the middle of the layout since the last paint operation. + */ + private boolean structuralChange = false; + /** * Constructor for grid of given size (number of cells). Note that grid's * final size depends on the items that are added into the grid. Grid grows @@ -119,11 +124,11 @@ public class GridLayout extends AbstractLayout implements * @param c * the component to be added. * @param column1 - * the column of the upper left corner of the area - * c is supposed to occupy. + * the column of the upper left corner of the area c + * is supposed to occupy. * @param row1 - * the row of the upper left corner of the area - * c is supposed to occupy. + * the row of the upper left corner of the area c is + * supposed to occupy. * @param column2 * the column of the lower right corner of the area * c is supposed to occupy. @@ -363,6 +368,9 @@ public class GridLayout extends AbstractLayout implements target.addAttribute("h", rows); target.addAttribute("w", cols); + target.addAttribute("structuralChange", structuralChange); + structuralChange = false; + if (spacing) { target.addAttribute("spacing", spacing); } @@ -561,7 +569,7 @@ public class GridLayout extends AbstractLayout implements /** * The row of the upper left corner cell of the area. */ - private final int row1; + private int row1; /** * The column of the lower right corner cell of the area. @@ -571,7 +579,7 @@ public class GridLayout extends AbstractLayout implements /** * The row of the lower right corner cell of the area. */ - private final int row2; + private int row2; /** * Component painted on the area. @@ -984,4 +992,80 @@ public class GridLayout extends AbstractLayout implements return spacing; } + /** + * Inserts an empty row at the chosen position in the grid. + * + * @param row + * Number of the row the new row will be inserted before + */ + public void insertRow(int row) { + if (row > rows) { + throw new IllegalArgumentException("Cannot insert row at " + row + + " in a gridlayout with height " + rows); + } + + for (Iterator i = areas.iterator(); i.hasNext();) { + Area existingArea = (Area) i.next(); + // Areas ending below the row needs to be moved down or stretched + if (existingArea.row2 >= row) { + existingArea.row2++; + + // Stretch areas that span over the selected row + if (existingArea.row1 >= row) { + existingArea.row1++; + } + + } + } + + if (cursorY >= row) { + cursorY++; + } + + setRows(rows + 1); + structuralChange = true; + requestRepaint(); + } + + /** + * Removes row and all components in the row. Components which span over + * several rows are removed if the selected row is the component's first + * row. + * + * @param row + * The row number to remove + */ + public void removeRow(int row) { + if (row >= rows) { + throw new IllegalArgumentException("Cannot delete row " + row + + " from a gridlayout with height " + rows); + } + + // Remove all components in row + for (int col = 0; col < getColumns(); col++) { + removeComponent(col, row); + } + + // Shrink or remove areas in the selected row + for (Iterator i = areas.iterator(); i.hasNext();) { + Area existingArea = (Area) i.next(); + if (existingArea.row2 >= row) { + existingArea.row2--; + + if (existingArea.row1 > row) { + existingArea.row1--; + } + } + } + + setRows(rows - 1); + if (cursorY > row) { + cursorY--; + } + + structuralChange = true; + requestRepaint(); + + } + } -- 2.39.5