BrightSide Workbench Full Report + Source Code
contacts/src/main/java/org/turro/contacts/zul/fields/FieldDefsGrid.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.contacts.zul.fields;
19 
20 import java.util.List;
21 import org.turro.contacts.FieldDef;
22 import org.turro.contacts.db.ContactsPU;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.i18n.I_;
26 import org.zkoss.zk.ui.Component;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.EventListener;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zul.*;
31 
36 public class FieldDefsGrid extends Grid {
37 
38  private Rows rows;
39  private Toolbar toolbar;
40  private Toolbarbutton addButton, saveButton;
41 
42  public FieldDefsGrid() {
43  addColumns();
44  rows = new Rows();
45  appendChild(rows);
46  addRows();
47  }
48 
49  public void setAddToolbar(boolean addToolbar) {
50  if(addToolbar) {
51  toolbar = new Toolbar();
52  getParent().appendChild(toolbar);
53  addToolbarButtons();
54  }
55  }
56 
57  private void addColumns() {
58  Columns cols = new Columns();
59  cols.setSizable(true);
60  cols.setMenupopup("auto");
61  appendChild(cols);
62 
63  Column col = new Column(I_.get("Tag discriminator"));
64  cols.appendChild(col);
65  col = new Column(I_.get("Tab label"));
66  cols.appendChild(col);
67  col = new Column(I_.get("Field label"));
68  cols.appendChild(col);
69  col = new Column(I_.get("Field type"));
70  cols.appendChild(col);
71  if(Application.getApplication().isInRole("contact-field:delete")) {
72  col = new Column();
73  col.setWidth("25px");
74  cols.appendChild(col);
75  }
76  }
77 
78  private void addRows() {
79  WhereClause wc = new WhereClause();
80  wc.addClause("select distinct fieldDef");
81  wc.addClause("from FieldDef as fieldDef");
82  wc.addClause("order by fieldDef.tabKey, fieldDef.labelKey");
83  for(FieldDef fd : (List<FieldDef>) new ContactsPU().getResultList(wc)) {
84  rows.appendChild(new FieldDefRow(fd));
85  }
86  }
87 
88  private void addToolbarButtons() {
89  saveButton = new Toolbarbutton(
90  I_.get("Save"),
91  "/_zul/images/save.png"
92  );
93  saveButton.addEventListener(Events.ON_CLICK, new EventListener() {
94 
95  @Override
96  public void onEvent(Event event) throws Exception {
97  ContactsPU cpu = new ContactsPU();
98  for(Component fdr : rows.getChildren()) {
99  FieldDef fd = cpu.saveObject(((FieldDefRow)fdr).getFieldDef());
100  ((FieldDefRow)fdr).setFieldDef(fd);
101  }
102  }
103 
104  });
105  toolbar.appendChild(saveButton);
106 
107  addButton = new Toolbarbutton(
108  I_.get("Add"),
109  "/_zul/images/new.png"
110  );
111  addButton.addEventListener(Events.ON_CLICK, new EventListener() {
112 
113  @Override
114  public void onEvent(Event event) throws Exception {
115  rows.appendChild(new FieldDefRow(new FieldDef()));
116  }
117 
118  });
119  toolbar.appendChild(addButton);
120  }
121 
122 }
static String get(String msg)
Definition: I_.java:41