]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #124 Insert & remove row for GridLayout
authorArtur Signell <artur.signell@itmill.com>
Fri, 8 Aug 2008 10:40:43 +0000 (10:40 +0000)
committerArtur Signell <artur.signell@itmill.com>
Fri, 8 Aug 2008 10:40:43 +0000 (10:40 +0000)
svn changeset:5159/svn branch:trunk

WebContent/ITMILL/themes/tests-tickets/styles.css
src/com/itmill/toolkit/terminal/gwt/client/ui/IGridLayout.java
src/com/itmill/toolkit/tests/tickets/Ticket124.java [new file with mode: 0644]
src/com/itmill/toolkit/ui/GridLayout.java

index 1f20f4589e7cc5640776311f3af520513a23797d..2fd070513b22f4c813ca16823b1a0f155a5daa5d 100644 (file)
  {
        width: 100px;
        height: 50px;
- }
\ No newline at end of file
+ }
+/*****************************************************************************/
+/* Ticket 124                                                               */
+/*****************************************************************************/
+
+.i-gridlayout-border td {
+       border: 1px solid black;
+}
index f0593e734f3cfec1e4857d29f4c63ff291db0d52..d62f6300001e714b79a1d09ad8c89139846efb55 100644 (file)
@@ -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 (file)
index 0000000..3c7843d
--- /dev/null
@@ -0,0 +1,91 @@
+package com.itmill.toolkit.tests.tickets;\r
+\r
+import com.itmill.toolkit.Application;\r
+import com.itmill.toolkit.ui.Button;\r
+import com.itmill.toolkit.ui.GridLayout;\r
+import com.itmill.toolkit.ui.Label;\r
+import com.itmill.toolkit.ui.TextField;\r
+import com.itmill.toolkit.ui.Window;\r
+import com.itmill.toolkit.ui.Button.ClickEvent;\r
+import com.itmill.toolkit.ui.Button.ClickListener;\r
+\r
+public class Ticket124 extends Application {\r
+\r
+    private TextField tf;\r
+    private GridLayout gl;\r
+\r
+    public void init() {\r
+        Window w = new Window("#124: Insert & remove row for GridLayout");\r
+        setMainWindow(w);\r
+        setTheme("tests-tickets");\r
+        // gl = new GridLayout(4, 4);\r
+        gl = new GridLayout(2, 2);\r
+\r
+        tf = new TextField("Row nr");\r
+        Button insert = new Button("Insert row", new ClickListener() {\r
+\r
+            public void buttonClick(ClickEvent event) {\r
+                insertRow();\r
+\r
+            }\r
+        });\r
+        Button delete = new Button("Delete row", new ClickListener() {\r
+\r
+            public void buttonClick(ClickEvent event) {\r
+                deleteRow();\r
+\r
+            }\r
+        });\r
+\r
+//        gl.addComponent(new Label("0-0"), 0, 0);\r
+//        gl.addComponent(new Label("0-1"), 1, 0);\r
+         gl.addComponent(new Label("1-0"), 1, 0);\r
+         gl.addComponent(new Label("1-1"), 1, 1);\r
+         gl.addComponent(new Label("0,0-1,0"), 0, 0, 1, 0);\r
+         gl.addComponent(new Label("2,0-3,0"), 2, 0, 3, 0);\r
+         Label l = new\r
+         Label("Large cell 0,1-2,2<br/>yadayada<br/>lorem ipsum");\r
+         l.setContentMode(Label.CONTENT_XHTML);\r
+         gl.addComponent(l, 0, 1, 2, 2);\r
+         gl.addComponent(new Label("3-1"), 3, 1);\r
+         gl.addComponent(new Label("3,2-3,3"), 3, 2, 3, 3);\r
+        gl.addComponent(tf, 0, 3);\r
+        gl.addComponent(insert, 1, 3);\r
+         gl.addComponent(delete, 2, 3);\r
+\r
+        gl.setStyleName("border");\r
+        w.addComponent(gl);\r
+    }\r
+\r
+    protected void deleteRow() {\r
+        int pos = Integer.parseInt(tf.getValue().toString());\r
+        gl.removeRow(pos);\r
+\r
+    }\r
+\r
+    protected void clearRow() {\r
+        int pos = Integer.parseInt(tf.getValue().toString());\r
+        for (int col = 0; col < gl.getColumns(); col++) {\r
+            try {\r
+                gl.removeComponent(col, pos);\r
+            } catch (Exception e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+    protected void insertRow() {\r
+        int pos = Integer.parseInt(tf.getValue().toString());\r
+        gl.insertRow(pos);\r
+        try {\r
+            TextField t = new TextField("",\r
+                    "Newly added row");\r
+            t.setWidth("100%");\r
+            gl.addComponent(t, 0, pos, 3, pos);\r
+        } catch (Exception e) {\r
+            // TODO: handle exception\r
+        }\r
+    }\r
+\r
+}\r
index b0e78ea0444670d30eb2d947315320b7a849fbbc..65ad77b613dd3f9f18b01a2424c370547f6c673d 100644 (file)
@@ -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
-     *                <code>c</code> is supposed to occupy.
+     *                the column of the upper left corner of the area <code>c</code>
+     *                is supposed to occupy.
      * @param row1
-     *                the row of the upper left corner of the area
-     *                <code>c</code> is supposed to occupy.
+     *                the row of the upper left corner of the area <code>c</code> is
+     *                supposed to occupy.
      * @param column2
      *                the column of the lower right corner of the area
      *                <code>c</code> 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();
+
+    }
+
 }