/* After building up the history we set up the layout that we show the user when the
   * applications starts. */
  private static void setUpFinalLayout(
      CPerspective perspective, Map<String, CDockablePerspective> dockables) {
    /* We minimize the "red" and the "black" dockable */
    CMinimizePerspective west = perspective.getContentArea().getWest();
    west.add(dockables.get("Red"));

    CMinimizePerspective east = perspective.getContentArea().getEast();
    east.add(dockables.get("Black"));
  }
  public static void main(String[] args) {
    JTutorialFrame frame = new JTutorialFrame(PerspectivesHistory.class);
    CControl control = new CControl(frame);
    frame.destroyOnClose(control);

    /* When working with perspectives it is always a good idea first to set up the
     * CControl, then create the perspectives. */
    ColorFactory colorFactory = new ColorFactory();
    control.addSingleDockableFactory(colorFactory, colorFactory);
    control.addMultipleDockableFactory(CUSTOM_MULTI_FACTORY_ID, new CustomMultiFactory());

    /* By creating the root-stations now we automatically create counterparts in the
     * perspective. Otherwise we would need to do some setting up with the perspectives as
     * well. */
    frame.add(control.getContentArea(), BorderLayout.CENTER);
    control.createWorkingArea("work");

    /* Access to the perspective API */
    CControlPerspective perspectives = control.getPerspectives();

    /* Creating a new, empty perspective */
    CPerspective perspective = perspectives.createEmptyPerspective();

    /* For building up a history we need to move around CDockablePerspectives. It is
     * not possible to use several dockables with the same identifier. To make things
     * easier we just store the dockables in a map. In a real application you probably
     * want to implement a more advanced mechanism to store and access your dockables.
     *
     * IMPORTANT: the unique identifier of MultipleCDockablePerspectives is set by this method. */
    Map<String, CDockablePerspective> dockables = collect();

    /* We start by assigned the minimized location of all dockables. */
    setUpMinimized(perspective, dockables);
    /* Now we assign the normalized location of all dockables. */
    setUpNormalized(perspective, dockables);
    /* It is also possible to access and modify the location history directly. */
    modifyHistoryDirectly(perspective, dockables);
    /* At the end we set up the layout that the user will see when the application starts. */
    setUpFinalLayout(perspective, dockables);

    /* By calling "shrink" we instruct the perspective to remove unnecessary
     * PerspectiveStations from the layout. This is what the SingleParentRemover will do
     * with real DockStations. */
    perspective.shrink();

    /* Finally we apply the perspective we just created */
    perspectives.setPerspective(perspective, true);

    frame.setVisible(true);
  }
  /* This method assigns the normalized location to the dockables. It works very much the same
   * as the method "setUpMinimized" just above. */
  private static void setUpNormalized(
      CPerspective perspective, Map<String, CDockablePerspective> dockables) {
    CGridPerspective center = perspective.getContentArea().getCenter();
    CWorkingPerspective work = (CWorkingPerspective) perspective.getRoot("work");

    center.gridAdd(0, 0, 50, 25, dockables.get("Red"));
    center.gridAdd(50, 0, 50, 25, dockables.get("Green"));
    center.gridAdd(0, 25, 50, 25, dockables.get("Blue"));
    center.gridAdd(50, 25, 50, 25, dockables.get("Yellow"));
    center.gridAdd(0, 50, 100, 50, work);

    work.gridAdd(0, 0, 50, 100, dockables.get("White"), dockables.get("Black"));
    for (int i = 0; i < 5; i++) {
      work.gridAdd(50, 0, 50, 100, dockables.get("m" + i));
    }

    perspective.storeLocations();
  }
  /* This method assigns the minimized location to the dockables */
  private static void setUpMinimized(
      CPerspective perspective, Map<String, CDockablePerspective> dockables) {
    /* In the beginning we access different minimize-perspectives and just drop our dockables
     * onto them. */
    CMinimizePerspective west = perspective.getContentArea().getWest();
    west.add(dockables.get("Red"));
    west.add(dockables.get("Green"));
    west.add(dockables.get("Blue"));

    CMinimizePerspective east = perspective.getContentArea().getEast();
    east.add(dockables.get("Yellow"));
    east.add(dockables.get("White"));
    east.add(dockables.get("Black"));

    CMinimizePerspective south = perspective.getContentArea().getSouth();
    for (int i = 0; i < 5; i++) {
      south.add(dockables.get("m" + i));
    }

    /* And then we instruct the framework that the current location of the dockables should
     * be stored as history information.
     * The dockables remain at their current location, but we can just re-arrange them later. */
    perspective.storeLocations();
  }
  /* It is possible to access and modify history information directly. In this case
   * modify the history of the "White" dockable such that it thinks it was minimized
   * on the "north" minimize-area. */
  private static void modifyHistoryDirectly(
      CPerspective perspective, Map<String, CDockablePerspective> dockables) {
    CMinimizePerspective north = perspective.getContentArea().getNorth();
    CDockablePerspective white = dockables.get("White");

    /* We first inform the north minimize-area that "white" was a child by
     * inserting a placeholder for "white" */
    north.addPlaceholder(white);

    /* Now we build up location information first by specifying the exact location of "white" */
    DockableProperty property =
        new FlapDockProperty(0, false, 100, white.intern().asDockable().getPlaceholder());
    /* We pack additional information like the mode and the root-station that was the parent of
     * "white" together. */
    Location location =
        new Location(ExtendedMode.MINIMIZED.getModeIdentifier(), north.getUniqueId(), property);
    /* And finally we add the new location information to the history. */
    white.getLocationHistory().add(ExtendedMode.MINIMIZED, location);
  }