@Override
  public void selectPerspective(Perspective perspective) {
    if (perspective.getName().equals(selectedPerspective)) {
      return;
    }

    openAndCloseMembers(perspective);

    selectedPerspective = perspective.getName();
    NbPreferences.root().put(SELECTED_PERSPECTIVE_PREFERENCE, selectedPerspective);
  }
 @Override
 public Perspective getSelectedPerspective() {
   for (Perspective p : perspectives) {
     if (p.getName().equals(selectedPerspective)) {
       return p;
     }
   }
   return null;
 }
  private void openAndCloseMembers(Perspective perspective) {
    // Close other perspective based on group name
    for (Perspective g : perspectives) {
      if (g != perspective) {
        TopComponentGroup tpg = WindowManager.getDefault().findTopComponentGroup(g.getName());
        if (tpg != null) {
          tpg.close();
        }
      }
    }

    // Open perspective
    TopComponentGroup tpg = WindowManager.getDefault().findTopComponentGroup(perspective.getName());
    if (tpg != null) {
      tpg.open();
    }

    // Close members
    for (TopComponent c : WindowManager.getDefault().getRegistry().getOpened()) {
      String pId = WindowManager.getDefault().findTopComponentID((TopComponent) c);
      for (PerspectiveMember perspectiveMember : members) {
        if (pId.equals(perspectiveMember.getTopComponentId())
            && !perspectiveMember.isMemberOf(perspective)) {
          boolean closed = c.close();
        }
      }
    }

    // Open members
    for (PerspectiveMember perspectiveMember : members) {
      if (perspectiveMember.isMemberOf(perspective)) {
        String pId = perspectiveMember.getTopComponentId();
        TopComponent c = WindowManager.getDefault().findTopComponent(pId);
        if (c != null && !c.isOpened()) {
          c.open();
        }
      }
    }
  }
  public PerspectiveControllerImpl() {
    // Load perspectives
    perspectives = Lookup.getDefault().lookupAll(Perspective.class).toArray(new Perspective[0]);
    members =
        Lookup.getDefault().lookupAll(PerspectiveMember.class).toArray(new PerspectiveMember[0]);

    // Find if there is a default
    String firstPerspective = perspectives.length > 0 ? perspectives[0].getName() : null;
    String defaultPerspectiveName = System.getProperty("org.gephi.perspective.default");
    if (defaultPerspectiveName != null) {
      for (Perspective p : perspectives) {
        if (p.getName().equals(defaultPerspectiveName)) {
          selectedPerspective = p.getName();
          break;
        }
      }
    }
    if (selectedPerspective == null) {
      selectedPerspective =
          NbPreferences.root().get(SELECTED_PERSPECTIVE_PREFERENCE, firstPerspective);
    }

    // Store selected in prefs
    NbPreferences.root().put(SELECTED_PERSPECTIVE_PREFERENCE, selectedPerspective);

    WindowManager.getDefault()
        .invokeWhenUIReady(
            new Runnable() {

              @Override
              public void run() {
                // Set to previously selected perspective
                Perspective selectedPerspectiveInstance = getSelectedPerspective();

                openAndCloseMembers(selectedPerspectiveInstance);
              }
            });
  }