Solved

Enabling / disabling of menu items

Posted on 2004-10-31
185 Views
Last Modified: 2010-05-18
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
Question by:cathalmchale
    13 Comments
     
    LVL 92

    Expert Comment

    by:objects
    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
     
    LVL 92

    Expert Comment

    by:objects
    The same actions can also be used for buttons, and toolbars
    0
     
    LVL 92

    Expert Comment

    by:objects
    0
     

    Author Comment

    by:cathalmchale
    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
     
    LVL 92

    Accepted Solution

    by:
    yes the action class will need access to all the objects required to perform the action.
    0
     

    Author Comment

    by:cathalmchale
    and when the active document in the workspace changes is this when to call the appropriate setEnabled(..) methods in the actions??
    0
     
    LVL 92

    Expert Comment

    by:objects
    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
     

    Author Comment

    by:cathalmchale
    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
     

    Author Comment

    by:cathalmchale
    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
     
    LVL 92

    Expert Comment

    by:objects
    Yes I think you'll need to set the index yourself.
    0
     

    Author Comment

    by:cathalmchale
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    >> 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
     
    LVL 92

    Expert Comment

    by:objects
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    Suggested Solutions

    Title # Comments Views Activity
    fizzArray  challenge 1 22
    map interface methods 3 35
    fix34  challenge 9 49
    ArrayLists in ArrayLists/HashMaps? 4 14
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now