BrightSide Workbench Full Report + Source Code
RoleGrid.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 java.io.IOException;
21 import java.util.List;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.auth.Authentication;
25 import org.turro.command.Command;
26 import org.turro.command.Context;
27 import org.turro.contacts.Contact;
28 import org.turro.contacts.Role;
29 import org.turro.elephant.context.Application;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.impl.security.ProfileMap;
32 import org.turro.elephant.util.ZkossUtils;
33 import org.turro.i18n.I_;
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.Grid;
38 import org.zkoss.zul.Hbox;
39 import org.zkoss.zul.Image;
40 import org.zkoss.zul.Listbox;
41 import org.zkoss.zul.Listitem;
42 import org.zkoss.zul.Row;
43 import org.zkoss.zul.Rows;
44 import org.zkoss.zul.Separator;
45 import org.zkoss.zul.Toolbar;
46 import org.zkoss.zul.Toolbarbutton;
47 
52 public class RoleGrid extends Grid {
53 
54  private Contact contact;
55  private Rows rows;
56  private Toolbar toolbar;
57  private Toolbarbutton addButton;
58 
59  public RoleGrid() {
60  rows = new Rows();
61  appendChild(rows);
62  }
63 
64  public void setContact(Contact contact) {
65  this.contact = contact;
66  rows.getChildren().clear();
67  if(contact != null) {
68  addRows();
69  }
70  }
71 
72  public void setAddToolbar(boolean addToolbar) {
73  if(addToolbar) {
74  toolbar = new Toolbar();
75  getParent().appendChild(toolbar);
76  addToolbarButtons();
77  }
78  }
79 
80  public void addRows() {
81  for(final Role r : contact.getRoles()) {
82  final Row row = new Row();
83  row.setValue(r);
84  rows.appendChild(row);
85 
86  Hbox hbox = new Hbox();
87  row.appendChild(hbox);
88 
89  final Listbox roles = new Listbox();
90  roles.setMold("select");
91  try {
92  for (String rs : (List<String>) ProfileMap.getRoleNames()) {
93  if(r.getName() == null) {
94  r.setName(rs);
95  }
96  Listitem li = new Listitem(rs);
97  li.setSelected(rs.equals(r.getName()));
98  roles.appendChild(li);
99  }
100  } catch (IOException ex) {
101  Logger.getLogger(RoleGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
102  }
103  roles.addEventListener(Events.ON_SELECT, new EventListener() {
104  @Override
105  public void onEvent(Event event) throws Exception {
106  r.setName(roles.getSelectedItem().getLabel());
107  }
108  });
109  hbox.appendChild(roles);
110 
111  if(Application.getApplication().isInRole("contact-role:delete")) {
112  hbox.appendChild(new Separator("vertical"));
113  Image img = new Image("/_zul/images/edit-delete.png");
114  img.setStyle("cursor:pointer");
115  img.addEventListener(Events.ON_CLICK, new EventListener() {
116  @Override
117  public void onEvent(Event event) throws Exception {
118  ZkossUtils.confirmDeletion(null, new Command() {
119  @Override
120  public Object execute(Context context) {
121  contact.getRoles().remove(r);
122  r.setContact(null);
123  row.detach();
124  return true;
125  }
126  });
127  }
128  });
129  hbox.appendChild(img);
130  }
131 
132  }
133  }
134 
135  private void addToolbarButtons() {
136  addButton = new Toolbarbutton(
137  I_.get("Add"),
138  "/_zul/images/new.png"
139  );
140  addButton.addEventListener(Events.ON_CLICK, new EventListener() {
141 
142  @Override
143  public void onEvent(Event event) throws Exception {
144  Role r = new Role();
145  r.setContact(contact);
147  contact.getRoles().add(r);
148  setContact(contact);
149  }
150 
151  });
152  toolbar.appendChild(addButton);
153  }
154 
155 }
Set< Role > getRoles()
Definition: Contact.java:406
void setContact(org.turro.contacts.Contact contact)
Definition: Role.java:84
void setOwner(String owner)
Definition: Role.java:60
void setContact(Contact contact)
Definition: RoleGrid.java:64
void setAddToolbar(boolean addToolbar)
Definition: RoleGrid.java:72
static void confirmDeletion(String message, Command command)
Definition: ZkossUtils.java:58
static String get(String msg)
Definition: I_.java:41