⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 staticlayoutmanager.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            height = (float) Math.max(absDim.getHeight() + absPos.getY(), height);
          }
        }
      }

      // now apply the minimum limit defined for that band in case the calculated height
      // is lower than the given minimum height.
      height = Math.max(height, (float) minSize.getHeight());
      width = Math.max(width, (float) minSize.getWidth());

      // now apply the maximum limit defined for that band in case the calculated height
      // is higher than the given max height.
      height = (float)Math.min(height, maxSize.getHeight());
      width = (float) Math.min(width, maxSize.getWidth());

      // Log.debug ("Mid Stream; " + width + ", " + height);
      // Log.debug ("Dimension after static correction [PREF]: " + width + " -> " + height);
      // final FloatDimension base = new FloatDimension(width, height);
      base.setHeight(height);

      Point2D absPos = null;
      Dimension2D absDim = null;

      // calculate relative widths
      for (int i = 0; i < elements.length; i++)
      {
        final Element e = elements[i];

        if (e.isVisible() == false)
        {
          continue;
        }
        final boolean staticWidth = isElementStaticWidth(e);
        final boolean staticHeight = isElementStaticHeight(e);
        if (staticWidth == false || staticHeight == false)
        {
          absPos = correctPoint((Point2D) e.getStyle().getStyleProperty(ABSOLUTE_POS, DEFAULT_POS),
              base, absPos);
          // check whether the element would be visible .. if not visible, then
          // dont do anything ...
          if (absPos.getX() > base.getWidth() || absPos.getY() > base.getHeight())
          {
            // dont display, as this element is larger than the container ...
            continue;
          }

          absDim = correctDimension
              (computePreferredSize(e, base, absDim, support), base, absDim);

          if (staticWidth == false)
          {
            width = Math.max((float) (absDim.getWidth() + absPos.getX()), width);
          }
          if (staticHeight == false)
          {
            height = Math.max((float) (absDim.getHeight() + absPos.getY()), height);
          }
        }
      }

      // now apply the minimum limit defined for that band in case the calculated height
      // is lower than the given minimum height.
      height = (float) Math.max(height, minSize.getHeight());
      width = (float) Math.max(width, minSize.getWidth());

      // now take the maximum limit defined for that band into account for a last time.
      // or specifying -140 would be a nice way to kill the layout ...
      height = (float) Math.min(height, maxSize.getHeight());
      width = (float) Math.min(width, maxSize.getWidth());

      // now align the calculated data ...
      base.setSize(align(width, support.getHorizontalAlignmentBorder()),
          align(height, support.getVerticalAlignmentBorder()));

      // Log.debug("<" + base + " vs - " + b.getName());
      return base;
    }
  }

  /**
   * Calculates the minimum layout size for a band. The width for the child elements
   * are not calculated, as we assume that the width's are defined fixed within the
   * parent.
   *
   * @param b  the band.
   * @param containerBounds the bounds of the bands parents.
   * @param support the layout support used to compute sizes.
   *
   * @return the minimum size.
   */
  public Dimension2D minimumLayoutSize
      (final Band b, final Dimension2D containerBounds, final LayoutSupport support)
  {
    if (support == null)
    {
      throw new NullPointerException("LayoutSupport is null.");
    }
    if (b == null)
    {
      throw new NullPointerException("Band is null.");
    }
    if (containerBounds == null)
    {
      throw new NullPointerException("ContainerBounds is null.");
    }
    synchronized (b.getTreeLock())
    {
      final ElementLayoutInformation eli =
        createLayoutInformationForMinimumSize(b, containerBounds);
      final Dimension2D maxSize = eli.getMaximumSize();
      final Dimension2D minSize = eli.getMinimumSize();

      // we use the max width, as the width is bound to the outside container,
      // either an other band or the page; the width is required to compute dynamic
      // elements or elements with an 100% width ...
      float height = (float) minSize.getHeight();
      float width = (float) maxSize.getWidth();

      // Check the position of the elements inside and calculate the minimum width
      // needed to display all elements
      final Element[] elements = b.getElementArray();
      final Dimension2D tmpResult = null;

      // calculate absolute width
      for (int i = 0; i < elements.length; i++)
      {
        final Element e = elements[i];

        if (e.isVisible() == false)
        {
          continue;
        }
        final boolean staticWidth = isElementStaticWidth(e);
        final boolean staticHeight = isElementStaticHeight(e);
        if (staticWidth || staticHeight)
        {
          // absPos is readonly ...
          final Point2D absPos = (Point2D) e.getStyle().getStyleProperty(ABSOLUTE_POS, DEFAULT_POS);
          // check whether the element would be visible .. if not visible, then
          // dont do anything ...
          if (absPos.getX() > maxSize.getWidth() || absPos.getY() > maxSize.getHeight())
          {
            // dont display, as this element is larger than the container ...
            continue;
          }
          final Dimension2D size = computeMinimumSize(e, maxSize, tmpResult, support);

          if (staticWidth)
          {
            width = (float) Math.max(size.getWidth() + absPos.getX(), width);
          }
          if (staticHeight)
          {
            height = (float) Math.max(size.getHeight() + absPos.getY(), height);
          }
        }
      }

      //Log.debug ("Dimension after static part: " + width + " -> " + height);
      // now apply the minimum limit defined for that band in case the calculated height
      // is lower than the given minimum height.
      height = (float) Math.max(height, minSize.getHeight());
      width = (float) Math.max(width, minSize.getWidth());

      // now apply the maximum limit defined for that band in case the calculated height
      // is higher than the given max height.
      height = (float) Math.min(height, maxSize.getHeight());
      width = (float) Math.min(width, maxSize.getWidth());

      //Log.debug ("Dimension after static correction [MIN]: " + width + " -> " + height);
      final FloatDimension base = new FloatDimension(width, height);
      Point2D absPos = null;
      Dimension2D absDim = null;

      // calculate relative widths
      for (int i = 0; i < elements.length; i++)
      {
        final Element e = elements[i];

        if (e.isVisible() == false)
        {
          continue;
        }
        final boolean staticWidth = isElementStaticWidth(e);
        final boolean staticHeight = isElementStaticHeight(e);
        if (staticWidth == false || staticHeight == false)
        {
          absPos = correctPoint((Point2D)
              e.getStyle().getStyleProperty(ABSOLUTE_POS, DEFAULT_POS), base, absPos);
          // check whether the element would be visible .. if not visible, then
          // dont do anything ...
          if (absPos.getX() > base.getWidth() || absPos.getY() > base.getHeight())
          {
            // dont display, as this element is larger than the container ...
            continue;
          }
          absDim = correctDimension(computeMinimumSize(e, base, absDim, support), base, absDim);

          if (staticWidth == false)
          {
            width = (float) Math.max(absDim.getWidth() + absPos.getX(), width);
          }
          if (staticHeight == false)
          {
            height = (float) Math.max(absDim.getHeight() + absPos.getY(), height);
          }
          //Log.debug ("Element " + e + " -> " + size);
        }
      }

      //Log.debug ("Dimension after dynamic part: " + width + " -> " + height);
      // now apply the minimum limit defined for that band in case the calculated height
      // is lower than the given minimum height.
      height = (float) Math.max(height, minSize.getHeight());
      width = (float) Math.max(width, minSize.getWidth());

      // now take the maximum limit defined for that band into account for a last time.
      height = (float) Math.min(height, maxSize.getHeight());
      width = (float) Math.min(width, maxSize.getWidth());

      //Log.debug ("Dimension after dynamic correction: " + maxSize);
      //Log.debug ("Dimension after dynamic correction: " + width + " -> " + height);
      // now align the calculated data ...
      final FloatDimension fdim = new FloatDimension(
          align(width, support.getHorizontalAlignmentBorder()),
          align(height, support.getVerticalAlignmentBorder()));
      return fdim;
    }
  }

  /**
   * Layout a single band with all elements contained within the band.
   * <p>
   * The band has its <code>BOUNDS</code> already set and all elements are laid out
   * within these bounds. The band's properties will not be changed during the layouting.
   * <p>
   * This layout manager requires that all direct child elements have the <code>ABSOLUTE_POS</code>
   * and <code>MINIMUM_SIZE</code> properties set to valid values.
   *
   * @param b the band to lay out.
   * @param support the layout support used to compute sizes.
   * @throws java.lang.IllegalStateException if the bands has no bounds set.
   */
  public synchronized void doLayout(final Band b, final LayoutSupport support)
  {
    if (support == null)
    {
      throw new NullPointerException("LayoutSupport is null.");
    }
    if (b == null)
    {
      throw new NullPointerException("Band is null.");
    }
    synchronized (b.getTreeLock())
    {
      final Element[] elements = b.getElementArray();
      final Rectangle2D parentBounds = BandLayoutManagerUtil.getBounds(b, null);
      if (parentBounds == null)
      {
        throw new IllegalStateException("Need the parent's bound set");
      }

      final Dimension2D parentDim = new FloatDimension((float) parentBounds.getWidth(),
          (float) parentBounds.getHeight());

      // Log.debug ("My LayoutSize: " + b.getName() + " " + parentDim);

      Dimension2D absDim = null;
      Point2D absPos = null;
      for (int i = 0; i < elements.length; i++)
      {
        final Element e = elements[i];

        if (e.isVisible() == false)
        {
          continue;
        }
        absPos = correctPoint((Point2D)
            e.getStyle().getStyleProperty(ABSOLUTE_POS, DEFAULT_POS), parentDim, absPos);
        // check whether the element would be visible .. if not visible, then
        // dont do anything ...
        if (absPos.getX() > parentDim.getWidth() || absPos.getY() > parentDim.getHeight())
        {
          // dont display, as this element is larger than the container ...
          // Log.debug ("Element Out of Bounds: " + e);
          BandLayoutManagerUtil.setBounds(e, new Rectangle2D.Float(0, 0, 0, 0));
          continue;
        }

        absDim = computePreferredSize(e, parentDim, absDim, support);
        // docmark: Compute preferred size does never return negative values!
        // Log.debug ("UBounds: Element: " + e.getName() + " Bounds: " + uncorrectedSize);
        // absDim = correctDimension(uncorrectedSize, parentDim, absDim);
        // Log.debug ("CBounds: Element: " + e.getName() + " Bounds: " + absDim);

        // here apply the maximum bounds ...
        final Rectangle2D bounds = new Rectangle2D.Float(
            align((float) absPos.getX(), support.getHorizontalAlignmentBorder()),
            align((float) absPos.getY(), support.getVerticalAlignmentBorder()),
            align((float) absDim.getWidth(), support.getHorizontalAlignmentBorder()),
            align((float) absDim.getHeight(), support.getVerticalAlignmentBorder()));
        BandLayoutManagerUtil.setBounds(e, bounds);
        // Log.debug ("Bounds: Element: " + e.getName() + " Bounds: " + bounds);
        if (e instanceof Band)
        {
          final BandLayoutManager lm =
            BandLayoutManagerUtil.getLayoutManager(e);
          lm.doLayout((Band) e, support);
        }
      }
    }
  }

  /**
   * Returns <code>true</code> if the element has a static width, and <code>false</code> otherwise.
   *
   * @param e  the element.
   *
   * @return <code>true</code> or </code>false</code>.
   */
  protected boolean isElementStaticWidth(final Element e)
  {
    final Point2D absPos = (Point2D) e.getStyle().getStyleProperty(ABSOLUTE_POS, DEFAULT_POS);
    if (absPos == null)
    {
      throw new IllegalArgumentException("Element " + e
          + " has no valid constraints. ABSOLUTE_POS is missing");
    }
    if (absPos.getX() < 0)
    {
      return false;
    }
    return super.isElementStaticWidth(e);
  }

  /**
   * Returns true if the element has a static height, and false otherwise.
   *
   * @param e  the element.
   *
   * @return true or false.
   */
  protected boolean isElementStaticHeight(final Element e)
  {
    final Point2D absPos = (Point2D) e.getStyle().getStyleProperty(ABSOLUTE_POS, DEFAULT_POS);
    if (absPos.getY() < 0)
    {
      return false;
    }
    return super.isElementStaticHeight(e);
  }

  /**
   * Clears any cached items used by the layout manager.
   *
   * @param container  the container band.
   */
  public void invalidateLayout(final Band container)
  {
    synchronized (container.getTreeLock())
    {
      cache.flush();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -