BrightSide Workbench Full Report + Source Code
DescriptorDefinitionGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.DescriptorDefinition;
25 import org.turro.dossier.entity.DescriptorType;
26 import org.turro.dossier.project.DescriptorTypeListbox;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.entity.EntityCollections;
30 import org.turro.zkoss.grid.CollectionGrid;
31 import org.turro.zkoss.grid.EditableCell;
32 import org.zkoss.zk.ui.HtmlBasedComponent;
33 import org.zkoss.zul.Row;
34 
39 public class DescriptorDefinitionGrid extends CollectionGrid<DescriptorDefinition> {
40 
41  private Category category;
42 
44  addColumns();
45  }
46 
48  super(category.getDescriptors());
49  this.category = category;
50  addColumns();
51  }
52 
53  public void setCategory(Category category) {
54  this.category = category;
55  if(category != null) {
56  updateCollection(category.getDescriptors());
57  } else {
58  updateCollection(null);
59  }
60  }
61 
62  @Override
63  protected void initiateRow(Row row, DescriptorDefinition value) {
64  if(value == null) {
65  value = new DescriptorDefinition();
66  value.setCategory(category);
67  if(category != null) {
68  category.getDescriptors().add(value);
69  }
70  }
71  row.setValue(value);
72  }
73 
74  @Override
75  protected boolean deleteRow(Row row) {
77  EntityCollections.entities(category.getDescriptors()).remove(p);
78  p.setCategory(null);
79  return true;
80  }
81 
82  @Override
83  protected boolean isValid(DescriptorDefinition v) {
84  return !v.isEmpty();
85  }
86 
87  @Override
88  protected String formatCell(EditableCell editableCell, Object value) {
89  if(editableCell.getCellIndex() == 5) {
90  DescriptorType dt = ((DescriptorType) value);
91  if(dt != null) {
92  return I_.byKey(dt.toString());
93  }
94  return "";
95  }
96  return super.formatCell(editableCell, value);
97  }
98 
99  @Override
100  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
101  if(editableCell.getCellIndex() == 0) {
102  Object value = getCellValue(editableCell);
104  if(value != null) gcb.setObjectValue((String) value);
105  return gcb;
106  } else if(editableCell.getCellIndex() == 5) {
107  Object value = getCellValue(editableCell);
109  ppl.setAllowNull(true);
110  ppl.setMold("select");
111  if(value != null) ppl.setObjectValue((DescriptorType) value);
112  return ppl;
113  }
114  return super.createEditor(editableCell);
115  }
116 
117  private void addColumns() {
118  try {
119  addColumn(I_.get("Grouping"), String.class, "grouping", null, 0, false, false).setHflex("2");
120  addColumn(I_.get("Ordering"), "int", "orderNumber", null, 0, false, false).setHflex("1");
121  addColumn(I_.get("Title"), String.class, "title", null, 0, false, false).setHflex("2");
122  addColumn(I_.get("Minimum"), "int", "minChars", null, 0, false, false).setHflex("1");
123  addColumn(I_.get("Required"), "boolean", "required", null, 0, true, false).setHflex("1");
124  addColumn(I_.get("Type"), DescriptorType.class, "type", null, 0, false, false).setHflex("1");
125  } catch (ClassNotFoundException ex) {
126  Logger.getLogger(DossierVersionGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
127  }
128  }
129 
130 }
Set< DescriptorDefinition > getDescriptors()
Definition: Category.java:160
String formatCell(EditableCell editableCell, Object value)
HtmlBasedComponent createEditor(EditableCell editableCell)
static String byKey(String key)
Definition: I_.java:83
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)