--- /dev/null
+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
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
* @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.
target.addAttribute("h", rows);
target.addAttribute("w", cols);
+ target.addAttribute("structuralChange", structuralChange);
+ structuralChange = false;
+
if (spacing) {
target.addAttribute("spacing", spacing);
}
/**
* 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.
/**
* The row of the lower right corner cell of the area.
*/
- private final int row2;
+ private int row2;
/**
* Component painted on the area.
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();
+
+ }
+
}