aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/contactlist/contactsource/MetaContactQuery.java
blob: 34e8bf021c7e3322bb6af9d2e9e480ea3f3036e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.main.contactlist.contactsource;

import java.util.*;

import net.java.sip.communicator.service.contactlist.*;

/**
 * The <tt>MetaContactQuery</tt> corresponds to a particular query made through
 * the <tt>MetaContactListSource</tt>. Each query once started could be
 * canceled. One could also register a listener in order to be notified for
 * changes in query status and query contact results.
 *
 * @author Yana Stamcheva
 */
public class MetaContactQuery
{
    private boolean isCanceled = false;

    private int resultCount = 0;

    /**
     * A list of all registered query listeners.
     */
    private final List<MetaContactQueryListener> queryListeners
        = new LinkedList<MetaContactQueryListener>();

    /**
     * Cancels this query.
     */
    public void cancel()
    {
        isCanceled = true;
        queryListeners.clear();
    }

    /**
     * Returns <tt>true</tt> if this query has been canceled, otherwise returns
     * <tt>false</tt>.
     * @return <tt>true</tt> if this query has been canceled, otherwise returns
     * <tt>false</tt>.
     */
    public boolean isCanceled()
    {
        return isCanceled;
    }

    /**
     * Returns the current number of results received for this query.
     * @return the current number of results received for this query
     */
    public int getResultCount()
    {
        return resultCount;
    }

    /**
     * Sets the result count of this query. This method is meant to be used to
     * set the initial result count which is before firing any events. The
     * result count would be then augmented each time the fireQueryEvent is
     * called.
     * @param resultCount the initial result count to set
     */
    public void setInitialResultCount(int resultCount)
    {
        this.resultCount = resultCount;
    }

    /**
     * Adds the given <tt>MetaContactQueryListener</tt> to the list of
     * registered listeners. The <tt>MetaContactQueryListener</tt> would be
     * notified each time a new <tt>MetaContactQuery</tt> result has been
     * received or if the query has been completed or has been canceled by user
     * or for any other reason.
     * @param l the <tt>MetaContactQueryListener</tt> to add
     */
    public void addContactQueryListener(MetaContactQueryListener l)
    {
        synchronized (queryListeners)
        {
            queryListeners.add(l);
        }
    }

    /**
     * Removes the given <tt>MetaContactQueryListener</tt> to the list of
     * registered listeners. The <tt>MetaContactQueryListener</tt> would be
     * notified each time a new <tt>MetaContactQuery</tt> result has been
     * received or if the query has been completed or has been canceled by user
     * or for any other reason.
     * @param l the <tt>MetaContactQueryListener</tt> to remove
     */
    public void removeContactQueryListener(MetaContactQueryListener l)
    {
        synchronized (queryListeners)
        {
            queryListeners.remove(l);
        }
    }

    /**
     * Notifies the <tt>MetaContactQueryListener</tt> that a new
     * <tt>MetaContact</tt> has been received as a result of a search.
     * @param metaContact the received <tt>MetaContact</tt>
     */
    public void fireQueryEvent(MetaContact metaContact)
    {
        resultCount++;

        MetaContactQueryEvent event
            = new MetaContactQueryEvent(this, metaContact);

        List<MetaContactQueryListener> listeners;
        synchronized (queryListeners)
        {
            listeners = new LinkedList<MetaContactQueryListener>(queryListeners);
        }

        Iterator<MetaContactQueryListener> listenersIter = listeners.iterator();
        while (listenersIter.hasNext())
        {
            MetaContactQueryListener listener = listenersIter.next();

            listener.metaContactReceived(event);
        }
    }

    /**
     * Notifies the <tt>MetaContactQueryListener</tt> that a new
     * <tt>MetaGroup</tt> has been received as a result of a search.
     * @param metaGroup the received <tt>MetaGroup</tt>
     */
    public void fireQueryEvent(MetaContactGroup metaGroup)
    {
        MetaGroupQueryEvent event
            = new MetaGroupQueryEvent(this, metaGroup);

        List<MetaContactQueryListener> listeners;
        synchronized (queryListeners)
        {
            listeners = new LinkedList<MetaContactQueryListener>(queryListeners);
        }

        Iterator<MetaContactQueryListener> listenersIter = listeners.iterator();
        while (listenersIter.hasNext())
        {
            listenersIter.next().metaGroupReceived(event);
        }
    }

    /**
     * Notifies the <tt>MetaContactQueryListener</tt> that this query has
     * changed its status.
     * @param queryStatus the new query status
     */
    public void fireQueryEvent(int queryStatus)
    {
        MetaContactQueryStatusEvent event
            = new MetaContactQueryStatusEvent(this, queryStatus);

        List<MetaContactQueryListener> listeners;
        synchronized (queryListeners)
        {
            listeners = new LinkedList<MetaContactQueryListener>(queryListeners);
        }

        Iterator<MetaContactQueryListener> listenersIter = listeners.iterator();
        while (listenersIter.hasNext())
        {
            listenersIter.next().metaContactQueryStatusChanged(event);
        }
    }
}