OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [swing/] [TextAreaDemo.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* TextAreaDemo.java -- An example showing various textareas in Swing.
2
 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3
 
4
 This file is part of GNU Classpath examples.
5
 
6
 GNU Classpath is free software; you can redistribute it and/or modify
7
 it under the terms of the GNU General Public License as published by
8
 the Free Software Foundation; either version 2, or (at your option)
9
 any later version.
10
 
11
 GNU Classpath is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 General Public License for more details.
15
 
16
 You should have received a copy of the GNU General Public License
17
 along with GNU Classpath; see the file COPYING.  If not, write to the
18
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 02110-1301 USA.
20
 */
21
 
22
 
23
package gnu.classpath.examples.swing;
24
 
25
import java.awt.BorderLayout;
26
import java.awt.Color;
27
import java.awt.Font;
28
import java.awt.Graphics;
29
import java.awt.GridLayout;
30
import java.awt.Point;
31
import java.awt.Rectangle;
32
import java.awt.Shape;
33
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionListener;
35
 
36
import javax.swing.BorderFactory;
37
import javax.swing.Box;
38
import javax.swing.BoxLayout;
39
import javax.swing.JButton;
40
import javax.swing.JCheckBox;
41
import javax.swing.JComponent;
42
import javax.swing.JFrame;
43
import javax.swing.JPanel;
44
import javax.swing.JScrollPane;
45
import javax.swing.JTabbedPane;
46
import javax.swing.JTextArea;
47
import javax.swing.SwingUtilities;
48
import javax.swing.text.BadLocationException;
49
import javax.swing.text.DefaultCaret;
50
import javax.swing.text.Highlighter;
51
import javax.swing.text.JTextComponent;
52
import javax.swing.text.View;
53
import javax.swing.text.LayeredHighlighter.LayerPainter;
54
 
55
/**
56
 * A simple textare demo showing various textareas in different states.
57
 */
58
public class TextAreaDemo
59
    extends JPanel
60
    implements ActionListener
61
{
62
 
63
  /**
64
   * A custom caret for demonstration purposes. This class is inspired by the
65
   * CornerCaret from the OReilly Swing book.
66
   *
67
   * @author Roman Kennke (kennke@aicas.com)
68
   */
69
  static class CornerCaret
70
      extends DefaultCaret
71
  {
72
    public CornerCaret()
73
    {
74
      super();
75
      setBlinkRate(500);
76
    }
77
 
78
    protected synchronized void damage(Rectangle r)
79
    {
80
      if (r == null)
81
        return;
82
      x = r.x;
83
      y = r.y + (r.height * 4 / 5 - 3);
84
      width = 5;
85
      height = 5;
86
      repaint();
87
    }
88
 
89
    public void paint(Graphics g)
90
    {
91
      JTextComponent comp = getComponent();
92
      if (comp == null)
93
        return;
94
      int dot = getDot();
95
      Rectangle r = null;
96
      try
97
        {
98
          r = comp.modelToView(dot);
99
        }
100
      catch (BadLocationException e)
101
        {
102
          return;
103
        }
104
      if (r == null)
105
        return;
106
      int dist = r.height * 4 / 5 - 3;
107
      if ((x != r.x) || (y != r.y + dist))
108
        {
109
          repaint();
110
          x = r.x;
111
          y = r.y + dist;
112
          width = 5;
113
          height = 5;
114
        }
115
      if (isVisible())
116
        {
117
          g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4);
118
          g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4);
119
        }
120
    }
121
  }
122
 
123
  static class DemoHighlightPainter
124
      extends LayerPainter
125
  {
126
    static DemoHighlightPainter INSTANCE = new DemoHighlightPainter();
127
 
128
    static Color[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
129
                             Color.MAGENTA, Color.ORANGE, Color.PINK,
130
                             Color.ORANGE, Color.RED, Color.BLUE, Color.YELLOW };
131
 
132
    public DemoHighlightPainter()
133
    {
134
      super();
135
    }
136
 
137
    private void paintHighlight(Graphics g, Rectangle rect)
138
    {
139
      g.fillRect(rect.x, rect.y, rect.width, rect.height);
140
    }
141
 
142
    public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent t)
