BrightSide Workbench Full Report + Source Code
CommentGrid.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.contact;
19 
20 import org.turro.auth.Authentication;
21 import org.turro.contacts.Comment;
22 import org.turro.contacts.Contact;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.Messages;
25 import org.turro.i18n.I_;
26 import org.zkoss.zk.ui.event.Event;
27 import org.zkoss.zk.ui.event.EventListener;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zul.Grid;
30 import org.zkoss.zul.Hbox;
31 import org.zkoss.zul.Image;
32 import org.zkoss.zul.Row;
33 import org.zkoss.zul.Rows;
34 import org.zkoss.zul.Separator;
35 import org.zkoss.zul.Textbox;
36 import org.zkoss.zul.Toolbar;
37 import org.zkoss.zul.Toolbarbutton;
38 
43 public class CommentGrid extends Grid {
44 
45  private Contact contact;
46  private Rows rows;
47  private Toolbar toolbar;
48  private Toolbarbutton addButton;
49 
50  public CommentGrid() {
51  rows = new Rows();
52  appendChild(rows);
53  }
54 
55  public void setContact(Contact contact) {
56  this.contact = contact;
57  rows.getChildren().clear();
58  if(contact != null) {
59  addRows();
60  }
61  }
62 
63  public void setAddToolbar(boolean addToolbar) {
64  if(addToolbar) {
65  toolbar = new Toolbar();
66  getParent().appendChild(toolbar);
67  addToolbarButtons();
68  }
69  }
70 
71  public void addRows() {
72  for(final Comment c : contact.getComments()) {
73  final Row row = new Row();
74  row.setValue(c);
75  rows.appendChild(row);
76 
77  Hbox hbox = new Hbox();
78  row.appendChild(hbox);
79 
80  final Textbox comment = new Textbox();
81  comment.setRows(8);
82  comment.setCols(80);
83  comment.setText(c.getComment());
84  comment.addEventListener(Events.ON_CHANGE, new EventListener() {
85  @Override
86  public void onEvent(Event event) throws Exception {
87  if(comment.getText().contains("#dir") && !Application.getApplication().isInRole("contact:file-ext")) {
88  comment.setText("");
89  }
90  c.setComment(comment.getText());
91  }
92  });
93  hbox.appendChild(comment);
94 
95  if(Application.getApplication().isInRole("contact-comment:delete")) {
96  hbox.appendChild(new Separator("vertical"));
97  Image img = new Image("/_zul/images/edit-delete.png");
98  img.setStyle("cursor:pointer");
99  img.addEventListener(Events.ON_CLICK, new EventListener() {
100  @Override
101  public void onEvent(Event event) throws Exception {
102  Messages.confirmDeletion().show(() -> {
103  contact.getComments().remove(c);
104  c.setContact(null);
105  row.detach();
106  });
107  }
108  });
109  hbox.appendChild(img);
110  }
111 
112  }
113  }
114 
115  private void addToolbarButtons() {
116  addButton = new Toolbarbutton(
117  I_.get("Add"),
118  "/_zul/images/new.png"
119  );
120  addButton.addEventListener(Events.ON_CLICK, new EventListener() {
121 
122  @Override
123  public void onEvent(Event event) throws Exception {
124  Comment c = new Comment();
125  c.setContact(contact);
127  setContact(contact);
128  }
129 
130  });
131  toolbar.appendChild(addButton);
132  }
133 
134 }
void setContact(Contact contact)
Definition: Comment.java:93
void setOwner(String owner)
Definition: Comment.java:61
Set< Comment > getComments()
Definition: Contact.java:415
void setAddToolbar(boolean addToolbar)
static Messages confirmDeletion()
Definition: Messages.java:87
static String get(String msg)
Definition: I_.java:41