bug4242228.java revision 11018:66682f651425
1/*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @bug 4242228
26   @summary Tests that HTMLEditorKit.setText() doesn't throw exceptions
27   @author Peter Zhelezniakov
28*/
29
30import javax.swing.*;
31import javax.swing.event.ChangeEvent;
32import javax.swing.event.ChangeListener;
33import javax.swing.text.StyledEditorKit;
34import javax.swing.text.html.HTMLDocument;
35import javax.swing.text.html.HTMLEditorKit;
36import java.awt.*;
37
38public class bug4242228 {
39    private static JTabbedPane tabPane;
40    private static JFrame frame;
41
42    public static void main(String[] argv) throws Exception {
43        SwingUtilities.invokeAndWait(new Runnable() {
44            @Override
45            public void run() {
46                frame = new JFrame("4242228 Test");
47
48                JScrollPane sourcePane = new JScrollPane();
49                final JTextPane htmlEditor = new JTextPane();
50                final JTextPane sourceEditor = new JTextPane();
51                final JScrollPane editorPane = new JScrollPane();
52
53                tabPane = new JTabbedPane();
54                htmlEditor.setText(" ");
55                htmlEditor.setEditorKit(new HTMLEditorKit());
56
57                sourceEditor.setText(" ");
58                sourceEditor.setEditorKit(new StyledEditorKit());
59
60                frame.setLayout(new BorderLayout());
61
62                editorPane.getViewport().add(htmlEditor);
63
64                tabPane.addTab("Editor", editorPane);
65                tabPane.addChangeListener(new ChangeListener() {
66                    public void stateChanged(ChangeEvent e) {
67                        if (tabPane.getSelectedComponent() == editorPane) {
68                            htmlEditor.setText(sourceEditor.getText());
69                        } else {
70                            sourceEditor.setText(htmlEditor.getText());
71                        }
72                    }
73                });
74
75                sourcePane.getViewport().add(sourceEditor);
76                tabPane.addTab("Source", sourcePane);
77                tabPane.setTabPlacement(SwingConstants.BOTTOM);
78                htmlEditor.setDocument(new HTMLDocument());
79
80                frame.add(tabPane);
81                frame.setSize(400, 300);
82                frame.setVisible(true);
83            }
84        });
85
86        Robot robot = new Robot();
87        robot.waitForIdle();
88
89        SwingUtilities.invokeAndWait(new Runnable() {
90            @Override
91            public void run() {
92                for (int i = 0; i < 50; i++) {
93                    tabPane.setSelectedIndex(i % 2);
94                }
95
96                frame.dispose();
97            }
98        });
99    }
100}
101