1 package org.enhydra.tool.swing.layout; 2 3 import java.awt.Component ; 4 import java.awt.Container ; 5 import java.awt.Dimension ; 6 import java.awt.Insets ; 7 import java.awt.LayoutManager2 ; 8 import java.awt.Rectangle ; 9 import java.io.Serializable ; 10 import java.util.Hashtable ; 11 12 13 14 public class XYLayout 15 implements LayoutManager2 , Serializable 16 { 17 18 private static final long serialVersionUID = 200L; 19 int width; 20 int height; 21 Hashtable info; 22 static final XYConstraints defaultConstraints = new XYConstraints(); 23 24 public XYLayout() 25 { 26 info = new Hashtable (); 27 } 28 29 public XYLayout(int width, int height) 30 { 31 info = new Hashtable (); 32 this.width = width; 33 this.height = height; 34 } 35 36 public int getWidth() 37 { 38 return width; 39 } 40 41 public void setWidth(int width) 42 { 43 this.width = width; 44 } 45 46 public int getHeight() 47 { 48 return height; 49 } 50 51 public void setHeight(int height) 52 { 53 this.height = height; 54 } 55 56 public String toString() 57 { 58 return String.valueOf(String.valueOf((new StringBuffer ("XYLayout[width=")).append(width).append(",height=").append(height).append("]"))); 59 } 60 61 public void addLayoutComponent(String s, Component component1) 62 { 63 } 64 65 public void removeLayoutComponent(Component component) 66 { 67 info.remove(component); 68 } 69 70 public Dimension preferredLayoutSize(Container target) 71 { 72 return getLayoutSize(target, true); 73 } 74 75 public Dimension minimumLayoutSize(Container target) 76 { 77 return getLayoutSize(target, false); 78 } 79 80 public void layoutContainer(Container target) 81 { 82 Insets insets = target.getInsets(); 83 int count = target.getComponentCount(); 84 for(int i = 0; i < count; i++) 85 { 86 Component component = target.getComponent(i); 87 if(component.isVisible()) 88 { 89 Rectangle r = getComponentBounds(component, true); 90 component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height); 91 } 92 } 93 94 } 95 96 public void addLayoutComponent(Component component, Object constraints) 97 { 98 if(constraints instanceof XYConstraints) 99 info.put(component, constraints); 100 } 101 102 public Dimension maximumLayoutSize(Container target) 103 { 104 return new Dimension (0x7fffffff, 0x7fffffff); 105 } 106 107 public float getLayoutAlignmentX(Container target) 108 { 109 return 0.5F; 110 } 111 112 public float getLayoutAlignmentY(Container target) 113 { 114 return 0.5F; 115 } 116 117 public void invalidateLayout(Container container) 118 { 119 } 120 121 Rectangle getComponentBounds(Component component, boolean doPreferred) 122 { 123 XYConstraints constraints = (XYConstraints)info.get(component); 124 if(constraints == null) 125 constraints = defaultConstraints; 126 Rectangle r = new Rectangle (constraints.x, constraints.y, constraints.width, constraints.height); 127 if(r.width <= 0 || r.height <= 0) 128 { 129 Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize(); 130 if(r.width <= 0) 131 r.width = d.width; 132 if(r.height <= 0) 133 r.height = d.height; 134 } 135 return r; 136 } 137 138 Dimension getLayoutSize(Container target, boolean doPreferred) 139 { 140 Dimension dim = new Dimension (0, 0); 141 if(width <= 0 || height <= 0) 142 { 143 int count = target.getComponentCount(); 144 for(int i = 0; i < count; i++) 145 { 146 Component component = target.getComponent(i); 147 if(component.isVisible()) 148 { 149 Rectangle r = getComponentBounds(component, doPreferred); 150 dim.width = Math.max(dim.width, r.x + r.width); 151 dim.height = Math.max(dim.height, r.y + r.height); 152 } 153 } 154 155 } 156 if(width > 0) 157 dim.width = width; 158 if(height > 0) 159 dim.height = height; 160 Insets insets = target.getInsets(); 161 dim.width += insets.left + insets.right; 162 dim.height += insets.top + insets.bottom; 163 return dim; 164 } 165 166 } 167 | Popular Tags |