summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRobert Schuster <theBohemian@gmx.net>2006-03-06 18:12:43 +0000
committerRobert Schuster <theBohemian@gmx.net>2006-03-06 18:12:43 +0000
commitb541e1084f4035164681f4864c86114634691671 (patch)
tree5f92a7aadacf42d32882ae7c0a3be44bce6cc137 /examples
parent7d8dc12885abfb1bef8ad9b83fbbcd301f1f5bd5 (diff)
downloadclasspath-b541e1084f4035164681f4864c86114634691671.tar.gz
2006-03-06 Robert Schuster <robertschuster@fsfe.org>
* examples/gnu/classpath/examples/swing/TextArea.java: New file. * examples/gnu/classpath/examples/swing/Demo.java: (mkButtonBar): Changed layout manager to GridLayout, added entry for textarea example. (mkMenuBar): Added entry for text area example.
Diffstat (limited to 'examples')
-rw-r--r--examples/gnu/classpath/examples/swing/Demo.java15
-rw-r--r--examples/gnu/classpath/examples/swing/TextAreaDemo.java606
2 files changed, 617 insertions, 4 deletions
diff --git a/examples/gnu/classpath/examples/swing/Demo.java b/examples/gnu/classpath/examples/swing/Demo.java
index b0604bb10..718d3ae4f 100644
--- a/examples/gnu/classpath/examples/swing/Demo.java
+++ b/examples/gnu/classpath/examples/swing/Demo.java
@@ -207,6 +207,10 @@ public class Demo
new PopUpAction("TextField",
(new TextFieldDemo("TextField Demo")).createContent(),
examples);
+
+ new PopUpAction("TextArea",
+ (new TextAreaDemo("TextArea Demo")).createContent(),
+ examples);
new PopUpAction("FileChooser",
(new FileChooserDemo("FileChooser Demo")).createContent(),
@@ -727,7 +731,6 @@ public class Demo
main.add(mkButtonBar());
component.add(main, BorderLayout.CENTER);
frame.pack();
- frame.setSize(800, 600);
frame.show();
}
@@ -983,7 +986,7 @@ public class Demo
private JPanel mkButtonBar()
{
- JPanel panel = new JPanel(new FlowLayout());
+ JPanel panel = new JPanel(new GridLayout(3, 1, 5, 5));
new PopUpAction("Buttons",
(new ButtonDemo("Button Demo")).createContent(),
panel);
@@ -1030,8 +1033,12 @@ public class Demo
new SpinnerDemo("Spinner Demo").createContent(), panel);
new PopUpAction("TextField",
- (new TextFieldDemo("TextField Demo")).createContent(),
- panel);
+ (new TextFieldDemo("TextField Demo")).createContent(),
+ panel);
+
+ new PopUpAction("TextArea",
+ (new TextAreaDemo("TextArea Demo")).createContent(),
+ panel);
new PopUpAction("FileChooser",
(new FileChooserDemo("FileChooser Demo")).createContent(),
diff --git a/examples/gnu/classpath/examples/swing/TextAreaDemo.java b/examples/gnu/classpath/examples/swing/TextAreaDemo.java
new file mode 100644
index 000000000..9b4263194
--- /dev/null
+++ b/examples/gnu/classpath/examples/swing/TextAreaDemo.java
@@ -0,0 +1,606 @@
+/* TextAreaDemo.java -- An example showing various textareas 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.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTabbedPane;
+import javax.swing.JTextArea;
+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 textare demo showing various textareas in different states.
+ */
+public class TextAreaDemo
+ extends JFrame
+ 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;
+ }
+ }
+
+ private JPanel content;
+
+ /**
+ * The non wrapping text areas and state buttons.
+ */
+ JTextArea textarea1;
+
+ JTextArea textarea2;
+
+ JTextArea textarea3;
+
+ JCheckBox enabled1;
+
+ JCheckBox editable1;
+
+ JPanel panel1;
+
+ /**
+ * The char wrapping textareas and state buttons.
+ */
+ JTextArea textarea4;
+
+ JTextArea textarea5;
+
+ JTextArea textarea6;
+
+ JCheckBox enabled2;
+
+ JCheckBox editable2;
+
+ /**
+ * The word wrapping textareas and state buttons.
+ */
+ JTextArea textarea7;
+
+ JTextArea textarea8;
+
+ JTextArea textarea9;
+
+ JCheckBox enabled3;
+
+ JCheckBox editable3;
+
+ /**
+ * The custom colored textareas and state buttons.
+ */
+ JTextArea textarea10;
+
+ JTextArea textarea11;
+
+ JTextArea textarea12;
+
+ JTextArea textarea13;
+
+ JTextArea textarea14;
+
+ JTextArea textarea14b;
+
+ JCheckBox enabled4;
+
+ JCheckBox editable4;
+
+ /**
+ * Some miscellaneous textarea demos.
+ */
+ JTextArea textarea15;
+
+ JTextArea textarea16;
+
+ JTextArea textarea17;
+
+ JCheckBox enabled5;
+
+ JCheckBox editable5;
+
+ /**
+ * Creates a new demo instance.
+ *
+ * @param title the frame title.
+ */
+ public TextAreaDemo(String title)
+ {
+ super(title);
+ JPanel content = createContent();
+ // initFrameContent() is only called (from main) when running this app
+ // standalone
+ }
+
+ /**
+ * 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.
+ */
+ public void initFrameContent()
+ {
+ JPanel closePanel = new JPanel();
+ JButton closeButton = new JButton("Close");
+ closeButton.setActionCommand("CLOSE");
+ closeButton.addActionListener(this);
+ closePanel.add(closeButton);
+ content.add(closePanel, BorderLayout.SOUTH);
+ getContentPane().add(content);
+ }
+
+ /**
+ * 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).
+ */
+ JPanel createContent()
+ {
+ if (content == null)
+ {
+ content = new JPanel(new BorderLayout());
+ JTabbedPane tabPane = new JTabbedPane();
+ tabPane.addTab("Non-wrap", createNonWrapPanel());
+ tabPane.addTab("Char-wrap", createCharWrapPanel());
+ tabPane.addTab("Word-wrap", createWordWrapPanel());
+ tabPane.addTab("Custom colors", createCustomColoredPanel());
+ tabPane.addTab("Misc", createMiscPanel());
+ content.add(tabPane);
+ // content.setPreferredSize(new Dimension(400, 300));
+ }
+ return content;
+ }
+
+ private JPanel createNonWrapPanel()
+ {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBorder(BorderFactory.createTitledBorder("Not wrapping"));
+
+ panel1 = new JPanel(new GridLayout(2, 2));
+
+ textarea1 = new JTextArea("Hello World!");
+ textarea1.setFont(new Font("Dialog", Font.PLAIN, 8));
+ panel1.add(new JScrollPane(textarea1));
+
+ textarea2 = new JTextArea("Hello World!");
+ textarea2.setFont(new Font("Dialog", Font.ITALIC, 12));
+ panel1.add(new JScrollPane(textarea2));
+
+ textarea3 = new JTextArea("Hello World!");
+ textarea3.setFont(new Font("Dialog", Font.BOLD, 14));
+ panel1.add(new JScrollPane(textarea3));
+
+ panel.add(panel1);
+
+ JPanel statePanel = new JPanel();
+ statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
+ statePanel.add(Box.createVerticalGlue());
+ enabled1 = new JCheckBox("enabled");
+ enabled1.setSelected(true);
+ enabled1.addActionListener(this);
+ enabled1.setActionCommand("ENABLED1");
+ statePanel.add(enabled1);
+ editable1 = new JCheckBox("editable");
+ editable1.setSelected(true);
+ editable1.addActionListener(this);
+ editable1.setActionCommand("EDITABLE1");
+ statePanel.add(editable1);
+ statePanel.add(Box.createVerticalGlue());
+ panel.add(statePanel, BorderLayout.EAST);
+
+ return panel;
+ }
+
+ private JPanel createCharWrapPanel()
+ {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBorder(BorderFactory.createTitledBorder("Wrap at char bounds"));
+
+ JPanel innerPanel = new JPanel(new GridLayout(2, 2));
+
+ textarea4 = new JTextArea("Hello World!");
+ textarea4.setLineWrap(true);
+ textarea4.setFont(new Font("Dialog", Font.PLAIN, 8));
+ innerPanel.add(new JScrollPane(textarea4));
+
+ textarea5 = new JTextArea("Hello World!");
+ textarea5.setLineWrap(true);
+ textarea5.setFont(new Font("Dialog", Font.ITALIC, 12));
+ innerPanel.add(new JScrollPane(textarea5));
+
+ textarea6 = new JTextArea("Hello World!");
+ textarea6.setLineWrap(true);
+ textarea6.setFont(new Font("Dialog", Font.BOLD, 14));
+ innerPanel.add(new JScrollPane(textarea6));
+
+ panel.add(innerPanel);
+
+ JPanel statePanel = new JPanel();
+ statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
+ statePanel.add(Box.createVerticalGlue());
+ enabled2 = new JCheckBox("enabled");
+ enabled2.setSelected(true);
+ enabled2.addActionListener(this);
+ enabled2.setActionCommand("ENABLED2");
+ statePanel.add(enabled2);
+ editable2 = new JCheckBox("editable");
+ editable2.setSelected(true);
+ editable2.addActionListener(this);
+ editable2.setActionCommand("EDITABLE2");
+ statePanel.add(editable2);
+ statePanel.add(Box.createVerticalGlue());
+ panel.add(statePanel, BorderLayout.EAST);
+
+ return panel;
+ }
+
+ private JPanel createWordWrapPanel()
+ {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBorder(BorderFactory.createTitledBorder("Wrap at word bounds"));
+
+ JPanel innerPanel = new JPanel(new GridLayout(2, 2));
+
+ textarea7 = new JTextArea("Hello World!");
+ textarea7.setWrapStyleWord(true);
+ textarea7.setLineWrap(true);
+ textarea7.setFont(new Font("Dialog", Font.PLAIN, 8));
+ innerPanel.add(new JScrollPane(textarea7));
+
+ textarea8 = new JTextArea("Hello World!");
+ textarea8.setWrapStyleWord(true);
+ textarea8.setLineWrap(true);
+ textarea8.setFont(new Font("Dialog", Font.ITALIC, 12));
+ innerPanel.add(new JScrollPane(textarea8));
+
+ textarea9 = new JTextArea("Hello World!");
+ textarea9.setWrapStyleWord(true);
+ textarea9.setLineWrap(true);
+ textarea9.setFont(new Font("Dialog", Font.BOLD, 14));
+ innerPanel.add(new JScrollPane(textarea9));
+
+ panel.add(innerPanel);
+
+ JPanel statePanel = new JPanel();
+ statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
+ statePanel.add(Box.createVerticalGlue());
+ enabled3 = new JCheckBox("enabled");
+ enabled3.setSelected(true);
+ enabled3.addActionListener(this);
+ enabled3.setActionCommand("ENABLED3");
+ statePanel.add(enabled3);
+ editable3 = new JCheckBox("editable");
+ editable3.setSelected(true);
+ editable3.addActionListener(this);
+ editable3.setActionCommand("EDITABLE3");
+ statePanel.add(editable3);
+ statePanel.add(Box.createVerticalGlue());
+ panel.add(statePanel, BorderLayout.EAST);
+
+ return panel;
+ }
+
+ private JPanel createCustomColoredPanel()
+ {
+ JPanel panel = new JPanel(new BorderLayout());
+
+ JPanel innerPanel = new JPanel(new GridLayout(3, 2));
+ panel.setBorder(BorderFactory.createTitledBorder("Custom colors"));
+
+ textarea10 = new JTextArea("custom foreground", 10, 15);
+ textarea10.setForeground(Color.GREEN);
+ innerPanel.add(new JScrollPane(textarea10));
+
+ textarea11 = new JTextArea("custom background", 10, 15);
+ textarea11.setForeground(Color.YELLOW);
+ innerPanel.add(new JScrollPane(textarea11));
+
+ textarea12 = new JTextArea("custom disabled textcolor", 10, 15);
+ textarea12.setDisabledTextColor(Color.BLUE);
+ innerPanel.add(new JScrollPane(textarea12));
+
+ textarea13 = new JTextArea("custom selected text color", 10, 15);
+ textarea13.setSelectedTextColor(Color.RED);
+ innerPanel.add(new JScrollPane(textarea13));
+
+ textarea14 = new JTextArea("custom selection color", 10, 15);
+ textarea14.setSelectionColor(Color.CYAN);
+ innerPanel.add(new JScrollPane(textarea14));
+
+ textarea14b = new JTextArea("custom selection and selected text color", 10, 15);
+ textarea14b.setSelectedTextColor(Color.WHITE);
+ textarea14b.setSelectionColor(Color.BLACK);
+ innerPanel.add(new JScrollPane(textarea14b));
+
+ panel.add(innerPanel);
+
+ JPanel statePanel = new JPanel();
+ statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
+ statePanel.add(Box.createVerticalGlue());
+ enabled4 = new JCheckBox("enabled");
+ enabled4.setSelected(true);
+ enabled4.addActionListener(this);
+ enabled4.setActionCommand("ENABLED4");
+ statePanel.add(enabled4);
+ editable4 = new JCheckBox("editable");
+ editable4.setSelected(true);
+ editable4.addActionListener(this);
+ editable4.setActionCommand("EDITABLE4");
+ statePanel.add(editable4);
+ statePanel.add(Box.createVerticalGlue());
+ panel.add(statePanel, BorderLayout.EAST);
+
+ return panel;
+ }
+
+ private JPanel createMiscPanel()
+ {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBorder(BorderFactory.createTitledBorder("Miscellaneous"));
+
+ JPanel innerPanel = new JPanel(new GridLayout(2, 2));
+
+ textarea15 = new JTextArea("Custom Caret");
+ textarea15.setCaret(new CornerCaret());
+ innerPanel.add(new JScrollPane(textarea15));
+
+ textarea16 = new JTextArea("Custom Caret color");
+ textarea16.setCaretColor(Color.MAGENTA);
+ innerPanel.add(new JScrollPane(textarea16));
+
+ textarea16 = new JTextArea("Custom Selection painter");
+ textarea16.setFont(new Font("Dialog", Font.PLAIN, 24));
+ textarea16.setCaret(new DefaultCaret()
+ {
+ public Highlighter.HighlightPainter getSelectionPainter()
+ {
+ return DemoHighlightPainter.INSTANCE;
+ }
+ });
+
+ innerPanel.add(new JScrollPane(textarea16));
+
+ panel.add(innerPanel);
+
+ JPanel statePanel = new JPanel();
+ statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
+ statePanel.add(Box.createVerticalGlue());
+ enabled5 = new JCheckBox("enabled");
+ enabled5.setSelected(true);
+ enabled5.addActionListener(this);
+ enabled5.setActionCommand("ENABLED5");
+ statePanel.add(enabled5);
+ editable5 = new JCheckBox("editable");
+ editable5.setSelected(true);
+ editable5.addActionListener(this);
+ editable5.setActionCommand("EDITABLE5");
+ statePanel.add(editable5);
+ statePanel.add(Box.createVerticalGlue());
+ panel.add(statePanel, BorderLayout.EAST);
+
+ return panel;
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ if (e.getActionCommand().equals("CLOSE"))
+ {
+ System.exit(0);
+ }
+ else if (e.getActionCommand().equals("ENABLED1"))
+ {
+ boolean enabled = enabled1.isSelected();
+ textarea1.setEnabled(enabled);
+ textarea2.setEnabled(enabled);
+ textarea3.setEnabled(enabled);
+ }
+ else if (e.getActionCommand().equals("EDITABLE1"))
+ {
+ boolean editable = editable1.isSelected();
+ textarea1.setEditable(editable);
+ textarea2.setEditable(editable);
+ textarea3.setEditable(editable);
+ }
+ else if (e.getActionCommand().equals("ENABLED2"))
+ {
+ boolean enabled = enabled2.isSelected();
+ textarea4.setEnabled(enabled);
+ textarea5.setEnabled(enabled);
+ textarea6.setEnabled(enabled);
+ }
+ else if (e.getActionCommand().equals("EDITABLE2"))
+ {
+ boolean editable = editable2.isSelected();
+ textarea4.setEditable(editable);
+ textarea5.setEditable(editable);
+ textarea6.setEditable(editable);
+ }
+ else if (e.getActionCommand().equals("ENABLED3"))
+ {
+ boolean enabled = enabled3.isSelected();
+ textarea7.setEnabled(enabled);
+ textarea8.setEnabled(enabled);
+ textarea9.setEnabled(enabled);
+ }
+ else if (e.getActionCommand().equals("EDITABLE3"))
+ {
+ boolean editable = editable3.isSelected();
+ textarea7.setEditable(editable);
+ textarea8.setEditable(editable);
+ textarea9.setEditable(editable);
+ }
+ else if (e.getActionCommand().equals("ENABLED4"))
+ {
+ boolean enabled = enabled4.isSelected();
+ textarea10.setEnabled(enabled);
+ textarea11.setEnabled(enabled);
+ textarea12.setEnabled(enabled);
+ textarea13.setEnabled(enabled);
+ textarea14.setEnabled(enabled);
+ textarea14b.setEnabled(enabled);
+ }
+ else if (e.getActionCommand().equals("EDITABLE4"))
+ {
+ boolean editable = editable4.isSelected();
+ textarea10.setEditable(editable);
+ textarea11.setEditable(editable);
+ textarea12.setEditable(editable);
+ textarea13.setEditable(editable);
+ textarea14.setEditable(editable);
+ textarea14b.setEditable(editable);
+ }
+ }
+
+ public static void main(String[] args)
+ {
+ TextAreaDemo app = new TextAreaDemo("TextArea Demo");
+ app.initFrameContent();
+ app.pack();
+ app.setVisible(true);
+ }
+
+}