aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/desktoputil/plaf/SIPCommTextFieldUI.java
blob: ce4c56fc24d752372266c2f949745a05a27fecd4 (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.plaf;

import java.awt.*;
import java.awt.event.*;

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

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

/**
 * SIPCommTextFieldUI implementation.
 *
 * @author Yana Stamcheva
 * @author Adam Netocny
 */
public class SIPCommTextFieldUI
    extends MetalTextFieldUI
    implements Skinnable,
               MouseMotionListener,
               MouseListener
{
    /**
     * Indicates if the mouse is currently over the delete button.
     */
    protected boolean isDeleteMouseOver = false;

    /**
     * Indicates if the mouse is currently pressed on the delete button.
     */
    protected boolean isDeleteMousePressed = false;

    /**
     * The gap between the delete button and the text in the field.
     */
    protected static int BUTTON_GAP = 5;

    /**
     * The image of the delete text button.
     */
    private Image deleteButtonImg;

    /**
     * The rollover image of the delete text button.
     */
    private Image deleteButtonRolloverImg;

    /**
     * The image for the pressed state of the delete text button.
     */
    private Image deleteButtonPressedImg;

    /**
     * Indicates if the text field contains a
     * delete button allowing to delete all the content at once.
     */
    private boolean isDeleteButtonEnabled = false;

    /**
     * The delete text button shown on the right of the field.
     */
    protected SIPCommButton deleteButton;

    /**
     * Indicates if the delete icon is visible.
     */
    private boolean isDeleteIconVisible = false;

    /**
     * The start background gradient color.
     */
    private Color bgStartColor
        = new Color(DesktopUtilActivator.getResources().getColor(
            "service.gui.SEARCH_BACKGROUND"));

    /**
     * The end background gradient color.
     */
    private Color bgEndColor
        = new Color(DesktopUtilActivator.getResources().getColor(
            "service.gui.SEARCH_GRADIENT"));

    /**
     * The start background gradient color.
     */
    private Color bgBorderStartColor
        = new Color(DesktopUtilActivator.getResources().getColor(
            "service.gui.SEARCH_BORDER"));

    /**
     * The end background gradient color.
     */
    private Color bgBorderEndColor
        = new Color(DesktopUtilActivator.getResources().getColor(
            "service.gui.SEARCH_BORDER_GRADIENT"));

    /**
     * Creates a <tt>SIPCommTextFieldUI</tt>.
     */
    public SIPCommTextFieldUI()
    {
        loadSkin();
    }

    /**
     * Returns <code>true</code> if the delete buttons is enabled and false -
     * otherwise.
     * @return <code>true</code> if the delete buttons is enabled and false -
     * otherwise
     */
    public boolean isDeleteButtonEnabled()
    {
        return isDeleteButtonEnabled;
    }

    /**
     * Updates the isDeleteButtonEnabled field.
     *
     * @param isDeleteButtonEnabled indicates if the delete buttons is enabled
     * or not
     */
    public void setDeleteButtonEnabled(boolean isDeleteButtonEnabled)
    {
        this.isDeleteButtonEnabled = isDeleteButtonEnabled;
    }

    /**
     * Adds the custom mouse listeners defined in this class to the installed
     * listeners.
     */
    protected void installListeners()
    {
        super.installListeners();

        getComponent().addMouseListener(this);

        getComponent().addMouseMotionListener(this);
    }

    /**
     * Uninstalls listeners for the UI.
     */
    protected void uninstallListeners()
    {
        super.uninstallListeners();

        getComponent().removeMouseListener(this);
        getComponent().removeMouseMotionListener(this);
    }

    /**
     * Paints the background of the associated component.
     * @param g the <tt>Graphics</tt> object used for painting
     */
    protected void customPaintBackground(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g.create();

        try
        {
            String roundedSet = DesktopUtilActivator.getResources().
            getSettingsString(
                    "impl.gui.IS_SIP_COMM_TEXT_FIELD_ROUNDED");

            boolean isRounded = true;

            if(roundedSet != null)
            {
                isRounded = new Boolean(roundedSet)
                    .booleanValue();
            }

            AntialiasingManager.activateAntialiasing(g2);
            JTextComponent c = this.getComponent();

            GradientPaint bgGradientColor =
                new GradientPaint(  c.getWidth() / 2, 0,
                                    bgStartColor,
                                    c.getWidth() / 2, c.getHeight(),
                                    bgEndColor);

            g2.setPaint(bgGradientColor);

            if(isRounded)
            {
                g2.fillRoundRect(1, 1, c.getWidth() - 1, c.getHeight() - 1,
                        8, 8);
            }
            else
            {
                g2.fillRect(1, 1, c.getWidth() - 1, c.getHeight() - 1);
            }

            Rectangle deleteButtonRect = getDeleteButtonRect();

            if(deleteButtonRect != null)
            {
                int dx = deleteButtonRect.x;
                int dy = deleteButtonRect.y;

                if (c.getText() != null
                        && c.getText().length() > 0
                        && isDeleteButtonEnabled)
                {
                    if (isDeleteMousePressed)
                        g2.drawImage(deleteButtonPressedImg, dx, dy, null);
                    if (isDeleteMouseOver)
                        g2.drawImage(deleteButtonRolloverImg, dx, dy, null);
                    else
                        g2.drawImage(deleteButtonImg, dx, dy, null);

                    isDeleteIconVisible = true;
                }
                else
                    isDeleteIconVisible = false;
            }
            else
                isDeleteIconVisible = false;

            g2.setStroke(new BasicStroke(1f));
            GradientPaint bgBorderGradientColor
                = new GradientPaint(  c.getWidth() / 2, 0,
                                    bgBorderStartColor,
                                    c.getWidth() / 2, c.getHeight(),
                                    bgBorderEndColor);

            g2.setPaint(bgBorderGradientColor);

            if(isRounded)
            {
                g2.drawRoundRect(
                    0, 0, c.getWidth() - 1, c.getHeight() - 1, 8, 8);
            }
            else
            {
                g2.drawRect(0, 0, c.getWidth() - 1, c.getHeight() - 1);
            }
        }
        finally
        {
            g2.dispose();
        }
    }

    /**
     * Updates the delete icon, changes the cursor and deletes the content of
     * the associated text component when the mouse is pressed over the delete
     * icon.
     *
     * @param evt the mouse event that has prompted us to update the delete
     * icon.
     */
    protected void updateDeleteIcon(MouseEvent evt)
    {
        // If the component is null we have nothing more to do here. Fixes a
        // NullPointerException in getDeleteButtonRectangle().
        if (getComponent() == null)
            return;

        int x = evt.getX();
        int y = evt.getY();

        if (!isDeleteButtonEnabled)
            return;

        Rectangle deleteRect = getDeleteButtonRect();

        if (isDeleteIconVisible && deleteRect.contains(x, y))
        {
            if (evt.getID() == MouseEvent.MOUSE_PRESSED)
            {
                isDeleteMouseOver = false;
                isDeleteMousePressed = true;
            }
            else
            {
                isDeleteMouseOver = true;
                isDeleteMousePressed = false;
            }

            getComponent().setCursor(Cursor.getDefaultCursor());

            if (evt.getID() == MouseEvent.MOUSE_CLICKED)
                getComponent().setText("");
        }
        else
        {
            isDeleteMouseOver = false;
            isDeleteMousePressed = false;
        }

        getComponent().repaint();
    }

    /**
     * Calculates the delete button rectangle.
     *
     * @return the delete button rectangle
     */
    protected Rectangle getDeleteButtonRect()
    {
        JTextComponent c = getComponent();

        if(c == null)
            return null;

        Rectangle rect = c.getBounds();

        int dx = rect.width - deleteButton.getWidth() - BUTTON_GAP;
        int dy = rect.height / 2 - deleteButton.getHeight()/2;

        return new Rectangle(   dx,
                                dy,
                                deleteButton.getWidth(),
                                deleteButton.getHeight());
    }

    /**
     * If we are in the case of disabled delete button, we simply call the
     * parent implementation of this method, otherwise we recalculate the editor
     * rectangle in order to leave place for the delete button.
     * @return the visible editor rectangle
     */
    protected Rectangle getVisibleEditorRect()
    {
        if (!isDeleteIconVisible)
        {
            return super.getVisibleEditorRect();
        }

        JTextComponent c = getComponent();

        if(c == null)
            return null;

        Rectangle alloc = c.getBounds();

        if ((alloc.width > 0) && (alloc.height > 0))
        {
            alloc.x = alloc.y = 0;
            Insets insets = c.getInsets();
            alloc.x += insets.left;
            alloc.y += insets.top;
            alloc.width -= insets.left + insets.right
                + getDeleteButtonRect().getWidth();
            alloc.height -= insets.top + insets.bottom;
            return alloc;
        }

        return null;
    }

    /**
     * @param bgStartColor the bgStartColor to set
     */
    public void setBgStartColor(Color bgStartColor)
    {
        this.bgStartColor = bgStartColor;
    }

    /**
     * @param bgEndColor the bgEndColor to set
     */
    public void setBgEndColor(Color bgEndColor)
    {
        this.bgEndColor = bgEndColor;
    }

    /**
     * @param bgBorderStartColor the bgBorderStartColor to set
     */
    public void setBgBorderStartColor(Color bgBorderStartColor)
    {
        this.bgBorderStartColor = bgBorderStartColor;
    }

    /**
     * @param bgBorderEndColor the bgBorderEndColor to set
     */
    public void setBgBorderEndColor(Color bgBorderEndColor)
    {
        this.bgBorderEndColor = bgBorderEndColor;
    }

    /**
     * Reloads skin information.
     */
    public void loadSkin()
    {
        deleteButtonImg = DesktopUtilActivator.getResources()
            .getImage("service.gui.lookandfeel.DELETE_TEXT_ICON").getImage();

        deleteButtonRolloverImg = DesktopUtilActivator.getResources()
            .getImage("service.gui.lookandfeel.DELETE_TEXT_ROLLOVER_ICON")
                .getImage();

        deleteButtonPressedImg = DesktopUtilActivator.getResources()
            .getImage("service.gui.lookandfeel.DELETE_TEXT_PRESSED_ICON")
                .getImage();

        if(deleteButton != null)
        {
            deleteButton.setBackgroundImage(deleteButtonImg);
            deleteButton.setRolloverImage(deleteButtonRolloverImg);
            deleteButton.setPressedImage(deleteButtonPressedImg);
        }
        else
        {
            deleteButton = new SIPCommButton(
                    deleteButtonImg,
                    deleteButtonRolloverImg,
                    deleteButtonPressedImg,
                    null, null, null);
        }

        deleteButton.setSize (  deleteButtonImg.getWidth(null),
                                deleteButtonImg.getHeight(null));
    }

    /**
     * Updates the delete icon when the mouse was clicked.
     * @param e the <tt>MouseEvent</tt> that notified us of the click
     */
    public void mouseClicked(MouseEvent e)
    {
        updateDeleteIcon(e);
        updateCursor(e);
    }

    /**
     * Updates the delete icon when the mouse is enters the component area.
     * @param e the <tt>MouseEvent</tt> that notified us
     */
    public void mouseEntered(MouseEvent e)
    {
        updateDeleteIcon(e);
        updateCursor(e);
    }

    /**
     * Updates the delete icon when the mouse exits the component area.
     * @param e the <tt>MouseEvent</tt> that notified us
     */
    public void mouseExited(MouseEvent e)
    {
        updateDeleteIcon(e);
        updateCursor(e);
    }

    public void mousePressed(MouseEvent e)
    {
        updateDeleteIcon(e);
    }

    public void mouseReleased(MouseEvent e)
    {
        updateDeleteIcon(e);
    }

    /**
     * Updates the delete icon when the mouse is dragged over.
     * @param e the <tt>MouseEvent</tt> that notified us
     */
    public void mouseDragged(MouseEvent e)
    {
        updateDeleteIcon(e);
        updateCursor(e);
    }

    /**
     * Updates the delete icon when the mouse is moved over.
     * @param e the <tt>MouseEvent</tt> that notified us
     */
    public void mouseMoved(MouseEvent e)
    {
        updateDeleteIcon(e);
        updateCursor(e);
    }

    /**
     * Updates the cursor type depending on a given <tt>MouseEvent</tt>.
     *
     * @param mouseEvent the <tt>MouseEvent</tt> on which the cursor depends
     */
    private void updateCursor(MouseEvent mouseEvent)
    {
        Rectangle rect = getVisibleEditorRect();
        if (rect != null && rect.contains(mouseEvent.getPoint()))
        {
            getComponent().setCursor(
                Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
        }
    }

    /**
     * Creates a UI for a SIPCommTextFieldUI.
     *
     * @param c the text field
     * @return the UI
     */
    public static ComponentUI createUI(JComponent c)
    {
        return new SIPCommTextFieldUI();
    }
}