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