BrightSide Workbench Full Report + Source Code
AttendeesGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.convocation;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.collections.CollectionUtil;
25 import org.turro.command.Command;
26 import org.turro.command.Context;
27 import org.turro.contacts.Attendee;
28 import org.turro.contacts.Connector;
29 import org.turro.contacts.Contact;
30 import org.turro.contacts.Convocation;
31 import org.turro.contacts.filter.ContactFilter;
32 import org.turro.contacts.filter.ContactFilterGrid;
33 import org.turro.contacts.util.ContactListbox;
34 import org.turro.contacts.zul.label.ContactInfo;
35 import org.turro.elephant.security.IUser;
36 import org.turro.elephant.util.Images;
37 import org.turro.i18n.I_;
38 import org.turro.zkoss.filter.GenericFilterListbox;
39 import org.turro.zkoss.grid.DefaultRowRenderer;
40 import org.turro.zkoss.grid.PagingGrid;
41 import org.turro.zul.frame.Framework;
42 import org.zkoss.zk.ui.event.Event;
43 import org.zkoss.zk.ui.event.EventListener;
44 import org.zkoss.zk.ui.event.Events;
45 import org.zkoss.zul.Column;
46 import org.zkoss.zul.Columns;
47 import org.zkoss.zul.Group;
48 import org.zkoss.zul.Image;
49 import org.zkoss.zul.ListModelList;
50 import org.zkoss.zul.Row;
51 import org.zkoss.zul.Space;
52 import org.zkoss.zul.Toolbarbutton;
53 
58 public class AttendeesGrid extends PagingGrid {
59 
60  private Convocation convocation;
61 
62  public void setConvocation(Convocation convocation) {
63  this.convocation = convocation;
64  addColumns();
65  addRows();
66  }
67 
68  public void addAttendeesByEntity(boolean deep) {
69  convocation.addAttendeesByEntity(deep);
70  addRows();
71  Events.postEvent(new Event(Events.ON_CHANGE));
72  }
73 
74  public void addContact(Contact contact) {
75  if(contact != null) {
76  convocation.addContact(contact);
77  addRows();
78  Events.postEvent(new Event(Events.ON_CHANGE));
79  }
80  }
81 
82  public void addContacts() {
84  new ContactFilter(), new ContactFilterGrid(), new ContactListbox());
85 
86  contacts.show(I_.get("Contacts"), "800px", "600px", new Command() {
87  @Override
88  public Object execute(Context context) {
89  if(contacts != null) {
90  for(Contact contact : contacts.getObjectValues()) {
91  convocation.addContact(contact);
92  }
93  refresh();
94  Events.postEvent(new Event(Events.ON_CHANGE));
95  }
96  return null;
97  }
98  });
99  }
100 
101  public void pasteContacts() {
102  List<Contact> contacts = (List<Contact>) Framework.getClipboard().get("bbf_clip_contact");
103  if(contacts != null) {
104  for(Contact contact : contacts) {
105  Connector email = contact.getConnectorMap().get(IUser.CONNECTOR_EMAIL);
106  if(email != null && !Strings.isBlank(email.getValue())) {
107  convocation.addContact(contact);
108  }
109  }
110  Framework.getClipboard().remove("bbf_clip_contact");
111  addRows();
112  }
113  }
114 
115  public void refresh() {
116  addRows();
117  }
118 
119  private void addRows() {
120  getRows(true).getChildren().clear();
121 
122  if(convocation == null) return;
123 
124  setRowRenderer(new DefaultRowRenderer<Attendee>() {
125  @Override
126  protected void renderRow(final Row row, final Attendee value) {
127  row.appendChild(new ContactInfo(value.getContact()));
128 
129  if(value.isAttended()) {
130  row.appendChild(new Image(Images.getImage("accepted")));
131  } else {
132  row.appendChild(new Space());
133  }
134 
135  Toolbarbutton sendAttendee = new Toolbarbutton();
136  sendAttendee.setImage("/_zul/images/mail_send.png");
137  sendAttendee.addEventListener(Events.ON_CLICK, new EventListener() {
138  @Override
139  public void onEvent(Event event) throws Exception {
140  Events.postEvent(new Event(Events.ON_USER, AttendeesGrid.this, value));
141  }
142  });
143  row.appendChild(sendAttendee);
144 
145  Toolbarbutton delAttendee = new Toolbarbutton();
146  delAttendee.setImage("/_zul/images/edit-delete.png");
147  delAttendee.addEventListener(Events.ON_CLICK, new EventListener() {
148  @Override
149  public void onEvent(Event event) throws Exception {
150  CollectionUtil.from(convocation.getAttendees()).remove(a -> a.getContact().getId(), value);
151  Events.postEvent(new Event(Events.ON_CHANGE));
152  refresh();
153  }
154  });
155  row.appendChild(delAttendee);
156  }
157 
158  @Override
159  protected void renderGroup(Group group, Attendee value) {
160  }
161  });
162 
163  Collection list = convocation.getSortedAttendees();
164 
165  setModel(new ListModelList(list));
166 
167  setRowCount(list.size());
168  }
169 
170  private void addColumns() {
171  Columns cols = getColumns(true);
172  cols.getChildren().clear();
173 
174  cols.appendChild(new Column(I_.get("Contact")));
175  cols.appendChild(new Column(null, null, "40px"));
176  cols.appendChild(new Column(null, null, "40px"));
177  cols.appendChild(new Column(null, null, "40px"));
178  }
179 
180 }
Set< Attendee > getAttendees()
void addContact(Contact contact)
TreeSet< Attendee > getSortedAttendees()
void addAttendeesByEntity(boolean deep)
static String getImage(String image)
Definition: Images.java:36
static String get(String msg)
Definition: I_.java:41
void show(String title, String width, String height, Command command)
Columns getColumns(boolean create)
Rows getRows(boolean create)
void setConvocation(Convocation convocation)
static FrameClipboard getClipboard()
Definition: Framework.java:220
static final String CONNECTOR_EMAIL
Definition: IUser.java:27