class SpoonPerspectiveManager {

  public void activatePerspective( Class<? extends SpoonPerspective> clazz ) throws KettleException {

    if ( this.forcePerspective ) {
      // we are currently prevented from switching perspectives
      return;
    }
    SpoonPerspective sp = perspectives.get( clazz );
    if ( sp == null ) {
      throw new KettleException( "Could not locate perspective by class: " + clazz );
    }
    PerspectiveManager perspectiveManager = getPerspectiveManagerMap().get( sp );
    if ( perspectiveManager != null ) {
      perspectiveManager.initializeIfNeeded();
    }
    unloadPerspective( activePerspective );
    activePerspective = sp;

    List<XulOverlay> overlays = sp.getOverlays();
    if ( overlays != null ) {
      for ( XulOverlay overlay : overlays ) {
        try {
          ResourceBundle res = null;
          if ( overlay.getResourceBundleUri() != null ) {
            try {
              res = ResourceBundle.getBundle( overlay.getResourceBundleUri() );
            } catch ( MissingResourceException ignored ) {
              // Ignore errors
            }
          } else {
            try {
              res = ResourceBundle.getBundle( overlay.getOverlayUri().replace( ".xul", ".properties" ) );
            } catch ( MissingResourceException ignored ) {
              // Ignore errors
            }
          }
          if ( res == null ) {
            res = new XulSpoonResourceBundle( sp.getClass() );
          }
          domContainer.loadOverlay( overlay.getOverlayUri(), res );
        } catch ( XulException e ) {
          log.logError( "Error activate perspective", e );
        }
      }
    }

    List<XulEventHandler> theXulEventHandlers = sp.getEventHandlers();
    if ( theXulEventHandlers != null ) {
      for ( XulEventHandler handler : theXulEventHandlers ) {
        domContainer.addEventHandler( handler );
      }
    }

    sp.setActive( true );
    if ( sp.equals( activePerspective ) ) {
      deck.setSelectedIndex( deck.getChildNodes().indexOf( deck.getElementById( "perspective-" + sp.getId() ) ) );
      getSpoon().enableMenus();
    }
  }

  private  SpoonPerspectiveManager();

}