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

📄 higlayout.java

📁 超市收银系统(运用JDBC连接ACCESS数据库) 界面美观,功能比较齐全
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package cz.autel.dmi;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;


public class HIGLayout implements LayoutManager2, java.io.Serializable
{

    /* since we number rows and columns from 1, size of these arrays 
     * must be nummber columns + 1*/
    private int[] colWidths;
    private int[] rowHeights;
    private int colCount;
    private int rowCount;

    private ArrayList[] colComponents;
    private ArrayList[] rowComponents;

    private int[] widenWeights;
    private int[] heightenWeights;

    // Add in preferred heights and widths
    private int []preferredWidths;
    private int []preferredHeights;
    
    private int widenWeightsSum = 0;
    private int heightenWeightsSum = 0;

    private final int invisible = 0;;

    private HashMap components = new HashMap();

    /* Following variables are for caching of computations: */
    private int cacheColumnsX[];
    private int cacheRowsY[];
    private Dimension cachePreferredLayoutSize;
    private Dimension cacheMinimumLayoutSize;

    /**
     * Construct a new layout object. Length of passed arrays define number of columns and
     * number of rows. Each width or height can be less then 0, equal 0 or greater then 0.
     * Passed arrays defines design grid - sizes and dependences between columns and rows.
     * For details see tutorial.
     * @param widths array of column widths.
     * @param heights array of row heights.
     */
    public HIGLayout(int[] widths, int[] heights)
    {
        colCount = widths.length;
        rowCount = heights.length;

        colWidths = new int[colCount + 1];
        System.arraycopy(widths, 0, colWidths, 1, colCount);
        rowHeights = new int[rowCount + 1];
        System.arraycopy(heights, 0, rowHeights, 1, rowCount);

        widenWeights = new int[colCount + 1];
        heightenWeights = new int[rowCount + 1];

        preferredWidths = new int[colCount +1];
        preferredHeights = new int[rowCount +1];

        colComponents = new ArrayList[colCount + 1];
        rowComponents = new ArrayList[rowCount + 1];
    }

    /**
     *
     * @since 0.97
     */
    private Object reallocArray(Object src, int newSize)
    {
        Object dest = java.lang.reflect.Array.newInstance(src.getClass().getComponentType(), newSize);
        System.arraycopy(src, 0, dest, 0, java.lang.reflect.Array.getLength(src));
        return dest;
    }

    /**
     * Sets column width, realloc arrays if there is need.
     * @since 0.97
     */
    public void setColumnWidth(int col, int width)
    {
        if (colCount < col)
        {
            colCount = col;
        }
        if (colWidths.length <= col)
        {
            colWidths = (int[]) reallocArray(colWidths, colCount + 3);
            widenWeights = (int[]) reallocArray(widenWeights, colCount + 3);
            colComponents = (ArrayList[]) reallocArray(colComponents, colCount + 3);
            preferredWidths = (int[])reallocArray(preferredWidths, colCount + 3);
        }
        colWidths[col] = width;
    }

    /**
     * Sets row height, realloc arrays if there is need.
     * @since 0.97
     */
    public void setRowHeight(int row, int height)
    {
        if (rowCount < row)
        {
            rowCount = row;
        }
        if (rowHeights.length <= row)
        {
            rowHeights = (int[]) reallocArray(rowHeights, rowCount + 3);
            heightenWeights = (int[]) reallocArray(heightenWeights, rowCount + 3);
            rowComponents = (ArrayList[]) reallocArray(rowComponents, rowCount + 3);
        }
        rowHeights[row] = height;
    }

    /**
     * Sets preferred width of specified column.
     * @param col index of column. Index must be > 0.
     * @param width the width to use in pixels
     * @since 1.0
     */
    public void setPreferredColumnWidth(int col, int width) {
        if(col > colCount) {
            throw new IllegalArgumentException("Column index cannot be greater then "
                                                    + colCount + ".");
        }
        preferredWidths[col] = width;
    }

    /**
     * Sets preferred height of specified row.
     * of difference when resizing.
     * @param row index of row. Index must be > 0.
     * @param height the height in pixels
     * @since 1.0
     */
    public void setPreferredRowHeight(int row, int height) {
        if(row > rowCount) {
            throw new IllegalArgumentException("Column index cannot be greater then "
                                                    + rowCount + ".");
        }
        preferredHeights[row] = height;
    }
    
    /**
     * Sets weight of specified column. Weight determines distribution
     * of difference when resizing.
     * @param col index of column. Index must be > 0.
     */
    public void setColumnWeight(int col, int weight)
    {
        if (col > colCount)
        {
            throw new RuntimeException("Column index cannot be greater then " + colCount + ".");
        }
        widenWeights[col] = weight;
        widenWeightsSum = 0;
        for (int i = 1; i <= colCount; i++) widenWeightsSum += widenWeights[i];
    }

    /**
     * Sets weight of specified row. Weight determines distribution
     * of difference when resizing.
     * @param row index of row. Index must be > 0.
     */
    public void setRowWeight(int row, int weight)
    {
        if (row > rowCount)
        {
            throw new RuntimeException("Column index cannot be greater then " + rowCount + ".");
        }
        heightenWeights[row] = weight;
        heightenWeightsSum = 0;
        for (int i = 1; i <= rowCount; i++) heightenWeightsSum += heightenWeights[i];
    }

