BrightSide Workbench Full Report + Source Code
PollVotesGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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 
19 package org.turro.polls;
20 
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.db.ElephantPU;
26 import org.turro.elephant.entities.db.Poll;
27 import org.turro.elephant.entities.db.PollVote;
28 import org.turro.i18n.I_;
29 import org.turro.util.CompareUtil;
30 import org.turro.zkoss.grid.DefaultRowRenderer;
31 import org.turro.zkoss.grid.PagingGrid;
32 import org.zkoss.zk.ui.event.Event;
33 import org.zkoss.zk.ui.event.EventListener;
34 import org.zkoss.zk.ui.event.Events;
35 import org.zkoss.zul.Column;
36 import org.zkoss.zul.Columns;
37 import org.zkoss.zul.Group;
38 import org.zkoss.zul.GroupsModelArray;
39 import org.zkoss.zul.Label;
40 import org.zkoss.zul.Row;
41 import org.zkoss.zul.Toolbarbutton;
42 
47 public class PollVotesGrid extends PagingGrid {
48 
49  private Poll poll;
50 
51  public PollVotesGrid() {
52  super();
53  addColumns();
54  }
55 
56  public void setPoll(Poll poll) {
57  this.poll = poll;
58  if(this.poll != null) {
59  addRows(poll.getVotes());
60  } else {
61  addRows(Collections.EMPTY_LIST);
62  }
63  }
64 
65  private void addColumns() {
66  Columns cols = new Columns();
67  cols.setSizable(true);
68  cols.setMenupopup("auto");
69  appendChild(cols);
70 
71  //addDetailColumn();
72 
73  Column col = new Column(I_.get("Contact"));
74  col.setHflex("1");
75  cols.appendChild(col);
76  col = new Column(I_.get("Vote"));
77  col.setHflex("1");
78  cols.appendChild(col);
79  if(Application.getApplication().isInRole("dossier:delete")) {
80  col = new Column();
81  col.setWidth("25px");
82  cols.appendChild(col);
83  }
84  }
85 
86  private void addRows(List votes) {
87 
88  setRowRenderer(new DefaultRowRenderer<PollVote>() {
89  @Override
90  protected void renderRow(Row row, PollVote value) {
91  fillRow(row, value);
92  }
93 
94  @Override
95  protected void renderGroup(Group group, PollVote value) {
96  group.setLabel(value.getPollData());
97  }
98  });
99 
100  setModel(new GroupsModelArray<PollVote, String, String, Object>(
101  (PollVote[]) votes.toArray(new PollVote[0]),
102  new Comparator<PollVote>() {
103  @Override
104  public int compare(PollVote o1, PollVote o2) {
105  return CompareUtil.compare(o1.getPollData(), o2.getPollData());
106  }
107  }
108  ) {
109  @Override
110  protected String createGroupFoot(PollVote[] groupdata, int index, int col) {
111  int against = 0, abstention = 0, inFavor = 0;
112  for(PollVote pv : groupdata) {
113  if(pv.getVote() > 0) {
114  inFavor++;
115  } else if(pv.getVote() < 0) {
116  against++;
117  } else {
118  abstention++;
119  }
120  }
121  return String.format("%s: %d - %s: %d - %s: %d",
122  I_.get("Against"), against,
123  I_.get("Abstention"), abstention,
124  I_.get("In favor"), inFavor);
125  }
126  });
127 
128  setRowCount(votes.size());
129  }
130 
131  private void fillRow(Row row, PollVote value) {
132  row.appendChild(new Label(value.getContact().getName()));
133  row.appendChild(new Label(String.valueOf(value.getVote())));
134  if(Application.getApplication().isInRole("dossier:delete")) {
135  Toolbarbutton delVote = new Toolbarbutton();
136  delVote.setImage("/_zul/images/edit-delete.png");
137  delVote.addEventListener(Events.ON_CLICK, new EventListener() {
138  @Override
139  public void onEvent(Event event) throws Exception {
140  new ElephantPU().deleteObject(value);
141  row.detach();
142  Events.postEvent(new Event(Events.ON_CHANGE));
143  }
144  });
145  row.appendChild(delVote);
146  }
147  }
148 
149 }
List< PollVote > getVotes()
Definition: Poll.java:235
static String get(String msg)
Definition: I_.java:41