File: LWButton.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (418 lines) | stat: -rw-r--r-- 12,122 bytes parent folder | download | duplicates (13)
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
/*
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package test.java.awt.event.helpers.lwcomponents;

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

/**
 * Lightweight <i>Button</i> component with some nice features. This
 * component provides the capabilities of Buttons, namely that you it
 * displays a label string and, when clicked, causes the
 * ActionListener method to be called.<p>
 *
 * The look of the button is a little unusual. There are three
 * rectangles drawn at the border that indicate various states
 * of the button.  These are (listed from outside in)<p>
 * <ol>
 * <li><b>Focus</b>: Indicates that the LWButton has the focus.
 * <li><b>Mouse Over</b>: Indicates that the mouse is over the component.
 * <li><b>Mouse Pressed</b>: Indicates that the mouse has been pressed.
 * </ol>
 *
 * In addition, when the button has been activated (mouse clicked or
 * via keyboard activation) the button flashes briefly.
 */

public class LWButton extends LWComponent {

  /*
   * The button's Label.
   * If Label is not specified it will default to "".
   * @serial
   * @see getLabel()
   * @see setLabel()
   */
  private String label;
  private boolean isInClick = false;

  private static final String base = "LWButton";
  private static int nameCounter = 0;

  private transient ActionListener actionListener;

  /*
   * The action to be performaed once a button has been
   * pressed.
   * actionCommand can be null.
   * @serial
   * @see getActionCommand()
   * @see setActionCommand()
   */
  String actionCommand;

  Color colMousePressed;

  public LWButton() { this(""); }

  public LWButton(String label) {
    this(label, Color.red, Color.green, Color.white);
  }

  /**
   * Initialize the LWButton, fully specifying all parameters.
   * @param label The string to display.
   * @param fgnd  The color to draw the label in.
   * @param bkgnd The color of the button itself.
   * @param mousePressed The Color of the MousePressed rectangle.
   */
  public LWButton(String label, Color fgnd, Color bkgnd, Color mousePressed) {
    super();
    this.label = label;
    setBackground(fgnd);
    setForeground(bkgnd);
    colMousePressed = mousePressed;
    setName(makeComponentName());

    enableEvents(  AWTEvent.MOUSE_EVENT_MASK
         | AWTEvent.KEY_EVENT_MASK
         | AWTEvent.ACTION_EVENT_MASK);
    setEnabled(true);
  }

  /**
   * Make the component flash briefly.
   */
  public void flash() {
    isInClick = true;
    repaint();

    class unClicker implements Runnable {
      @Override
      public void run() {
        try { Thread.sleep(100); } catch (InterruptedException ee) {}
        isInClick = false;
        repaint();
      }
    }
    try {
      unClicker uc = new unClicker();
      new Thread(uc).start();
    } catch (Exception e) {
      // In case we're in an applet and the security has not been
      // turned off (in which case we can't start a new thread)
      // we can catch that and set the flag back to how it should be.
      isInClick = false;
      repaint();
    }
  }

  /**
   * Set the MousePressed color (the color shown in the MousePressed rectangle
   * when the mouse is over the component).
   * @param c The color of the MousePressed rectangle.
   */
  public void setMousePressedColor(Color c) { colMousePressed = c; }

  /**
   * Get the MousePressed color.
   * @return The color of the MousePressed rectangle.
   */
  public Color getMousePressedColor() { return colMousePressed; }

  /**
   * Used to dispatch out the ActionEvent for a corresponding InputEvent.
   * @param e The InputEvent that is causing the ActionEvent dispatch.
   */
  private void sendActionEvent(InputEvent e) {

    int modifiers = e.getModifiers();
    int aModifiers = 0;

    if ((modifiers & MouseEvent.SHIFT_MASK) != 0) {
      aModifiers |= ActionEvent.SHIFT_MASK;
    }
    if ((modifiers & MouseEvent.CTRL_MASK) != 0) {
      aModifiers |= ActionEvent.CTRL_MASK;
    }
    if ((modifiers & MouseEvent.META_MASK) != 0) {
      aModifiers |= ActionEvent.META_MASK;
    }
    if ((modifiers & MouseEvent.ALT_MASK) != 0) {
      aModifiers |= ActionEvent.ALT_MASK;
    }

    ActionEvent ae = new ActionEvent(this,
                     ActionEvent.ACTION_PERFORMED,
                     actionCommand,
                     aModifiers);
    // XXX: What's the right way to send out the ActionEvent?
    //   My assumption was to put it into the system event queue
    //   and the it will be dispatched back into <i>processEvent</i>
    //   for us.  However this doesn't happen...?
    if (actionListener != null) {
      actionListener.actionPerformed(ae);
    }
    //Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ae);
  }

