bobbylight / RSyntaxTextArea

A syntax highlighting, code folding text editor for Java Swing applications.
BSD 3-Clause "New" or "Revised" License
1.12k stars 259 forks source link

ClassCastException when copying/cutting text from a RTextArea #490

Closed priand closed 1 year ago

priand commented 1 year ago

Description A class cast exception happens when a user cut/copy the text contains in a RTextArea (-> not the syntax text one):

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class org.monflabs.devtoolbox.swing.components.TextArea cannot be cast to class org.fife.ui.rsyntaxtextarea.RSyntaxTextArea (org.monflabs.devtoolbox.swing.components.TextArea and org.fife.ui.rsyntaxtextarea.RSyntaxTextArea are in unnamed module of loader 'app')
    at org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit$CopyCutAsStyledTextAction.actionPerformedImpl(RSyntaxTextAreaEditorKit.java:684)
    at org.fife.ui.rtextarea.RecordableTextAction.actionPerformed(RecordableTextAction.java:106)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)

It happens in this statement: ((RSyntaxTextArea)textArea).copyAsStyledText(theme);

When I look at the editor kit assigned to the RTextArea, it is of class org.fife.ui.rtextarea.RTextAreaEditorKit, so I'm not sure how it delegates to RSyntaxTextAreaEditorKit

Steps to Reproduce Create an application with both a RTextArea and a RSyntaxTextArea. In the former, display the popup menu and select 'copy' or 'cut'

Expected behavior Test should be properly copied

Actual behavior Text is not copied and an exception is thrown

Java version 17

bobbylight commented 1 year ago

This should be fixed in a more robust way, but fixing this in the simplest way possible for now. The actual issue is how how RTextArea and RSyntaxTextArea instances share the actions displayed in their context menus, which means if you mix both components in an application, they'll both get the RSTA actions.

priand commented 1 year ago

Thank you!