KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > samples > contacts > FilteredContacts


1
2 package org.objectweb.jac.samples.contacts;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 public class FilteredContacts extends ContactRepository {
9    
10     ContactRepository contactRepository = null;
11     CompanyRepository companyRepository = null;
12
13     public FilteredContacts() {
14     }
15
16     public FilteredContacts(ContactRepository repository) {
17         contactRepository = repository;
18         contacts.addAll(contactRepository.getContacts());
19     }
20
21     public ContactRepository getContactRepository() {
22         return contactRepository;
23     }
24     public void setContactRepository(ContactRepository cr) {
25         this.contactRepository = cr;
26     }
27
28     /**
29     * Add a contact to the contact repository.
30     * @param contact the contact added.
31     */

32     public void addContact(Person contact) {
33         contactRepository.addContact(contact);
34         contacts.add(contact);
35     }
36
37     /**
38     * Remove a contact from contact repository.
39     * @param contact the contact removed.
40     */

41     public void removeContact(Person contact) {
42         contactRepository.removeContact(contact);
43         contacts.remove(contact);
44     }
45
46     public void showAll() {
47         search(".*", null);
48     }
49
50     public CompanyRepository companies() {
51         if (companyRepository == null)
52             companyRepository = new CompanyRepository(contactRepository);
53         companyRepository.showAll();
54         return companyRepository;
55     }
56    
57     /**
58      * Filter the contacts of the contact repository regarding a
59      * searching criteria.
60      *
61      * @param criteria can be a regexp
62      */

63     public void search(String JavaDoc criteria, Company company) {
64         contacts.clear();
65         String JavaDoc name = "";
66         if (company!=null) {
67             name = company.getName();
68         }
69         List JavaDoc foundContacts = contactRepository.find(criteria,name);
70         Iterator JavaDoc it = foundContacts.iterator();
71         while (it.hasNext()) {
72             Person contact = (Person) it.next();
73             if (!contacts.contains(contact))
74                 contacts.add(contact);
75         }
76
77       /*
78       java.util.Iterator it = contactRepository.getContacts().iterator();
79       if( criteria.equals("") ) criteria = ".*";
80       RE recrit = null;
81       try {
82          recrit = new RE(criteria);
83       } catch( Exception e ) {
84          e.printStackTrace();
85          return;
86       }
87       while( it.hasNext() ) {
88          Person contact = (Person) it.next();
89          if( company!=null && !company.getName().equals("") ) {
90             if( company != contact.getCompany() ) {
91                continue;
92             }
93          }
94          if( (!contacts.contains(contact)) &&
95              recrit.isMatch( contact.getLastName() ) ) {
96             contacts.add(contact);
97             System.out.println("Contact "+contact+" added to filtered");
98          }
99          if( (!contacts.contains(contact)) &&
100              recrit.isMatch( contact.getFirstName() ) ) {
101             contacts.add(contact);
102             System.out.println("Contact "+contact+" added to filtered");
103          }
104          }*/

105     }
106
107 }
108
Popular Tags