BrightSide Workbench Full Report + Source Code
ContactFinder.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.util.ArrayList;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.control.ContactFieldList;
26 import org.turro.contacts.util.ContactListbox;
27 import org.turro.elephant.context.Application;
28 import org.turro.i18n.I_;
29 import org.turro.zkoss.input.GenericBandbox;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.EventListener;
32 import org.zkoss.zk.ui.event.Events;
33 import org.zkoss.zk.ui.event.InputEvent;
34 import org.zkoss.zul.Button;
35 import org.zkoss.zul.Hbox;
36 import org.zkoss.zul.Vlayout;
37 
42 public class ContactFinder extends GenericBandbox<Contact> {
43 
44  private ContactListbox contacts = new ContactListbox();
45  private boolean typing = false;
46  private ContactFieldList fields = new ContactFieldList();
47 
49  return fields;
50  }
51 
52  @Override
53  protected void fillPopup() {
54  if(!isFilled()) {
55  getBandPopup().setHeight("300px");
56  getBandPopup().setWidth("400px");
57  setAutodrop(true);
58  Vlayout vbox = new Vlayout();
59  vbox.setVflex("true");
60  contacts.setVflex(true);
61  contacts.setSelectFirst(false);
62  vbox.appendChild(contacts);
63  addEventListener(Events.ON_CHANGING, new EventListener() {
64  @Override
65  public void onEvent(Event event) throws Exception {
66  try {
67  typing = true;
68  contacts.setPartialName(((InputEvent) event).getValue());
69  } finally {
70  typing = false;
71  }
72  }
73  });
74  contacts.afterCompose();
75  contacts.addEventListener(Events.ON_SELECT, new EventListener() {
76  @Override
77  public void onEvent(Event event) {
78  if(!typing && getObjectValue() != null) {
79  Events.postEvent(new Event(Events.ON_CHANGE, ContactFinder.this));
80  ContactFinder.this.close();
81  }
82  }
83  });
84  if(Application.getApplication().isInRole("contact:new")) {
85  Hbox butCont = new Hbox();
86  butCont.setSclass("buttonDialogContainer");
87  butCont.setHflex("true");
88  butCont.setPack("end");
89  butCont.setStyle("padding:10px");
90  butCont.setSpacing("15px");
91  Button newContact = new Button(I_.get("New contact"));
92  newContact.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
93  @Override
94  public void onEvent(Event event) throws Exception {
95  if(fields.isEmpty()) {
96  fields.createMinimumContact();
97  }
98  ContactForm.newContact(fields, new Command() {
99  @Override
100  public Object execute(Context context) {
101  Contact contact = (Contact) context.get("contact");
102  if(contact != null) {
103  if(contacts.getCollection() == null) {
104  contacts.setCollection(new ArrayList<Contact>());
105  }
106  contacts.getCollection().add(contact);
107  contacts.updateCollection();
108  setObjectValue(contact);
109  }
110  return null;
111  }
112  });
113  }
114  });
115  butCont.appendChild(newContact);
116  vbox.appendChild(butCont);
117  }
118  setPopupComponent(vbox);
119  }
120  }
121 
122  @Override
124  Contact v = contacts.getObjectValue();
125  if(v != null) {
126  setBandText(v.getName());
127  } else {
128  setBandText("");
129  }
130  return v;
131  }
132 
133  @Override
134  public void setObjectValue(Contact v) {
135  contacts.setObjectValue(v);
136  if(v != null) {
137  setBandText(v.getName());
138  } else {
139  setBandText("");
140  }
141  }
142 
143 }
static String get(String msg)
Definition: I_.java:41
void setPopupComponent(Component component)
void setSelectFirst(boolean selectFirst)