    /**
     * @deprecated  replaced by <code>addLayoutComponent(Component, Object)</code>. Throws
     * <EM>UnsupportedOperationException</EM>.
     */
    public void addLayoutComponent(String name, Component comp)
    {
        throw new UnsupportedOperationException();
    }

    /**
     * Removes the specified component from the layout.
     * @param comp the component to be removed
     */
    public void removeLayoutComponent(Component comp)
    {
        synchronized (comp.getTreeLock())
        {
            HIGConstraints c = (HIGConstraints) components.remove(comp);
            if (c == null) return;
            if (colComponents[c.x] != null) colComponents[c.x].remove(comp);
            if (rowComponents[c.y] != null) rowComponents[c.y].remove(comp);
        }

    }


    private void solveCycles(int g[], int lengths[])
    {
        /* TODO: handle cycles of length 1*/
        int path[] = new int[g.length];
        int stackptr = 0;

        /* marks of visited vertices. 0 - not visited, 1 - visited, 2 - visited and set final value */
        byte visited[] = new byte[g.length];
        for (int i = g.length - 1; i > 0; i--)
        {
            if ((g[i] < 0) && (visited[i] == 0))
            {
                int current = i;

                /* find cycle or path with cycle */
                stackptr = 0;
                int maxLength = 0;
                int last;
                do
                {
                    maxLength = (lengths[current] > maxLength) ? lengths[current] : maxLength;
                    path[stackptr++] = current;
                    visited[current] = 1;
                    last = current;
                    current = -g[current];
                }
                while ((current > 0) && (visited[current] == 0));

                if (current <= 0)
                {
                    /* there is no cycle, only end of path */
                    maxLength = lengths[last];
                }
                else if (current == 0)
                {
                    maxLength = lengths[last];
                }
                else if (visited[current] == 1)
                {
                    /* cycle, max. cannot lie outside the cycle, find it */
                    int start = current;
                    maxLength = 0;
                    do
                    {
                        maxLength = (lengths[current] > maxLength) ? lengths[current] : maxLength;
                        current = -g[current];
                    }
                    while (start != current);
                }
                else if (visited[current] == 2)
                {
                    /* this vertice already has final value */
                    maxLength = lengths[current];
                }
                else
                {
                    throw new RuntimeException("This should not happen.");
                }
                while (stackptr > 0)
                {
                    lengths[path[--stackptr]] = maxLength;
                    visited[path[stackptr]] = 2;
                }
            }
        }
    }

    private int[] calcMinWidths()
    {
        int[] widths = new int[colCount + 1];
        for (int i = 1; i <= colCount; i++)
        {
            if (colWidths[i] > 0)
            {
                widths[i] = colWidths[i];
            }
            else
            {
                ArrayList iComps = colComponents[i];
                int maxWidth = 0;
                if (iComps != null)
                {
                    for (int j = iComps.size() - 1; j > -1; j--)
                    {
                        Component c = (Component) iComps.get(j);
                        int width = c.isVisible() ? c.getMinimumSize().width : 0;

                        if(width > 0)
                        {
                            HIGConstraints constr = (HIGConstraints) components.get(c);
                            if (constr.w < 0)
                                width = -constr.w;
                            else
                                width += constr.wCorrection;
                        }
                        maxWidth = (width > maxWidth) ? width : maxWidth;
                    }
                }
                widths[i] = maxWidth;
            }
        }
        solveCycles(colWidths, widths);

        return widths;
    }

    private int[] calcMinHeights()
    {
        int[] heights = new int[rowCount + 1];
        for (int i = 1; i <= rowCount; i++)
        {
            if (rowHeights[i] > 0)
            {
                heights[i] = rowHeights[i];
            }
            else
            {
                int maxHeight = 0;
                ArrayList iComps = rowComponents[i];
                if (iComps != null)
                {
                    for (int j = iComps.size() - 1; j > -1; j--)
                    {
                        Component c = (Component) iComps.get(j);
                        int height = c.isVisible() ? c.getMinimumSize().height : invisible;
                        if(height > 0)
                        {
                            HIGConstraints constr = (HIGConstraints) components.get(c);
                            if (constr.h < 0)
                                height = -constr.h;
                            else
                                height += constr.hCorrection;
                        }
                        maxHeight = (height > maxHeight) ? height : maxHeight;
                    }
                }
                heights[i] = maxHeight;
            }
        }
        solveCycles(rowHeights, heights);

        return heights;
    }

    private int[] calcPreferredWidths()
    {
        int[] widths = new int[colCount + 1];
        for (int i = 1; i <= colCount; i++) {
            if (colWidths[i] > 0) {
                widths[i] = colWidths[i];
            } else if (preferredWidths[i] > 0) {
                widths[i] = preferredWidths[i];
            } else {
                int maxWidth = 0;
                ArrayList iComps = colComponents[i];
                if (iComps != null)
                {
                    for (int j = iComps.size() - 1; j > -1; j--)

⌨️ 快捷键说明

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