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