void handleChildAddition(MPerspectiveStack parent, MPerspective element) {
    if (element.isToBeRendered() && element.isVisible()) {
      int idx = getRenderedIndex(parent, element);

      AbstractRenderer<MPerspective, ?> renderer = factory.getRenderer(element);
      WPerspectiveStack<N, I, IC> stack = getWidget(parent);
      WStackItem<I, IC> item = createStackItem(getWidget(parent), element, renderer);

      stack.addItems(idx, Collections.singletonList(item));
    }
  }
  @Override
  public void doProcessContent(MPerspectiveStack element) {
    WPerspectiveStack<N, I, IC> stack = getWidget(element);
    List<WStackItem<I, IC>> items = new ArrayList<WStackItem<I, IC>>();
    WStackItem<I, IC> initalItem = null;

    for (MPerspective e : element.getChildren()) {
      // Precreate the rendering context for the subitem
      AbstractRenderer<MPerspective, ?> renderer = factory.getRenderer(e);
      if (renderer != null && e.isToBeRendered() && e.isVisible()) {
        WStackItem<I, IC> item = createStackItem(stack, e, renderer);
        items.add(item);

        if (e == element.getSelectedElement()) {
          initalItem = item;
        }
      }
    }

    if (!items.isEmpty()) {
      if (initalItem == null || items.size() == 1 || items.get(0) == initalItem) {
        stack.addItems(items);
      } else {
        stack.addItem(initalItem);
        if (items.get(items.size() - 1) == initalItem) {
          stack.addItems(0, items.subList(0, items.size() - 1));
        } else {
          int idx = items.indexOf(initalItem);
          stack.addItems(0, items.subList(0, idx));
          stack.addItems(items.subList(idx + 1, items.size()));
        }
      }
    }

    if (element.getSelectedElement() != null) {
      handleSelectedElement(element, null, element.getSelectedElement());
    } else if (!element.getChildren().isEmpty()) {
      // TODO Should this be done through the part service????
      element.setSelectedElement(element.getChildren().get(0));
    }
  }
  @Override
  public void processContents(MElementContainer<MUIElement> element) {
    if (element.getChildren().isEmpty()) return;

    MPerspectiveStack perspectiveStack = (MPerspectiveStack) (MElementContainer<?>) element;
    MPerspective selectedPerspective = perspectiveStack.getSelectedElement();

    if (selectedPerspective == null) {
      selectedPerspective = (MPerspective) findFirstRenderableAndVisibleElement(perspectiveStack);
      if (selectedPerspective != null) switchPerspective(selectedPerspective);
    } else if (!selectedPerspective.isToBeRendered() || !selectedPerspective.isVisible()) {
      selectedPerspective = (MPerspective) findFirstRenderableAndVisibleElement(perspectiveStack);
      if (selectedPerspective != null) switchPerspective(selectedPerspective);
      else perspectiveStack.setSelectedElement(null);
    } else {
      // reset selected element (set selected element handler will work)
      perspectiveStack.setSelectedElement(null);
      switchPerspective(selectedPerspective);
    }

    refreshPerspectiveStackVisibility(perspectiveStack);
  }
  private void openMenuFor(ToolItem item, MPerspective persp) {
    final Menu menu = new Menu(psTB);
    menu.setData(persp);
    if (persp.getParent().getSelectedElement() == persp) {
      addSaveAsItem(menu);
      addResetItem(menu);
    }

    if (persp.isVisible()) {
      addCloseItem(menu);
    }

    new MenuItem(menu, SWT.SEPARATOR);
    // addDockOnSubMenu(menu);
    addShowTextItem(menu);

    Rectangle bounds = item.getBounds();
    Point point = psTB.toDisplay(bounds.x, bounds.y + bounds.height);
    menu.setLocation(point.x, point.y);
    menu.setVisible(true);
    menu.addMenuListener(
        new MenuListener() {

          public void menuHidden(MenuEvent e) {
            psTB.getDisplay()
                .asyncExec(
                    new Runnable() {

                      public void run() {
                        menu.dispose();
                      }
                    });
          }

          public void menuShown(MenuEvent e) {
            // Nothing to do
          }
        });
  }
 void handleChildRemove(MPerspectiveStack parent, MPerspective element) {
   if (element.isToBeRendered() && element.isVisible()) {
     hideChild(parent, element);
   }
 }
  private Component createPerspectiveButton(final MPerspective perspective) {
    if (!perspective.isVisible()) return null;
    boolean iconsOnly = perspectiveStackForSwitcher.getTags().contains(Tags.ICONS_ONLY);
    String label = iconsOnly ? null : Commons.trim(perspective.getLabel());
    String iconURI = Commons.trim(perspective.getIconURI());

    final TwoStateToolbarButton button = new TwoStateToolbarButton(label, iconURI);

    if (perspective.getTooltip() != null) {
      button.setDescription(perspective.getLocalizedTooltip());
    }

    button.addListener(
        new ClickListener() {

          public void buttonClick(ClickEvent event) {
            MPerspectiveStack perspectiveStack =
                (MPerspectiveStack) (MElementContainer<?>) perspective.getParent();
            switchPerspective(perspective);
          }
        });

    // TODO: replace VerticalLayout on more thin layout (for example SimpleLayout addon which
    // consist of just one div)
    //		 VerticalLayout wrapperLayout = new VerticalLayout();
    //		 wrapperLayout.setSizeUndefined();
    //		 wrapperLayout.addComponent(button);
    //		 wrapperLayout.addListener(new LayoutEvents.LayoutClickListener() {
    //
    //			 @Override
    //			 public void layoutClick(LayoutClickEvent event)
    //			 {
    //				 if (LayoutClickEvent.BUTTON_RIGHT == event.getButton())
    //				 {
    //					 lastClickedPerspective = perspective;
    ////					 menu.open(event.getClientX(), event.getClientY());
    //				 }
    //			 }
    //		 });

    // Create context menu
    // Context menu
    ContextMenu menu = new ContextMenu();
    contextMenu2Button.put(menu, button);
    button2ContextMenu.put(button, menu);

    final ContextMenuItem showTextItem;

    final ContextMenuItem closeItem = menu.addItem("Close");
    // closeItem.setSeparatorVisible(true);

    if (iconsOnly) showTextItem = menu.addItem("Show Text");
    else showTextItem = menu.addItem("Hide Text");

    // showTextItem.addStyleName("close-perspective-item"); //bugfixing style for ie9 (context menu
    // addon has bug for ie9)

    menu.addItemClickListener(
        new ContextMenu.ContextMenuItemClickListener() {

          @Override
          public void contextMenuItemClicked(ContextMenuItemClickEvent event) {
            ContextMenuItem clickedItem = (ContextMenuItem) event.getSource();

            if (clickedItem == closeItem) {
              if (perspective == activePerspective) {
                MPerspective prevRenderableAndVisiblePerspective = null,
                    nextRenderableAndVisiblePerspective = null;
                boolean startSearch = false;
                for (MPerspective p : perspectiveStackForSwitcher.getChildren()) {
                  if (startSearch && p.isToBeRendered() && p.isVisible()) {
                    nextRenderableAndVisiblePerspective = p;
                    break;
                  }

                  if (p == perspective) startSearch = true;

                  if (!startSearch && p.isToBeRendered() && p.isVisible()) {
                    prevRenderableAndVisiblePerspective = p;
                  }
                }

                MPerspective newSelectedPerspective =
                    nextRenderableAndVisiblePerspective != null
                        ? nextRenderableAndVisiblePerspective
                        : prevRenderableAndVisiblePerspective;

                if (newSelectedPerspective != null) switchPerspective(newSelectedPerspective);
              }

              perspective.setToBeRendered(false);
            } else if (clickedItem == showTextItem) {
              if (perspectiveStackForSwitcher.getTags().contains(Tags.ICONS_ONLY))
                perspectiveStackForSwitcher.getTags().remove(Tags.ICONS_ONLY);
              else perspectiveStackForSwitcher.getTags().add(Tags.ICONS_ONLY);
            }
          }
        });

    menu.setAsContextMenuOf(button);

    perspective_button.put(perspective, button);
    // return wrapperLayout;
    return button;
  }