aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/desktoputil/SIPCommScrollPane.java
blob: 2eb3b1b7cffe9df52fc4d120be7d66d9baf04468 (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
/*
 * 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;

import java.awt.*;
import java.awt.image.*;
import java.beans.*;
import java.lang.reflect.*;

import javax.swing.*;

import net.java.sip.communicator.util.*;
import net.java.sip.communicator.util.skin.*;

import org.jitsi.util.*;

/**
 * The SCScrollPane is a JScrollPane with a custom viewport that allows to
 * set an image as a background. Depending on the
 * "impl.gui.IS_CONTACT_LIST_TEXTURE_BG_ENABLED" property we'll be setting a
 * single image or a texture of images.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 */
public class SIPCommScrollPane
    extends JScrollPane
    implements Skinnable
{
    private static final long serialVersionUID = 0L;

    /**
     * Creates an <tt>SCSCrollPane</tt>.
     */
    public SIPCommScrollPane()
    {
        this.setBorder(BorderFactory.createMatteBorder(
            1, 0, 1, 0, Color.GRAY));

        this.setViewport(new SCViewport());

        this.getVerticalScrollBar().setUnitIncrement(100);
    }

    /**
     * Sets the view of this JViewport.
     *
     * @param view the view to set.
     */
    @Override
    public void setViewportView(Component view)
    {
        if (view instanceof JComponent)
        {
            JComponent viewAsJComponent = (JComponent) view;

            viewAsJComponent.setBorder(
                    BorderFactory.createEmptyBorder(3, 3, 3, 3));
            viewAsJComponent.setOpaque(false);
        }

        super.setViewportView(view);
    }

    /**
     * Reloads skin information in viewport.
     */
    public void loadSkin()
    {
        ((SCViewport) getViewport()).loadSkin();
    }

    /**
     * The <tt>SCViewport</tt> used as viewport in this scrollpane.
     */
    private static class SCViewport
        extends JViewport
        implements Skinnable
    {
        private static final long serialVersionUID = 1L;

        private BufferedImage bgImage;

        private Color color;

        private TexturePaint texture;

        /**
         * Creates the <tt>SCViewport</tt>.
         */
        public SCViewport()
        {
            this.setBackground(Color.WHITE);

            loadSkin();
        }

        /**
         * Returns the boolean value of the property given by <tt>key</tt>.
         * @param key the key of the property we look for
         * @return the boolean value of the searched property
         */
        private boolean getSettingsBoolean(String key)
        {
            return
                Boolean.parseBoolean(
                        DesktopUtilActivator.getResources().getSettingsString(key));
        }

        /**
         * Paints this viewport.
         * @param g the <tt>Graphics</tt> object used for painting
         */
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            g = g.create();
            try
            {
                AntialiasingManager.activateAntialiasing(g);

                Graphics2D g2 = (Graphics2D) g;
                int width = getWidth();
                int height = getHeight();

                // paint the image
                if (bgImage != null)
                {
                    if (texture != null)
                    {
                        g2.setPaint(texture);

                        g2.fillRect(0, 0, width, height);
                    }
                    else
                    {
                        g.setColor(color);

                        // paint the background with the chosen color
                        g.fillRect(0, 0, width, height);

                        g2.drawImage(bgImage, width - bgImage.getWidth(),
                            height - bgImage.getHeight(), this);
                    }
                }
            }
            finally
            {
                g.dispose();
            }
        }

        /**
         * Reloads background.
         */
        public void loadSkin()
        {
            if(getSettingsBoolean("impl.gui.IS_CONTACT_LIST_IMG_BG_ENABLED"))
            {
                bgImage =
                    DesktopUtilActivator.getImage("service.gui.MAIN_WINDOW_BACKGROUND");

                if (getSettingsBoolean(
                    "impl.gui.IS_CONTACT_LIST_TEXTURE_BG_ENABLED")
                    && (bgImage != null))
                {
                    texture =
                        new TexturePaint(bgImage, new Rectangle(0, 0, bgImage
                            .getWidth(null), bgImage.getHeight(null)));

                    color = null;
                }
                else
                {
                    texture = null;
                    color =
                        new Color(DesktopUtilActivator.getResources().getColor(
                            "service.gui.CONTACT_LIST_BACKGROUND"));
                }
            }
            else
            {
                bgImage = null;
                texture = null;
                color = null;
            }
        }
    }

    /**
     * Releases the resources allocated by this instance throughout its lifetime
     * and prepares it for garbage collection.
     */
    public void dispose()
    {
        if(OSUtils.IS_MAC)
        {
            // Apple introduced a memory leak in JViewport class -
            // they add a PropertyChangeListeners to the CToolkit
            try
            {
                Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
                PropertyChangeListener[] pcl
                    = defaultToolkit.getPropertyChangeListeners(
                            "apple.awt.contentScaleFactor");

                for(PropertyChangeListener pc : pcl)
                {
                    // find the reference to the object created the listener
                    Field f = pc.getClass().getDeclaredField("this$0");

                    f.setAccessible(true);
                    // If we are the parent, clean up.
                    if(f.get(pc).equals(this.getViewport()))
                    {
                        defaultToolkit.removePropertyChangeListener(
                                "apple.awt.contentScaleFactor",
                                pc);
                        break;
                    }
                }
            }
            catch(Throwable t)
            {
                if (t instanceof ThreadDeath)
                    throw (ThreadDeath) t;
            }
        }
    }
}