BrightSide Workbench Full Report + Source Code
CommentItGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.commentit;
20 
21 import org.turro.command.CommandUtil;
22 import org.turro.commentit.CommentItUtil;
23 import org.turro.contacts.CommentIt;
24 import org.turro.contacts.db.ContactsPU;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.util.Messages;
27 import org.turro.html.HTMLEntities;
28 import org.turro.i18n.I_;
29 import org.turro.zkoss.grid.PagingGrid;
30 import org.turro.zkoss.label.LabelExtended;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.event.Events;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 import org.zkoss.zul.Button;
36 import org.zkoss.zul.Column;
37 import org.zkoss.zul.Columns;
38 import org.zkoss.zul.Hlayout;
39 import org.zkoss.zul.Label;
40 import org.zkoss.zul.Row;
41 import org.zkoss.zul.Rows;
42 
47 public class CommentItGrid extends PagingGrid implements AfterCompose {
48 
49  private Application app = Application.getApplication();
50  private String entityPath;
51 
52  public CommentItGrid() {
53  addColumns();
54  }
55 
56  public void setEntityPath(String entityPath) {
57  this.entityPath = entityPath;
58  }
59 
60  @Override
61  public void afterCompose() {
62  addRows();
63  }
64 
65  private void addColumns() {
66  Columns cols = new Columns();
67  cols.setSizable(true);
68  cols.setMenupopup("auto");
69  appendChild(cols);
70 
71  Column col = new Column(I_.get("Date"), null, "120px");
72  cols.appendChild(col);
73 
74  col = new Column(I_.get("Author"), null, "250px");
75  cols.appendChild(col);
76 
77  col = new Column(I_.get("Comment"));
78  cols.appendChild(col);
79 
80  col = new Column("", null, "250px");
81  cols.appendChild(col);
82  }
83 
84  private void addRows() {
85  Rows rows = getRows(true);
86  rows.getChildren().clear();
87 
88  for(final CommentIt ci : CommentItUtil.allComments(entityPath)) {
89  final Row row = new Row();
90  row.setValign("top");
91  rows.appendChild(row);
92 
93  LabelExtended lext = new LabelExtended();
94  lext.setDate(ci.getDateCreation());
95  lext.setSclass("ldate");
96  row.appendChild(lext);
97 
98  if(ci.getCreator() != null) {
99  row.appendChild(CommandUtil.getLinkOrSpace(ci.getCreator()));
100  } else {
101  Label author = new Label(
102  ci.getName() + " (" +
103  ci.getEmail() +
104  ") " +
105  " " + ci.getWeb() + " " +
106  ci.getAuthor_ip());
107  author.setSclass("lauthor");
108  row.appendChild(author);
109  }
110 
111  Label body = new Label(HTMLEntities.unescape(ci.getBody()));
112  body.setSclass("lbody");
113  row.appendChild(body);
114 
115  Hlayout hbox = new Hlayout();
116  if(app.isInRole("publication:accept")) {
117  if(!ci.isAccepted()) {
118  final Button confirm = new Button(I_.get("Accept"));
119  confirm.addEventListener(Events.ON_CLICK, new EventListener() {
120  @Override
121  public void onEvent(Event event) throws Exception {
122  Messages.confirmAcceptation().show(() -> {
123  CommentItUtil.acceptComment(ci);
124  confirm.detach();
125  });
126  }
127  });
128  hbox.appendChild(confirm);
129  }
130  Button delete = new Button(I_.get("Delete"));
131  delete.addEventListener(Events.ON_CLICK, new EventListener() {
132  @Override
133  public void onEvent(Event event) throws Exception {
134  Messages.confirmDeletion().show(() -> {
135  new ContactsPU().deleteObject(ci);
136  row.detach();
137  });
138  }
139  });
140  hbox.appendChild(delete);
141  }
142  row.appendChild(hbox);
143  }
144  }
145 
146 }
static String get(String msg)
Definition: I_.java:41
Rows getRows(boolean create)
void setEntityPath(String entityPath)