|
1 /* |
|
2 * Copyright (c) 2019, 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 8219156 |
|
28 * @summary Verify RTFEditorKit does not read background color |
|
29 */ |
|
30 |
|
31 import java.awt.Color; |
|
32 import java.awt.image.BufferedImage; |
|
33 import java.io.InputStream; |
|
34 import java.io.OutputStream; |
|
35 import java.nio.file.Files; |
|
36 import java.nio.file.Paths; |
|
37 import java.util.Enumeration; |
|
38 |
|
39 import javax.swing.JTextPane; |
|
40 import javax.swing.SwingUtilities; |
|
41 import javax.swing.JFrame; |
|
42 import javax.swing.text.MutableAttributeSet; |
|
43 import javax.swing.text.StyleConstants; |
|
44 import javax.swing.text.SimpleAttributeSet; |
|
45 import javax.swing.text.StyledDocument; |
|
46 import javax.swing.text.AttributeSet; |
|
47 import javax.swing.text.rtf.RTFEditorKit; |
|
48 |
|
49 public class RTFReadBGColorTest { |
|
50 static JTextPane text; |
|
51 static String BGTEXT = "yellow_background\n"; |
|
52 |
|
53 public static void main(String[] a) throws Exception { |
|
54 SwingUtilities.invokeAndWait(() -> { |
|
55 JFrame f = new JFrame(); |
|
56 f.setBounds(200, 600, 400, 300); |
|
57 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
58 text = new JTextPane(); |
|
59 text.setEditorKit(new RTFEditorKit()); |
|
60 |
|
61 MutableAttributeSet attrBackground = new SimpleAttributeSet(); |
|
62 StyleConstants.setBackground(attrBackground, Color.YELLOW); |
|
63 |
|
64 try { |
|
65 text.getDocument().insertString(0, BGTEXT, attrBackground); |
|
66 } catch (Exception e) { |
|
67 throw new RuntimeException(e); |
|
68 } |
|
69 write(); |
|
70 read(); |
|
71 |
|
72 f.getContentPane().add(text); |
|
73 f.setVisible(true); |
|
74 text.setCaretPosition(BGTEXT.length()+6); |
|
75 StyledDocument style = text.getStyledDocument(); |
|
76 AttributeSet oldSet = style.getCharacterElement(BGTEXT.length()+6).getAttributes(); |
|
77 f.dispose(); |
|
78 if (!style.getBackground(oldSet).equals(Color.YELLOW)) { |
|
79 throw new RuntimeException("RTFEditorKit does not read background color"); |
|
80 } |
|
81 }); |
|
82 } |
|
83 |
|
84 static void write() { |
|
85 try (OutputStream o = Files.newOutputStream(Paths.get("test.rtf"))) { |
|
86 text.getEditorKit().write(o, text.getDocument(), 0, 0); |
|
87 } catch (Exception e2) { |
|
88 throw new RuntimeException(e2); |
|
89 } |
|
90 } |
|
91 |
|
92 static void read() { |
|
93 try (InputStream in = Files.newInputStream(Paths.get("test.rtf"))) { |
|
94 text.getEditorKit().read(in, text.getDocument(), 0); |
|
95 } catch (Exception e2) { |
|
96 throw new RuntimeException(e2); |
|
97 } |
|
98 } |
|
99 } |
|
100 |