aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/propertieseditor/SearchField.java
blob: 99bcf520fe19fdd8f361da6fdb50ded73eb27ebf (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 * 
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.propertieseditor;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.table.*;

import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.plugin.desktoputil.plaf.*;
import net.java.sip.communicator.util.skin.*;

import org.apache.commons.lang3.*;
import org.jitsi.service.configuration.*;

/**
 * The field used for searching in the properties table.
 * 
 * @author Marin Dzhigarov
 */
public class SearchField
    extends SIPCommTextField
    implements Skinnable
{
    /**
     * Serial Version UID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * The number of milliseconds that {@link #filterThread} is allowed to stay
     * alive idle i.e. without performing any filtering. After the timeout
     * elapses, <tt>filterThread</tt> will commit suicide.
     */
    private static final long FILTER_THREAD_TIMEOUT = 5 * 60 * 1000;

    /**
     * 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, SearchFieldUI.class.getName());
    }

    private final ConfigurationService confService
        = PropertiesEditorActivator.getConfigurationService();

    /**
     * The <tt>String</tt> that filters the <tt>ConfigurationService</tt>
     * properties to be displayed.
     */
    private String filter;

    /**
     * The <tt>Object</tt> which is to be used for synchronization purposes
     * related to <tt>ConfigurationService</tt> property filtering (e.g.
     * {@link #filter}, {@link #filterThread}).
     */
    private final Object filterSyncRoot = new Object();

    private Thread filterThread;

    /**
     * The table on which the search will be performed.
     */
    private final JTable table;

    /**
     * Creates an instance <tt>SearchField</tt>.
     * 
     * @param text the text we would like to enter by default
     * @param sorter the sorter which will be used for filtering.
     */
    public SearchField(String text, JTable table)
    {
        super(text);

        this.table = table;

        ComponentUI ui = getUI();

        if (ui instanceof SearchFieldUI)
        {
            SearchFieldUI searchFieldUI = (SearchFieldUI) ui;

            searchFieldUI.setCallButtonEnabled(false);
            searchFieldUI.setDeleteButtonEnabled(true);
        }

        setBorder(null);
        setDragEnabled(true);
        setOpaque(false);
        setPreferredSize(new Dimension(100, 25));

        InputMap imap
            = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");

        ActionMap amap = getActionMap();
        @SuppressWarnings("serial")
        AbstractAction escapeAction
            = new AbstractAction()
            {
                public void actionPerformed(ActionEvent e)
                {
                    setText("");
                }
            };

        amap.put("escape", escapeAction);

        getDocument().addDocumentListener(
                new DocumentListener()
                {
                    public void changedUpdate(DocumentEvent e) {}

                    public void insertUpdate(DocumentEvent e)
                    {
                        filter(getText());
                    }

                    public void removeUpdate(DocumentEvent e)
                    {
                        filter(getText());
                    }
                });

        loadSkin();
    }

    /**
     * Schedules a filter to be applied to <tt>ConfigurationService</tt>
     * properties so that only the ones matching the specified <tt>filter</tt>
     * will be displayed.
     *
     * @param filter the <tt>String</tt> to filter the
     * <tt>ConfigurationService</tt> properties to be displayed
     */
    private void filter(String filter)
    {
        synchronized (filterSyncRoot)
        {
            this.filter = filter;

            if (filterThread == null)
            {
                filterThread
                    = new Thread()
                    {
                        @Override
                        public void run()
                        {
                            try
                            {
                                runInFilterThread();
                            }
                            finally
                            {
                                /*
                                 * XXX Making sure here that filterThread is
                                 * aware of its death is just a precaution for
                                 * the cases of abnormal execution of the method
                                 * runInFilterThread(); otherwise, it's
                                 * insufficient in terms of synchronization
                                 * because the lock on filterSyncRoot has been
                                 * relinquished and the method
                                 * runInFilterThread() should take care of
                                 * making sure that filterThread is aware of its
                                 * death while it holds filterSyncRoot and has
                                 * determined that filterThread has idled too
                                 * long.
                                 */
                                synchronized (filterSyncRoot)
                                {
                                    if (Thread.currentThread().equals(
                                            filterThread))
                                    {
                                        filterThread = null;
                                    }
                                }
                            }
                        }
                    };
                filterThread.setDaemon(true);
                filterThread.setName(
                        SearchField.class.getName() + ".filterThread");
                filterThread.start();
            }
            else
            {
                filterSyncRoot.notify();
            }
        }
    }

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

    /**
     * Reloads text field UI defs.
     */
    public void loadSkin()
    {
        ComponentUI ui = getUI();

        if (ui instanceof SearchFieldUI)
            ((SearchFieldUI) ui).loadSkin();
    }

    /**
     * Runs in {@link #filterThread} to apply {@link #filter} to the displayed
     * <tt>ConfigurationService</tt> properties.
     */
    private void runInFilterThread()
    {
        String prevFilter = null;
        long prevFilterTime = 0;

        do
        {
            final String filter;

            synchronized (filterSyncRoot)
            {
                filter = this.filter;

                /*
                 * If the currentThread is idle for too long (which also means
                 * that the filter has not been changed), kill it because we do
                 * not want to keep it alive forever.
                 */
                if ((prevFilterTime != 0)
                        && StringUtils.equalsIgnoreCase(filter, prevFilter))
                {
                    long timeout
                        = FILTER_THREAD_TIMEOUT
                            - (System.currentTimeMillis() - prevFilterTime);

                    if (timeout > 0)
                    {
                        // The currentThread has been idle but not long enough.
                        try
                        {
                            filterSyncRoot.wait(timeout);
                            continue;
                        }
                        catch (InterruptedException ie)
                        {
                            // The currentThread will die bellow at the break.
                        }
                    }
                    // Commit suicide.
                    if (Thread.currentThread().equals(filterThread))
                        filterThread = null;
                    break;
                }
            }

            List<String> properties = confService.getAllPropertyNames();
            final List<Object[]> rows
                = new ArrayList<Object[]>(properties.size());

            for (String property : properties)
            {
                String value = (String) confService.getProperty(property);
                if ((filter == null)
                    || StringUtils.containsIgnoreCase(property, filter)
                    || StringUtils.containsIgnoreCase(value, filter))
                {
                    rows.add(
                            new Object[]
                                    {
                                        property,
                                        confService.getProperty(property)
                                    });
                }
            }

            // If in the meantime someone has changed the filter, we don't want
            // to update the GUI but filter the results again.
            if (StringUtils.equalsIgnoreCase(filter, this.filter))
            {
                LowPriorityEventQueue.invokeLater(
                        new Runnable()
                        {
                           public void run()
                           {
                               DefaultTableModel model
                                   = (DefaultTableModel) table.getModel();

                               model.setRowCount(0);
                               for (Object[] row : rows)
                               {
                                   model.addRow(row);
                                   if (filter != SearchField.this.filter)
                                       return;
                               }
                           }
                        });
            }

            prevFilter = filter;
            prevFilterTime = System.currentTimeMillis();
        }
        while (true);
    }
}