143
    {
144
      try
145
        {
146
 
147
          for (int i = p0; i < p1; i++)
148
            {
149
              Rectangle r = t.modelToView(i);
150
              Point l1 = t.modelToView(i + 1).getLocation();
151
 
152
              g.setColor(colors[(int) (Math.random() * colors.length)]);
153
              g.fillOval(r.x, r.y, l1.x - r.x, r.height);
154
            }
155
        }
156
      catch (BadLocationException ble)
157
        {
158
        }
159
 
160
    }
161
 
162
    public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
163
                            JTextComponent c, View view)
164
    {
165
      paint(g, p0, p1, bounds, c);
166
 
167
      return bounds;
168
    }
169
  }
170
 
171
  /**
172
   * The non wrapping text areas and state buttons.
173
   */
174
  JTextArea textarea1;
175
 
176
  JTextArea textarea2;
177
 
178
  JTextArea textarea3;
179
 
180
  JCheckBox enabled1;
181
 
182
  JCheckBox editable1;
183
 
184
  JPanel panel1;
185
 
186
  /**
187
   * The char wrapping textareas and state buttons.
188
   */
189
  JTextArea textarea4;
190
 
191
  JTextArea textarea5;
192
 
193
  JTextArea textarea6;
194
 
195
  JCheckBox enabled2;
196
 
197
  JCheckBox editable2;
198
 
199
  /**
200
   * The word wrapping textareas and state buttons.
201
   */
202
  JTextArea textarea7;
203
 
204
  JTextArea textarea8;
205
 
206
  JTextArea textarea9;
207
 
208
  JCheckBox enabled3;
209
 
210
  JCheckBox editable3;
211
 
212
  /**
213
   * The custom colored textareas and state buttons.
214
   */
215
  JTextArea textarea10;
216
 
217
  JTextArea textarea11;
218
 
219
  JTextArea textarea12;
220
 
221
  JTextArea textarea13;
222
 
223
  JTextArea textarea14;
224
 
225
  JTextArea textarea14b;
226
 
227
  JCheckBox enabled4;
228
 
229
  JCheckBox editable4;
230
 
231
  /**
232
   * Some miscellaneous textarea demos.
233
   */
234
  JTextArea textarea15;
235
 
236
  JTextArea textarea16;
237
 
238
  JTextArea textarea17;
239
 
240
  JCheckBox enabled5;
241
 
242
  JCheckBox editable5;
243
 
244
  /**
245
   * Creates a new demo instance.
246
   */
247
  public TextAreaDemo()
248
  {
249
    super();
250
    createContent();
251
  }
252
 
253
  /**
254
   * When the demo is run independently, the frame is displayed, so we should
255
   * initialise the content panel (including the demo content and a close
256
   * button). But when the demo is run as part of the Swing activity board, only
257
   * the demo content panel is used, the frame itself is never displayed, so we
258
   * can avoid this step.
259
   */
260
  void initFrameContent()
261
  {
262
    JPanel closePanel = new JPanel();
263
    JButton closeButton = new JButton("Close");
264
    closeButton.setActionCommand("CLOSE");
265
    closeButton.addActionListener(this);
266
    closePanel.add(closeButton);
267
    add(closePanel, BorderLayout.SOUTH);
268
  }
269
 
270
  /**
271
   * Returns a panel with the demo content. The panel uses a BorderLayout(), and
272
   * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
273
   * the bottom of the panel if they want to (a close button is added if this
274
   * demo is being run as a standalone demo).
275
   */
276
  private void createContent()
277
  {
278
    setLayout(new BorderLayout());
279
    JTabbedPane tabPane = new JTabbedPane();
280
    tabPane.addTab("Non-wrap", createNonWrapPanel());
281
    tabPane.addTab("Char-wrap", createCharWrapPanel());
282
    tabPane.addTab("Word-wrap", createWordWrapPanel());
283
    tabPane.addTab("Custom colors", createCustomColoredPanel());
284
    tabPane.addTab("Misc", createMiscPanel());
285
    add(tabPane);
286
  }
287
 
288
  private JPanel createNonWrapPanel()
