Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Hot Plugins activation and deactivation



Plugins will be allowed to be enabled and disabled at runtime in the AboutPlugins dialog depending on the result of the MDIApplication.getPluginsDeactivationPolicy() method. The method to set the policy is the MDIApplication.setPluginsDeactivationPolicy(short) and it should be set in the Application initialization:
By default Plugins cannot be enabled and disabled at runtime in the AboutPlugins dialog.

Prerequisites

Application configuration

You need to setup the application configuration for this mechanism to work, but you don't need to create a Configuration class.

This example will not work:
   public class MyApplication extends AbstractMDIApplication {      
      public MyApplication {
        super("MyApplication");
        this.setPluginsDeactivationPolicy(PluginsDeactivationPolicy.DEACTIVATION_IMMEDIATE);      
      }
   }
will not work because the application has no configuration management.

   public class MyApplication extends AbstractMDIApplication {
      private Preferences pref = null;
      
      public MyApplication {
        super("MyApplication");
        conf = AppliConfiguration.getInstance();
        Preferences pref = Preferences.userRoot();
        this.initConfiguration(pref, null);
        this.setPluginsDeactivationPolicy(PluginsDeactivationPolicy.DEACTIVATION_IMMEDIATE);      
      }
   }
will work because the application has configuration management.

And also:
   public class MyApplication extends AbstractMDIApplication {
      private Preferences pref = null;
      
      public MyApplication {
        super("MyApplication");
        Preferences pref = Preferences.userRoot();
        this.initConfiguration(pref, null);
        this.setPluginsDeactivationPolicy(PluginsDeactivationPolicy.DEACTIVATION_IMMEDIATE);      
      }
   }
will also work because the application has configuration management, even if no application configuration is defined.

When to activate the policy

You need to set the Plugins deactivation policy: For example:
   pref = Preferences.userRoot();
   this.initConfiguration(pref, null);
   this.setPluginsDeactivationPolicy(PluginsDeactivationPolicy.DEACTIVATION_IMMEDIATE);
   this.registerPlugins();

Example

This example allows Plugins to be enabled or disabled at runtime:
   public class ConfigSampleMDI extends AbstractMDIApplication {
      private Preferences pref = null;

      public ConfigSampleMDI() {
        super("ActionsTutorialMDI");

        ImageIcon splash = new ImageIcon(this.getClass().getResource("splash.png"));
        SplashScreen splashdialog = new SplashScreen(splash, "0.1", "Build xxxx", true);

        pluginsDir = new File(System.getProperty("user.dir"), "plugins"); // plugins directory

        // initialize configuration
        pref = Preferences.userRoot();
        this.initConfiguration(pref, null);
        this.setPluginsDeactivationPolicy(PluginsDeactivationPolicy.DEACTIVATION_IMMEDIATE);
        // register plugins in the plugins directory (same as main application jar directory)
        this.registerPlugins();

        // create the menus with the Application menu factory
        mfactory = new SecondMenuFactory();
        mfactory.setProgressInterface(splashdialog);
        super.preparePanels(4, true, true, mfactory);
        // set the size of the Application window
        this.setSize(500, 500);

        splashdialog.dispose();
        splashdialog = null;
      }

      public static void main(String[] args) {
        ConfigSampleMDI mdi = new ConfigSampleMDI();
        mdi.setVisible(true);
      }
   }

Categories: Gui | Plugins | Swing

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