/* (non-Javadoc)
   * @see de.enough.polish.ui.ContainerView#initContent(de.enough.polish.ui.Container, int, int)
   */
  protected void initContent(
      Item parentContainerItem, int firstLineWidth, int availWidth, int availHeight) {

    // #if polish.hasPointerEvents
    if (!isPointerDraggedEnabled) {
      isPointerDraggedEnabled = Display.getInstance().hasPointerMotionEvents();
    }
    // #endif
    this.isVertical = false;
    this.isHorizontal = true;
    Container parent = (Container) parentContainerItem;
    // #debug
    // # System.out.println("FishEye: intialising content for " + this + " with vertical-padding " +
    // this.paddingVertical + ", availWidth=" + availWidth );

    this.parentContainer = parent;
    Item[] myItems = parent.getItems();
    int length = myItems.length;
    if (this.focusedIndex == -1 && length != 0) {
      // this.parentContainer.focus(0);
      if (parent.focusedIndex != -1) {
        this.focusedIndex = parent.focusedIndex;
      } else {
        this.focusedIndex = 0;
      }
      // System.out.println("AUTO-FOCUSSING ITEM " + this.focusedIndex );
      this.focusedItem = myItems[this.focusedIndex];
      this.focusedStyle = this.focusedItem.getFocusedStyle();
    }
    if (this.referenceXCenterPositions != null && this.referenceXCenterPositions.length == length) {
      // only another item has been focused, so nothing needs to be adjusted.
      return;
    }

    // #if polish.css.show-text-in-title
    // # if (this.isRemoveText && this.focusedLabel == null && !this.isShowTextInTitle) {
    // # this.focusedLabel = new StringItem(null, null);
    // #if polish.css.fisheyeview-text-style
    // # if (this.focusedLabelStyle != null) {
    // # this.focusedLabel.setStyle(this.focusedLabelStyle);
    // # }
    // #endif
    // # }
    // #else
    if (this.isRemoveText && this.focusedLabel == null) {
      this.focusedLabel = new StringItem(null, null);
      // #if polish.css.fisheyeview-text-style
      // # if (this.focusedLabelStyle != null) {
      // # this.focusedLabel.setStyle(this.focusedLabelStyle);
      // # }
      // #endif
    }
    // #endif

    if (this.isRemoveText && (this.labels == null || this.labels.length != length)) {
      this.labels = new String[length];
    }

    int maxWidth = 0;
    int maxHeight = 0;
    boolean hasFocusableItem = false;
    // #if polish.midp2
    this.originalRgbData = new int[length][];
    this.originalRgbDataWidths = new int[length];
    this.shownRgbData = new int[length][];
    this.shownRgbDataWidths = new int[length];
    this.shownRgbDataHeights = new int[length];
    // #endif
    for (int i = 0; i < length; i++) {
      Item item = myItems[i];
      if (this.isRemoveText) {
        String text = item.getLabel();
        if (text != null) {
          this.labels[i] = text;
          item.setLabel(null);
        } else if (item instanceof IconItem) {
          IconItem iconItem = (IconItem) item;
          text = iconItem.getText();
          if (text != null) {
            this.labels[i] = text;
            iconItem.setTextVisible(false);
            // iconItem.setText(null);
          }
        }
      }
      int width = item.getItemWidth(firstLineWidth, availWidth, availHeight);
      int height = item.getItemHeight(firstLineWidth, availWidth, availHeight);
      // #if polish.midp2
      int[] data = item.getRgbData(true, 255);
      this.originalRgbData[i] = data;
      this.originalRgbDataWidths[i] = width;
      if (this.scaleFactor == 100) {
        this.shownRgbData[i] = data;
        this.shownRgbDataWidths[i] = width;
        this.shownRgbDataHeights[i] = height;
      } else {
        int newWidth = (width * this.scaleFactor) / 100;
        int newHeight = (height * this.scaleFactor) / 100;
        // this.shownRgbData[i] = ImageUtil.scale(data, newWidth, newHeight, width, height );
        int alpha =
            this
                .endTranslucency; // calculateAlpha( getDistance( i, this.focusedIndex, length ),
                                  // length );
        this.shownRgbData[i] = new int[data.length];
        // #if polish.FishEye.scaleHq
        // # ImageUtil.scaleDownHq(this.shownRgbData[i], data,width, newWidth, 0, alpha, false);
        // #else
        ImageUtil.scale(alpha, data, newWidth, newHeight, width, height, this.shownRgbData[i]);
        // #endif

        this.shownRgbDataWidths[i] = newWidth;
        this.shownRgbDataHeights[i] = newHeight;
      }
      // #endif
      if (item.appearanceMode != Item.PLAIN) {
        hasFocusableItem = true;
      }
      if (width > maxWidth) {
        maxWidth = width;
      }
      if (height > maxHeight) {
        maxHeight = height;
      }
    }
    this.maxItemHeight = maxHeight;
    if (hasFocusableItem) {
      this.appearanceMode = Item.INTERACTIVE;
    } else {
      this.appearanceMode = Item.PLAIN;
    }

    initItemArrangement(availWidth, availHeight, myItems, length, maxWidth, maxHeight);

    // use reference positions to set the position for the items:
    for (int i = 0; i < length; i++) {
      Item item = myItems[i];
      int distance = getDistance(i, this.focusedIndex, length);
      if (distance != 0) {
        distance--;
      }
      int halfItemWidth = (item.getItemWidth(availWidth, availWidth, availHeight) >> 1);
      int halfItemHeight = (item.getItemHeight(availWidth, availWidth, availHeight) >> 1);
      // #if polish.midp2
      if (i != this.focusedIndex) {
        int factor = getScaleFactor(distance, length);
        halfItemWidth = (halfItemWidth * factor) / 100;
        halfItemHeight = (halfItemHeight * factor) / 100;
      }
      // #endif
      // System.out.println(i + ": initContent:  with distance " + distance + ", halfItemWidth=" +
      // halfItemWidth + ", focusedIndex=" + this.focusedIndex);
      item.relativeX = this.referenceXCenterPositions[i] - halfItemWidth;
      if (this.referenceYCenterPositions != null) {
        item.relativeY = this.referenceYCenterPositions[i] - halfItemHeight;
      }
      // System.out.println( i + ": relativeX=" + item.relativeX);

      //			System.out.println("item.relativeX for " + i + "=" + item.relativeX);
    }
    if (this.focusedStyle != null) {
      focusItem(this.focusedIndex, this.focusedItem, this.focusedDirection, this.focusedStyle);
      this.focusedItem.relativeX =
          this.referenceXCenterPositions[this.focusedIndex]
              - (this.focusedItem.getItemWidth(availWidth, availWidth, availHeight) >> 1);
      if (this.referenceYCenterPositions != null) {
        this.focusedItem.relativeY =
            this.referenceYCenterPositions[this.focusedIndex]
                - (this.focusedItem.getItemHeight(availWidth, availWidth, availHeight) >> 1);
      }
      //			System.out.println("focused.relativeX=" + this.focusedItem.relativeX);
      this.focusedStyle = null;
    }

    this.contentWidth = availWidth; // TODO: this can change when no expanded layout is used
    this.contentHeight =
        this.focusedLabel == null
            ? maxHeight
            : maxHeight
                + this.focusedLabel.getItemHeight(
                    availWidth,
                    availWidth,
                    availHeight); // maxItemHeight + this.paddingVertical +
                                  // this.focusedLabel.getItemHeight(lineWidth, lineWidth);

    if (!this.isFocused) {
      AnimationThread.addAnimationItem(parent);
    }
  }
  /* (non-Javadoc)
   * @see de.enough.polish.ui.ContainerView#initContent(de.enough.polish.ui.Container, int, int)
   */
  protected void initContent(Item parentItm, int firstLineWidth, int availWidth, int availHeight) {
    Container parent = (Container) parentItm;
    // #debug
    System.out.println(
        "Initalizing HorizontalContainerView with focusedIndex="
            + parent.getFocusedIndex()
            + " for parent "
            + parent);

    this.availableWidth = availWidth;

    int arrowWidth = 0;
    if (this.arrowLeft != null && this.arrowRight != null) {
      arrowWidth =
          this.arrowLeft.getWidth() + this.arrowRight.getWidth() + (this.paddingHorizontal * 2);
      availWidth -= arrowWidth;
      firstLineWidth -= arrowWidth;
    }

    int selectedItemIndex = parent.getFocusedIndex();
    int maxHeight = 0;
    int completeWidth = 0;
    if (arrowWidth > 0) {
      completeWidth = this.arrowLeft.getWidth() + this.paddingHorizontal;
    }
    Item[] items = parent.getItems();
    // #if polish.css.show-text-in-title
    if (this.isShowTextInTitle && (this.labels == null || this.labels.length != items.length)) {
      this.labels = new String[items.length];
    }
    // #endif

    int availItemWidth = availWidth;
    int availItemWidthWithPaddingShift8 = 0;
    // #if polish.css.horizontalview-distribution
    if (this.isDistributeEquals) {
      int left = availItemWidth - ((items.length - 1) * this.paddingHorizontal);
      availItemWidth = left / items.length;
      availItemWidthWithPaddingShift8 = (availWidth << 8) / items.length;
      // check if we have too many items for the available width:
      if (items.length > 0) {
        Item child = items[0];
        int childWidth = child.getItemWidth(availItemWidth, availItemWidth, availHeight);
        if (child.getContentWidth() > childWidth) {
          childWidth = child.getContentWidth() + child.getPaddingLeft() + child.getPaddingRight();
        }
        // System.out.println("childWidth=" + childWidth + ", available=" + availItemWidth);
        if (childWidth > availItemWidth) {
          availItemWidth = childWidth;
          availItemWidthWithPaddingShift8 = (childWidth + this.paddingHorizontal) << 8;
        }
      }
    }
    // #endif
    for (int i = 0; i < items.length; i++) {
      Item item = items[i];
      // #if polish.css.show-text-in-title
      if (this.isShowTextInTitle) {
        String text = item.getLabel();
        if (text != null) {
          this.labels[i] = text;
          item.setLabel(null);
        } else if (item instanceof IconItem) {
          IconItem iconItem = (IconItem) item;
          text = iconItem.getText();
          if (text != null) {
            this.labels[i] = text;
            iconItem.setTextVisible(false);
          }
        }
      }
      // #endif
      int itemHeight = item.getItemHeight(availItemWidth, availItemWidth, availHeight);
      int itemWidth = item.itemWidth;
      if (itemHeight > maxHeight) {
        maxHeight = itemHeight;
      }
      boolean isLast = (i == items.length - 1);
      if (isLast && item.isLayoutRight() && (completeWidth + item.itemWidth < availWidth)) {
        completeWidth = availWidth - item.itemWidth;
      }
      int startX = completeWidth;
      item.relativeX = completeWidth;
      item.relativeY = 0;
      completeWidth += itemWidth + (isLast ? 0 : this.paddingHorizontal);
      // #if polish.css.horizontalview-distribution
      if (this.isDistributeEquals) {
        completeWidth = (availItemWidthWithPaddingShift8 * (i + 1)) >> 8;
        if (itemWidth < availItemWidth) {
          if (item.isLayoutCenter()) {
            item.relativeX += (availItemWidth - itemWidth) / 2;
          } else if (item.isLayoutRight() && !isLast) {
            item.relativeX += (availItemWidth - itemWidth);
          }
        }
      }
      // #endif
      if (i == selectedItemIndex) {
        if (startX + getScrollXOffset() < 0) {
          setScrollXOffset(-startX, true);
        } else if (completeWidth + getScrollTargetXOffset() > availWidth) {
          setScrollXOffset(availWidth - completeWidth, true);
        }
        // System.out.println("initContent: xOffset=" + xOffset);
        this.focusedItem = item;
      }
      if (item.appearanceMode != Item.PLAIN) {
        this.appearanceMode = Item.INTERACTIVE;
      }
    }
    for (int i = 0; i < items.length; i++) {
      Item item = items[i];
      // #if polish.css.horizontalview-align-heights
      if (this.isAlignHeights && !item.isLayoutVerticalShrink()) {
        item.setItemHeight(maxHeight);
      } else
      // #endif
      if (item.isLayoutVerticalCenter()) {
        item.relativeY += (maxHeight - item.itemHeight) >> 1;
      } else if (item.isLayoutBottom()) {
        item.relativeY += (maxHeight - item.itemHeight);
      }
      if (i == items.length - 1 && item.isLayoutRight() && completeWidth < availWidth) {
        item.relativeX = availWidth - item.itemWidth;
        completeWidth = availWidth;
      }
    }
    this.contentHeight = maxHeight;
    if (arrowWidth > 0) {
      if (parent.isLayoutVerticalCenter()) {
        this.arrowLeftYAdjust = (maxHeight - this.arrowLeft.getHeight()) / 2;
        this.arrowRightYAdjust = (maxHeight - this.arrowRight.getHeight()) / 2;
      } else if (parent.isLayoutBottom()) {
        this.arrowLeftYAdjust = (maxHeight - this.arrowLeft.getHeight());
        this.arrowRightYAdjust = (maxHeight - this.arrowRight.getHeight());
      }
    }
    if (completeWidth > availWidth) {
      this.isClippingRequired = true;
    } else {
      this.isClippingRequired = false;
    }
    if (arrowWidth > 0) {
      completeWidth += this.arrowRight.getWidth() + this.paddingHorizontal;
    }
    this.contentWidth = completeWidth;

    if (parent.isLayoutRight() && parent.isLayoutExpand()) {
      this.isExpandRightLayout = true;
    } else {
      this.isExpandRightLayout = false;
    }
    // System.out.println("init of horizontal: " + this.contentWidth + "x" + this.contentHeight);
  }
  /* (non-Javadoc)
   * @see de.enough.polish.ui.ContainerView#initContent(de.enough.polish.ui.Container, int, int)
   */
  protected void initContent(
      Item parentContainerItem, int firstLineWidth, int availWidth, int availHeight) {
    synchronized (this.lock) {
      this.isVertical = false;
      this.isHorizontal = true;
      Container parent = (Container) parentContainerItem;
      // #debug
      System.out.println(
          "CoverFlow: intialising content for "
              + this
              + " with vertical-padding "
              + this.paddingVertical);

      this.parentContainer = parent;
      Item[] myItems = parent.getItems();
      int length = myItems.length;
      if (this.focusedIndex == -1 && length != 0) {
        // this.parentContainer.focus(0);
        if (parent.focusedIndex != -1) {
          this.focusedIndex = parent.focusedIndex;
        } else {
          this.focusedIndex = 0;
        }
        // System.out.println("AUTO-FOCUSSING ITEM " + this.focusedIndex );
        this.focusedItem = myItems[this.focusedIndex];
        this.focusedStyle = this.focusedItem.getFocusedStyle();
      }
      if (this.referenceXCenterPositions != null
          && this.referenceXCenterPositions.length == length) {
        return;
      }

      // #if polish.css.show-text-in-title
      if (this.isRemoveText && this.focusedLabel == null && !this.isShowTextInTitle) {
        this.focusedLabel = new StringItem(null, null);
      }
      // #else
      if (this.isRemoveText && this.focusedLabel == null) {
        this.focusedLabel = new StringItem(null, null);
      }
      // #endif

      if (this.isRemoveText && (this.labels == null || this.labels.length != length)) {
        this.labels = new String[length];
      }

      int maxWidth = 0;
      int maxHeight = 0;
      boolean hasFocusableItem = false;
      // #if polish.midp2
      this.originalRgbData = new int[length][];
      this.originalRgbDataWidths = new int[length];
      this.shownRgbData = new int[length][];
      this.shownRgbDataWidths = new int[length];
      this.shownRgbDataHeights = new int[length];
      // #endif
      for (int i = 0; i < length; i++) {
        Item item = myItems[i];
        if (this.isRemoveText) {
          String text = item.getLabel();
          if (text != null) {
            this.labels[i] = text;
            item.setLabel(null);
          } else if (item instanceof IconItem) {
            IconItem iconItem = (IconItem) item;
            text = iconItem.getText();
            if (text != null) {
              this.labels[i] = text;
              iconItem.setTextVisible(false);
              // iconItem.setText(null);
            }
          }
        }
        int width = item.getItemWidth(firstLineWidth, availWidth, availHeight);
        int height = item.getItemHeight(firstLineWidth, availWidth, availHeight);
        // #if polish.midp2
        int[] data = UiAccess.getRgbData(item);
        this.originalRgbData[i] = data;
        this.originalRgbDataWidths[i] = width;
        if (this.scaleFactorWidth == 100) {
          this.shownRgbData[i] = data;
          this.shownRgbDataWidths[i] = width;
          this.shownRgbDataHeights[i] = height;
        } else {
          int newWidth = (width * this.scaleFactorWidth) / 100;
          int newHeightInner = (height * this.scaleFactorInnerHeight) / 100;
          int newHeightOuter = (height * this.scaleFactorOuterHeight) / 100;
          this.shownRgbData[i] = new int[data.length];
          if (isLeftOfFocus(i, this.focusedIndex, length)) {
            ImageUtil.perspectiveShear(
                data,
                this.shownRgbData[i],
                width,
                newWidth,
                newHeightOuter,
                newHeightInner,
                255,
                ImageUtil.EDGEDETECTION_MAP_FAST_AND_SIMPLE);
          } else {
            ImageUtil.perspectiveShear(
                data,
                this.shownRgbData[i],
                width,
                newWidth,
                newHeightInner,
                newHeightOuter,
                255,
                ImageUtil.EDGEDETECTION_MAP_FAST_AND_SIMPLE);
          }
          //					System.out.println("newWidth=" + newWidth);
          this.shownRgbDataWidths[i] = width;
          this.shownRgbDataHeights[i] = height;
        }
        // #endif
        if (item.appearanceMode != Item.PLAIN) {
          hasFocusableItem = true;
        }
        if (width > maxWidth) {
          maxWidth = width;
        }
        if (height > maxHeight) {
          maxHeight = height;
        }
      }
      this.maxItemHeight = maxHeight;
      if (hasFocusableItem) {
        this.appearanceMode = Item.INTERACTIVE;
      } else {
        this.appearanceMode = Item.PLAIN;
      }

      initItemArrangement(availWidth, myItems, length, maxWidth, maxHeight);

      // use reference positions to set the position for the items:
      for (int i = 0; i < length; i++) {
        Item item = myItems[i];
        int distance = getDistance(i, this.focusedIndex, length);
        if (distance != 0) {
          distance--;
        }
        int halfItemWidth = (item.getItemWidth(availWidth, availWidth, availHeight) >> 1);
        int halfItemHeight = (item.getItemHeight(availWidth, availWidth, availHeight) >> 1);
        // #if polish.midp2
        if (i != this.focusedIndex) {
          int factor = getScaleFactor(distance, length);
          halfItemWidth = (halfItemWidth * factor) / 100;
          halfItemHeight = (halfItemHeight * factor) / 100;
        }
        // #endif
        // System.out.println(i + ": initContent:  with distance " + distance + ", halfItemWidth=" +
        // halfItemWidth + ", focusedIndex=" + this.focusedIndex);
        item.relativeX = this.referenceXCenterPositions[i] - halfItemWidth;
        if (this.referenceYCenterPositions != null) {
          item.relativeY = this.referenceYCenterPositions[i] - halfItemHeight;
        }
        // System.out.println( i + ": relativeX=" + item.relativeX);

        //			System.out.println("item.relativeX for " + i + "=" + item.relativeX);
      }
      if (this.focusedStyle != null) {
        focusItem(this.focusedIndex, this.focusedItem, this.focusedDirection, this.focusedStyle);
        this.focusedItem.relativeX =
            this.referenceXCenterPositions[this.focusedIndex]
                - (this.focusedItem.getItemWidth(availWidth, availWidth, availHeight) >> 1);
        if (this.referenceYCenterPositions != null) {
          this.focusedItem.relativeY =
              this.referenceYCenterPositions[this.focusedIndex]
                  - (this.focusedItem.getItemHeight(availWidth, availWidth, availHeight) >> 1);
        }
        //			System.out.println("focused.relativeX=" + this.focusedItem.relativeX);
        this.focusedStyle = null;
      }

      this.contentWidth = availWidth; // TODO: this can change when no expanded layout is used
      this.contentHeight =
          this.focusedLabel == null
              ? maxHeight
              : maxHeight
                  + this.focusedLabel.getItemHeight(
                      availWidth,
                      availWidth,
                      availHeight); // maxItemHeight + this.paddingVertical +
      // this.focusedLabel.getItemHeight(lineWidth, lineWidth);

      if (!this.isFocused) {
        AnimationThread.addAnimationItem(parent);
      }
      animate(System.currentTimeMillis(), null);
    }
  }