BrightSide Workbench Full Report + Source Code
FieldItGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.zul.fieldit;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.contacts.FieldIt;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.util.Images;
26 import org.turro.entities.Entities;
27 import org.turro.fieldit.FieldItUtil;
28 import org.turro.fieldit.StringList;
29 import org.turro.i18n.I_;
30 import org.turro.reflection.Reflections;
31 import org.turro.zkoss.grid.CollectionGrid;
32 import org.turro.zkoss.grid.EditableCell;
33 import org.turro.zkoss.text.FileEditor;
34 import org.zkoss.zk.ui.HtmlBasedComponent;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zul.Radio;
39 import org.zkoss.zul.Row;
40 
45 public class FieldItGrid extends CollectionGrid<FieldIt> {
46 
47  private String entityPath;
48 
49  public FieldItGrid() {
50  addColumns();
51  setShowZeros(true);
52  }
53 
54  public String getEntityPath() {
55  return entityPath;
56  }
57 
58  public void setEntityPath(String entityPath) {
59  this.entityPath = entityPath;
60  updateCollection(FieldItUtil.fields(entityPath));
61  }
62 
63  public void setEntity(Object entity) {
65  }
66 
67  @Override
68  protected void initiateRow(Row row, FieldIt value) {
69  if(value == null) {
70  value = new FieldIt();
71  value.setPath(entityPath);
72  value.setDescription(true);
73  value.setPublishable(true);
74  value.setSearchable(true);
75  }
76  row.setValue(value);
77  }
78 
79  @Override
80  protected boolean deleteRow(Row row) {
81  FieldIt field = row.getValue();
82  if(field != null) {
83  FieldItUtil.delete(field);
84  }
85  return true;
86  }
87 
88  @Override
89  protected boolean isValid(FieldIt v) {
90  return v.isValid();
91  }
92 
93  @Override
94  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
95  if(editableCell.getCellIndex() == 1) {
96  FieldIt h = editableCell.getRow().getValue();
97  ClassesListbox el = new ClassesListbox();
98  el.setMold("select");
100  return el;
101  } else if(editableCell.getCellIndex() == 7) {
102  FieldIt h = editableCell.getRow().getValue();
103  Radio el = new Radio();
104  el.setImage(Images.getImage("properties"));
105  if(h != null && h.getJavaClass() != null && Reflections.of(h.getJavaClass()).canCast(StringList.class)) {
106  el.addEventListener(Events.ON_CHECK, new EventListener<Event>() {
107  @Override
108  public void onEvent(Event event) throws Exception {
109  if(h.getId() != null) {
110  FileEditor.editFile(StringList.getValueFile(h));
111  }
112  }
113  });
114  } else {
115  el.setDisabled(true);
116  }
117  return el;
118  }
119  return super.createEditor(editableCell);
120  }
121 
122  @Override
123  protected Object getEditorValue(EditableCell editableCell) {
124  if(editableCell.getCellIndex() == 1) {
125  ClassesListbox rtl = (ClassesListbox) editableCell.getEditor();
126  return rtl.getObjectValue();
127  }
128  return super.getEditorValue(editableCell);
129  }
130 
131  @Override
132  protected String formatCell(EditableCell editableCell, Object value) {
133  if(editableCell.getCellIndex() == 1) {
134  return FieldItUtil.getLabel((Class) value);
135  } else if(editableCell.getCellIndex() == 7) {
136  return "";
137  }
138  return super.formatCell(editableCell, value);
139  }
140 
141  private void addColumns() {
142  try {
143  addColumn(I_.get("Name"), String.class,
144  "name", null, 0, false, false).setWidth("35%");
145  addColumn(I_.get("Type"), Class.class,
146  "javaClass", null, 0, false, false).setWidth("25%");
147  addColumn(I_.get("Ordering"), "int",
148  "order", null, 0, false, false).setWidth("8%");
149  addColumn(I_.get("Description"), "boolean",
150  "description", null, 0, false, false).setWidth("8%");
151  addColumn(I_.get("Publishable"), "boolean",
152  "publishable", null, 0, false, false).setWidth("8%");
153  addColumn(I_.get("Composite"), "boolean",
154  "composite", null, 0, false, false).setWidth("8%");
155  addColumn(I_.get("Searchable"), "boolean",
156  "searchable", null, 0, false, false).setWidth("8%");
157  addColumn(I_.get("Extra"), StringList.class,
158  null, null, 0, false, false).setWidth("8%");
159  } catch (ClassNotFoundException ex) {
160  Logger.getLogger(FieldItGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
161  }
162  }
163 
164 }
void setDescription(boolean description)
Definition: FieldIt.java:101
void setSearchable(boolean searchable)
Definition: FieldIt.java:125
void setPath(String path)
Definition: FieldIt.java:77
void setPublishable(boolean publishable)
Definition: FieldIt.java:109
static String getImage(String image)
Definition: Images.java:36
static IElephantEntity getController(String path)
Definition: Entities.java:78
static void delete(FieldIt field)
static FieldItSet fields(Object entity)
static String getLabel(Class javaClass)
static String get(String msg)
Definition: I_.java:41
void updateCollection(Collection< V > collection)
void setShowZeros(boolean showZeros)
String formatCell(EditableCell editableCell, Object value)
void initiateRow(Row row, FieldIt value)
HtmlBasedComponent createEditor(EditableCell editableCell)
Object getEditorValue(EditableCell editableCell)
void setEntityPath(String entityPath)