NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Context aware action and activated node

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users
View previous topic :: View next topic  
Author Message
theophil



Joined: 03 Aug 2009
Posts: 15

PostPosted: Wed Dec 02, 2009 8:36 am    Post subject: Context aware action and activated node Reply with quote

Hi,

I have a question concerning the context actions listen to. I've tried to implement a context-aware action which works fine within my Project view. Depending on which node is selected, my actions in the menu bar and toolbar are enabled/disabled. The problem is, as soon as the Properties window gets the focus, all actions are activated, no matter what is currently shown in that window.

Now, all my actions listen on a specific node type, and, with all projects closed and Project window closed, the actions are still enabled when opening the Properties window. I have no idea wher he gets the nodes from...

My actions are strutured exactly as the example in the DevFaq (under 'Roll your own'), with the same methods getting called at the same time. In that example, the listener is added in the init() method, which gets called in isEnabled() and actionPerformed(). Maybe I should call init() in the main constructor too?

http://wiki.netbeans.org/DevFaqActionContextSensitive

I think I'm just missing something here, maybe someone can give ma a hint?

Thanks

Philippe
Back to top
theophil



Joined: 03 Aug 2009
Posts: 15

PostPosted: Wed Dec 02, 2009 8:41 am    Post subject: Reply with quote

init() in the private constructor doesn't do it, as I'm getting an AssertionError ("this shall be called just from AWT thread") at startup. And my actions aren't shown/created at all....
Back to top
Michal Bachorik
Posted via mailing list.





PostPosted: Wed Dec 02, 2009 8:52 am    Post subject: [platform-dev] Re: Context aware action and activated node Reply with quote

you can override the "enable(..)" method of your action, and explicitly
get "cookies" from a global lookup. we had similar issue, and such
approach solved it.

m.

theophil wrote:
Quote:
Hi,

I have a question concerning the context actions listen to. I've tried to implement a context-aware action which works fine within my Project view. Depending on which node is selected, my actions in the menu bar and toolbar are enabled/disabled. The problem is, as soon as the Properties window gets the focus, all actions are activated, no matter what is currently shown in that window.

Now, all my actions listen on a specific node type, and, with all projects closed and Project window closed, the actions are still enabled when opening the Properties window. I have no idea wher he gets the nodes from...

My actions are strutured exactly as the example in the DevFaq (under 'Roll your own'), with the same methods getting called at the same time. In that example, the listener is added in the init() method, which gets called in isEnabled() and actionPerformed(). Maybe I should call init() in the main constructor too?

http://wiki.netbeans.org/DevFaqActionContextSensitive

I think I'm just missing something here, maybe someone can give ma a hint?

Thanks

Philippe




Back to top
theophil



Joined: 03 Aug 2009
Posts: 15

PostPosted: Wed Dec 02, 2009 9:28 am    Post subject: Context aware action and activated node Reply with quote

Thanks for the answer Michal. You probably mean the setEnabled() method, as there is no enable() in AbstractAction.

I found another solution after trying for a while. I'm just posting it here so others might benefit from it:

The problem was that, for an obscure reason, lkpInfo.allItems() wasn't empty when a Properties window opened. Don't ask me why....

setEnabled(lkpInfo.allItems().size() != 0);

Thus, when resultChanged(null) is called in the init() method, it would enable the action. I inspected the single Item<NodeWeListenFor> returned by lkpInfo.allItems() and it had in fact no underlying node (=null). This looks like a bug to me. The solution to my problem was simply to replace allItems() with allInstances(), so no node would be found and the action wouldn't be enabled:

setEnabled(lkpInfo.allInstances().size() != 0);

Maybe the DevFaq entry should be updated?

Regards,

Philippe
Back to top
Jesse Glick
Posted via mailing list.





PostPosted: Wed Dec 02, 2009 6:06 pm    Post subject: [platform-dev] Re: Context aware action and activated node Reply with quote

theophil wrote:
Quote:
setEnabled(lkpInfo.allItems().size() != 0);

[...] I inspected the single Item<NodeWeListenFor> returned by lkpInfo.allItems() and it had in fact no underlying node (=null).

DefaultTopComponentLookup.NoNodesPair is probably what you got. It seems that if TopComponent.getActivatedNodes() is null (as opposed to a zero-length array), then the
TopComponent.getLookup().lookupResult(Node.class) intentionally includes an Item with a null instance. This does not affect Result.allInstances, which always discards any
nulls, but enables certain actions to use allItems to differentiate between a context that momentarily happens to include no selected nodes (e.g. Projects tab with
nothing selected), vs. a selection that inherently cannot have any nodes (e.g. Output Window). NodeAction takes advantage of this trick to avoid disabling an action
unnecessarily: say you clicked a file in Projects, then gave focus to the Output Window; Tools > Add to Favorites will remain enabled (and if invoked will add that file)
even though the current selection has no nodes in it.

As you found, allInstances is more likely what you want. I will update DevFaqActionContextSensitive, and mention this fairly obscure behavior in Javadoc for
TopComponent.getLookup.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo