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