Test6968363.java revision 11111:4ef86895869c
1/*
2 * Copyright (c) 2013, 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 */
23import java.awt.Robot;
24import javax.swing.JFrame;
25import javax.swing.JLabel;
26import javax.swing.JTextField;
27import javax.swing.event.DocumentListener;
28import javax.swing.event.UndoableEditListener;
29import javax.swing.text.AttributeSet;
30import javax.swing.text.BadLocationException;
31import javax.swing.text.Document;
32import javax.swing.text.Element;
33import javax.swing.text.PlainDocument;
34import javax.swing.text.Position;
35import javax.swing.text.Segment;
36
37import static java.awt.BorderLayout.NORTH;
38import static java.awt.BorderLayout.SOUTH;
39import static java.awt.Toolkit.getDefaultToolkit;
40import static java.awt.event.KeyEvent.VK_LEFT;
41import static javax.swing.SwingUtilities.invokeAndWait;
42
43/*
44 * @test
45 * @bug 6968363
46 * @summary Ensures that a custom document may not extend AbstractDocument
47 * @author Sergey Malenkov
48 * @library ../../../../../lib/testlibrary/
49 * @build ExtendedRobot
50 * @run main Test6968363
51 */
52public class Test6968363 implements Runnable, Thread.UncaughtExceptionHandler {
53    private JFrame frame;
54
55    public static void main(String[] args) throws Exception {
56        Runnable task = new Test6968363();
57        invokeAndWait(task);
58        ExtendedRobot robot = new ExtendedRobot();
59        robot.waitForIdle(100);
60        robot.keyPress(VK_LEFT);
61        robot.waitForIdle(100);
62        robot.keyRelease(VK_LEFT);
63        robot.waitForIdle(100);
64        invokeAndWait(task);
65    }
66
67    @Override
68    public void uncaughtException(Thread thread, Throwable throwable) {
69        throwable.printStackTrace();
70        System.exit(1);
71    }
72
73    @Override
74    public void run() {
75        if (this.frame == null) {
76            Thread.setDefaultUncaughtExceptionHandler(this);
77            this.frame = new JFrame(getClass().getSimpleName());
78            this.frame.add(NORTH, new JLabel("Copy Paste a HINDI text into the field below"));
79            this.frame.add(SOUTH, new JTextField(new MyDocument(), "\u0938", 10));
80            this.frame.pack();
81            this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
82            this.frame.setLocationRelativeTo(null);
83            this.frame.setVisible(true);
84        } else {
85            this.frame.dispose();
86            this.frame = null;
87        }
88    }
89
90    private static class MyDocument implements Document {
91        private final Document document = new PlainDocument();
92
93        @Override
94        public int getLength() {
95            return this.document.getLength();
96        }
97
98        @Override
99        public void addDocumentListener(DocumentListener listener) {
100            this.document.addDocumentListener(listener);
101        }
102
103        @Override
104        public void removeDocumentListener(DocumentListener listener) {
105            this.document.removeDocumentListener(listener);
106        }
107
108        @Override
109        public void addUndoableEditListener(UndoableEditListener listener) {
110            this.document.addUndoableEditListener(listener);
111        }
112
113        @Override
114        public void removeUndoableEditListener(UndoableEditListener listener) {
115            this.document.removeUndoableEditListener(listener);
116        }
117
118        @Override
119        public Object getProperty(Object key) {
120            return this.document.getProperty(key);
121        }
122
123        @Override
124        public void putProperty(Object key, Object value) {
125            this.document.putProperty(key, value);
126        }
127
128        @Override
129        public void remove(int offset, int length) throws BadLocationException {
130            this.document.remove(offset, length);
131        }
132
133        @Override
134        public void insertString(int offset, String string, AttributeSet set) throws BadLocationException {
135            for (int i = 0; i < string.length(); i++) {
136                System.out.println("i: " + i + "; ch: " + Integer.toHexString(string.charAt(i)));
137            }
138            this.document.insertString(offset, string, set);
139        }
140
141        @Override
142        public String getText(int offset, int length) throws BadLocationException {
143            return this.document.getText(offset, length);
144        }
145
146        @Override
147        public void getText(int offset, int length, Segment segment) throws BadLocationException {
148            this.document.getText(offset, length, segment);
149        }
150
151        @Override
152        public Position getStartPosition() {
153            return this.document.getStartPosition();
154        }
155
156        @Override
157        public Position getEndPosition() {
158            return this.document.getEndPosition();
159        }
160
161        @Override
162        public Position createPosition(int offset) throws BadLocationException {
163            return this.document.createPosition(offset);
164        }
165
166        @Override
167        public Element[] getRootElements() {
168            Element[] elements = this.document.getRootElements();
169            Element[] wrappers = new Element[elements.length];
170            for (int i = 0; i < elements.length; i++) {
171                wrappers[i] = new MyElement(elements[i]);
172            }
173            return wrappers;
174        }
175
176        @Override
177        public Element getDefaultRootElement() {
178            return new MyElement(this.document.getDefaultRootElement());
179        }
180
181        @Override
182        public void render(Runnable task) {
183            this.document.render(task);
184        }
185
186        private class MyElement implements Element {
187            private final Element element;
188
189            private MyElement(Element element) {
190                this.element = element;
191            }
192
193            @Override
194            public Document getDocument() {
195                return MyDocument.this;
196            }
197
198            @Override
199            public Element getParentElement() {
200                return new MyElement(this.element.getParentElement());
201            }
202
203            @Override
204            public String getName() {
205                return this.element.getName();
206            }
207
208            @Override
209            public AttributeSet getAttributes() {
210                return this.element.getAttributes();
211            }
212
213            @Override
214            public int getStartOffset() {
215                return this.element.getStartOffset();
216            }
217
218            @Override
219            public int getEndOffset() {
220                return this.element.getEndOffset();
221            }
222
223            @Override
224            public int getElementIndex(int offset) {
225                return this.element.getElementIndex(offset);
226            }
227
228            @Override
229            public int getElementCount() {
230                return this.element.getElementCount();
231            }
232
233            @Override
234            public Element getElement(int index) {
235                return new MyElement(this.element.getElement(index));
236            }
237
238            @Override
239            public boolean isLeaf() {
240                return this.element.isLeaf();
241            }
242        }
243    }
244}
245