• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Enabling / disabling of menu items

This is a rather poxy and general question, but perhaps someone can give me a few ideas.

Menu items (and toolbar buttons) may change a little to have extra options / enable or disable certain items etc. depending on what perspective / window is "on" at that moment in time.
Without getting to bogged down with the precise nature and structure of my project:
  * where should the menus be created? (and if a general menu is shared by 2 windows is there a good way of getting the action command so things dont get hard-coded in several classes)
  * and what about the methods which decide what menu items are enabled / disabled depending on certain local criteria? (is an interface a good idea here?
  * and for toolbar buttons.

I guess my question is how do YOU solve these issues. Any good ideas and direction much appreciated!

Regards,
Cathal.
0
cathalmchale
Asked:
cathalmchale
  • 7
  • 5
1 Solution
 
objectsCommented:
Define the actions performed by your menu items as Action subclasses.
You can then share these actions between different windows.
And to enable/disable the menu item, you enable/disable the action.
0
 
objectsCommented:
The same actions can also be used for buttons, and toolbars
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
cathalmchaleAuthor Commented:
so a package called .Actions could be a good idea - with lots of different action sub-classes?

But I still don't fully understand. In the Sun example there are 2 ways of performing a general action. What would it look like if I wanted to implement a save or save as action -> we have to decide if there is something saveable in the workspace, if so then perform this guys save routine (so does this action class not need to know a little more - eg a way of getting a reference to the active "workspace document") - and then in this case who is telling the action whether to have save enabled / disabled depending on whether there is a saveable entity in the workspace at the current time.

Hope this doesnt read to gibberish-ly!! but another similar response would send me on my way nicely, thanks!
0
 
objectsCommented:
yes the action class will need access to all the objects required to perform the action.
0
 
cathalmchaleAuthor Commented:
and when the active document in the workspace changes is this when to call the appropriate setEnabled(..) methods in the actions??
0
 
objectsCommented:
yes it could be handled that way, another way would be to use listeners so the action is informed when the active doc changews allowing it to enable/disable itself as appropriate.
0
 
cathalmchaleAuthor Commented:
Hi again!

for the action we have:
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);

can i do the same for an accelerator key? and an mnemonic displayed index?
0
 
cathalmchaleAuthor Commented:
OK I see how this can be done, although there is no fileld in AbstractAction for a "mnemonic displayed index"??  so will I just have to set this on my menu item / button myself?
0
 
objectsCommented:
Yes I think you'll need to set the index yourself.
0
 
cathalmchaleAuthor Commented:
This all seems to be working quite nicely now, but what about the following:

I have a popup menu with options such as     New, open, delete etc.   These pop-ups appear when the user right clicks in a certain window.   This window has a tree and a table, and the action is slightly different depending on the origin of the right click (eg, a delete on the tree deletes the entire selected directory, where as a delete on the table just deletes the selected file!!)

can i still solve this problem using the action idea -> is the solution simply - if the actions are different then make 2 actions (TableDeleteAction, TreeDeleteAction)

Thanks.
0
 
zzynxSoftware engineerCommented:
>> is the solution simply - if the actions are different then make 2 actions (TableDeleteAction, TreeDeleteAction)
I tend to answer: yes, indeed.
You could do it with one but I think that would make your action code more (/too?) complex.
0
 
objectsCommented:
The two actions could inherit from the say a DeleteAction which contained the non-component specific functionality.
Or you use a single action that works regardless of the componwent.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now