File: LWComponent.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 (464 lines) | stat: -rw-r--r-- 14,724 bytes parent folder | download | duplicates (16)
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
/*
 * 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.io.*;
import java.awt.*;
import java.awt.event.*;

/**
 * This is experimental - The idea is to subclass all the LW components
 * from LWComponent to provide for some common capabilities.  The main
 * capability to be provided is the status rectangles as done for LWButton.
 * In particular the Focus and MouseOver rectangles are generically
 * useful, while other rectangles might be useful to other components.<p>
 *
 * To implement that, here is the idea ... borrowed from Win32 ... Each
 * of the LW components has both a client and non-client region.  We
 * call paintNC to paint the non-client region (Focus and MouseOver
 * rectangles), and the subclass might be permitted to implement paintNC
 * but for now they aren't.<p>
 *
 * Then the paint{Enabled,Disabled} methods are called as appropriate.
 * Note that paintDisabled is implemented in LWComponent to call paintEnabled
 * then stipple over the top of it.<p>
 *
 * So it is paintEnabled that the component should implement.  This method
 * needs to know the dimensions of the client area (getClientRegion?) and
 * the Graphics needs to have it's clip region set appropriately.<p>
 *
 * <b>KVETCHING</b>: <i>Kvetch</i> is a Yiddish word which means, basically,
 * to complain very precisely.  The LWComponent family tracks various pieces
 * of information over time that are used to check closely for correct behavior
 * in some circumstances.  The method <i>kvetch</i> is where this code lives
 * and is intended to check a broad range of conditions.<p>
 *
 * To turn off specific kvetch's, one simply specifies a System property
 * as in this table:<p>
 *
 * <table border="1">
 * <tr><th>Property name</th><th>Value</th><th>Discussion</th></tr>
 * <tr>
 *    <th>javasoft.awtsqe.lw.IGNORE_FOCUS_KVETCH</th>
 *    <th>true or false</th>
 *    <td>Specify whether the <i>hasFocus</i> kvetch is checked.</td>
 * </tr>
 * </table><p>
 *
 * <b>XXX To implement</b> - specifying colors.  NCBackground,
 * FocusRectColor, MouseOverColor are the threee colors.  paintNC
 * fills the NC region with NCBackground, and then pains the two
 * colors as appropriate.  There needs to be methods to get/specify
 * these colors.<p>
 *
 * <b>XXX To implement</b> - Specifying the component name and toString().
 * The subclass should only give the base class name, and a method
 * in LWComponent should construct a name from that.  For toString()
 * there needs to be a small amount of infrastructure built.<p>
 */

public abstract class LWComponent extends Component {

  protected static Color ncBackgroundColor;
  protected static Color focusColor;
  protected static Color focusWrongColor;
  protected static Color mouseOverColor;

  static {
    ncBackgroundColor = Color.white;
    focusColor        = Color.black;
    focusWrongColor   = Color.magenta;
    mouseOverColor    = Color.blue;
  }

  /**
   * Flag indicating whether our records indicate that the component
   * should have focus.
   */
  protected boolean _shouldHaveFocus = false;
  protected boolean _shouldBeShowing = false;

  protected boolean mouseB1Pressed = false;
  protected boolean mouseB2Pressed = false;
  protected boolean mouseB3Pressed = false;
  protected boolean mouseInside    = false;

  protected static boolean tracingOn = false;
  protected static PrintStream traceOutput = null;

  // Uncommenting these lines turns on tracing for the package.
  //  static {
  //    tracingOn = true;
  //    traceOutput = System.err;
  //  }

  public LWComponent() {
    enableEvents(AWTEvent.MOUSE_EVENT_MASK
         /*| AWTEvent.MOUSE_MOTION_EVENT_MASK*/
           | AWTEvent.FOCUS_EVENT_MASK
           | AWTEvent.COMPONENT_EVENT_MASK);
  }

  /**
   * Print out an error message.
   * @param msg  the message
   */
  public static void errorMsg(String msg) {
    System.err.println("ERROR: " + msg);
  }

  /**
   * Print out a tracing message
   * @param msg  the message
   */
  public static void traceMsg(String msg) {
    if (LWComponent.tracingOn) {
      LWComponent.traceOutput.println(msg);
    }
  }

  /////////////////////////////////////////////
  /////// FLAGS FOR IGNORING KVETCH's /////////
  /////////////////////////////////////////////

  static boolean bIgnFocus = false;

  static {
    // Initialize the kvetch ignoring flags here.
    String ignFocus = System.getProperty("javasoft.awtsqe.lw.IGNORE_FOCUS_KVETCH",
                                         "false");
    bIgnFocus = ignFocus.trim().toLowerCase().equals("true");
  }

