BrightSide Workbench Full Report + Source Code
ContactForm.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.contacts.zul.control;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Properties;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.string.Strings;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.contacts.Address;
30 import org.turro.contacts.Connector;
31 import org.turro.contacts.Contact;
32 import org.turro.contacts.control.ContactField;
33 import org.turro.contacts.control.ContactFieldList;
34 import static org.turro.contacts.control.ContactFieldType.CONTACT_ADDRESS;
35 import static org.turro.contacts.control.ContactFieldType.CONTACT_CONNECTOR;
36 import static org.turro.contacts.control.ContactFieldType.CONTACT_GID;
37 import static org.turro.contacts.control.ContactFieldType.CONTACT_NAME;
38 import org.turro.contacts.form.ContactWrapper;
39 import org.turro.contacts.util.MostUsed;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.elephant.impl.util.FileUtil;
42 import org.turro.elephant.util.Images;
43 import org.turro.i18n.I_;
44 import org.turro.zkoss.dialog.SelectionDialog;
45 import org.turro.zkoss.label.StyledLabel;
46 import org.turro.zkoss.layout.GridLayout;
47 import org.turro.zul.frame.Framework;
48 import org.zkoss.zk.ui.ext.AfterCompose;
49 import org.zkoss.zul.Image;
50 import org.zkoss.zul.Space;
51 import org.zkoss.zul.Textbox;
52 
57 public class ContactForm extends GridLayout implements AfterCompose {
58 
59  private ContactFieldList fields;
60  private Contact contact;
61 
62  public static void newContact(ContactFieldList fields, final Command command) {
63  final ContactForm cf = new ContactForm();
64  if(fields.isEmpty()) {
65  fields.createMinimumContact();
66  }
67  cf.setFields(fields);
68  cf.setContact(new Contact());
70  I_.get("New contact"),
71  cf, "500px", "600px", new Command() {
72 
73  @Override
74  public Object execute(Context context) {
75  if(cf != null) {
76  final Contact contact = cf.getContact();
77  if(!contact.isEmpty()) {
78  ContactWrapper.saveAnyway(contact, new Command() {
79  @Override
80  public Object execute(Context context) {
81  Contact newc = new ContactWrapper(contact).save();
82  if(context == null) {
83  context = new Context();
84  }
85  context.put("contact", newc);
86  if(command != null) command.execute(context);
87  return null;
88  }
89  });
90  }
91  }
92  return null;
93  }
94  });
95  }
96 
97  public static void editContact(ContactFieldList fields, Contact contact, final Command command) {
98  final ContactForm cf = new ContactForm();
99  if(fields.isEmpty()) {
100  fields.createMinimumContact();
101  }
102  cf.setFields(fields);
103  cf.setContact(contact);
105  I_.get("Contact"),
106  cf, "500px", "600px", new Command() {
107  @Override
108  public Object execute(Context context) {
109  if(cf != null) {
110  final Contact contact = cf.getContact();
111  if(!contact.isEmpty()) {
112  ContactWrapper.saveAnyway(contact, new Command() {
113  @Override
114  public Object execute(Context context) {
115  Contact newc = new ContactWrapper(contact).save();
116  if(context == null) {
117  context = new Context();
118  }
119  context.put("contact", newc);
120  if(command != null) command.execute(context);
121  return null;
122  }
123  });
124  }
125  }
126  return null;
127  }
128  });
129  }
130 
131  public void setFields(ContactFieldList fields) {
132  this.fields = fields;
133  }
134 
135  public void setContact(Contact contact) {
136  this.contact = contact;
137  }
138 
139  public Contact getContact() {
140  assignFields();
141  return contact;
142  }
143 
144  @Override
145  public void afterCompose() {
146  try {
147  getChildren().clear();
148  setHflex("true");
149  setColumns("min,1,min");
150  Properties properties = new Properties();
151  properties.load(FileUtil.getBufferedFile(
152  new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/vCard.properties"))));
153  addFields(properties);
154  } catch (IOException ex) {
155  Logger.getLogger(ContactForm.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
156  }
157  }
158 
159  private void addFields(Properties properties) {
160  for(ContactField cf : fields) {
161  addRow();
162  addField(cf, properties);
163  }
164  }
165 
166  private void addField(ContactField cf, Properties properties) {
167  switch(cf.getType()) {
168  case CONTACT_NAME:
169  addCaption(I_.get("Name"));
170  cf.setEditor(new Textbox(contact.getName()));
171  cf.getEditor().setHflex("true");
172  addComponent(cf.getEditor());
173  addRequired(cf.isRequired());
174  break;
175  case CONTACT_GID:
176  addCaption(I_.get("Global identifier"));
177  cf.setEditor(new Textbox(contact.getGlobalIdentifier()));
178  cf.getEditor().setHflex("true");
179  addComponent(cf.getEditor());
180  addRequired(cf.isRequired());
181  break;
182  case CONTACT_CONNECTOR:
183  if(Strings.isBlank(cf.getFinalLabel())) {
184  String connectorName = MostUsed.getConnector(
185  properties.getProperty(cf.getFieldLabel(), cf.getFieldLabel()),
186  cf.getFieldLabel());
187  cf.setFinalLabel(connectorName);
188  }
189  addCaption(cf.getFinalLabel());
190  Connector connector = contact.getConnectorMap().get(cf.getFinalLabel());
191  cf.setEditor(new Textbox(connector == null ? null : connector.getValue()));
192  cf.getEditor().setHflex("true");
193  addComponent(cf.getEditor());
194  addRequired(cf.isRequired());
195  break;
196  case CONTACT_ADDRESS:
197  if(Strings.isBlank(cf.getFinalLabel())) {
198  String addressName = MostUsed.getAddress(
199  properties.getProperty(cf.getFieldLabel(), cf.getFieldLabel()),
200  cf.getFieldLabel());
201  cf.setFinalLabel(addressName);
202  }
203  addSpannedComponent(new StyledLabel(I_.get("Address") + " " + cf.getFinalLabel(),
204  "font-size:12px;font-weight:bold;"), 3);
205  Address address = contact.getAddressMap().get(cf.getFinalLabel());
206  addRow();
207  addCaption(I_.get("Street"));
208  cf.addEditor(new Textbox(address == null ? null : address.getStreet()));
209  cf.getLastEditor().setHflex("true");
210  addComponent(cf.getLastEditor());
211  addRequired(cf.isRequired());
212  addRow();
213  addCaption(I_.get("Zip code"));
214  cf.addEditor(new Textbox(address == null ? null : address.getZipCode()));
215  cf.getLastEditor().setWidth("100px");
216  addComponent(cf.getLastEditor());
217  addRequired(false);
218  addRow();
219  addCaption(I_.get("City"));
220  cf.addEditor(new Textbox(address == null ? null : address.getCity()));
221  cf.getLastEditor().setHflex("true");
222  addComponent(cf.getLastEditor());
223  addRequired(cf.isRequired());
224  addRow();
225  addCaption(I_.get("Province"));
226  cf.addEditor(new Textbox(address == null ? null : address.getProvince()));
227  cf.getLastEditor().setHflex("true");
228  addComponent(cf.getLastEditor());
229  addRequired(false);
230  addRow();
231  addCaption(I_.get("State"));
232  cf.addEditor(new Textbox(address == null ? null : address.getState()));
233  cf.getLastEditor().setHflex("true");
234  addComponent(cf.getLastEditor());
235  addRequired(false);
236  break;
237  }
238  }
239 
240  private void addRequired(boolean required) {
241  if(required) {
242  addComponent(new Image(Images.getImage("required")));
243  } else {
244  addComponent(new Space());
245  }
246  }
247 
248  private void assignFields() {
249  for(ContactField cf : fields) {
250  assignField(cf);
251  }
252  }
253 
254  private void assignField(ContactField cf) {
255  switch(cf.getType()) {
256  case CONTACT_NAME:
257  contact.setName(((Textbox) cf.getEditor()).getValue());
258  break;
259  case CONTACT_GID:
260  contact.setGlobalIdentifier(((Textbox) cf.getEditor()).getValue());
261  break;
262  case CONTACT_CONNECTOR:
263  Connector connector = contact.getConnectorMap().get(cf.getFinalLabel());
264  if(connector == null) {
265  connector = new Connector();
266  connector.setContact(contact);
267  contact.getConnectors().add(connector);
268  connector.setDescription(cf.getFinalLabel());
269  }
270  connector.setValue(((Textbox) cf.getEditor()).getValue());
271  break;
272  case CONTACT_ADDRESS:
273  Address address = contact.getAddressMap().get(cf.getFinalLabel());
274  if(address == null) {
275  address = new Address();
276  address.setContact(contact);
277  contact.getAddresses().add(address);
278  address.setDescription(cf.getFinalLabel());
279  }
280  address.setStreet(((Textbox) cf.getEditors().get(0)).getValue());
281  address.setZipCode(((Textbox) cf.getEditors().get(1)).getValue());
282  address.setCity(((Textbox) cf.getEditors().get(2)).getValue());
283  address.setProvince(((Textbox) cf.getEditors().get(3)).getValue());
284  address.setState(((Textbox) cf.getEditors().get(4)).getValue());
285  break;
286  }
287  }
288 
289 }
void setName(String name)
Definition: Contact.java:208
void setGlobalIdentifier(String globalIdentifier)
Definition: Contact.java:262
Set< Address > getAddresses()
Definition: Contact.java:346
Map< String, Connector > getConnectorMap()
Definition: Contact.java:492
Map< String, Address > getAddressMap()
Definition: Contact.java:496
Set< Connector > getConnectors()
Definition: Contact.java:358
static void saveAnyway(Contact contact, final Command onSaveAnyway)
static void newContact(ContactFieldList fields, final Command command)
static void editContact(ContactFieldList fields, Contact contact, final Command command)
void setFields(ContactFieldList fields)
static BufferedReader getBufferedFile(String folder, String file)
Definition: FileUtil.java:145
static String get(String msg)
Definition: I_.java:41
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols)
GridLayout addCaption(String label)
static Framework getCurrent()
Definition: Framework.java:204