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