aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/contactlist/SearchField.java
blob: f4ce14e0c9532f33495ddfc17d015dd4dd12447c (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.gui.main.contactlist;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.main.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.plugin.desktoputil.event.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.gui.event.*;
import net.java.sip.communicator.util.skin.*;

/**
 * The field shown on the top of the main window, which allows the user to
 * search for users.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 * @author Marin Dzhigarov
 */
public class SearchField
    extends SIPCommTextField
    implements  TextFieldChangeListener,
                FilterQueryListener,
                Skinnable
{
    /**
     * Class id key used in UIDefaults.
     */
    private static final String uiClassID
        = SearchField.class.getName() +  "FieldUI";

    /**
     * Adds the ui class to UIDefaults.
     */
    static
    {
        UIManager.getDefaults().put(
                uiClassID,
                ContactSearchFieldUI.class.getName());
    }

    /**
     * The contact list on which we apply the filter.
     */
    private ContactList contactList;

    /**
     * The current filter query.
     */
    private FilterQuery currentFilterQuery = null;

    /**
     * The main application window.
     */
    private final MainFrame mainFrame;

    /**
     * The filter to apply on search.
     */
    private final ContactListSearchFilter searchFilter;

    /**
     * Creates the <tt>SearchField</tt>.
     *
     * @param frame the main application window
     * @param searchFilter the filter to apply on search
     * @param isCallButtonEnabled indicates if the call button should be
     * enabled in this search field
     * @param isSMSButtonEnabled indicates if the sms button should be
     * enabled in this search field
     */
    public SearchField( MainFrame frame,
                        ContactListSearchFilter searchFilter,
                        boolean isCallButtonEnabled,
                        boolean isSMSButtonEnabled)
    {
        super(GuiActivator.getResources()
                .getI18NString("service.gui.ENTER_NAME_OR_NUMBER"));

        this.mainFrame = frame;
        this.searchFilter = searchFilter;

        if (getUI() instanceof ContactSearchFieldUI)
        {
            ((ContactSearchFieldUI) getUI()).setupListeners();
            ((ContactSearchFieldUI) getUI()).setDeleteButtonEnabled(true);
            ((ContactSearchFieldUI) getUI())
                .setCallButtonEnabled(isCallButtonEnabled);
            ((ContactSearchFieldUI) getUI())
                .setSMSButtonEnabled(isSMSButtonEnabled);
        }

        this.setBorder(null);
        this.setOpaque(false);

        this.setDragEnabled(true);
        this.addTextChangeListener(this);

        InputMap imap
            = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
        ActionMap amap = getActionMap();
        amap.put("escape", new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                setText("");

                if (SearchField.this.mainFrame != null)
                    SearchField.this.mainFrame.requestFocusInContactList();
            }
        });

        loadSkin();
    }

    /**
     * Do not need this for the moment.
     * @param e the <tt>DocumentEvent</tt> that notified us
     */
    @Override
    public void changedUpdate(DocumentEvent e) {}

    /**
     * Sets the unknown contact view to the main contact list window.
     *
     * @param isEnabled indicates if the unknown contact view should be enabled
     * or disabled.
     */
    public void enableUnknownContactView(final boolean isEnabled)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                if (mainFrame != null)
                    mainFrame.enableUnknownContactView(isEnabled);
            }
        });
    }

    /**
     * Indicates that the given <tt>query</tt> has finished with failure, i.e.
     * no results for the filter were found.
     *
     * @param query the <tt>FilterQuery</tt>, where this listener is registered
     */
    public void filterQueryFailed(FilterQuery query)
    {
        // If the query has failed then we don't have results.
        if (currentFilterQuery.equals(query))
            filterQueryFinished(query, false);
    }

    /**
     * Performs all needed updates when a filter query has finished.
     *
     * @param query the query that has finished
     * @param hasResults indicates if the query has results
     */
    private void filterQueryFinished(FilterQuery query, boolean hasResults)
    {
        // If the unknown contact view was previously enabled, but we
        // have found matching contacts we enter the normal view.
        enableUnknownContactView(!hasResults);

        query.setQueryListener(null);
    }

    /**
     * Indicates that the given <tt>query</tt> has finished with success, i.e.
     * the filter has returned results.
     *
     * @param query the <tt>FilterQuery</tt>, where this listener is registered
     */
    public void filterQuerySucceeded(FilterQuery query)
    {
        // If the query has succeeded then we have results.
        if (currentFilterQuery.equals(query))
            filterQueryFinished(query, true);
    }

    /**
     * Returns the name of the L&F class that renders this component.
     *
     * @return the string "TreeUI"
     * @see JComponent#getUIClassID
     * @see UIDefaults#getUI
     */
    @Override
    public String getUIClassID()
    {
        return uiClassID;
    }

    /**
     * Reloads text field UI defs.
     */
    public void loadSkin()
    {
        if (getUI() instanceof ContactSearchFieldUI)
            ((ContactSearchFieldUI) getUI()).loadSkin();
    }

    /**
     * Sets the contact list, in which the search is performed.
     *
     * @param contactList the contact list in which the search is performed
     */
    public void setContactList(ContactList contactList)
    {
        this.contactList = contactList;
    }

    /**
     * Handles the change when a char has been inserted in the field.
     */
    public void textInserted()
    {
        // Should explicitly check if there's a text, because the default text
        // triggers also an insertUpdate event.
        String filterString = this.getText();
        if (filterString == null || filterString.length() <= 0)
            return;

        updateContactListView();
    }

    /**
     * Handles the change when a char has been removed from the field.
     */
    public void textRemoved()
    {
        updateContactListView();
    }

    /**
     * Schedules an update if necessary.
     */
    private void updateContactListView()
    {
        String filterString = getText();

        boolean isDefaultFilter = false;

        searchFilter.setFilterString(filterString.trim());

        // First finish the last filter.
        if (currentFilterQuery != null)
            filterQueryFinished(currentFilterQuery, true);

        if (filterString != null && filterString.length() > 0)
        {
            currentFilterQuery = contactList.applyFilter(searchFilter);
        }
        else
        {
            currentFilterQuery = contactList.applyDefaultFilter();
            isDefaultFilter = true;
        }

        if (currentFilterQuery != null && !currentFilterQuery.isCanceled())
        {
            // If we already have a result here we update the interface.
            // In the case of default filter we don't need to know if the
            // query has succeeded, as event if it isn't we would like to
            // remove the unknown contact view.
            if (isDefaultFilter || currentFilterQuery.isSucceeded())
                enableUnknownContactView(false);
            else
                // Otherwise we will listen for events for changes in status
                // of this query.
                currentFilterQuery.setQueryListener(this);
        }
        else
        {
            // If the query is null or is canceled, we would simply check the
            // contact list content.
            filterQueryFinished(currentFilterQuery, !contactList.isEmpty());
        }
    }

    public void dispose()
    {
        if(getUI() instanceof ContactSearchFieldUI)
        {
            ((ContactSearchFieldUI)getUI()).removeListeners();
        }
    }
}