BrightSide Workbench Full Report + Source Code
GridLayout.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.layout;
19 
20 import org.turro.zkoss.grid.GroupExtended;
21 import org.turro.zkoss.label.StyledLabel;
22 import org.zkoss.zk.ui.Component;
23 import org.zkoss.zk.ui.HtmlBasedComponent;
24 import org.zkoss.zk.ui.ext.AfterCompose;
25 import org.zkoss.zul.*;
26 
31 public class GridLayout extends Grid {
32 
33  private Object data;
34 
35  protected Row currentRow;
36 
37  public GridLayout() {
38  super();
39  setSclass("grid-layout");
40  }
41 
42  public GridLayout(int columns) {
43  super();
44  setSclass("grid-layout");
45  setColumns(columns);
46  }
47 
48  public GridLayout(String hflexs) {
49  super();
50  setSclass("grid-layout");
51  setColumns(hflexs);
52  }
53 
54  public void setColumns(int columns) {
55  setSpan(true);
56  Columns cols = getColumns();
57  if(cols == null) {
58  cols = new Columns();
59  appendChild(cols);
60  } else {
61  cols.getChildren().clear();
62  }
63  cols.setVisible(false);
64  for(int i = 0; i < columns; i++) {
65  Column col = new Column();
66  col.setHflex("min");
67  cols.appendChild(col);
68  }
69  }
70 
71  public void setColumns(String hflexs) {
72  setSpan(true);
73  String hfs[] = hflexs.split(",");
74  Columns cols = getColumns();
75  if(cols == null) {
76  cols = new Columns();
77  appendChild(cols);
78  } else {
79  cols.getChildren().clear();
80  }
81  cols.setStyle("height:0px");
82  for(int i = 0; i < hfs.length; i++) {
83  Column col = new Column();
84  if(hfs[i].startsWith("right-")) {
85  col.setAlign("right");
86  setWidthFlex(col, hfs[i].substring(6));
87  } else {
88  setWidthFlex(col, hfs[i]);
89  }
90  cols.appendChild(col);
91  }
92  }
93 
94  public void setColCaptions(String captions) {
95  setColCaptions(captions.split(","));
96  }
97 
98  public void setColCaptions(String[] captions) {
99  setSclass("grid-layout-head");
100  invalidate();
101  int index = 0;
102  for(Component col : getColumns().getChildren()) {
103  ((Column)col).setLabel(captions[index++]);
104  }
105  getColumns().setStyle(null);
106  }
107 
108  public void clearRows() {
109  getRows(true).getChildren().clear();
110  currentRow = null;
111  }
112 
113  public GridLayout addGroup(String title, boolean open) {
114  GroupExtended group = new GroupExtended(title);
115  group.setOpen(open);
116  getRows(true).appendChild(group);
117  return this;
118  }
119 
120  public GridLayout addRow() {
121  currentRow = new Row();
122  getRows(true).appendChild(currentRow);
123  return this;
124  }
125 
126  public GridLayout insertBeforeRow(Row row) {
127  currentRow = new Row();
128  getRows(true).insertBefore(currentRow, row);
129  return this;
130  }
131 
132  public GridLayout addCaption(String label) {
133  getCurrentRow().appendChild(new CaptionLabel(label));
134  return this;
135  }
136 
137  public GridLayout addValue(String value) {
138  getCurrentRow().appendChild(new Label(value));
139  return this;
140  }
141 
142  public GridLayout addBoldValue(String value) {
143  getCurrentRow().appendChild(new StyledLabel(value, "font-weight:bold"));
144  return this;
145  }
146 
147  public GridLayout addComponent(HtmlBasedComponent comp) {
148  getCurrentRow().appendChild(comp);
149  if(comp instanceof AfterCompose) {
150  ((AfterCompose) comp).afterCompose();
151  }
152  return this;
153  }
154 
155  public GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols) {
156  Cell cell = new Cell();
157  cell.setColspan(cols);
158  cell.appendChild(comp);
159  getCurrentRow().appendChild(cell);
160  if(comp instanceof AfterCompose) {
161  ((AfterCompose) comp).afterCompose();
162  }
163  return this;
164  }
165 
166  public GridLayout addRowSpannedComponent(HtmlBasedComponent comp, int rows) {
167  Cell cell = new Cell();
168  cell.setRowspan(rows);
169  cell.appendChild(comp);
170  getCurrentRow().appendChild(cell);
171  if(comp instanceof AfterCompose) {
172  ((AfterCompose) comp).afterCompose();
173  }
174  return this;
175  }
176 
177  public GridLayout addSpace() {
178  getCurrentRow().appendChild(new Space());
179  return this;
180  }
181 
182  public Row getCurrentRow() {
183  if(currentRow == null) {
184  currentRow = new Row();
185  getRows(true).appendChild(currentRow);
186  }
187  return currentRow;
188  }
189 
190  public Rows getRows(boolean create) {
191  Rows rows = super.getRows();
192  if(rows == null && create) {
193  rows = new Rows();
194  appendChild(rows);
195  }
196  return rows;
197  }
198 
199  public Object getData() {
200  return data;
201  }
202 
203  public void setData(Object data) {
204  this.data = data;
205  }
206 
207  private void setWidthFlex(Column col, String flex) {
208  if(flex.matches("[0-9]+(px|\\%)")) {
209  col.setWidth(flex);
210  } else {
211  col.setHflex(flex);
212  }
213  }
214 
215 }
void setColCaptions(String captions)
Definition: GridLayout.java:94
void setColumns(String hflexs)
Definition: GridLayout.java:71
GridLayout addGroup(String title, boolean open)
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout insertBeforeRow(Row row)
Rows getRows(boolean create)
GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols)
GridLayout addCaption(String label)
void setColCaptions(String[] captions)
Definition: GridLayout.java:98
GridLayout addValue(String value)
GridLayout addBoldValue(String value)
GridLayout addRowSpannedComponent(HtmlBasedComponent comp, int rows)