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