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/*
25 * @test
26 * @key headful
27 * @bug 4242228
28 * @summary Tests that HTMLEditorKit.setText() doesn't throw exceptions
29 * @author Peter Zhelezniakov
30 */
31
32import javax.swing.*;
33import javax.swing.event.ChangeEvent;
34import javax.swing.event.ChangeListener;
35import javax.swing.text.StyledEditorKit;
36import javax.swing.text.html.HTMLDocument;
37import javax.swing.text.html.HTMLEditorKit;
38import java.awt.*;
39
40public class bug4242228 {
41    private static JTabbedPane tabPane;
42    private static JFrame frame;
43
44    public static void main(String[] argv) throws Exception {
45        SwingUtilities.invokeAndWait(new Runnable() {
46            @Override
47            public void run() {
48                frame = new JFrame("4242228 Test");
49
50                JScrollPane sourcePane = new JScrollPane();
51                final JTextPane htmlEditor = new JTextPane();
52                final JTextPane sourceEditor = new JTextPane();
53                final JScrollPane editorPane = new JScrollPane();
54
55                tabPane = new JTabbedPane();
56                htmlEditor.setText(" ");
57                htmlEditor.setEditorKit(new HTMLEditorKit());
58
59                sourceEditor.setText(" ");
60                sourceEditor.setEditorKit(new StyledEditorKit());
61
62                frame.setLayout(new BorderLayout());
63
64                editorPane.getViewport().add(htmlEditor);
65
66                tabPane.addTab("Editor", editorPane);
67                tabPane.addChangeListener(new ChangeListener() {
68                    public void stateChanged(ChangeEvent e) {
69                        if (tabPane.getSelectedComponent() == editorPane) {
70                            htmlEditor.setText(sourceEditor.getText());
71                        } else {
72                            sourceEditor.setText(htmlEditor.getText());
73                        }
74                    }
75                });
76
77                sourcePane.getViewport().add(sourceEditor);
78                tabPane.addTab("Source", sourcePane);
79                tabPane.setTabPlacement(SwingConstants.BOTTOM);
80                htmlEditor.setDocument(new HTMLDocument());
81
82                frame.add(tabPane);
83                frame.setSize(400, 300);
84                frame.setVisible(true);
85            }
86        });
87
88        Robot robot = new Robot();
89        robot.waitForIdle();
90
91        SwingUtilities.invokeAndWait(new Runnable() {
92            @Override
93            public void run() {
94                for (int i = 0; i < 50; i++) {
95                    tabPane.setSelectedIndex(i % 2);
96                }
97
98                frame.dispose();
99            }
100        });
101    }
102}
103