  /**
   * Check the <i>shoulds</i> and return a string indicating which
   * do not match the components actual state.
   *
   * @return  the string indicating which do not match the components actual state
   */
  public String kvetch() {
    String ret = this.toString();
    boolean errors = false;

    if (!bIgnFocus) {
      if (hasFocus()) {
        if (!shouldHaveFocus()) {
          ret += "\nERROR: hasFocus indicates we have Focus, when we shouldn't.";
          errors = true;
        }
      } else {
        if (shouldHaveFocus()) {
          ret += "\nERROR: (see bug#4233658) hasFocus does not indicate we have Focus, when we should.";
          errors = true;
        }
      }
    }

    if (errors) {
      return ret;
    } else {
      return null;
    }
  }

  /**
   * Check the <i>shoulds</i> and return a string indicating which
   * do not match the components actual state.  Prints the output
   * to the given PrintStream.
   * @param out The PrintStream to print to.
   */
  public void kvetch(PrintStream out) {
    if (out != null) {
      String s = kvetch();
      if (s != null) {
        LWComponent.errorMsg(s);
      }
    }
  }

  /**
   * Turn on tracing for the LWComponent family.
   * @param out  the output stream
   */
  public static void startTracing(PrintStream out) {
    tracingOn = true;
    traceOutput = out;
  }

  /**
   * Turn off tracing for the LWComponent family.
   */
  public static void stopTracing() { tracingOn = false; traceOutput = null; }

  /**
   * Indicate whether it is believed the component should have focus.
   * @return {@code true} if the component should have focus
   */
  public boolean shouldHaveFocus() { return _shouldHaveFocus; }

  /**
   * Indicate whether it is believed the component should be showing.
   * @return  {@code true} if the component should be showing
   */
  public boolean shouldBeShowing() { return _shouldBeShowing; }

  @Override
  protected void processFocusEvent(FocusEvent e) {
    super.processFocusEvent(e);
    LWComponent.traceMsg("processFocusEvent " + e.toString());
    switch (e.getID()) {
    case FocusEvent.FOCUS_GAINED:
      _shouldHaveFocus = true;
      repaint();
      break;
    case FocusEvent.FOCUS_LOST:
      _shouldHaveFocus = false;
      repaint();
      break;
    }
  }

  @Override
  protected void processComponentEvent(ComponentEvent e) {
    super.processComponentEvent(e);
    LWComponent.traceMsg("processComponentEvent " + e.toString());
    switch (e.getID()) {
      case ComponentEvent.COMPONENT_MOVED:   break;
      case ComponentEvent.COMPONENT_RESIZED: break;
      case ComponentEvent.COMPONENT_SHOWN:   _shouldBeShowing = true;  break;
      case ComponentEvent.COMPONENT_HIDDEN:  _shouldBeShowing = false; break;
    }
  }

  @Override
  protected void processMouseEvent(MouseEvent e) {
    int mod = e.getModifiers();
    super.processMouseEvent(e);
    LWComponent.traceMsg("processMouseEvent " + e.toString());
    switch (e.getID()) {
    case MouseEvent.MOUSE_PRESSED:
      if ((mod & MouseEvent.BUTTON1_MASK) != 0) {
        if (mouseB1Pressed) {
          errorMsg("ERROR: MOUSE_PRESSED for B1 when already pressed, on "
              + this.toString());
        }
        mouseB1Pressed = true;
        break;
      }
      if ((mod & MouseEvent.BUTTON2_MASK) != 0) {
        if (mouseB2Pressed) {
          errorMsg("ERROR: MOUSE_PRESSED for B2 when already pressed, on "
              + this.toString());
        }
        mouseB2Pressed = true;
        break;
      }
      if ((mod & MouseEvent.BUTTON3_MASK) != 0) {
        if (mouseB3Pressed) {
          errorMsg("ERROR: MOUSE_PRESSED for B3 when already pressed, on "
              + this.toString());
        }
        mouseB3Pressed = true;
        break;
      }
      repaint();
      break;
    case MouseEvent.MOUSE_RELEASED:
      if ((mod & MouseEvent.BUTTON1_MASK) != 0) {
        if (!mouseB1Pressed) {
          errorMsg("ERROR: MOUSE_RELEASED for B1 when not pressed, on "
              + this.toString());
        }
        mouseB1Pressed = false;
        break;
      }
      if ((mod & MouseEvent.BUTTON2_MASK) != 0) {
        if (!mouseB2Pressed) {
          errorMsg("ERROR: MOUSE_RELEASED for B2 when not pressed, on "
              + this.toString());
        }
        mouseB2Pressed = false;
        break;
      }
      if ((mod & MouseEvent.BUTTON3_MASK) != 0) {
        if (!mouseB3Pressed) {
          errorMsg("ERROR: MOUSE_RELEASED for B3 when not pressed, on "
              + this.toString());
        }
        mouseB3Pressed = false;
        break;
      }
      repaint();
      break;
    case MouseEvent.MOUSE_CLICKED:
      break;
    case MouseEvent.MOUSE_ENTERED:
      if (mouseInside) {
        errorMsg("ERROR: MOUSE_ENTERED when mouse already inside component, on "
            + this.toString());
      }
      mouseInside = true;
      repaint();
      break;
    case MouseEvent.MOUSE_EXITED:
      if (!mouseInside) {
        errorMsg("ERROR: MOUSE_EXITED when mouse not inside component, on "
            + this.toString());
      }
      mouseInside = false;
      repaint();
      break;
    case MouseEvent.MOUSE_MOVED:
      break;
    case MouseEvent.MOUSE_DRAGGED:
      break;
    }
  }

