aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/desktoputil/SIPCommTabbedPane.java
blob: 467f2c8ad492fa03ee18dd99cc161a1543cc97b9 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * 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.desktoputil;

/*
 * The following code borrowed from David Bismut, davidou@mageos.com Intern,
 * SETLabs, Infosys Technologies Ltd. May 2004 - Jul 2004 Ecole des Mines de
 * Nantes, France
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;

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

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

/**
 * A JTabbedPane with some added UI functionalities. A close and max/detach
 * icons are added to every tab, typically to let the user close or detach the
 * tab by clicking on these icons.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 */
public class SIPCommTabbedPane
    extends JTabbedPane
    implements  ChangeListener,
                Skinnable
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    private int overTabIndex = -1;

    private int lastSelectedIndex;

    public SIPCommTabbedPane()
    {
        this(false, false);
    }

    /**
     * Creates the <code>CloseAndMaxTabbedPane</code> with an enhanced UI if
     * <code>enhancedUI</code> parameter is set to <code>true</code>.
     *
     * @param closingTabs support for closable tabs
     * @param maximizingTabs support for maximisable tabs
     */
    public SIPCommTabbedPane(boolean closingTabs, boolean maximizingTabs)
    {
        super.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        UIManager.getDefaults()
            .put("TabbedPane.tabAreaInsets", new Insets(0, 5, 0, 0));

        UIManager.getDefaults()
            .put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

        this.setForeground(
            new Color(DesktopUtilActivator.getResources()
                .getColor("service.gui.TAB_TITLE")));

        this.setUI(new SIPCommTabbedPaneEnhancedUI());

        if(closingTabs)
            this.setCloseIcon(true);

        if(maximizingTabs)
            this.setMaxIcon(true);

        this.addChangeListener(this);
    }

    /**
     * Returns the index of the last tab on which the mouse did an action.
     *
     * @return
     */
    public int getOverTabIndex()
    {
        return overTabIndex;
    }

    /**
     * Returns <code>true</code> if the close icon is enabled.
     *
     * @return
     */
    public boolean isCloseEnabled()
    {
        SIPCommTabbedPaneUI ui = (SIPCommTabbedPaneUI) this.getUI();
        return ui.isCloseEnabled();
    }

    /**
     * Returns <code>true</code> if the max/detach icon is enabled.
     *
     * @return
     */
    public boolean isMaxEnabled()
    {
        return ((SIPCommTabbedPaneUI) getUI()).isMaxEnabled();
    }

    /**
     * Override JTabbedPane method. Does nothing.
     * @param tabLayoutPolicy The tab layout policy.
     */
    @Override
    public void setTabLayoutPolicy(int tabLayoutPolicy)
    {
    }

    /**
     * Override JTabbedPane method. Does nothing.
     * @param tabPlacement The tab placement.
     */
    @Override
    public void setTabPlacement(int tabPlacement)
    {
    }

    /**
     * Sets whether the tabbedPane should have a close icon or not.
     *
     * @param b whether the tabbedPane should have a close icon or not
     */
    public void setCloseIcon(boolean b)
    {
        ((SIPCommTabbedPaneUI) getUI()).setCloseIcon(b);
    }

    /**
     * Sets whether the tabbedPane should have a max/detach icon or not.
     *
     * @param b whether the tabbedPane should have a max/detach icon or not
     */
    public void setMaxIcon(boolean b)
    {
        ((SIPCommTabbedPaneUI) getUI()).setMaxIcon(b);
    }

    /**
     * Detaches the <code>index</code> tab in a separate frame. When the frame
     * is closed, the tab is automatically reinserted into the tabbedPane.
     *
     * @param index index of the tabbedPane to be detached
     */
    public void detachTab(int index)
    {
        if (index < 0 || index >= getTabCount())
            return;

        final int tabIndex = index;
        final JComponent c = (JComponent) getComponentAt(tabIndex);

        final Icon icon = getIconAt(tabIndex);
        final String title = getTitleAt(tabIndex);
        final String toolTip = getToolTipTextAt(tabIndex);
        final Border border = c.getBorder();

        final JFrame frame = new SIPCommFrame()
        {
            /**
             * Serial version UID.
             */
            private static final long serialVersionUID = 0L;

            @Override
            protected void close(boolean isEscaped)
            {
                if (isEscaped)
                    return;

                dispose();

                insertTab(title, icon, c, toolTip, Math.min(tabIndex,
                    getTabCount()));

                c.setBorder(border);
                setSelectedComponent(c);
            }
        };

        Window parentWindow = SwingUtilities.windowForComponent(this);

        removeTabAt(index);

        c.setPreferredSize(c.getSize());

        frame.setTitle(title);
        frame.getContentPane().add(c);
        frame.setLocation(parentWindow.getLocation());
        frame.pack();

        WindowFocusListener windowFocusListener = new WindowFocusListener() {
            long start;

            long end;

            public void windowGainedFocus(WindowEvent e) {
                start = System.currentTimeMillis();
            }

            public void windowLostFocus(WindowEvent e) {
                end = System.currentTimeMillis();
                long elapsed = end - start;

                if (elapsed < 100)
                    frame.toFront();

                frame.removeWindowFocusListener(this);
            }
        };

        /*
         * This is a small hack to avoid Windows GUI bug, that prevent a new
         * window from stealing focus (without this windowFocusListener, most of
         * the time the new frame would just blink from foreground to
         * background). A windowFocusListener is added to the frame, and if the
         * time between the frame beeing in foreground and the frame beeing in
         * background is less that 100ms, it just brings the windows to the
         * front once again. Then it removes the windowFocusListener. Note that
         * this hack would not be required on Linux or UNIX based systems.
         */

        frame.addWindowFocusListener(windowFocusListener);

        // frame.show();
        frame.setVisible(true);
        frame.toFront();

    }

    /**
     * Adds a <code>CloseListener</code> to the tabbedPane.
     *
     * @param l the <code>CloseListener</code> to add
     * @see #fireCloseTabEvent
     * @see #removeCloseListener
     */
    public synchronized void addCloseListener(CloseListener l)
    {
        listenerList.add(CloseListener.class, l);
    }

    /**
     * Adds a <code>MaxListener</code> to the tabbedPane.
     *
     * @param l the <code>MaxListener</code> to add
     * @see #fireMaxTabEvent
     * @see #removeMaxListener
     */
    public synchronized void addMaxListener(MaxListener l)
    {
        listenerList.add(MaxListener.class, l);
    }

    /**
     * Adds a <code>DoubleClickListener</code> to the tabbedPane.
     *
     * @param l the <code>DoubleClickListener</code> to add
     * @see #fireDoubleClickTabEvent
     * @see #removeDoubleClickListener
     */
    public synchronized void addDoubleClickListener(DoubleClickListener l)
    {
        listenerList.add(DoubleClickListener.class, l);
    }

    /**
     * Adds a <code>PopupOutsideListener</code> to the tabbedPane.
     *
     * @param l the <code>PopupOutsideListener</code> to add
     * @see #firePopupOutsideTabEvent
     * @see #removePopupOutsideListener
     */
    public synchronized void addPopupOutsideListener(PopupOutsideListener l)
    {
        listenerList.add(PopupOutsideListener.class, l);
    }

    /**
     * Removes a <code>CloseListener</code> from this tabbedPane.
     *
     * @param l the <code>CloseListener</code> to remove
     * @see #fireCloseTabEvent
     * @see #addCloseListener
     */
    public synchronized void removeCloseListener(CloseListener l)
    {
        listenerList.remove(CloseListener.class, l);
    }

    /**
     * Removes a <code>MaxListener</code> from this tabbedPane.
     *
     * @param l the <code>MaxListener</code> to remove
     * @see #fireMaxTabEvent
     * @see #addMaxListener
     */
    public synchronized void removeMaxListener(MaxListener l)
    {
        listenerList.remove(MaxListener.class, l);
    }

    /**
     * Removes a <code>DoubleClickListener</code> from this tabbedPane.
     *
     * @param l
     *            the <code>DoubleClickListener</code> to remove
     * @see #fireDoubleClickTabEvent
     * @see #addDoubleClickListener
     */
    public synchronized void removeDoubleClickListener(DoubleClickListener l)
    {
        listenerList.remove(DoubleClickListener.class, l);
    }

    /**
     * Removes a <code>PopupOutsideListener</code> from this tabbedPane.
     *
     * @param l
     *            the <code>PopupOutsideListener</code> to remove
     * @see #firePopupOutsideTabEvent
     * @see #addPopupOutsideListener
     */
    public synchronized void removePopupOutsideListener(
                                        PopupOutsideListener l)
    {
        listenerList.remove(PopupOutsideListener.class, l);
    }

    /**
     * Sends a <code>MouseEvent</code>, whose source is this tabbedpane, to
     * every <code>CloseListener</code>. The method also updates the
     * <code>overTabIndex</code> of the tabbedPane with a value coming from
     * the UI. This method method is called each time a <code>MouseEvent</code>
     * is received from the UI when the user clicks on the close icon of the tab
     * which index is <code>overTabIndex</code>.
     *
     * @param e
     *            the <code>MouseEvent</code> to be sent
     * @param overTabIndex
     *            the index of a tab, usually the tab over which the mouse is
     *
     * @see #addCloseListener
     */
    public void fireCloseTabEvent(MouseEvent e, int overTabIndex)
    {
        this.overTabIndex = overTabIndex;

        EventListener[] closeListeners = getListeners(CloseListener.class);
        for (int i = 0; i < closeListeners.length; i++)
        {
            ((CloseListener) closeListeners[i]).closeOperation(e);
        }
    }

    /**
     * Sends a <code>MouseEvent</code>, whose source is this tabbedpane, to
     * every <code>MaxListener</code>. The method also updates the
     * <code>overTabIndex</code> of the tabbedPane with a value coming from
     * the UI. This method method is called each time a <code>MouseEvent</code>
     * is received from the UI when the user clicks on the max icon of the tab
     * which index is <code>overTabIndex</code>.
     *
     * @param e
     *            the <code>MouseEvent</code> to be sent
     * @param overTabIndex
     *            the index of a tab, usually the tab over which the mouse is
     *
     * @see #addMaxListener
     */
    public void fireMaxTabEvent(MouseEvent e, int overTabIndex)
    {
        this.overTabIndex = overTabIndex;

        EventListener[] maxListeners = getListeners(MaxListener.class);
        for (int i = 0; i < maxListeners.length; i++)
        {
            ((MaxListener) maxListeners[i]).maxOperation(e);
        }
    }

    /**
     * Sends a <code>MouseEvent</code>, whose source is this tabbedpane, to
     * every <code>DoubleClickListener</code>. The method also updates the
     * <code>overTabIndex</code> of the tabbedPane with a value coming from
     * the UI. This method method is called each time a <code>MouseEvent</code>
     * is received from the UI when the user double-clicks on the tab which
     * index is <code>overTabIndex</code>.
     *
     * @param e
     *            the <code>MouseEvent</code> to be sent
     * @param overTabIndex
     *            the index of a tab, usually the tab over which the mouse is
     *
     * @see #addDoubleClickListener
     */
    public void fireDoubleClickTabEvent(MouseEvent e, int overTabIndex)
    {
        this.overTabIndex = overTabIndex;

        EventListener[] dClickListeners
            = getListeners(DoubleClickListener.class);
        for (int i = 0; i < dClickListeners.length; i++)
        {
            ((DoubleClickListener) dClickListeners[i]).doubleClickOperation(e);
        }
    }

    /**
     * Sends a <code>MouseEvent</code>, whose source is this tabbedpane, to
     * every <code>PopupOutsideListener</code>. The method also sets the
     * <code>overTabIndex</code> to -1. This method method is called each time
     * a <code>MouseEvent</code> is received from the UI when the user
     * right-clicks on the inactive part of a tabbedPane.
     *
     * @param e
     *            the <code>MouseEvent</code> to be sent
     *
     * @see #addPopupOutsideListener
     */
    public void firePopupOutsideTabEvent(MouseEvent e)
    {
        this.overTabIndex = -1;

        EventListener[] popupListeners
            = getListeners(PopupOutsideListener.class);
        for (int i = 0; i < popupListeners.length; i++)
        {
            ((PopupOutsideListener) popupListeners[i]).popupOutsideOperation(e);
        }
    }

    /**
     * Overrides setSelectedIndex in JTabbedPane in order to remove the
     * highlight if the tab which is selected.
     * @param tabIndex The index of the tab to be selected.
     */
    @Override
    public void setSelectedIndex(int tabIndex)
    {
        SIPCommTabbedPaneEnhancedUI ui
            = (SIPCommTabbedPaneEnhancedUI) this.getUI();
        if (ui.isTabHighlighted(tabIndex))
        {
            ui.tabRemoveHighlight(tabIndex);
        }
        super.setSelectedIndex(tabIndex);
    }

    /**
     * Highlights the tab with the given index.
     *
     * @param tabIndex The tab index.
     */
    public void highlightTab(int tabIndex)
    {
        SIPCommTabbedPaneEnhancedUI ui
            = (SIPCommTabbedPaneEnhancedUI) this.getUI();

        if (!ui.isTabHighlighted(tabIndex)
                && this.getSelectedIndex() != tabIndex)
            ui.tabAddHightlight(tabIndex);

        this.repaint();
    }

    @Override
    public void removeTabAt(int index)
    {
        if (index < lastSelectedIndex)
        {
            this.setSelectedIndex(lastSelectedIndex - 1);
        }
        else if (index > lastSelectedIndex)
        {
            this.setSelectedIndex(lastSelectedIndex);
        }

        super.removeTabAt(index);
    }

    public void stateChanged(ChangeEvent e)
    {
        lastSelectedIndex = this.getSelectedIndex();
    }

    /**
     * Reloads skin information.
     */
    public void loadSkin()
    {
        this.setForeground(
            new Color(DesktopUtilActivator.getResources()
                .getColor("service.gui.TAB_TITLE")));

        ((SIPCommTabbedPaneEnhancedUI)this.getUI()).loadSkin();
    }
}