289
  {
290
    JPanel panel = new JPanel(new BorderLayout());
291
    panel.setBorder(BorderFactory.createTitledBorder("Not wrapping"));
292
 
293
    panel1 = new JPanel(new GridLayout(2, 2));
294
 
295
    textarea1 = new JTextArea("Hello World!");
296
    textarea1.setFont(new Font("Dialog", Font.PLAIN, 8));
297
    panel1.add(new JScrollPane(textarea1));
298
 
299
    textarea2 = new JTextArea("Hello World!");
300
    textarea2.setFont(new Font("Dialog", Font.ITALIC, 12));
301
    panel1.add(new JScrollPane(textarea2));
302
 
303
    textarea3 = new JTextArea("Hello World!");
304
    textarea3.setFont(new Font("Dialog", Font.BOLD, 14));
305
    panel1.add(new JScrollPane(textarea3));
306
 
307
    panel.add(panel1);
308
 
309
    JPanel statePanel = new JPanel();
310
    statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
311
    statePanel.add(Box.createVerticalGlue());
312
    enabled1 = new JCheckBox("enabled");
313
    enabled1.setSelected(true);
314
    enabled1.addActionListener(this);
315
    enabled1.setActionCommand("ENABLED1");
316
    statePanel.add(enabled1);
317
    editable1 = new JCheckBox("editable");
318
    editable1.setSelected(true);
319
    editable1.addActionListener(this);
320
    editable1.setActionCommand("EDITABLE1");
321
    statePanel.add(editable1);
322
    statePanel.add(Box.createVerticalGlue());
323
    panel.add(statePanel, BorderLayout.EAST);
324
 
325
    return panel;
326
  }
327
 
328
  private JPanel createCharWrapPanel()
329
  {
330
    JPanel panel = new JPanel(new BorderLayout());
331
    panel.setBorder(BorderFactory.createTitledBorder("Wrap at char bounds"));
332
 
333
    JPanel innerPanel = new JPanel(new GridLayout(2, 2));
334
 
335
    textarea4 = new JTextArea("Hello World!");
336
    textarea4.setLineWrap(true);
337
    textarea4.setFont(new Font("Dialog", Font.PLAIN, 8));
338
    innerPanel.add(new JScrollPane(textarea4));
339
 
340
    textarea5 = new JTextArea("Hello World!");
341
    textarea5.setLineWrap(true);
342
    textarea5.setFont(new Font("Dialog", Font.ITALIC, 12));
343
    innerPanel.add(new JScrollPane(textarea5));
344
 
345
    textarea6 = new JTextArea("Hello World!");
346
    textarea6.setLineWrap(true);
347
    textarea6.setFont(new Font("Dialog", Font.BOLD, 14));
348
    innerPanel.add(new JScrollPane(textarea6));
349
 
350
    panel.add(innerPanel);
351
 
352
    JPanel statePanel = new JPanel();
353
    statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
354
    statePanel.add(Box.createVerticalGlue());
355
    enabled2 = new JCheckBox("enabled");
356
    enabled2.setSelected(true);
357
    enabled2.addActionListener(this);
358
    enabled2.setActionCommand("ENABLED2");
359
    statePanel.add(enabled2);
360
    editable2 = new JCheckBox("editable");
361
    editable2.setSelected(true);
362
    editable2.addActionListener(this);
363
    editable2.setActionCommand("EDITABLE2");
364
    statePanel.add(editable2);
365
    statePanel.add(Box.createVerticalGlue());
366
    panel.add(statePanel, BorderLayout.EAST);
367
 
368
    return panel;
369
  }
370
 
371
  private JPanel createWordWrapPanel()
372
  {
373
    JPanel panel = new JPanel(new BorderLayout());
374
    panel.setBorder(BorderFactory.createTitledBorder("Wrap at word bounds"));
375
 
376
    JPanel innerPanel = new JPanel(new GridLayout(2, 2));
377
 
378
    textarea7 = new JTextArea("Hello World!");
379
    textarea7.setWrapStyleWord(true);
380
    textarea7.setLineWrap(true);
381
    textarea7.setFont(new Font("Dialog", Font.PLAIN, 8));
382
    innerPanel.add(new JScrollPane(textarea7));
383
 
384
    textarea8 = new JTextArea("Hello World!");
385
    textarea8.setWrapStyleWord(true);
386
    textarea8.setLineWrap(true);
387
    textarea8.setFont(new Font("Dialog", Font.ITALIC, 12));
388
    innerPanel.add(new JScrollPane(textarea8));
389
 
390
    textarea9 = new JTextArea("Hello World!");
391
    textarea9.setWrapStyleWord(true);
392
    textarea9.setLineWrap(true);
393
    textarea9.setFont(new Font("Dialog", Font.BOLD, 14));
394
    innerPanel.add(new JScrollPane(textarea9));
395
 
396
    panel.add(innerPanel);
397
 
398
    JPanel statePanel = new JPanel();
399
    statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
400
    statePanel.add(Box.createVerticalGlue());
401
    enabled3 = new JCheckBox("enabled");
402
    enabled3.setSelected(true);
403
    enabled3.addActionListener(this);
404
    enabled3.setActionCommand("ENABLED3");
405
    statePanel.add(enabled3);
406
    editable3 = new JCheckBox("editable");
407
    editable3.setSelected(true);
408
    editable3.addActionListener(this);
409
    editable3.setActionCommand("EDITABLE3");
410
    statePanel.add(editable3);
411
    statePanel.add(Box.createVerticalGlue());
412
    panel.add(statePanel, BorderLayout.EAST);
413
 
414
    return panel;
415
  }