  public Point getClientLocation() {
    return new Point(5, 5);
  }

  public Dimension getClientSize() {
    Dimension dim = getSize();
    dim.width -= 10;
    dim.height -= 10;
    return dim;
  }

  public Rectangle getClientBounds() {
    Dimension dim = getClientSize();
    return new Rectangle(5, 5, dim.width, dim.height);
  }

  public int getClientX() { return 5; }
  public int getClientY() { return 5; }

  /**
   * Set the color used for painting the non-client area of the component.
   * The default for this is Color.white.
   *
   * @param c The new color to use.
   */
  public void setNonClientColor(Color c) {
    LWComponent.ncBackgroundColor = c;
  }

  /**
   * Handle painting for the component.
   */
  @Override
  public void paint(Graphics g) {
    Dimension dim = getSize();

    kvetch(System.err);

    Color saveColor = g.getColor();
    super.paint(g);

    // ------------------- Paint the background -----------------

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

    // ------------------- Paint the non-client area ------------

    g.setColor(ncBackgroundColor);
    //         x              y                width      height
    g.fillRect(0,             0,               dim.width, 5);
    g.fillRect(0,             5,               5,         dim.height - 10);
    g.fillRect(dim.width - 5, 5,               5,         dim.height - 10);
    g.fillRect(0,             dim.height - 5,  dim.width, 5);

    if (shouldHaveFocus() || hasFocus()) {
      g.setColor(shouldHaveFocus() && hasFocus()
         ? focusColor
         : focusWrongColor);
      g.drawRect(1, 1, dim.width - 3, dim.height - 3);
    }

    if (mouseInside) {
      g.setColor(mouseOverColor);
      g.drawRect(3, 3, dim.width - 7, dim.height - 7);
    }

    // ------------------- Paint disabledness, if true -----------

    if (!isEnabled()) {
      g.setColor(getBackground());
      Dimension size = getSize();
      int borderThickness = 0;
      int startX = borderThickness;
      int startY = borderThickness;
      int endX = startX + size.width  - 2 * borderThickness - 2;
      int endY = startY + size.height - 2 * borderThickness - 2;
      int x, y;
      for (y = startY; y <= endY; y += 1) {
        for (x = startX + (y % 2); x <= endX; x += 2) {
          g.fillRect(x, y, 1, 1);
        } // x
      } // y
    }

    g.setColor(saveColor);
  }

  /**
   * Restricts the Graphics to be within the "client area" of the
   * component.  Recall that the LWComponent series of components has
   * a "non-client area" of 5 pixels wide in which it draws two
   * status rectangles showing mouse-over and has-focus status. <p>
   *
   * Child classes of LWComponent are to call {@code restrictGraphicsToClientArea}
   * at the beginning of their {@code paint} method, and then call
   * {@code unrestrictGraphicsFromClientArea} afterwards.<p>
   *
   * In order to make those paint methods as convenient as possible, these
   * two methods make it appear as if the Graphics available to the
   * component is slightly smaller than it really is, by the amount
   * used in the non-client area (5 pixel wide border).<p>
   *
   * @param g The Graphics to restrict.
   */
  public void restrictGraphicsToClientArea(Graphics g) {
    Dimension dim = getSize();
    g.translate(5, 5);
    g.setClip(0, 0, dim.width - 10, dim.height - 10);
  }

  /**
   * Undo the restriction done in restrictGraphicsToClientArea.
   *
   * @param g The Graphics to unrestrict.
   */
  public void unrestrictGraphicsFromClientArea(Graphics g) {
    g.translate(-5, -5);
    Dimension dim = getSize();
    g.setClip(0, 0, dim.width, dim.height);
  }

}