본문 바로가기
청춘다반사

프로그래머를 위한 JAVA 2 PositionLAyout.JAVA

by 필팔청춘 2011. 6. 13.

프로그래머를 위한 JAVA 2  PositionLAyout.JAVA

치느라고 힘들었네.



import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;

public class PositionLayout implements LayoutManager{
 protected Vector positions = new Vector();
 protected Vector components = new Vector();
 private boolean leftRatio, leftInsets;
 private boolean topRatio, topInsets;
 private boolean rightRatio, rightInsets;
 private boolean bottomRatio, bottomInsets;
 
 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;
  }
  
  return new Dimension(maxW, maxH);
 }
 
 public Dimension minimulLayoutSize(Container target) {
  return target.getSize();
 }
 
 public void layoutContainer(Container target){
  boolean unknownX = false, unknownY = false;
  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), ",");
   String leftSt = null;
   if(token.hasMoreTokens())
    leftSt = token.nextToken().trim();
   
   if(!leftSt.startsWith("?")) {
    try {
     if(leftSt.endsWith("%")) {
      leftRatio = true;
      left = Integer.parseInt(leftSt.substring(0, leftSt.length()-1));
     }else {
      left = Integer.parseInt(leftSt);
     }
    } catch(Exception e) {
     leftRatio = false;
     left = getValue(leftSt, target);
     leftInsets = true;
    }
   } else
    unknownX = true;
   String topSt = null;
   if(token.hasMoreTokens())
    topSt = token.nextToken().trim();
   
   if(!topSt.startsWith("?")){
    try {
     if(topSt.endsWith("%")){
      topRatio = true;
      top = Integer.parseInt(topSt.substring(0, topSt.length()-1));
     } else {
      top = Integer.parseInt(topSt);
     }
    } catch(Exception e) {
     topRatio = false;
     top = getValue(topSt, target);
     topInsets = true;
    }
   } else
    unknownY = true;
   
   String rightSt = null;
   if(token.hasMoreTokens())
    rightSt = token.nextToken().trim();
   
   try {
    if(rightSt.startsWith("?")) {
     rightRatio = false;
     right = comp.getPreferredSize().width;
     rightInsets = false;
    } else {
     if(rightSt.endsWith("%")){
      rightRatio = true;
      right =Integer.parseInt(rightSt.substring(0, rightSt.length()-1));
     } else {
      right = Integer.parseInt(rightSt);
     }
    }
   } catch(Exception e){
    rightRatio = false;
    rightInsets = true;
    right = getValue(rightSt, target);
    if(right == 0 ){
     right = comp.getPreferredSize().width;
     rightInsets = false;
    }
   }
   
   String bottomSt = null;
   if(token.hasMoreTokens())
    bottomSt = token.nextToken().trim();
   
   try {
    if(bottomSt.startsWith("?")){
     bottom = comp.getPreferredSize().height;
     bottomInsets = false;
     bottomRatio = false;
    } else {
     if(bottomSt.endsWith("%")) {
      bottomRatio = true;
      bottom = Integer.parseInt(bottomSt.substring(0, bottomSt.length()-1));
     } else {
      bottom =Integer.parseInt(bottomSt);
     }
    }
   } catch(Exception e) {
    bottomRatio = false;
    bottomInsets = true;
    bottom = getValue(bottomSt, target);
    if(bottom == 0) {
     bottom = comp.getPreferredSize().height;
     bottomInsets = false;
    }
   }
   if(!unknownX){
    if(leftRatio)
     startX = (d.width * left)/100;
    else if(leftInsets)
     startX = left;
    else
     startX = left+insets.left;
   }
   
   if(unknownY){
    if(topRatio)
     startY = (d.height * top/100);
    else if(topInsets)
     startY = top;
    else
     startY = top+insets.top;
   }
   
   if(rightRatio)
    endX = (d.width * right)/100;
   else {
    if(right < 0)
     endX = d.width + right + insets.right;
    else if(rightInsets)
     endX = right;
    else
     endX = startX + right;
   }
   if(unknownX) {
    startX = endX - comp.getPreferredSize().width;
   }
   
   if(bottomRatio)
    endY = (d.height * bottom)/100;
   else {
    if(bottom < 0)
     endY = d.height + bottom + insets.top;
    else if(bottomInsets)
     endY = bottom;
    else
     endY = startY + bottom;
   }
   if(unknownY) {
    startY = endY - comp.getPreferredSize().height;
   }
   if(startX > endX){
    int temp = startX;
    startX = endX;
    endX = temp;
   }
   if(startY > endY ) {
    int temp = startY;
    startY = endY;
    endY = temp;
   }
   int w = endX - startX;
   if(rightRatio || leftRatio) {
    w = endX - startX;
    startX += insets.left;
   }
   
   int h = endY - startY;
   if(bottomRatio || topRatio) {
    h = endY - startY;
    startY += insets.top;
   }
   topRatio = false;
   leftRatio =false;
   rightRatio = false;
   bottomRatio = false;
   leftInsets = false;
   topInsets = false;
   rightInsets = false;
   bottomInsets = false;
   unknownX = false;
   unknownY = false;
  }
 }
 public String toStirng() {
  return getClass().getName();
 }
 private int getValue(String v, Container target) {
  if(v == null)
   return 0;
  
  int ncomponents = target.getComponentCount();
  StringTokenizer st = new StringTokenizer(v, ".+=", true);
  String name = null, direction = null, valueSt = null;
  int value = 0;
  if(st.hasMoreTokens()) {
   name = st.nextToken().trim();
   st.nextToken();
  }
  String sign = "";
  if(st.hasMoreTokens()){
   direction = st.nextToken().trim();
   if(st.hasMoreTokens())
    sign = st.nextToken().trim();
  }
  if(st.hasMoreTokens()){
   valueSt = st.nextToken().trim();
   try {
    if(sign.startsWith("-"))
     value = - Integer.parseInt(valueSt);
    else
     value = Integer.parseInt(valueSt);
   } catch (Exception e) {
    value = 0;
   }
  }
  for(int j =0; j < ncomponents; j++) {
   Component co = target.getComponent(j);
   if(co.getName().equals(name)) {
    if(direction.equalsIgnoreCase("left")) {
     return co.getBounds().x+value;
    } else if(direction.equalsIgnoreCase("top")){
     return co.getBounds().y+value;
    } else if(direction.equalsIgnoreCase("right")){
     Rectangle r = co.getBounds();
     return r.x = r.width + value;
    } else if(direction.equalsIgnoreCase("bottom")) {
     Rectangle r = co.getBounds();
     return r.y + r.height + value;
    }
   }
  }
  return 0;
 }

 @Override
 public Dimension minimumLayoutSize(Container parent) {
  // TODO Auto-generated method stub
  return null;
 }
}