416
 
417
  private JPanel createCustomColoredPanel()
418
  {
419
    JPanel panel = new JPanel(new BorderLayout());
420
 
421
    JPanel innerPanel = new JPanel(new GridLayout(3, 2));
422
    panel.setBorder(BorderFactory.createTitledBorder("Custom colors"));
423
 
424
    textarea10 = new JTextArea("custom foreground", 10, 15);
425
    textarea10.setForeground(Color.GREEN);
426
    innerPanel.add(new JScrollPane(textarea10));
427
 
428
    textarea11 = new JTextArea("custom background", 10, 15);
429
    textarea11.setBackground(Color.YELLOW);
430
    innerPanel.add(new JScrollPane(textarea11));
431
 
432
    textarea12 = new JTextArea("custom disabled textcolor", 10, 15);
433
    textarea12.setDisabledTextColor(Color.BLUE);
434
    innerPanel.add(new JScrollPane(textarea12));
435
 
436
    textarea13 = new JTextArea("custom selected text color", 10, 15);
437
    textarea13.setSelectedTextColor(Color.RED);
438
    innerPanel.add(new JScrollPane(textarea13));
439
 
440
    textarea14 = new JTextArea("custom selection color", 10, 15);
441
    textarea14.setSelectionColor(Color.RED);
442
    innerPanel.add(new JScrollPane(textarea14));
443
 
444
    textarea14b = new JTextArea("custom selection and selected text color", 10, 15);
445
    textarea14b.setSelectedTextColor(Color.WHITE);
446
    textarea14b.setSelectionColor(Color.BLACK);
447
    innerPanel.add(new JScrollPane(textarea14b));
448
 
449
    panel.add(innerPanel);
450
 
451
    JPanel statePanel = new JPanel();
452
    statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
453
    statePanel.add(Box.createVerticalGlue());
454
    enabled4 = new JCheckBox("enabled");
455
    enabled4.setSelected(true);
456
    enabled4.addActionListener(this);
457
    enabled4.setActionCommand("ENABLED4");
458
    statePanel.add(enabled4);
459
    editable4 = new JCheckBox("editable");
460
    editable4.setSelected(true);
461
    editable4.addActionListener(this);
462
    editable4.setActionCommand("EDITABLE4");
463
    statePanel.add(editable4);
464
    statePanel.add(Box.createVerticalGlue());
465
    panel.add(statePanel, BorderLayout.EAST);
466
 
467
    return panel;
468
  }
469
 
470
  private JPanel createMiscPanel()
471
  {
472
    JPanel panel = new JPanel(new BorderLayout());
473
    panel.setBorder(BorderFactory.createTitledBorder("Miscellaneous"));
474
 
475
    JPanel innerPanel = new JPanel(new GridLayout(2, 2));
476
 
477
    textarea15 = new JTextArea("Custom Caret");
478
    textarea15.setCaret(new CornerCaret());
479
    innerPanel.add(new JScrollPane(textarea15));
480
 
481
    textarea16 = new JTextArea("Custom Caret color");
482
    textarea16.setCaretColor(Color.MAGENTA);
483
    innerPanel.add(new JScrollPane(textarea16));
484
 
485
    textarea16 = new JTextArea("Custom Selection painter");
486
    textarea16.setFont(new Font("Dialog", Font.PLAIN, 24));
487
    textarea16.setCaret(new DefaultCaret()
488
    {
489
      public Highlighter.HighlightPainter getSelectionPainter()
490
      {
491
        return DemoHighlightPainter.INSTANCE;
492
      }
493
    });
494
 
495
    innerPanel.add(new JScrollPane(textarea16));
496
 
497
    panel.add(innerPanel);
498
 
499
    JPanel statePanel = new JPanel();
500
    statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
501
    statePanel.add(Box.createVerticalGlue());
502
    enabled5 = new JCheckBox("enabled");
503
    enabled5.setSelected(true);
504
    enabled5.addActionListener(this);
505
    enabled5.setActionCommand("ENABLED5");
506
    statePanel.add(enabled5);
507
    editable5 = new JCheckBox("editable");
508
    editable5.setSelected(true);
509
    editable5.addActionListener(this);
510
    editable5.setActionCommand("EDITABLE5");
511
    statePanel.add(editable5);
512
    statePanel.add(Box.createVerticalGlue());
513
    panel.add(statePanel, BorderLayout.EAST);
514
 
515
    return panel;
516
  }
