BrightSide Workbench Full Report + Source Code
elephant-dossier/src/main/java/org/turro/dossier/zul/fields/FieldDefRow.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.dossier.zul.fields;
19 
20 import java.util.Date;
21 import org.turro.dossier.db.DossierPU;
22 import org.turro.dossier.dossier.CategoryCombobox;
23 import org.turro.dossier.entity.FieldDef;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.elephant.util.Messages;
27 import org.turro.i18n.I_;
28 import org.turro.jpa.Dao;
29 import org.turro.zul.frame.Framework;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.EventListener;
32 import org.zkoss.zk.ui.event.Events;
33 import org.zkoss.zul.Checkbox;
34 import org.zkoss.zul.Image;
35 import org.zkoss.zul.Listbox;
36 import org.zkoss.zul.Row;
37 import org.zkoss.zul.Spinner;
38 import org.zkoss.zul.Textbox;
39 
44 public class FieldDefRow extends Row {
45 
46  private FieldDef fieldDef;
47 
48  public FieldDefRow(FieldDef fieldDef) {
49  this.fieldDef = fieldDef;
50  addCells();
51  }
52 
53  public FieldDef getFieldDef() {
54  return fieldDef;
55  }
56 
57  public void setFieldDef(FieldDef fieldDef) {
58  this.fieldDef = fieldDef;
59  }
60 
61  private void addCells() {
62  final CategoryCombobox category = new CategoryCombobox();
63  category.setCols(50);
64  category.setCategory(fieldDef.getCategory());
65  category.addEventListener(Events.ON_CHANGE, new EventListener() {
66  @Override
67  public void onEvent(Event event) throws Exception {
68  fieldDef.setCategory(category.getCategory());
69  }
70  });
71  appendChild(category);
72 
73  final Textbox labelKey = new Textbox(fieldDef.getLabelKey());
74  labelKey.setCols(30);
75  labelKey.addEventListener(Events.ON_CHANGE, new EventListener() {
76  @Override
77  public void onEvent(Event event) throws Exception {
78  fieldDef.setLabelKey(labelKey.getText());
79  }
80  });
81  appendChild(labelKey);
82 
83  final Listbox classes = new Listbox();
84  classes.setMold("select");
85  classes.appendItem(I_.get("String"), String.class.getName())
86  .setSelected(String.class.equals(fieldDef.getJavaClass()));
87  classes.appendItem(I_.get("Long integer"), Long.class.getName())
88  .setSelected(Long.class.equals(fieldDef.getJavaClass()));
89  classes.appendItem(I_.get("Double"), Double.class.getName())
90  .setSelected(Double.class.equals(fieldDef.getJavaClass()));
91  classes.appendItem(I_.get("Date"), Date.class.getName())
92  .setSelected(Date.class.equals(fieldDef.getJavaClass()));
93  classes.appendItem(I_.get("Boolean"), Boolean.class.getName())
94  .setSelected(Boolean.class.equals(fieldDef.getJavaClass()));
95  classes.addEventListener(Events.ON_SELECT, new EventListener() {
96  @Override
97  public void onEvent(Event event) throws Exception {
98  fieldDef.setJavaClass(Class.forName((String) classes.getSelectedItem().getValue()));
99  }
100  });
101  if(classes.getSelectedItem() == null) {
102  fieldDef.setJavaClass(String.class);
103  }
104  appendChild(classes);
105 
106  final Spinner order = new Spinner(fieldDef.getOrder());
107  order.setCols(3);
108  order.addEventListener(Events.ON_CHANGE, new EventListener() {
109  @Override
110  public void onEvent(Event event) throws Exception {
111  fieldDef.setOrder(order.getValue());
112  }
113  });
114  appendChild(order);
115 
116  final Checkbox description = new Checkbox();
117  description.setChecked(fieldDef.isDescription());
118  description.addEventListener(Events.ON_CHECK, new EventListener() {
119  @Override
120  public void onEvent(Event event) throws Exception {
121  fieldDef.setDescription(description.isChecked());
122  }
123  });
124  appendChild(description);
125 
126  final Checkbox publishable = new Checkbox();
127  publishable.setChecked(fieldDef.isPublishable());
128  publishable.addEventListener(Events.ON_CHECK, new EventListener() {
129  @Override
130  public void onEvent(Event event) throws Exception {
131  fieldDef.setPublishable(publishable.isChecked());
132  }
133  });
134  appendChild(publishable);
135 
136  if(Application.getApplication().isInRole("contact-field:delete")) {
137  Image img = new Image("/_zul/images/edit-delete.png");
138  img.setStyle("cursor:pointer");
139  img.addEventListener(Events.ON_CLICK, new EventListener() {
140  @Override
141  public void onEvent(Event event) throws Exception {
142  Messages.confirmDeletion().show(() -> {
143  deleteFieldDef();
144  Framework.getCurrent().invalidateSelected();
145  });
146  }
147  });
148  appendChild(img);
149  }
150  }
151 
152  private void deleteFieldDef() {
153  Dao dao = new DossierPU();
154  WhereClause wc = new WhereClause();
155  wc.addClause("delete from FieldValue as fieldValue");
156  wc.addClause("where fieldValue.fieldDef = :fieldDef");
157  wc.addNamedValue("fieldDef", fieldDef);
158  dao.executeUpdate(wc);
159  wc = new WhereClause();
160  wc.addClause("delete from FieldDef as fieldDef");
161  wc.addClause("where fieldDef = :fieldDef");
162  wc.addNamedValue("fieldDef", fieldDef);
163  dao.executeUpdate(wc);
164  }
165 }