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