BrightSide Workbench Full Report + Source Code
ContactInfo.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.label;
19 
20 import org.turro.string.Strings;
21 import org.turro.action.Contacts;
22 import org.turro.contacts.*;
23 import org.turro.contacts.db.ContactsPU;
24 import org.turro.contacts.zul.fields.FieldLabel;
25 import org.turro.contacts.zul.relation.RelationLayout;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.util.Images;
28 import org.turro.file.util.FileAttach;
29 import org.turro.i18n.I_;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.zkoss.image.ImageWrapper;
32 import org.turro.zkoss.label.LabelTypes;
33 import org.turro.zul.frame.Framework;
34 import org.zkoss.zk.ui.Component;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zul.*;
39 
44 public class ContactInfo extends A {
45 
46  private Contact contact;
47  private String icon;
48  private boolean onlyIcon;
49 
50  public ContactInfo(Contact contact) {
51  setContact(contact);
52  initInfo();
53  }
54 
55  public ContactInfo(Contact contact, String icon) {
56  this.icon = icon;
57  setContact(contact);
58  initInfo();
59  }
60 
61  public ContactInfo(String contactId) {
62  this(contactId, null);
63  }
64 
65  public ContactInfo(String contactId, String icon) {
66  this.icon = icon;
67  //TODO: make it run faster
68  IContact ic = Contacts.getContactById(contactId);
69  if(ic != null && ic.isValid()) {
70  contact = (Contact) ic.getContact();
71  }
72  initInfo();
73  }
74 
75  public ContactInfo() {
76  }
77 
78  public ContactInfo setInfoStyle(String style) {
79  setStyle(style);
80  return this;
81  }
82 
83  public Contact getContact() {
84  return contact;
85  }
86 
87  public final void setContact(Contact contact) {
88  this.contact = contact;
89  updateLabel();
90  }
91 
92  public final void setIContact(IContact icontact) {
93  setContact(icontact == null ? null : (Contact) icontact.getContact());
94  }
95 
96  public boolean isOnlyIcon() {
97  return onlyIcon;
98  }
99 
100  public void setOnlyIcon(boolean onlyIcon) {
101  this.onlyIcon = onlyIcon;
102  updateLabel();
103  }
104 
105  public final void initInfo() {
106  updateLabel();
107  addClickListener();
108  }
109 
110  private void addClickListener() {
111  addEventListener(Events.ON_CLICK, new EventListener() {
112  @Override
113  public void onEvent(Event event) throws Exception {
114  fillContactPopup();
115  }
116  });
117  }
118 
119  private void fillContactPopup() {
120  if(contact == null) {
121  return;
122  }
123  Popup popup = getContactPopup(contact);
124  if(popup != null) popup.open(this);
125  }
126 
127  public static Popup getContactPopup(Contact contact) {
129  Popup popup = Framework.getCurrent().getGlobalPopup();
130 
131  if(popup == null) return null;
132 
133  Hlayout backlayout = new Hlayout();
134  backlayout.setSpacing("10px");
135  popup.appendChild(backlayout);
136 
137  FileAttach fileAttach = new FileAttach(ContactsPU.getObjectPath(contact));
138  String face = fileAttach.getPublicFile("/profile/face.png", false);
139 
140  if(!org.zkoss.lang.Strings.isBlank(face)) {
141  backlayout.appendChild(new ImageWrapper(face, 50, 50));
142  }
143 
144  Vlayout vbox = new Vlayout();
145  vbox.setHflex("1");
146  vbox.setStyle("font-size:11px");
147  backlayout.appendChild(vbox);
148 
149  String name = contact.getName();
150  name += " [" + I_.byKey(contact.getType().toString()) +
151  (!Strings.isBlank(contact.getGrouping()) ? " - " + contact.getGrouping() : "") + "]";
152  vbox.appendChild(LabelTypes.getCaptionLabel(name));
153 
154  if(!Strings.isEmpty(contact.getGlobalIdentifier())) {
155  vbox.appendChild(LabelTypes.getSoftLabel(I_.get("Global identifier") + ": " + contact.getGlobalIdentifier()));
156  }
157 
158  if(app.isInRole("contact-tag:list")) {
159  String tags = contact.getTagsString();
160  if(!Strings.isEmpty(tags)) {
161  vbox.appendChild(LabelTypes.getTinyLabel(I_.get("Tags") + " " + tags));
162  }
163  }
164 
165  if(app.isInRole("contact-address:list")) {
166  for(Address address : contact.getAddresses()) {
167  vbox.appendChild(LabelTypes.getSoftLabel(address.getFullAddress()));
168  }
169  }
170 
171  if(app.isInRole("contact-connector:list")) {
172  for(Connector connector : contact.getConnectors()) {
173  vbox.appendChild(LabelTypes.getSoftLabel(connector.getFullConnector()));
174  }
175  }
176 
177  if(app.isInRole("contact-value:list")) {
178  for(FieldValue fv : contact.getFieldValues()) {
179  vbox.appendChild(getFieldLine(fv));
180  }
181  }
182 
183  if(app.isInRole("contact-relation:list")) {
184  RelationLayout rl = new RelationLayout(contact);
185  rl.fillContactInfo();
186  if(rl.hasContent()) {
187  vbox.appendChild(rl);
188  }
189  }
190  if(!vbox.getChildren().isEmpty()) {
191  return popup;
192  } else {
193  return null;
194  }
195  }
196 
197  private static Component getFieldLine(FieldValue fv) {
198  Hlayout hbox = new Hlayout();
199  FieldLabel fl = new FieldLabel();
200  fl.setSclass("softLabel");
201  fl.setFieldDef(fv.getFieldDef());
202  hbox.appendChild(fl);
203  hbox.appendChild(new Label(": "));
204  fl = new FieldLabel();
205  fl.setSclass("softLabel");
206  fl.setFieldValue(fv);
207  hbox.appendChild(fl);
208  return hbox;
209  }
210 
211  private void updateLabel() {
212  if(contact != null) {
213  if(!onlyIcon) {
214  String name = contact.getName();
215  if(!org.zkoss.lang.Strings.isBlank(contact.getGrouping())) {
216  name += " [" + contact.getGrouping() + "]";
217  }
218  setLabel(name);
219  setImage(icon == null ? Images.getImage("contact") : icon);
220  } else {
221  setLabel(null);
222  setImage(icon == null ? Images.getImage("info") : icon);
223  }
224  } else {
225  setLabel(null);
226  setImage(icon == null ? Images.getImage("info") : icon);
227  }
228  }
229 
230 }
static IContact getContactById(String id)
Definition: Contacts.java:72
Set< FieldValue > getFieldValues()
Definition: Contact.java:427
Set< Address > getAddresses()
Definition: Contact.java:355
Set< Connector > getConnectors()
Definition: Contact.java:367
static String getObjectPath(Object object)
Definition: ContactsPU.java:68
void setFieldDef(FieldDef fieldDef)
Definition: FieldLabel.java:47
void setFieldValue(FieldValue fieldValue)
Definition: FieldLabel.java:32
ContactInfo(Contact contact, String icon)
ContactInfo setInfoStyle(String style)
final void setIContact(IContact icontact)
ContactInfo(String contactId, String icon)
static Popup getContactPopup(Contact contact)
final void setContact(Contact contact)
static String getImage(String image)
Definition: Images.java:36
String getPublicFile(String file)
Definition: FileAttach.java:59
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
static Label getCaptionLabel(String value)
Definition: LabelTypes.java:40
static Label getTinyLabel(String value)
Definition: LabelTypes.java:34
static Label getSoftLabel(String value)
Definition: LabelTypes.java:28
static Framework getCurrent()
Definition: Framework.java:203