Menu

Actions at the view level

2010-11-15
2013-05-01
  • Serge Rosmorduc

    Serge Rosmorduc - 2010-11-15

    Hello,

    I'm using the application framework in jhotdraw 7 right now (without the graphical layer) to benefit from its infrastructure in an already existing application.

    In this application, I build Actions object which are built into my graphical components. Now, I'd like to add menu entries for them.

    The problem is the following: I can export them through the view action map. But in this respect they only exist if  a view is available, which is not always the case (MacOSXApplication or MDIApplication). I can figure a number of ways to fix this, but
    is there some kind of best practice in this case?

    (to sum up:
        a graphical object has a number of actions (and hence, for instance, some keyboard shortcuts actions by default).
        the application uses a view which knows the object.
        to create a menu, one has to know the action, but the action must exist even when view is null.
    )

     
  • Werner Randelshofer

    Dear rosmord,

    This functionality is supported by the framework, but it is not (well) documented.

    Short answer:
    Make sure that your ApplicationModel populates the global action map when it is requested by the application object using ApplicationModel.createActionMap(application, null).

    Long answer:
    The application framework supports a hierarchy of ActionMap's:

    Root action map:
    At the root, there is an action map with actions that are crucial for the functionality of a specific application type. This map is created by the Application object itself. For example OSXApplication puts actions which deal with Apple events into this map.

    Global action map:
    On the next level there is an action map with global actions for all views and for JFrame's and JDialogs which are not associated to a specific view (for example for the screen menu bar). This map is created by the ApplicationModel when applicationModel.createActionMap(application, null) is invoked. Most actions should go in here, for example FileNewAction.

    View action map:
    Next, there is an action map with actions for a specific view. This map is created by the ApplicationModel when applicationModel.createActionMap(application, view) is invoked. Actions which have different states per view should go in here, for example UndoAction.

    These ActionMap's are connected using their "parent" attributes. If an action is not found in an ActionMap, search continues in the parent map.

    When building menu bars, Application objects make use of this hierarchy in different ways:

    SDIApplication creates a menu bar for each view and thus retrieves the corresponding actions from the view action map.

    MDIApplication creates a global menu bar for all views and retrieves all corresponding actions from the global action map.

    OSXApplications creates multiple global menu bars and per-view menu bars. To prevent that the global menu bars look totally different from view-specific menu bars, it is recommended to put stub actions into the global action map for each action added to the view action map.

    The Teddy sample application is a good demo for the application framework.
    http://www.randelshofer.ch/oop/jhotdraw/Documentation/sample_teddy_application.html

    Try running it on OSX and then close or minimize all views so that only the screen menu bar is active. Notice how the screen menu bar still contains menu items. Some of them are active, such as "File > New" or "Edit > Copy". Others are inactive such as "File > Close" or "Edit > Undo".

    Important information: I am in the process of reworking the way menu bars are built by the application framework, because the current design is not fine grained enough and because it leaks memory. Method ApplicationModel.createMenus(application, view) will be replaced by method .getMenuBuilder() which will return an abstract factory class named MenuBuilder. MenuBuilder will be used each time a JMenuBar needs to be created by the application. I plan to commit these changes in the next few weeks. They will go into JHotDraw 7.6 which I plan to release in January 2011.

    I hope this helps,
    Werner

     

Log in to post a comment.