aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/lookandfeel/SIPCommTextFieldUI.java
blob: 2fbf4b4e534043c293f4b417ad643d9e4ca91d06 (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
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.lookandfeel;

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.impl.gui.customcontrols.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.util.swing.*;

/**
 * SIPCommTextFieldUI implementation.
 *
 * @author Yana Stamcheva
 */
public class SIPCommTextFieldUI
    extends MetalTextFieldUI
{
    private boolean mouseOver = false;

    private static int BUTTON_GAP = 5;

    private Image deleteButtonImg;

    private Image deleteButtonRolloverImg;

    private boolean isDeleteButtonEnabled = false;

    private SIPCommButton deleteButton;

    /**
     * Creates a <tt>SIPCommTextFieldUI</tt>.
     */
    public SIPCommTextFieldUI()
    {
        deleteButtonImg
            = ImageLoader.getImage(ImageLoader.DELETE_TEXT_ICON);

        deleteButtonRolloverImg
            = ImageLoader.getImage(ImageLoader.DELETE_TEXT_ROLLOVER_ICON);

        deleteButton = new SIPCommButton(   deleteButtonImg,
                                            deleteButtonRolloverImg);

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

    /**
     * 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(
            new TextFieldMouseListener());

        getComponent().addMouseMotionListener(
            new TextFieldMouseMotionListener());
    }

    /**
     * Creates the UI.
     *
     * @param c the component associated with this UI implementation.
     * @return an instance of this UI implementation
     */
    public static ComponentUI createUI(JComponent c)
    {
        return new SIPCommTextFieldUI();
    }

    /**
     * Implements parent paintSafely method and enables antialiasing.
     */
    protected void paintSafely(Graphics g)
    {
        AntialiasingManager.activateAntialiasing(g);
        super.paintSafely(g);
    }

    /**
     * Paints the background of the associated component.
     */
    protected void paintBackground(Graphics g)
    {
        AntialiasingManager.activateAntialiasing(g);
        JTextComponent c = this.getComponent();
        g.setColor(c.getBackground());
        g.fillRoundRect(1, 1, c.getWidth() - 2, c.getHeight() - 2, 5, 5);

        int dx = c.getX() + c.getWidth() - deleteButton.getWidth() - BUTTON_GAP;
        int dy = (c.getY() + c.getHeight()) / 2 - deleteButton.getHeight()/2;

        if (c.getText() != null
                && c.getText().length() > 0
                && isDeleteButtonEnabled)
        {
            if (mouseOver)
                g.drawImage(deleteButtonRolloverImg, dx, dy + 1, null);
            else
                g.drawImage(deleteButtonImg, dx, dy + 1, null);
        }
    }

    /**
     * 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)
    {
        int x = evt.getX();
        int y = evt.getY();
        if (!isDeleteButtonEnabled)
            return;

        Rectangle deleteRect = getDeleteButtonRect();

        if (deleteRect.contains(x, y))
        {
            mouseOver = true;
            getComponent().setCursor(Cursor.getDefaultCursor());

            if (evt.getID() == MouseEvent.MOUSE_CLICKED)
                getComponent().setText("");
        }
        else
        {
            mouseOver = false;
            getComponent().setCursor(
                Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
        }

        getComponent().repaint();
    }

    /**
     * Calculates the delete button rectangle.
     *
     * @return the delete button rectangle
     */
    protected Rectangle getDeleteButtonRect()
    {
        Rectangle rect = getVisibleEditorRect();

        int dx = rect.x + rect.width;
        int dy = (rect.y + 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.
     */
    protected Rectangle getVisibleEditorRect()
    {
        if (!isDeleteButtonEnabled)
        {
            return super.getVisibleEditorRect();
        }

        JTextComponent c = getComponent();

        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
                + deleteButton.getWidth();
            alloc.height -= insets.top + insets.bottom;
            return alloc;
        }

        return null;
    }

    /**
     * The <tt>MouseListener</tt> that listens for mouse events in order to
     * update the delete icon.
     */
    protected class TextFieldMouseListener implements MouseListener
    {
        public void mouseClicked(MouseEvent e)
        {
            updateDeleteIcon(e);
        }

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

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

        public void mousePressed(MouseEvent e)
        {
        }

        public void mouseReleased(MouseEvent e)
        {
        }
    }

    /**
     * The <tt>MouseMotionListener</tt> that listens for mouse events in order
     * to update the delete icon.
     */
    protected class TextFieldMouseMotionListener implements MouseMotionListener
    {
        public void mouseDragged(MouseEvent e)
        {
            updateDeleteIcon(e);
        }

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