bboks.net™

PositionLayout 본문

Java/Java

PositionLayout

bboks.net 2009. 2. 24. 13:33
Postion Layout은 사용자 정의 레이아웃입니다.
사용법은 null 레이아웃으로 설정하여 setBounds를 이용하는 방법과 동일합니다.
하지만 Postion Layout은 위치를 퍼센트로 계산하기 때문에 크기 조정시 레이아웃에 포함된 컴포넌트의 크기가 조정됩니다.

import java.awt.*;
import java.util.*;

public class PositionLayout implements LayoutManager {
    Vector  positions = new Vector(10);
    Vector  components = new Vector(10);
    private  boolean   leftRatio;
    private  boolean   topRatio;
    private  boolean   rightRatio;
    private  boolean   bottomRatio;

    public void addLayoutComponent(String p, Component c) {
        positions.addElement(p);
        components.addElement(c);
    }


    public void removeLayoutComponent(Component c) {

        int i = components.indexOf(c);
        if(i != -1) {
            positions.removeElementAt(i);
            components.removeElementAt(i);
        }
    }


    public Dimension preferredLayoutSize(Container parent) {
        Dimension dim = new Dimension(0,0);
        int ncomponents = parent.getComponentCount();
        Insets  insets = parent.getInsets();
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom;

        int maxW = dim.width;
        int maxH = dim.height;
        for(int i = 0; i < ncomponents; i++) {
            Component  com = parent.getComponent(i);
            if(com.isVisible() == false)
                continue;
            int  w = com.getPreferredSize().width + dim.width;
            int  h = com.getPreferredSize().height + dim.height;
            if( w > maxW )
                maxW = w;
            if( h > maxH )
                maxH = h;
        }
        //System.out.println("prefer Size: width="+maxW+"\t\theight="+maxH);
        return  new Dimension(maxW, maxH);
    }


    public Dimension minimumLayoutSize(Container target) {
        return  target.getSize();
    }


    public void layoutContainer(Container target) {
        Insets insets = target.getInsets();
        int    ncomponents = target.getComponentCount();
        Dimension d = target.getSize();
        d.width  -= insets.left + insets.right;
        d.height  -= insets.top + insets.bottom;
        int  startX =0, startY =0, endX =0, endY =0;
        int  left = 0, top =0, right = 0, bottom =0;
        for(int i=0; i< ncomponents; i++) {
            Component comp = target.getComponent(i);
         StringTokenizer token = new StringTokenizer((String)positions.elementAt(i), ", \t");
            String  leftSt = token.nextToken();
            if(leftSt.endsWith("%")) {
                leftRatio = true;
                left  = Integer.parseInt(leftSt.substring(0, leftSt.length()-1));
            } else {
                left  = Integer.parseInt(leftSt);
            }

            String  topSt = token.nextToken();
            if(topSt.endsWith("%")) {
                topRatio = true;
                top  = Integer.parseInt(topSt.substring(0, topSt.length()-1));
            } else {
                top  = Integer.parseInt(topSt);
            }

            String  rightSt = token.nextToken();
            if(rightSt.endsWith("%")) {
                rightRatio = true;
                right  = Integer.parseInt(rightSt.substring(0, rightSt.length()-1));
            } else {
                right  = Integer.parseInt(rightSt);
            }

            String  bottomSt = token.nextToken();
            if(bottomSt.endsWith("%")) {
                bottomRatio = true;
                bottom  = Integer.parseInt(bottomSt.substring(0, bottomSt.length()-1));
            } else {
                bottom  = Integer.parseInt(bottomSt);
            }

            if(leftRatio)
                startX = (d.width * left)/100;
            else
                startX = left;

            if(topRatio)
                startY = (d.height * top)/100;
            else
                startY = top;

            if(rightRatio)
                endX = (d.width * right)/100;
            else
                endX = right;

            if(bottomRatio)
                endY = (d.height * bottom)/100;
            else
                endY = bottom;

            if(startX > endX) {
                int temp = startX;
                startX = endX;
                endX = temp;
            }
            if(startY > endY) {
                int temp = startY;
                startY = endY;
                endY = temp;
            }
            int  w = endX - startX;
            int  h = endY - startY;
            comp.setBounds(startX+insets.left, startY+insets.top, w, h);
            topRatio = false;
            leftRatio = false;
            rightRatio = false;
            bottomRatio = false;
        }
    }

    public String toString() {
        return getClass().getName();
    }
}