  /**
   * Set whether the component is enabled ({@code true}) or not.
   * @param enabled If {@code true}, the component is to be enabled.
   */
  @Override
  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);

    if (enabled) {
      enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
    } else {
      disableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }
    repaint(1);
  }

  /**
   * Indicates that LWButton component can receive focus.
   * @return  {@code true} if the LWButton component can receive focus
   */
  @Override
  public boolean isFocusTraversable() { return true; }

  /**
   * Construct a name for this component. Called by getName() when the
   * name is null.
   */
  String makeComponentName() {
    synchronized (getClass()) {
      return base + nameCounter++;
    }
  }

  /**
   * Handle painting the enabled version of the component.
   *
   * ASSUMES: g.color may be changed
   */
  @Override
  public void paint(Graphics g) {

    super.paint(g);
    restrictGraphicsToClientArea(g);

    Dimension dim = getClientSize();

    int s = Math.min(dim.width - 1, dim.height - 1);

    if (isInClick) {
      g.setColor(Color.white);
    } else {
      g.setColor(getBackground());
    }

    // In jdk 1.2 (pre-release) there was a bug using clearRect
    // to paint the background of a lightweight.
    //g.clearRect(loc.x, loc.y, dim.width, dim.height);
    g.fillRect(0, 0, dim.width, dim.height);

    if (mouseB1Pressed) {
      g.setColor(colMousePressed);
      //LWComponent.traceMsg("paint mousePressed " + this.toString());
      g.drawRect(1, 1, dim.width - 3, dim.height - 3);
    }

    Font f = getFont();
    if (f != null) {
      FontMetrics fm = getFontMetrics(f);
      g.setColor(getForeground());
      g.drawString(label,
                   s/2 - fm.stringWidth(label)/2,
                   s/2 + fm.getMaxDescent());
    }

    unrestrictGraphicsFromClientArea(g);
  }

  @Override
  public Dimension getPreferredSize() {
    Font f = getFont();
    if (f != null) {
      FontMetrics fm = getFontMetrics(f);
      int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
      return new Dimension(max, max);
    } else {
      return new Dimension(100, 100);
    }
  }

  @Override
  public Dimension getMinimumSize() {
    return getPreferredSize();
  }

  /**
   * Get the text displayed in the LWButton.
   * @return  the text displayed in the LWButton
   */
  public String getText() { return label; }

  /**
   * Set the text displayed in the LWButton.
   * @param s The text to be displayed.
   */
  public void setText(String s) {
    Font f = getFont();
    int oWidth = 0;
    int oHeight = 0;
    int nWidth = 0;
    int nHeight = 0;
    int invalidated = 0;
    FontMetrics fm = null;

    if (f != null) {
      fm = getFontMetrics(f);
      oWidth = fm.stringWidth(label);
      oHeight = fm.getHeight();
    }

    this.label = s;

    if (f != null) {
      nWidth = fm.stringWidth(label);
      nHeight = fm.getHeight();

      if ((nWidth > oWidth) || (nHeight > oHeight)) {
        invalidate();
        invalidated = 1;
      }
    }

    if (invalidated == 0) {
      repaint();
    }
  }

  /**
   * Set the command name for the action event fired
   * by this button. By default this action command is
   * set to match the label of the button.
   * @param     command  A string used to set the button's
   *                     action command.
   *            If the string is <code>null</code> then the action command
   *            is set to match the label of the button.
   * @see       java.awt.event.ActionEvent
   * @since     JDK1.1
   */
  public void setActionCommand(String command) {
    actionCommand = command;
  }

  /**
   * Returns the command name of the action event fired by this button.
   * If the command name is {@code null} (default) then this method
   * returns the label of the button.
   *
   * @return the command name of the action event fired by this button
   *         or the label of the button (in case of {@code null})
   */
  public String getActionCommand() {
    return (actionCommand == null? label : actionCommand);
  }

  /**
   * Add the specified action listener to receive action events from
   * this button. Action events occur when a user presses or releases
   * the mouse over this button.
   * @param         l the action listener.
   * @see           java.awt.event.ActionListener
   * @see           #removeActionListener
   * @since         JDK1.1
   */
  public synchronized void addActionListener(ActionListener l) {
    actionListener = AWTEventMulticaster.add(actionListener, l);
    enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  }

  /**
   * Remove the specified action listener so that it no longer
   * receives action events from this button. Action events occur
   * when a user presses or releases the mouse over this button.
   * @param         l     the action listener.
   * @see           java.awt.event.ActionListener
   * @see           #addActionListener
   * @since         JDK1.1
   */
  public synchronized void removeActionListener(ActionListener l) {
    actionListener = AWTEventMulticaster.remove(actionListener, l);
  }

  @Override
  protected void processKeyEvent(KeyEvent e) {
    super.processKeyEvent(e);
    if (!isEnabled()) { return; }
    switch(e.getID()) {
    case KeyEvent.KEY_TYPED:
      switch (e.getKeyCode()) {
        case KeyEvent.VK_ENTER:
        case KeyEvent.VK_SPACE:
          flash();
          sendActionEvent(e);
          break;
      }
      break;
    }
  }

  @Override
  protected void processMouseEvent(MouseEvent e) {
    super.processMouseEvent(e);
    if (!isEnabled()) { return; }
    switch(e.getID()) {
    case MouseEvent.MOUSE_PRESSED:
      requestFocus();
      repaint();
      break;
    case MouseEvent.MOUSE_RELEASED:
      repaint();
      break;
    case MouseEvent.MOUSE_CLICKED:
      if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
        flash();
        sendActionEvent(e);
      }
      break;
    }
  }

  /**
   * Returns the parameter string representing the state of this
   * button. This string is useful for debugging.
   * @return     the parameter string of this button.
   */
  @Override
  protected String paramString() {
    return super.paramString() + ", label = " + label;
  }

}