BrightSide Workbench Full Report + Source Code
GroupModelManager.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.jpa.model;
19 
20 import java.util.Comparator;
21 import java.util.List;
22 import org.turro.jpa.entity.EntityCollections;
23 import org.turro.jpa.entity.IDaoEntity;
24 import org.zkoss.zul.Grid;
25 import org.zkoss.zul.GroupsModel;
26 import org.zkoss.zul.event.GroupsDataEvent;
27 import org.zkoss.zul.event.GroupsDataListener;
28 import org.zkoss.zul.ext.GroupsSortableModel;
29 
34 public class GroupModelManager implements GroupsModel, GroupsSortableModel<Object> {
35 
36  private GroupDefaultModel _model;
37  private final Comparator _comparator;
38  private final Grid _grid;
39  private final List _list;
40 
41  public GroupModelManager(List list, Comparator comparator, Grid grid) {
42  this._comparator = comparator;
43  this._list = list;
44  this._grid = grid;
45  createModel();
46  }
47 
48  public void removeItem(IDaoEntity object) {
49  EntityCollections.entities(_list).remove(object);
50  createModel();
51  _grid.setModel(_grid.getGroupsModel());
52  fireEvent(GroupsDataEvent.GROUPS_CHANGED,-1,-1,-1);
53  }
54 
55  public void addItem(IDaoEntity object) {
56  _list.add(object);
57  createModel();
58  _grid.setModel(_grid.getGroupsModel());
59  fireEvent(GroupsDataEvent.GROUPS_CHANGED,-1,-1,-1);
60  }
61 
62  public void replaceItem(IDaoEntity object) {
63  EntityCollections.entities(_list).replace(object);
64  createModel();
65  _grid.setModel(_grid.getGroupsModel());
66  fireEvent(GroupsDataEvent.GROUPS_CHANGED,-1,-1,-1);
67  }
68 
69  /* Delegation */
70 
71  @Override
72  public void removeGroupsDataListener(GroupsDataListener l) {
73  _model.removeGroupsDataListener(l);
74  }
75 
76  @Override
77  public void addGroupsDataListener(GroupsDataListener l) {
78  _model.addGroupsDataListener(l);
79  }
80 
81  @Override
82  public void sort(Comparator cmpr, boolean ascending, int col) {
83  _model.sort(cmpr, ascending, col);
84  }
85 
86  @Override
87  public boolean addOpenGroup(int groupIndex) {
88  return _model.addOpenGroup(groupIndex);
89  }
90 
91  @Override
92  public boolean removeOpenGroup(int groupIndex) {
93  return _model.removeOpenGroup(groupIndex);
94  }
95 
96  @Override
97  public boolean isGroupOpened(int groupIndex) {
98  return _model.isGroupOpened(groupIndex);
99  }
100 
101  @Override
102  public boolean hasGroupfoot(int groupIndex) {
103  return _model.hasGroupfoot(groupIndex);
104  }
105 
106  @Override
107  public void group(Comparator cmpr, boolean ascending, int col) {
108  _model.group(cmpr, ascending, col);
109  }
110 
111  @Override
112  public Object getGroupfoot(int groupIndex) {
113  return _model.getGroupfoot(groupIndex);
114  }
115 
116  @Override
117  public int getGroupCount() {
118  return _model.getGroupCount();
119  }
120 
121  @Override
122  public Object getGroup(int groupIndex) {
123  return _model.getGroup(groupIndex);
124  }
125 
126  @Override
127  public int getChildCount(int groupIndex) {
128  return _model.getChildCount(groupIndex);
129  }
130 
131  @Override
132  public Object getChild(int groupIndex, int index) {
133  return _model.getChild(groupIndex, index);
134  }
135 
136  protected void fireEvent(int type, int groupIndex, int index0, int index1) {
137  _model.fireEvent(type, groupIndex, index0, index1);
138  }
139 
140  protected boolean createGroupClose(Object[] groupdata, int index, int col) {
141  return false;
142  }
143 
144  protected Object createGroupFoot(Object[] groupdata, int index, int col) {
145  return null;
146  }
147 
148  protected Object createGroupHead(Object[] groupdata, int index, int col) {
149  return null;
150  }
151 
152  private void createModel() {
153  _model = new GroupDefaultModel(_list, _comparator) {
154  @Override
155  protected boolean createGroupOpen(Object[] groupdata, int index, int col) {
156  boolean result = super.createGroupOpen(groupdata, index, col);
157  return GroupModelManager.this.doCreateGroupOpen(groupdata, index, col, result);
158  }
159  @Override
160  protected Object createGroupFoot(Object[] groupdata, int index, int col) {
161  Object result = super.createGroupFoot(groupdata, index, col);
162  return GroupModelManager.this.doCreateGroupFoot(groupdata, index, col, result);
163  }
164  @Override
165  protected Object createGroupHead(Object[] groupdata, int index, int col) {
166  Object result = super.createGroupHead(groupdata, index, col);
167  return GroupModelManager.this.doCreateGroupHead(groupdata, index, col, result);
168  }
169  };
170  }
171 
172  private boolean doCreateGroupOpen(Object[] groupdata, int index, int col, boolean result) {
173  boolean _result = createGroupClose(groupdata, index, col);
174  return _result || result;
175  }
176 
177  private Object doCreateGroupFoot(Object[] groupdata, int index, int col, Object result) {
178  Object _result = createGroupFoot(groupdata, index, col);
179  return _result != null ? _result : result;
180  }
181 
182  private Object doCreateGroupHead(Object[] groupdata, int index, int col, Object result) {
183  Object _result = createGroupHead(groupdata, index, col);
184  return _result != null ? _result : result;
185  }
186 
187 }
static EntityCollections entities(Collection values)
void fireEvent(int type, int groupIndex, int index0, int index1)
boolean createGroupOpen(Object[] groupdata, int index, int col)
Object createGroupHead(Object[] groupdata, int index, int col)
void removeGroupsDataListener(GroupsDataListener l)
Object createGroupFoot(Object[] groupdata, int index, int col)
void sort(Comparator cmpr, boolean ascending, int col)
GroupModelManager(List list, Comparator comparator, Grid grid)
Object getChild(int groupIndex, int index)
void group(Comparator cmpr, boolean ascending, int col)
boolean createGroupClose(Object[] groupdata, int index, int col)
void addGroupsDataListener(GroupsDataListener l)
void fireEvent(int type, int groupIndex, int index0, int index1)