517
 
518
  public void actionPerformed(ActionEvent e)
519
  {
520
    if (e.getActionCommand().equals("CLOSE"))
521
      {
522
        System.exit(0);
523
      }
524
    else if (e.getActionCommand().equals("ENABLED1"))
525
      {
526
        boolean enabled = enabled1.isSelected();
527
        textarea1.setEnabled(enabled);
528
        textarea2.setEnabled(enabled);
529
        textarea3.setEnabled(enabled);
530
      }
531
    else if (e.getActionCommand().equals("EDITABLE1"))
532
      {
533
        boolean editable = editable1.isSelected();
534
        textarea1.setEditable(editable);
535
        textarea2.setEditable(editable);
536
        textarea3.setEditable(editable);
537
      }
538
    else if (e.getActionCommand().equals("ENABLED2"))
539
      {
540
        boolean enabled = enabled2.isSelected();
541
        textarea4.setEnabled(enabled);
542
        textarea5.setEnabled(enabled);
543
        textarea6.setEnabled(enabled);
544
      }
545
    else if (e.getActionCommand().equals("EDITABLE2"))
546
      {
547
        boolean editable = editable2.isSelected();
548
        textarea4.setEditable(editable);
549
        textarea5.setEditable(editable);
550
        textarea6.setEditable(editable);
551
      }
552
    else if (e.getActionCommand().equals("ENABLED3"))
553
      {
554
        boolean enabled = enabled3.isSelected();
555
        textarea7.setEnabled(enabled);
556
        textarea8.setEnabled(enabled);
557
        textarea9.setEnabled(enabled);
558
      }
559
    else if (e.getActionCommand().equals("EDITABLE3"))
560
      {
561
        boolean editable = editable3.isSelected();
562
        textarea7.setEditable(editable);
563
        textarea8.setEditable(editable);
564
        textarea9.setEditable(editable);
565
      }
566
    else if (e.getActionCommand().equals("ENABLED4"))
567
      {
568
        boolean enabled = enabled4.isSelected();
569
        textarea10.setEnabled(enabled);
570
        textarea11.setEnabled(enabled);
571
        textarea12.setEnabled(enabled);
572
        textarea13.setEnabled(enabled);
573
        textarea14.setEnabled(enabled);
574
        textarea14b.setEnabled(enabled);
575
      }
576
    else if (e.getActionCommand().equals("EDITABLE4"))
577
      {
578
        boolean editable = editable4.isSelected();
579
        textarea10.setEditable(editable);
580
        textarea11.setEditable(editable);
581
        textarea12.setEditable(editable);
582
        textarea13.setEditable(editable);
583
        textarea14.setEditable(editable);
584
        textarea14b.setEditable(editable);
585
      }
586
  }
587
 
588
  public static void main(String[] args)
589
  {
590
    SwingUtilities.invokeLater
591
    (new Runnable()
592
     {
593
       public void run()
594
       {
595
         TextAreaDemo app = new TextAreaDemo();
596
         app.initFrameContent();
597
         JFrame frame = new JFrame();
598
         frame.getContentPane().add(app);
599
         frame.pack();
600
         frame.setVisible(true);
601
       }
602
     });
603
  }
604
 
605
  /**
606
   * Returns a DemoFactory that creates a TextAreaDemo.
607
   *
608
   * @return a DemoFactory that creates a TextAreaDemo
609
   */
610
  public static DemoFactory createDemoFactory()
611
  {
612
    return new DemoFactory()
613
    {
614
      public JComponent createDemo()
615
      {
616
        return new TextAreaDemo();
617
      }
618
    };
619
  }
620
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.