BrightSide Workbench Full Report + Source Code
CollectionGrid.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.grid;
19 
20 import java.util.Collection;
21 import org.zkoss.zk.ui.ext.AfterCompose;
22 import org.zkoss.zul.Row;
23 import org.zkoss.zul.Rows;
24 
30 public abstract class CollectionGrid<V> extends EditableGrid<V> implements AfterCompose {
31 
32  private Collection<V> collection;
33  protected Row dependingRow;
34 
35  public CollectionGrid() {
36  super();
37  }
38 
39  public CollectionGrid(Collection<V> collection) {
40  super();
41  this.collection = collection;
42  }
43 
44  public Collection<V> getCollection() {
45  return collection;
46  }
47 
48  public void setCollection(Collection<V> collection) {
49  this.collection = collection;
50  }
51 
52  public void updateCollection(Collection<V> collection) {
53  clearRows();
54  setCollection(collection);
55  afterCompose();
56  }
57 
58  public Row getDependingRow() {
59  return dependingRow;
60  }
61 
62  public void setDependingRow(Row dependingRow) {
63  this.dependingRow = dependingRow;
64  }
65 
66  @Override
67  public void clearTable() {
68  super.clearTable();
69  if(collection != null) {
70  collection.clear();
71  }
72  }
73 
74  @Override
75  public void deleteSelectedRow(Row row) {
76  super.deleteSelectedRow(row);
77  processDependingRow();
78  }
79 
80  @Override
81  protected void cellChanged(EditableCell editableCell, Object value) {
82  super.cellChanged(editableCell, value);
83  if(editableCell.getColumn() instanceof EditableColumn) {
84  ChangeValueCallback cmd = ((EditableColumn) editableCell.getColumn()).getOnCheckDuplicate();
85  if(cmd != null) {
86  V current = editableCell.getRow().getValue();
87  for(V v : collection) {
88  if(value != null && v != null && !v.equals(current) && cmd.check(value, v)) break;
89  }
90  }
91  }
92  }
93 
94  @Override
95  public void afterCompose() {
96  addTableRows();
97  super.afterCompose();
98  }
99 
100  @Override
101  protected void rowChanged(Row row) {
102  processDependingRow();
103  }
104 
105  protected boolean filterValue(V v) {
106  return true;
107  }
108 
109  private void addTableRows() {
110  if(getRows() == null) {
111  appendChild(new Rows());
112  }
113  boolean emptyLine = false;
114  if(collection != null) {
115  for(V v : collection) {
116  if(filterValue(v)) {
117  Row row = new Row();
118  getRows().appendChild(row);
119  initiateRow(row, v);
120  emptyLine = emptyLine ? true : !isValid(v);
121  }
122  }
123  }
124  if(isAllowInsertions() && !emptyLine) {
125  Row row = new Row();
126  getRows().appendChild(row);
127  row.setSclass("invalid");
128  initiateRow(row, null);
129  }
130  }
131 
132  private void processDependingRow() {
133  if(dependingRow != null && dependingRow.getGrid() instanceof EditableGrid) {
135  }
136  }
137 
138 }
void setDependingRow(Row dependingRow)
CollectionGrid(Collection< V > collection)
void cellChanged(EditableCell editableCell, Object value)
void setCollection(Collection< V > collection)
void updateCollection(Collection< V > collection)
abstract void initiateRow(Row row, V value)
abstract boolean isValid(V v)
Rows getRows(boolean create)
boolean check(Object newValue, Object rowValue)