Esempio n. 1
0
  @Override
  public void doLayout(TerminalSize area, List<Component> components) {
    // Sanity check, if the area is way too small, just return
    Component[][] table = buildTable(components);
    table = eliminateUnusedRowsAndColumns(table);

    if (area.equals(TerminalSize.ZERO)
        || table.length == 0
        || area.getColumns()
            <= leftMarginSize + rightMarginSize + ((table[0].length - 1) * horizontalSpacing)
        || area.getRows()
            <= bottomMarginSize + topMarginSize + ((table.length - 1) * verticalSpacing)) {
      return;
    }

    // Adjust area to the margins
    area = area.withRelative(-leftMarginSize - rightMarginSize, -topMarginSize - bottomMarginSize);

    Map<Component, TerminalSize> sizeMap = new IdentityHashMap<Component, TerminalSize>();
    Map<Component, TerminalPosition> positionMap =
        new IdentityHashMap<Component, TerminalPosition>();

    // Figure out each column first, this can be done independently of the row heights
    int[] columnWidths = getPreferredColumnWidths(table);

    // Take notes of which columns we can expand if the usable area is larger than what the
    // components want
    Set<Integer> expandableColumns = getExpandableColumns(table);

    // Next, start shrinking to make sure it fits the size of the area we are trying to lay out on.
    // Notice we subtract the horizontalSpacing to take the space between components into account
    TerminalSize areaWithoutHorizontalSpacing =
        area.withRelativeColumns(-horizontalSpacing * (table[0].length - 1));
    int totalWidth = shrinkWidthToFitArea(areaWithoutHorizontalSpacing, columnWidths);

    // Finally, if there is extra space, make the expandable columns larger
    while (areaWithoutHorizontalSpacing.getColumns() > totalWidth && !expandableColumns.isEmpty()) {
      totalWidth =
          grabExtraHorizontalSpace(
              areaWithoutHorizontalSpacing, columnWidths, expandableColumns, totalWidth);
    }

    // Now repeat for rows
    int[] rowHeights = getPreferredRowHeights(table);
    Set<Integer> expandableRows = getExpandableRows(table);
    TerminalSize areaWithoutVerticalSpacing =
        area.withRelativeRows(-verticalSpacing * (table.length - 1));
    int totalHeight = shrinkHeightToFitArea(areaWithoutVerticalSpacing, rowHeights);
    while (areaWithoutVerticalSpacing.getRows() > totalHeight && !expandableRows.isEmpty()) {
      totalHeight =
          grabExtraVerticalSpace(
              areaWithoutVerticalSpacing, rowHeights, expandableRows, totalHeight);
    }

    // Ok, all constraints are in place, we can start placing out components. To simplify, do it
    // horizontally first
    // and vertically after
    TerminalPosition tableCellTopLeft = TerminalPosition.TOP_LEFT_CORNER;
    for (int y = 0; y < table.length; y++) {
      tableCellTopLeft = tableCellTopLeft.withColumn(0);
      for (int x = 0; x < table[y].length; x++) {
        Component component = table[y][x];
        if (component != null && !positionMap.containsKey(component)) {
          GridLayoutData layoutData = getLayoutData(component);
          TerminalSize size = component.getPreferredSize();
          TerminalPosition position = tableCellTopLeft;

          int availableHorizontalSpace = 0;
          int availableVerticalSpace = 0;
          for (int i = 0; i < layoutData.horizontalSpan; i++) {
            availableHorizontalSpace += columnWidths[x + i] + (i > 0 ? horizontalSpacing : 0);
          }
          for (int i = 0; i < layoutData.verticalSpan; i++) {
            availableVerticalSpace += rowHeights[y + i] + (i > 0 ? verticalSpacing : 0);
          }

          // Make sure to obey the size restrictions
          size = size.withColumns(Math.min(size.getColumns(), availableHorizontalSpace));
          size = size.withRows(Math.min(size.getRows(), availableVerticalSpace));

          switch (layoutData.horizontalAlignment) {
            case CENTER:
              position =
                  position.withRelativeColumn((availableHorizontalSpace - size.getColumns()) / 2);
              break;
            case END:
              position = position.withRelativeColumn(availableHorizontalSpace - size.getColumns());
              break;
            case FILL:
              size = size.withColumns(availableHorizontalSpace);
              break;
            default:
              break;
          }
          switch (layoutData.verticalAlignment) {
            case CENTER:
              position = position.withRelativeRow((availableVerticalSpace - size.getRows()) / 2);
              break;
            case END:
              position = position.withRelativeRow(availableVerticalSpace - size.getRows());
              break;
            case FILL:
              size = size.withRows(availableVerticalSpace);
              break;
            default:
              break;
          }

          sizeMap.put(component, size);
          positionMap.put(component, position);
        }
        tableCellTopLeft = tableCellTopLeft.withRelativeColumn(columnWidths[x] + horizontalSpacing);
      }
      tableCellTopLeft = tableCellTopLeft.withRelativeRow(rowHeights[y] + verticalSpacing);
    }

    // Apply the margins here
    for (Component component : components) {
      component.setPosition(positionMap.get(component).withRelative(leftMarginSize, topMarginSize));
      component.setSize(sizeMap.get(component));
    }
    this.changed = false;
  }
