aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/lookandfeel/SIPCommButtonUI.java
blob: 5c7c269a072154294831eced9e88eff67c538b0c (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
/*
 * 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.lookandfeel;

import java.awt.*;
import java.awt.image.*;

import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;

import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.util.skin.*;

/**
 * SIPCommButtonUI implementation.
 *
 * @author Yana Stamcheva
 */
public class SIPCommButtonUI
    extends MetalButtonUI
    implements Skinnable
{
    /**
     * The background button image.
     */
    private static BufferedImage buttonBG
        = ImageLoader.getImage(ImageLoader.BUTTON);

    /**
     * The background image for the pressed button state.
     */
    private static BufferedImage buttonRolloverBG
        = ImageLoader.getImage(ImageLoader.BUTTON_ROLLOVER);

    /**
     * Indicates if the button is in rollover state.
     */
    private boolean bufferIsRollover = false;

    /**
     * The paint buffer.
     */
    private BufferedImage paintBuffer = null;

    /**
     * The buffered component.
     */
    private Component bufferedComponent = null;

    /**
     * Creates the UI for the given <tt>JComponent</tt>.
     *
     * @param c the <tt>JComponent</tt>, for which to create an UI
     * @return the component UI
     */
    public static ComponentUI createUI(JComponent c)
    {
        return new SIPCommButtonUI();
    }

    /**
     * Installs default configurations for the given <tt>AbstractButton</tt>.
     *
     * @param b the button, for which we're installing the defaults
     */
    @Override
    public void installDefaults(AbstractButton b)
    {
        super.installDefaults(b);

        b.setOpaque(false);
        b.setBorderPainted(false);
        b.setFocusPainted(true);
        b.setRolloverEnabled(true);
    }

    /**
     * Uninstalls default configurations for the given <tt>AbstractButton</tt>.
     *
     * @param b the button, for which we're uninstalling the defaults
     */
    @Override
    public void uninstallDefaults(AbstractButton b)
    {
        super.uninstallDefaults(b);

        b.setBorderPainted(true);
        b.setFocusPainted(false);
        b.setOpaque(true);
        b.setRolloverEnabled(false);
    }

    /**
     * Paints this button UI.
     *
     * @param g the <tt>Graphics</tt> object used for painting
     * @param c the <tt>Component</tt> to paint
     */
    @Override
    public void paint(Graphics g, JComponent c)
    {
        AbstractButton button = (AbstractButton)c;
        ButtonModel model = button.getModel();

        // check if the context of the buffer is consistent or else recreate it
        if (paintBuffer == null || c != bufferedComponent
                || bufferIsRollover != model.isRollover())
        {
            // create a buffer in the best available format
            paintBuffer = ((Graphics2D) g).getDeviceConfiguration().
                createCompatibleImage(c.getWidth(), c.getHeight(),
                        Transparency.TRANSLUCENT);

            // save the context
            bufferedComponent = c;
            bufferIsRollover = model.isRollover();

            // draw in the buffer
            BufferedImage leftImg;
            BufferedImage middleImg;
            BufferedImage rightImg;

            int imgWidth;
            int imgHeight;
            int indentWidth  = 10;
            if(bufferIsRollover){
                imgWidth = buttonRolloverBG.getWidth();
                imgHeight = buttonRolloverBG.getHeight();

                leftImg = buttonRolloverBG.getSubimage(0, 0, indentWidth,
                                                        imgHeight);
                middleImg = buttonRolloverBG.getSubimage(indentWidth, 0,
                                                        imgWidth-2*indentWidth,
                                                        imgHeight);
                rightImg = buttonRolloverBG.getSubimage(imgWidth-indentWidth, 0,
                                                        indentWidth, imgHeight);
            }
            else{
                imgWidth = buttonBG.getWidth();
                imgHeight = buttonBG.getHeight();

                leftImg = buttonBG.getSubimage(0, 0, 10, imgHeight);
                middleImg = buttonBG.getSubimage(10, 0, imgWidth-20, imgHeight);
                rightImg = buttonBG.getSubimage(imgWidth-10, 0, 10, imgHeight);
            }

            Graphics2D g2 = paintBuffer.createGraphics();

            AntialiasingManager.activateAntialiasing(g2);

            g2.drawImage(leftImg, 0, 0, indentWidth, c.getHeight(), null);
            g2.drawImage(middleImg, indentWidth, 0,
                    c.getWidth() - 2 * indentWidth, c.getHeight(), null);
            g2.drawImage(rightImg, c.getWidth() - indentWidth, 0,
                    indentWidth, c.getHeight(), null);
        }

        AntialiasingManager.activateAntialiasing(g);

        // draw the buffer in the graphics object
        g.drawImage(paintBuffer, 0, 0, c.getWidth(), c.getHeight(),
                0, 0, c.getWidth(), c.getHeight(), null);

        super.paint(g, c);
    }

    /**
     * Paints the focused view of the given <tt>AbstractButton</tt>.
     *
     * @param g the <tt>Graphics</tt> object used for painting
     * @param b the button to paint
     * @param viewRect the rectangle indicating the bounds of the focused button
     * @param textRect the rectangle indicating the bounds of the text
     * @param iconRect the rectangle indicating the bounds of the icon
     */
    @Override
    protected void paintFocus(Graphics g, AbstractButton b,
            Rectangle viewRect, Rectangle textRect, Rectangle iconRect)
    {
        Graphics2D g2 = (Graphics2D)g;

        Rectangle focusRect = new Rectangle();
        String text = b.getText();
        boolean isIcon = b.getIcon() != null;

        // If there is text
        if ( text != null && !text.equals( "" ) )
        {
            if ( !isIcon )
            {
                focusRect.setBounds( textRect );
            }
            else
            {
                focusRect.setBounds( iconRect.union( textRect ) );
            }
        }
        // If there is an icon and no text
        else if ( isIcon )
        {
            focusRect.setBounds( iconRect );
        }

        g2.setStroke(new BasicStroke(0.5f,// Width
                BasicStroke.CAP_ROUND,    // End cap
                BasicStroke.JOIN_ROUND,   // Join style
                10.0f,                    // Miter limit
                new float[] {1.0f,1.0f},// Dash pattern
                2.0f));
        g2.setColor(Color.GRAY);
        g2.drawRoundRect((focusRect.x-3), (focusRect.y-3),
                focusRect.width+4, focusRect.height+4, 5, 5);
    }

    /**
     * Overriden to do nothing.
     */
    @Override
    protected void paintButtonPressed(Graphics g, AbstractButton b)
    {
        if ( b.isContentAreaFilled() )
        {
            Dimension size = b.getSize();
            g.setColor(getSelectColor());
            g.fillRoundRect(0, 0, size.width, size.height, 5, 5);
        }
    }

    /**
     * Reloads buffered images.
     */
    public void loadSkin()
    {
        buttonBG
            = ImageLoader.getImage(ImageLoader.BUTTON);

        buttonRolloverBG
            = ImageLoader.getImage(ImageLoader.BUTTON_ROLLOVER);

        paintBuffer = null;
    }
}