|
| 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 | +import java.awt.Container; |
| 24 | +import java.awt.event.KeyEvent; |
| 25 | +import java.awt.Robot; |
| 26 | +import javax.swing.JFrame; |
| 27 | +import javax.swing.JScrollPane; |
| 28 | +import javax.swing.JTextArea; |
| 29 | +import javax.swing.SwingUtilities; |
| 30 | +import javax.swing.event.DocumentEvent; |
| 31 | +import javax.swing.event.UndoableEditEvent; |
| 32 | +import javax.swing.event.UndoableEditListener; |
| 33 | +import javax.swing.text.BadLocationException; |
| 34 | +import javax.swing.text.AbstractDocument; |
| 35 | +import javax.swing.text.Document; |
| 36 | +import javax.swing.text.PlainDocument; |
| 37 | +import javax.swing.undo.CompoundEdit; |
| 38 | +import javax.swing.undo.UndoManager; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @key headful |
| 43 | + * @bug 8190763 |
| 44 | + * @summary Class Cast Exception on (CompoundEdit) UndoableEditEvent.getEdit() |
| 45 | + * @run main TestCCEOnEditEvent |
| 46 | + */ |
| 47 | +public class TestCCEOnEditEvent { |
| 48 | + private Container contentPane = null; |
| 49 | + private JTextArea textArea = null; |
| 50 | + private static Robot robot; |
| 51 | + private static JFrame frame; |
| 52 | + public static void main(String[] arguments) throws Exception { |
| 53 | + try{ |
| 54 | + robot = new Robot(); |
| 55 | + robot.setAutoDelay(50); |
| 56 | + TestCCEOnEditEvent test = new TestCCEOnEditEvent(); |
| 57 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + test.createAndShowGUI(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + robot.keyPress(KeyEvent.VK_ENTER); |
| 65 | + robot.keyRelease(KeyEvent.VK_ENTER); |
| 66 | + robot.waitForIdle(); |
| 67 | + } finally { |
| 68 | + SwingUtilities.invokeLater(new Runnable(){ |
| 69 | + @Override |
| 70 | + public void run(){ |
| 71 | + frame.dispose(); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void createAndShowGUI() { |
| 78 | + frame = new JFrame(); |
| 79 | + contentPane = frame.getContentPane(); |
| 80 | + createTextArea(); |
| 81 | + frame.setSize(200, 200); |
| 82 | + frame.setVisible(true); |
| 83 | + } |
| 84 | + |
| 85 | + private void createTextArea() { |
| 86 | + textArea = new JTextArea("Text Area") { |
| 87 | + @Override |
| 88 | + protected Document createDefaultModel() { |
| 89 | + return new PlainDocument() { |
| 90 | + @Override |
| 91 | + protected void fireUndoableEditUpdate(UndoableEditEvent event) { |
| 92 | + Object[] listeners = listenerList.getListenerList(); |
| 93 | + for (int index = listeners.length - 2; index >= 0; index -= 2) { |
| 94 | + Object listenerType = listeners[index]; |
| 95 | + if (listenerType == UndoableEditListener.class) { |
| 96 | + super.fireUndoableEditUpdate(event); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | + }; |
| 103 | + textArea.getDocument().addUndoableEditListener(new UndoManager() { |
| 104 | + @Override |
| 105 | + public void undoableEditHappened(UndoableEditEvent event) { |
| 106 | + CompoundEdit edit = null; |
| 107 | + try { |
| 108 | + edit = (CompoundEdit) event.getEdit(); |
| 109 | + } |
| 110 | + catch(ClassCastException e ) { |
| 111 | + throw new RuntimeException("Class Cast Exception is thrown on (CompoundEdit) UndoableEditEvent.getEdit()"); |
| 112 | + } |
| 113 | + AbstractDocument.DefaultDocumentEvent documentEvent = |
| 114 | + (AbstractDocument.DefaultDocumentEvent) edit; |
| 115 | + DocumentEvent.EventType editType = documentEvent.getType(); |
| 116 | + int editOffset = documentEvent.getOffset(); |
| 117 | + int editLength = documentEvent.getLength(); |
| 118 | + |
| 119 | + } |
| 120 | + }); |
| 121 | + try { |
| 122 | + textArea.setSelectionStart(textArea.getLineEndOffset(0)); |
| 123 | + } |
| 124 | + catch (BadLocationException exception) { |
| 125 | + } |
| 126 | + JScrollPane scrollPane = new JScrollPane(textArea, |
| 127 | + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 128 | + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 129 | + |
| 130 | + contentPane.add(scrollPane); |
| 131 | + } |
| 132 | +} |
0 commit comments