Esempio n. 2
0
  @Override
  public void doLayout(TerminalSize area, List<Component> components) {
    EnumMap<Location, Component> layout = makeLookupMap(components);
    int availableHorizontalSpace = area.getColumns();
    int availableVerticalSpace = area.getRows();

    // We'll need this later on
    int topComponentHeight = 0;
    int leftComponentWidth = 0;

    // First allocate the top
    if (layout.containsKey(Location.TOP)) {
      Component topComponent = layout.get(Location.TOP);
      topComponentHeight =
          Math.min(topComponent.getPreferredSize().getRows(), availableVerticalSpace);
      topComponent.setPosition(TerminalPosition.TOP_LEFT_CORNER);
      topComponent.setSize(new TerminalSize(availableHorizontalSpace, topComponentHeight));
      availableVerticalSpace -= topComponentHeight;
    }

    // Next allocate the bottom
    if (layout.containsKey(Location.BOTTOM)) {
      Component bottomComponent = layout.get(Location.BOTTOM);
      int bottomComponentHeight =
          Math.min(bottomComponent.getPreferredSize().getRows(), availableVerticalSpace);
      bottomComponent.setPosition(new TerminalPosition(0, area.getRows() - bottomComponentHeight));
      bottomComponent.setSize(new TerminalSize(availableHorizontalSpace, bottomComponentHeight));
      availableVerticalSpace -= bottomComponentHeight;
    }

    // Now divide the remaining space between LEFT, CENTER and RIGHT
    if (layout.containsKey(Location.LEFT)) {
      Component leftComponent = layout.get(Location.LEFT);
      leftComponentWidth =
          Math.min(leftComponent.getPreferredSize().getColumns(), availableHorizontalSpace);
      leftComponent.setPosition(new TerminalPosition(0, topComponentHeight));
      leftComponent.setSize(new TerminalSize(leftComponentWidth, availableVerticalSpace));
      availableHorizontalSpace -= leftComponentWidth;
    }
    if (layout.containsKey(Location.RIGHT)) {
      Component rightComponent = layout.get(Location.RIGHT);
      int rightComponentWidth =
          Math.min(rightComponent.getPreferredSize().getColumns(), availableHorizontalSpace);
      rightComponent.setPosition(
          new TerminalPosition(area.getColumns() - rightComponentWidth, topComponentHeight));
      rightComponent.setSize(new TerminalSize(rightComponentWidth, availableVerticalSpace));
      availableHorizontalSpace -= rightComponentWidth;
    }
    if (layout.containsKey(Location.CENTER)) {
      Component centerComponent = layout.get(Location.CENTER);
      centerComponent.setPosition(new TerminalPosition(leftComponentWidth, topComponentHeight));
      centerComponent.setSize(new TerminalSize(availableHorizontalSpace, availableVerticalSpace));
    }

    // Set the remaining components to 0x0
    for (Component component : components) {
      if (!layout.values().contains(component)) {
        component.setPosition(TerminalPosition.TOP_LEFT_CORNER);
        component.setSize(TerminalSize.ZERO);
      }
    }
  }