Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Actions



Actions are classes which implement the MDIAction interface, which can perfom an action in the application. The code relevant to the action itself should be called in the MDIAction.run() method.

Executing the action

Actions are executed in a background Thread. The application has several methods which can be used to execute the action:
  • MDIApplication.executeAction(MDIAction), which will perfom the action in a background Thread using the current application ClassLoader, and with the default blocking behavior for the application (which is not blocking by default)
  • MDIApplication.executeAction(MDIAction, short, ClassLoader), which will perfom the action in a background Thread using the current application ClassLoader, and a specified blocking behavior for the application
  • MDIApplication.executeAction(MDIAction, ClassLoader), which will perform the action in a background Thread using the a specified ClassLoader, and with the default blocking behavior for the application (which is not blocking by default). This can be useful when executing an action in a Plugin, if the Plugin uses resources[1]
    In that case, they would not be seen in the application ClassLoader, because the Plugin is loaded by reflection
  • MDIApplication.executeAction(MDIAction, ClassLoader), which will perform the action in a background Thread using the a specified ClassLoader, and a specified blocking behavior for the application

Blocking and not blocking actions

By default the application will execute the actions in a background thread (not blocking). However it is possible to change this behavior by setting the AbstractApplication.setDefaultBlockingActions(boolean) method (true means that the actions will be blocking by default).

It is also possible to override this default behavior at the action level (see executing the action). The types of blocking are:
  • The BlockingType.NOT_BLOCKING will perform the computation in a background thread (not blocking for the calling thread). It means that the action MDIAction.run() method will not be performed in the AWT event Thread
  • The BlockingType.BLOCKING will perform the computation in the same Thread as the caller, and will therefore be blocking
  • The BlockingType.UI_NOT_BLOCKING will perform the computation in the Swing event Thread. It will be performed asynchronously on the AWT event dispatching thread
  • The BlockingType.UI_BLOCKING will perform the computation in the Swing event Thread. It will be performed synchronously on the AWT event dispatching thread and will therefore be blocking
  • The BlockingType.DEFAULT will use the default behavior

Before and after the action

When the action is performed, the AbstractApplication.endAction(MDIAction) method will be called. It is also possible to register a MDIActionHandler with the action, which will be fired before and after the action is performed.

Independently of the blocking type, the MDIAction.endAction() method is guaranteed to be performed in the AWT event Thread after the execution has ended.


actionThread

GUI Swing action

The AbstractMDIAction is an Action which is also a Swing AbstractAction, and as such can be simply used in Swing GUIs.

Note that by default the MDIAction.run() method will be executing in a background Thread, so not in the AWT event Thread. If you want to perform actions in the AWT event Thread when the action has finished, you should do so in the MDIAction.endAction() method.

Examples

Basic example

Suppose a simple action which only prints something on the output stream:
      protected class PrintAction extends AbstractMDIAction {
        public PrintAction(String name) {
          super(app, name);
          this.setDescription("Print text", "Print text");
        }

         public void run() throws Exception {
           System.out.println("Hello World");
         }
        }
We can execute this action in a background thread in the Menu factory simply by:
      PrintAction action = new PrintAction();
      getApplication().executeAction(action);

Example which creates a new tab at the end of the action

Suppose an action which perform some lengthy operation, and then creates a new tab in the application. We need to perform the operation in a background Thread, and create the tab in the AWT event Thread:

      protected class CreateTabAction extends AbstractMDIAction {
        Object result = null; // will be the result of the action

        public CreateTabAction(String name) {
          super(app, name);
          this.setDescription("Create Tab", "Do something lenghty");
        }

         public void run() throws Exception {
           result = ... Perform a lengthy computation
         }

         public void endAction() {
           JComponent comp = // create JComponent from result;
           ((TabbedApplication)app).addTab(pane, result, getActionName());
         }
        }

Example which only adds a tab

This example only creates a new tab in the application. We don't need to perform any lengthy operation in a background thread, so we will do nohing in the run() method:

      protected class CreateTabAction extends AbstractMDIAction {
        public CreateTabAction(String name) {
          super(app, name);
          this.setDescription("Create Tab", "Do something in the GUI");
        }

         public void run() throws Exception {
           // do nothing
         }

         public void endAction() {
           JComponent comp = // create the JComponent
           ((TabbedApplication)app).addTab(pane, result, getActionName());
         }
        }

Notes

  1. ^ In that case, they would not be seen in the application ClassLoader, because the Plugin is loaded by reflection

See also


Categories: general | swing

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences