This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 161211

Summary: Cut, Copy and Paste Actions Should Support Text Components
Product: platform Reporter: tomwheeler <tomwheeler>
Component: ActionsAssignee: Jaroslav Tulach <jtulach>
Status: VERIFIED FIXED    
Severity: blocker CC: saubrecht
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Attachments: My testing application
Changes that make it work (without tests)

Description tomwheeler 2009-03-26 01:44:34 UTC
A user on the dev@openide list asked why the Cut, Copy and Paste items on the edit menu are not enabled when a standard
Swing component has focus since these components offer default actions for cutting, copying and pasting text.  

It seems that these can only be enabled by changing each TopComponent in the application to swap out the action map to
use the Swing actions when the TopComponent receives focus and then swapping it back again when the TopComponent loses
focus using code like:

    @Override
    protected void componentActivated() {
        ActionMap actionMap = getActionMap();
        actionMap.put(DefaultEditorKit.copyAction, new DefaultEditorKit.CopyAction());
        actionMap.put(DefaultEditorKit.cutAction, new DefaultEditorKit.CutAction());
        actionMap.put(DefaultEditorKit.pasteAction, new DefaultEditorKit.PasteAction());

        super.componentActivated();
    }

    @Override
    protected void componentDeactivated() {
        ActionMap actionMap = getActionMap();
        actionMap.put(DefaultEditorKit.copyAction, SystemAction.get(CopyAction.class));
        actionMap.put(DefaultEditorKit.cutAction, SystemAction.get(CutAction.class));
        actionMap.put(DefaultEditorKit.pasteAction, SystemAction.get(PasteAction.class));

        super.componentDeactivated();
    }

This is tedious and duplicative.  Perhaps there is an easier way, in which case it should be documented.  If not, would
it be possible to make the NetBeans Cut, Copy and Paste actions could delegate to the Swing Cut, Copy and Paste actions
so that cutting, copying and pasting text as well as nodes will work.

For context, see this thread:
   http://openide.netbeans.org/servlets/ReadMsg?list=dev&msgNo=38176
Comment 1 Jaroslav Tulach 2010-11-08 13:32:16 UTC
I've created TextTopComponent with one JTextArea and as far as I can tell, the Copy and Cut actions from Toolbar are working correctly. But:

The copy and cut actions from Edit menu do not work.
The paste action does not even get enabled.
Comment 2 Jaroslav Tulach 2010-11-08 13:47:38 UTC
The reason why toolbar works and menu does not is that the code in: DelegateActionMap.java:

owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

returns JTextArea in the first place and JRootPane in the second, Standa, please help me!
Comment 3 Jaroslav Tulach 2010-11-08 14:02:57 UTC
Per Standa's suggestion: Using getPermanentFocusOwner() in core-main#a362f628409b
Comment 4 Jaroslav Tulach 2010-11-09 15:51:40 UTC
Created attachment 102859 [details]
My testing application
Comment 5 Jaroslav Tulach 2010-11-09 15:53:45 UTC
Created attachment 102860 [details]
Changes that make it work (without tests)
Comment 6 Quality Engineering 2010-11-10 06:11:40 UTC
Integrated into 'main-golden', will be available in build *201011100000* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/a362f628409b
User: Jaroslav Tulach <jtulach@netbeans.org>
Log: #161211: Using getPermanentFocusOwner()
Comment 7 Jaroslav Tulach 2010-11-10 12:00:05 UTC
core-main#c0de415adb58
Comment 8 Quality Engineering 2010-11-11 06:10:25 UTC
Integrated into 'main-golden', will be available in build *201011110000* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/a9af868ff294
User: Jaroslav Tulach <jtulach@netbeans.org>
Log: #161211: TopComponent's ActionMap needs to return keys of currently focused component from allKeys()
Comment 9 tomwheeler 2010-11-11 17:10:11 UTC
I verified that cut/copy/paste work correctly on a textfield inside a TopComponent using all combinations of those actions from the main menu, toolbar buttons and context menu.  This was done using the most recent nightly build (details below).  

Thanks!


Product Version: NetBeans IDE Dev (Build 201011110000)
Java: 1.6.0_21; Java HotSpot(TM) Client VM 17.0-b16
System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)