BrightSide Workbench Full Report + Source Code
PollOptionGrid.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.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.elephant.entities.db.Poll;
25 import org.turro.elephant.entities.db.PollOption;
26 import org.turro.i18n.I_;
27 import org.turro.zkoss.grid.CollectionGrid;
28 import org.turro.zkoss.grid.EditableCell;
29 import org.zkoss.zk.ui.HtmlBasedComponent;
30 import org.zkoss.zul.Row;
31 
36 public class PollOptionGrid extends CollectionGrid<PollOption> {
37 
38  private Poll poll;
39 
40  public PollOptionGrid() {
41  super();
42  addColumns();
43  }
44 
45  public void setPoll(Poll poll) {
46  this.poll = poll;
47  if(this.poll != null) {
48  updateCollection(this.poll.getOptions());
49  } else {
50  clearRows();
51  }
52  }
53 
54  @Override
55  protected void initiateRow(Row row, PollOption value) {
56  if(value == null && poll != null) {
57  value = new PollOption();
58  value.setIdPoll(poll.getId());
59  }
60  row.setValue(value);
61  }
62 
63  @Override
64  protected boolean deleteRow(Row row) {
65  PollOption po = (PollOption) row.getValue();
67  return true;
68  }
69 
70  @Override
71  protected boolean isValid(PollOption v) {
72  return !v.isEmpty();
73  }
74 
75  @Override
76  protected String formatCell(EditableCell editableCell, Object value) {
77  if(editableCell.getCellIndex() == 0) {
78  String formatted = PollControl.getFormattedString(poll.getOptionClass(), value);
79  if(formatted != null) return formatted;
80  }
81  return super.formatCell(editableCell, value);
82  }
83 
84  @Override
85  protected void setCellValue(EditableCell editableCell, Object value) {
86  if(editableCell.getCellIndex() == 0) {
87  super.setCellValue(editableCell, formatCell(editableCell, value));
88  } else {
89  super.setCellValue(editableCell, value);
90  }
91  }
92 
93  @Override
94  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
95  if(editableCell.getCellIndex() == 0) {
96  Object value = getCellValue(editableCell);
97  HtmlBasedComponent editor = PollControl.getEditor(poll.getOptionClass(), editableCell.isReadOnly(), value);
98  if(editor != null) {
99  return editor;
100  }
101  }
102  return super.createEditor(editableCell);
103  }
104 
105  private void addColumns() {
106  try {
107  addColumn(I_.get("Option"), String.class,
108  "pollData", null, 0, false, false).setHflex("3");
109  addColumn(I_.get("Description"), String.class,
110  "description", null, 0, false, false).setHflex("3");
111  addColumn(I_.get("Action"), "boolean",
112  "actionButton", null, 0, false, false).setHflex("1");
113  addColumn(I_.get("Ordering"), "int",
114  "ordering", null, 0, false, false).setHflex("1");
115  } catch (ClassNotFoundException ex) {
116  Logger.getLogger(PollOptionGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
117  }
118  }
119 
120 }
List< PollOption > getOptions()
Definition: Poll.java:226
static String get(String msg)
Definition: I_.java:41
static HtmlBasedComponent getEditor(String optionClass, boolean readonly, Object value)
static String getFormattedString(String optionClass, Object value)
void initiateRow(Row row, PollOption value)
boolean isValid(PollOption v)
HtmlBasedComponent createEditor(EditableCell editableCell)
void setCellValue(EditableCell editableCell, Object value)
String formatCell(EditableCell editableCell, Object value)
static void removeOption(PollOption po)
Definition: PollsUtil.java:190
void updateCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)