Search code examples
javaeclipsepluginseclipse-plugin

Open a prespective on eclipse startup - programmatically


I am developing a eclipse plugin. I need to open my prespective when we open the eclipse at first time. Any ways to achieve this? i guess some listener must be available but could not trace out.

We can open a prespective after eclipse start using PlatformUI.getWorkbench().showPrespective(<prespective id>)

Similarly is there a way to open the prespective on eclipse startup, so that our desired prespective gets opened when starting the eclipse.


Solution

  • You can use the org.eclipse.ui.startup extension point in your plugin. When the plugin is activated, check/set a preference to decide if you want to switch perspectives and then schedule a UIJob do do it.

    1. Implement the extension point. Some class in the plugin needs implements org.eclipse.ui.IStartup. The activator class is fine in this case. Particularly, since you don't need anything in the earlyStartup method.

    2. In the start method, make the decision to switch and schedule it:

      public void start(BundleContext context) throws Exception {
          super.start(context);
          plugin = this;
      
          final boolean switchPerpective = processPluginUpgrading();
          if (switchPerpective) {
              final IWorkbench workbench = PlatformUI.getWorkbench();
              new UIJob("Switching perspectives"){
                  @Override
                  public IStatus runInUIThread(IProgressMonitor monitor) {
                      try {
                          workbench.showPerspective(perspectiveId, workbench.getActiveWorkbenchWindow());
                      } catch (WorkbenchException e) {
                          return new Status(IStatus.ERROR,PLUGIN_ID,"Error while switching perspectives", e);
                      }
                      return Status.OK_STATUS;
                  }}
              .run(new NullProgressMonitor());
          }
      }
      
    3. Use the preference store to keep data for your decision logic. In this implementation, the perspective is switched once per workspace whenever the plugin is upgraded. The data recorded in the preference store will allow a future version to have a difference policy. It uses the getPreferenceStore from AbstractUIPlugin so it is scoped per workspace. If you want to use other scopes, see the FAQ.

      private Boolean processPluginUpgrading() {
          final Version version = getDefault().getBundle().getVersion();
          final IPreferenceStore preferenceStore = getDefault().getPreferenceStore();
          final String preferenceName = "lastVersionActivated";
          final String lastVersionActivated = preferenceStore.getString(preferenceName);
          final boolean upgraded = 
                  "".equals(lastVersionActivated)
                  || (version.compareTo(new Version(lastVersionActivated)) > 0);
          preferenceStore.setValue(preferenceName, version.toString());
          return upgraded;
      }