aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.7/libjava/classpath/examples/gnu/classpath/examples/swing/TextFieldDemo.java
blob: e630228804f382028910f2aaeaf8dbe7b6a34b09 (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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* TextFieldDemo.java -- An example showing various textfields in Swing.
   Copyright (C) 2005, 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath examples.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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 for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/


package gnu.classpath.examples.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
import javax.swing.text.LayeredHighlighter.LayerPainter;

/**
 * A simple textfield demo showing various textfields in different states.
 */
public class TextFieldDemo
  extends JPanel
  implements ActionListener
{

  /**
   * A custom caret for demonstration purposes. This class is inspired by the
   * CornerCaret from the OReilly Swing book.
   *
   * @author Roman Kennke (kennke@aicas.com)
   */
  static class CornerCaret extends DefaultCaret
  {
    public CornerCaret()
    {
      super();
      setBlinkRate(500);
    }

    protected synchronized void damage(Rectangle r)
    {
      if (r == null) return;
      x = r.x;
      y = r.y + (r.height * 4 / 5 - 3);
      width = 5;
      height = 5;
      repaint();
    }

    public void paint(Graphics g)
    {
      JTextComponent comp = getComponent();
      if (comp == null) return;
      int dot = getDot();
      Rectangle r = null;
      try
        {
          r = comp.modelToView(dot);
        }
      catch (BadLocationException e)
        {
          return;
        }
      if (r == null) return;
      int dist = r.height * 4 / 5 - 3;
      if ((x != r.x) || (y != r.y + dist))
        {
          repaint();
          x = r.x;
          y = r.y + dist;
          width = 5;
          height = 5;
        }
      if (isVisible())
        {
          g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4);
          g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4);
        }
    }
  }

  static class DemoHighlightPainter
  extends LayerPainter
  {

    static DemoHighlightPainter INSTANCE = new DemoHighlightPainter();


    static Color[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
                         Color.MAGENTA, Color.ORANGE, Color.PINK,
                         Color.ORANGE, Color.RED, Color.BLUE, Color.YELLOW };


    public DemoHighlightPainter()
    {
      super();
    }

    private void paintHighlight(Graphics g, Rectangle rect)
    {
      g.fillRect(rect.x, rect.y, rect.width, rect.height);
    }

    public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent t)
    {
      try
        {

          for (int i = p0; i < p1; i++)
            {
              Rectangle r = t.modelToView(i);
              Point l1 = t.modelToView(i + 1).getLocation();

              g.setColor(colors[(int) (Math.random() * colors.length)]);
              g.fillOval(r.x, r.y, l1.x - r.x, r.height);
            }
        }
      catch (BadLocationException ble)
        {
        }
    }

    public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
                            JTextComponent c, View view)
    {
      paint(g, p0, p1, bounds, c);

      return bounds;
    }

  }

  /**
   * The left aligned textfields and state buttons.
   */
  Compound compound1;

  /**
   * The right aligned textfields and state buttons.
   */
  Compound compound2;

  /**
   * The centered textfields and state buttons.
   */
  Compound compound3;

  /**
   * The custom colored textfields and state buttons.
   */
  Compound compound4;
  Compound compound5;

  /**
   * Some miscellaneous textfield demos.
   */
  Compound compound6;

  /**
   * Some textfields with custom borders.
   */
  Compound compound7;

  /**
   * Creates a new demo instance.
   */
  public TextFieldDemo()
  {
    super();
    createContent();
  }

  /**
   * When the demo is run independently, the frame is displayed, so we should
   * initialise the content panel (including the demo content and a close
   * button).  But when the demo is run as part of the Swing activity board,
   * only the demo content panel is used, the frame itself is never displayed,
   * so we can avoid this step.
   */
  void initFrameContent()
  {
    JPanel closePanel = new JPanel();
    JButton closeButton = new JButton("Close");
    closeButton.setActionCommand("CLOSE");
    closeButton.addActionListener(this);
    closePanel.add(closeButton);
    add(closePanel, BorderLayout.SOUTH);
  }

  /**
   * Returns a panel with the demo content.  The panel
   * uses a BorderLayout(), and the BorderLayout.SOUTH area
   * is empty, to allow callers to add controls to the
   * bottom of the panel if they want to (a close button is
   * added if this demo is being run as a standalone demo).
   */
  private void createContent()
  {
    setLayout(new BorderLayout());
    JPanel panel = new JPanel(new GridLayout(7, 1));
    panel.add(createLeftAlignedPanel());
    panel.add(createRightAlignedPanel());
    panel.add(createCenteredPanel());
    panel.add(createCustomColorPanel1());
    panel.add(createCustomColorPanel2());
    panel.add(createCustomBordersPanel());
    panel.add(createMiscPanel());

    // Put everything in a scroll pane to make it neccessary
    // to reach the bottom inner panels if the screen is to small.
    add(new JScrollPane(panel));
  }

  private JPanel createLeftAlignedPanel()
  {
    compound1 = createTextFieldCompound("Left aligned", 1);

    compound1.setupTextfields("Hello World!",
                             JTextField.LEFT,
                             new Font[] { new Font("Dialog", Font.PLAIN, 8),
                                          new Font("Dialog", Font.ITALIC, 12),
                                          new Font("Dialog", Font.BOLD, 14)
                             });

    return compound1.panel;
  }

  private Compound createTextFieldCompound(String title, int actionCommandNo)
  {
    Compound compound = new Compound();
    compound.panel = new JPanel(new BorderLayout());
    compound.panel.setBorder(BorderFactory.createTitledBorder(title));

    compound.textFieldPanel = new JPanel();
    compound.textFieldPanel.setLayout(new BoxLayout(compound.textFieldPanel, BoxLayout.X_AXIS));

    compound.panel.add(compound.textFieldPanel);

    JPanel statePanel = new JPanel();
    statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
    statePanel.add(Box.createVerticalGlue());
    compound.enabled = new JCheckBox("enabled");
    compound.enabled.setSelected(true);
    compound.enabled.addActionListener(this);
    compound.enabled.setActionCommand("ENABLED" + actionCommandNo);
    statePanel.add(compound.enabled);
    compound.editable = new JCheckBox("editable");
    compound.editable.setSelected(true);
    compound.editable.addActionListener(this);
    compound.editable.setActionCommand("EDITABLE" + actionCommandNo);
    statePanel.add(compound.editable);
    statePanel.add(Box.createVerticalGlue());
    compound.panel.add(statePanel, BorderLayout.EAST);

    return compound;
  }

  private JPanel createRightAlignedPanel()
  {
    compound2 = createTextFieldCompound("Right aligned", 2);

    compound2.setupTextfields("Hello World!",
                              JTextField.RIGHT,
                              new Font[] { new Font("Dialog", Font.PLAIN, 8),
                                           new Font("Dialog", Font.ITALIC, 12),
                                           new Font("Dialog", Font.BOLD, 14)
                              });

    return compound2.panel;
  }

  private JPanel createCenteredPanel()
  {
    compound3 = createTextFieldCompound("Centered", 3);

    compound3.setupTextfields("Hello World!",
                              JTextField.CENTER,
                              new Font[] { new Font("Dialog", Font.PLAIN, 8),
                                           new Font("Dialog", Font.ITALIC, 12),
                                           new Font("Dialog", Font.BOLD, 14)
                              });

    return compound3.panel;
  }

  private JPanel createCustomColorPanel1()
  {
    compound4 = createTextFieldCompound("Custom colors I", 4);

    compound4.textfield1 = new JTextField("custom foreground");
    compound4.textfield1.setForeground(Color.RED);
    compound4.textFieldPanel.add(compound4.textfield1);

    compound4.textfield2 = new JTextField("custom background");
    compound4.textfield2.setBackground(Color.YELLOW);
    compound4.textFieldPanel.add(compound4.textfield2);

    compound4.textfield3 = new JTextField("custom foreground and background");
    compound4.textfield3.setForeground(Color.RED);
    compound4.textfield3.setBackground(Color.YELLOW);
    compound4.textFieldPanel.add(compound4.textfield3);

    return compound4.panel;

  }

  private JPanel createCustomColorPanel2()
  {
    compound5 = createTextFieldCompound("Custom colors II", 5);

    compound5.textfield1 = new JTextField("custom disabled textcolor");
    compound5.textfield1.setDisabledTextColor(Color.BLUE);
    compound5.textFieldPanel.add(compound5.textfield1);

    compound5.textfield2 = new JTextField("custom selected text color");
    compound5.textfield2.setSelectedTextColor(Color.RED);
    compound5.textFieldPanel.add(compound5.textfield2);

    compound5.textfield3 = new JTextField("custom selection color");
    compound5.textfield3.setSelectionColor(Color.BLACK);
    compound5.textFieldPanel.add(compound5.textfield3);

    return compound5.panel;

  }

  private JPanel createMiscPanel()
  {
    compound6 = createTextFieldCompound("Miscellaneous", 6);

    compound6.textfield1 = new JTextField("Custom Caret");
    compound6.textfield1.setCaret(new CornerCaret());
    compound6.textFieldPanel.add(compound6.textfield1);

    compound6.textfield2 = new JTextField("Custom Caret color");
    compound6.textfield2.setForeground(Color.LIGHT_GRAY);
    compound6.textfield2.setBackground(Color.BLACK);
    compound6.textfield2.setSelectedTextColor(Color.BLACK);
    compound6.textfield2.setCaretColor(Color.WHITE);
    compound6.textfield2.setSelectionColor(Color.DARK_GRAY);
    compound6.textFieldPanel.add(compound6.textfield2);

    compound6.textfield3 = new JTextField("Custom highlighter");
    compound6.textfield3.setCaret(new DefaultCaret()
    {
      public Highlighter.HighlightPainter getSelectionPainter()
      {
        return DemoHighlightPainter.INSTANCE;
      }
    });
    compound6.textFieldPanel.add(compound6.textfield3);

    return compound6.panel;
  }

  private JPanel createCustomBordersPanel()
  {
    compound7 = createTextFieldCompound("Custom borders", 7);

    compound7.textfield1 = new JTextField("red 5 pixel lineborder");
    compound7.textfield1.setBorder(new LineBorder(Color.RED, 5));
    compound7.textFieldPanel.add(compound7.textfield1);

    compound7.textfield2 = new JTextField("complex irregular border");

    CompoundBorder innerCompound = new CompoundBorder(new EmptyBorder(5, 40, 15, 10), new LineBorder(Color.BLACK));
    CompoundBorder outerCompound = new CompoundBorder(new LineBorder(Color.BLACK), innerCompound);
    compound7.textfield2.setBorder(outerCompound);
    compound7.textFieldPanel.add(compound7.textfield2);

    compound7.textfield3 = new JTextField("a titled border", 10);
    compound7.textfield3.setBorder(new TitledBorder(null, "Freak Out Border", TitledBorder.CENTER, TitledBorder.LEFT));
    compound7.textFieldPanel.add(compound7.textfield3);

    return compound7.panel;
  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getActionCommand().equals("CLOSE"))
      {
        System.exit(0);
      }
    else if (e.getActionCommand().equals("ENABLED1"))
      {
        boolean enabled = compound1.enabled.isSelected();
        compound1.textfield1.setEnabled(enabled);
        compound1.textfield2.setEnabled(enabled);
        compound1.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE1"))
      {
        boolean editable = compound1.editable.isSelected();
        compound1.textfield1.setEditable(editable);
        compound1.textfield2.setEditable(editable);
        compound1.textfield3.setEditable(editable);
      }
    else if (e.getActionCommand().equals("ENABLED2"))
      {
        boolean enabled = compound2.enabled.isSelected();
        compound2.textfield1.setEnabled(enabled);
        compound2.textfield2.setEnabled(enabled);
        compound2.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE2"))
      {
        boolean editable = compound2.editable.isSelected();
        compound2.textfield1.setEditable(editable);
        compound2.textfield2.setEditable(editable);
        compound2.textfield3.setEditable(editable);
      }
    else if (e.getActionCommand().equals("ENABLED3"))
      {
        boolean enabled = compound3.enabled.isSelected();
        compound3.textfield1.setEnabled(enabled);
        compound3.textfield2.setEnabled(enabled);
        compound3.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE3"))
      {
        boolean editable = compound3.editable.isSelected();
        compound3.textfield1.setEditable(editable);
        compound3.textfield2.setEditable(editable);
        compound3.textfield3.setEditable(editable);
      }
    else if (e.getActionCommand().equals("ENABLED4"))
      {
        boolean enabled = compound4.enabled.isSelected();
        compound4.textfield1.setEnabled(enabled);
        compound4.textfield2.setEnabled(enabled);
        compound4.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE4"))
      {
        boolean editable = compound4.editable.isSelected();
        compound4.textfield1.setEditable(editable);
        compound4.textfield2.setEditable(editable);
        compound4.textfield3.setEditable(editable);
      }
    else if (e.getActionCommand().equals("ENABLED5"))
      {
        boolean enabled = compound5.enabled.isSelected();
        compound5.textfield1.setEnabled(enabled);
        compound5.textfield2.setEnabled(enabled);
        compound5.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE5"))
      {
        boolean editable = compound5.editable.isSelected();
        compound5.textfield1.setEditable(editable);
        compound5.textfield2.setEditable(editable);
        compound5.textfield3.setEditable(editable);
      }
    else if (e.getActionCommand().equals("ENABLED6"))
      {
        boolean enabled = compound6.enabled.isSelected();
        compound6.textfield1.setEnabled(enabled);
        compound6.textfield2.setEnabled(enabled);
        compound6.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE6"))
      {
        boolean editable = compound6.editable.isSelected();
        compound6.textfield1.setEditable(editable);
        compound6.textfield2.setEditable(editable);
        compound6.textfield3.setEditable(editable);
      }
    else if (e.getActionCommand().equals("ENABLED7"))
      {
        boolean enabled = compound7.enabled.isSelected();
        compound7.textfield1.setEnabled(enabled);
        compound7.textfield2.setEnabled(enabled);
        compound7.textfield3.setEnabled(enabled);
      }
    else if (e.getActionCommand().equals("EDITABLE7"))
      {
        boolean editable = compound7.editable.isSelected();
        compound7.textfield1.setEditable(editable);
        compound7.textfield2.setEditable(editable);
        compound7.textfield3.setEditable(editable);
      }
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater
    (new Runnable()
     {
       public void run()
       {
         TextFieldDemo app = new TextFieldDemo();
         app.initFrameContent();
         JFrame frame = new JFrame("TextField demo");
         frame.getContentPane().add(app);
         frame.pack();
         frame.setVisible(true);
       }
     });
  }

  /**
   * Returns a DemoFactory that creates a TextFieldDemo.
   *
   * @return a DemoFactory that creates a TextFieldDemo
   */
  public static DemoFactory createDemoFactory()
  {
    return new DemoFactory()
    {
      public JComponent createDemo()
      {
        return new TextFieldDemo();
      }
    };
  }

  static class Compound
  {
    JTextField textfield1;
    JTextField textfield2;
    JTextField textfield3;
    JCheckBox enabled;
    JCheckBox editable;
    JPanel textFieldPanel;
    JPanel panel;

    /** Creates and initializes the textfields with the same text and
     * alignment but with a different font.
     *
     * @param title The text for the textfields.
     * @param align The alignment for the textfields.
     * @param fonts The fonts to be used for the textfields.
     */
    void setupTextfields(String title, int align, Font[] fonts)
    {
      textfield1 = new JTextField(title);
      textfield1.setHorizontalAlignment(align);
      textfield1.setFont(fonts[0]);
      textFieldPanel.add(textfield1);

      textfield2 = new JTextField(title);
      textfield2.setHorizontalAlignment(align);
      textfield2.setFont(fonts[1]);
      textFieldPanel.add(textfield2);

      textfield3 = new JTextField(title);
      textfield3.setHorizontalAlignment(align);
      textfield3.setFont(fonts[2]);
      textFieldPanel.add(textfield3);
    }

  }

}