BrightSide Workbench Full Report + Source Code
AssistantsGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.sendable;
20 
21 import java.util.Collection;
22 import java.util.TreeSet;
23 import org.turro.elephant.db.ElephantPU;
24 import org.turro.elephant.entities.db.Sendable;
25 import org.turro.elephant.entities.db.SendableAssistant;
26 import org.turro.elephant.util.Messages;
27 import org.turro.entities.Entities;
28 import org.turro.i18n.I_;
29 import org.turro.plugin.contacts.IContact;
30 import org.turro.zkoss.grid.DefaultRowRenderer;
31 import org.turro.zkoss.grid.PagingGrid;
32 import org.zkoss.zk.ui.Component;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zul.Column;
37 import org.zkoss.zul.Columns;
38 import org.zkoss.zul.Group;
39 import org.zkoss.zul.Image;
40 import org.zkoss.zul.ListModelList;
41 import org.zkoss.zul.Row;
42 import org.zkoss.zul.Space;
43 import org.zkoss.zul.Toolbarbutton;
44 
49 public class AssistantsGrid extends PagingGrid {
50 
51  private Sendable sendable;
52 
53  public void setSendable(Sendable sendable) {
54  this.sendable = sendable;
55  addColumns();
56  addRows();
57  }
58 
59  public void addAssistantsByEntity(String entityPath, boolean deep) {
60  sendable.addAssistantsByEntity(entityPath, deep);
61  addRows();
62  Events.postEvent(new Event(Events.ON_CHANGE));
63  }
64 
65  public void addContact(IContact contact) {
66  if(contact != null) {
67  sendable.addContact(contact);
68  addRows();
69  Events.postEvent(new Event(Events.ON_CHANGE));
70  }
71  }
72 
73  public void refresh() {
74  addRows();
75  }
76 
77  public void removeAll() {
78  Messages.confirmDeletion().show(() -> {
79  sendable.removeAssistants();
80  addRows();
81  });
82  }
83 
84  private void addRows() {
85  getRows(true).getChildren().clear();
86 
87  if(sendable == null) return;
88 
89  setRowRenderer(new DefaultRowRenderer<SendableAssistant>() {
90  @Override
91  protected void renderRow(final Row row, final SendableAssistant value) {
92  row.appendChild((Component) Entities.getController(value.getIContact().getContact()).getLabelCtrl());
93 
94  row.appendChild(value.isDelivered() ?
95  new Image("/_zul/images/mail_queue.png") :
96  new Space());
97 
98  Toolbarbutton delAttendee = new Toolbarbutton();
99  delAttendee.setImage("/_zul/images/edit-delete.png");
100  delAttendee.addEventListener(Events.ON_CLICK, new EventListener() {
101  @Override
102  public void onEvent(Event event) throws Exception {
103  new ElephantPU().deleteObject(value);
104  row.detach();
105  Events.postEvent(new Event(Events.ON_CHANGE));
106  }
107  });
108  row.appendChild(delAttendee);
109  }
110 
111  @Override
112  protected void renderGroup(Group group, SendableAssistant value) {
113  }
114  });
115 
116  Collection list = getSortedAttendees();
117 
118  setModel(new ListModelList(list));
119 
120  setRowCount(list.size());
121  }
122 
123  private void addColumns() {
124  Columns cols = getColumns(true);
125  cols.getChildren().clear();
126 
127  cols.appendChild(new Column(I_.get("Contact")));
128  cols.appendChild(new Column(null, null, "40px"));
129  cols.appendChild(new Column(null, null, "40px"));
130  }
131 
132  private Collection getSortedAttendees() {
133  TreeSet<SendableAssistant> set = new TreeSet(new AssistantComparator());
134  set.addAll(sendable.getAssistants(true));
135  return set;
136  }
137 
138 }
void addAssistantsByEntity(String entityPath, boolean deep)
Definition: Sendable.java:88
void addContact(IContact contact)
Definition: Sendable.java:96
Collection< SendableAssistant > getAssistants(boolean all)
Definition: Sendable.java:106
static Messages confirmDeletion()
Definition: Messages.java:87
static IElephantEntity getController(String path)
Definition: Entities.java:78
void deleteObject(Object obj)
Definition: Dao.java:162
void addContact(IContact contact)
void setSendable(Sendable sendable)
void addAssistantsByEntity(String entityPath, boolean deep)
Columns getColumns(boolean create)
Rows getRows(boolean create)