BrightSide Workbench Full Report + Source Code
WorthDefinitionGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.dossier.zul.dossier;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.dossier.entity.Category;
24 import org.turro.dossier.entity.WorthDefinition;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.i18n.I_;
27 import org.turro.jpa.entity.EntityCollections;
28 import org.turro.zkoss.grid.CollectionGrid;
29 import org.turro.zkoss.grid.EditableCell;
30 import org.zkoss.zk.ui.HtmlBasedComponent;
31 import org.zkoss.zul.Row;
32 
37 public class WorthDefinitionGrid extends CollectionGrid<WorthDefinition> {
38 
39  private Category category;
40 
42  addColumns();
43  }
44 
45  public WorthDefinitionGrid(Category category) {
46  super(category.getWorths());
47  this.category = category;
48  addColumns();
49  }
50 
51  public void setCategory(Category category) {
52  this.category = category;
53  if(category != null) {
54  updateCollection(category.getWorths());
55  } else {
56  updateCollection(null);
57  }
58  }
59 
60  @Override
61  protected void initiateRow(Row row, WorthDefinition value) {
62  if(value == null) {
63  value = new WorthDefinition();
64  value.setCategory(category);
65  if(category != null) {
66  category.getWorths().add(value);
67  }
68  }
69  row.setValue(value);
70  }
71 
72  @Override
73  protected boolean deleteRow(Row row) {
74  WorthDefinition p = (WorthDefinition) row.getValue();
75  EntityCollections.entities(category.getWorths()).remove(p);
76  p.setCategory(null);
77  return true;
78  }
79 
80  @Override
81  protected boolean isValid(WorthDefinition v) {
82  return !v.isEmpty();
83  }
84 
85  @Override
86  protected String formatCell(EditableCell editableCell, Object value) {
87  return super.formatCell(editableCell, value);
88  }
89 
90  @Override
91  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
92  if(editableCell.getCellIndex() == 1) {
93  Object value = getCellValue(editableCell);
95  if(value != null) gcb.setObjectValue((String) value);
96  return gcb;
97  }
98  return super.createEditor(editableCell);
99  }
100 
101  private void addColumns() {
102  try {
103  addColumn("#", "long", "id", null, 0, false, true).setHflex("1");
104  addColumn(I_.get("Grouping"), String.class, "grouping", null, 0, false, false).setHflex("2");
105  addColumn(I_.get("Title"), String.class, "title", null, 0, false, false).setHflex("2");
106  addColumn(I_.get("Description"), String.class, "description", null, 5, false, false).setHflex("3");
107  addColumn(I_.get("Values"), String.class, "possibleValues", null, 0, false, false).setHflex("2");
108  addColumn(I_.get("Colors"), String.class, "colors", null, 0, false, false).setHflex("2");
109  } catch (ClassNotFoundException ex) {
110  Logger.getLogger(WorthDefinitionGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
111  }
112  }
113 
114 }
115 
Set< WorthDefinition > getWorths()
Definition: Category.java:168
HtmlBasedComponent createEditor(EditableCell editableCell)
void initiateRow(Row row, WorthDefinition value)
String formatCell(EditableCell editableCell, Object value)
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
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)