1 14 package org.compiere.apps; 15 16 import java.awt.*; 17 import java.util.*; 18 19 import org.compiere.util.Log; 20 21 35 public class ALayout implements LayoutManager2 36 { 37 40 public ALayout() 41 { 42 this (2,4, true); 43 } 45 51 public ALayout(int spaceH, int spaceV, boolean colFill) 52 { 53 setSpaceH(spaceH); 54 setSpaceV(spaceV); 55 m_colFill = colFill; 56 } 58 59 private ALayoutCollection m_data = new ALayoutCollection(); 60 61 private int m_spaceH; 62 63 private int m_spaceV; 64 65 private boolean m_colFill; 66 67 73 public void addLayoutComponent(String name, Component comp) 74 { 75 addLayoutComponent (comp, null); 76 } 78 89 public void addLayoutComponent(Component component, Object constraint) 90 { 91 ALayoutConstraint con = null; 92 if (constraint instanceof ALayoutConstraint) 93 con = (ALayoutConstraint)constraint; 94 m_data.put(con, component); 96 } 98 102 public void removeLayoutComponent(Component comp) 103 { 104 if (!m_data.containsValue(comp)) 105 return; 106 Iterator it = m_data.keySet().iterator(); 107 while (it.hasNext()) 108 { 109 Object key = it.next(); 110 if (m_data.get(key).equals(comp)) 111 { 112 m_data.remove(key); 113 return; 114 } 115 } 116 } 118 125 public Dimension preferredLayoutSize(Container parent) 126 { 127 return calculateLayoutSize (parent, 'P'); 128 } 130 137 public Dimension minimumLayoutSize(Container parent) 138 { 139 return calculateLayoutSize (parent, 'm'); 140 } 142 150 public Dimension maximumLayoutSize(Container parent) 151 { 152 return calculateLayoutSize (parent, 'M'); 153 } 155 161 private Dimension calculateLayoutSize(Container parent, char how) 162 { 163 checkComponents(parent); 164 int rows = getRowCount(); 166 int cols = getColCount(); 167 Dimension[][] dim = new Dimension[rows][cols]; 168 Object [] keys = m_data.keySet().toArray(); 170 Arrays.sort(keys); 171 for (int i = 0; i < keys.length; i++) 172 { 173 ALayoutConstraint constraint = (ALayoutConstraint)keys[i]; 174 Component component = (Component)m_data.get(keys[i]); 175 Dimension d = null; 176 if (how == 'P') 177 d = component.getPreferredSize(); 178 else if (how == 'M') 179 d = component.getMaximumSize(); 180 else 181 d = component.getMinimumSize(); 182 if (component.isVisible()) 183 dim [constraint.getRow()][constraint.getCol()] = d; 184 else 185 dim [constraint.getRow()][constraint.getCol()] = null; 186 } 187 188 Insets insets = parent.getInsets(); 190 Dimension retValue = new Dimension (insets.left+insets.right, insets.top+insets.bottom); 191 retValue.height += m_spaceH; 192 retValue.width += m_spaceV; 193 int maxWidth = 0; 194 for (int r = 0; r < rows; r++) 195 { 196 int height = 0; 197 int width = 0; 198 for (int c = 0; c < cols; c++) 199 { 200 Dimension d = dim[r][c]; 201 if (d != null) 202 { 203 width += d.width; 204 height = Math.max(height, d.height); 205 } 206 width += m_spaceV; 207 } retValue.height += height + m_spaceH; 209 maxWidth += Math.max(maxWidth, width); 210 } retValue.width += maxWidth; 212 return retValue; 214 } 216 220 public void layoutContainer(Container parent) 221 { 222 checkComponents(parent); 223 int rows = getRowCount(); 225 int cols = getColCount(); 226 Component[][] com = new Component[rows][cols]; 227 Object [] keys = m_data.keySet().toArray(); 229 Arrays.sort(keys); 230 for (int i = 0; i < keys.length; i++) 231 { 232 ALayoutConstraint constraint = (ALayoutConstraint)keys[i]; 233 Component component = (Component)m_data.get(keys[i]); 234 if (component.isVisible()) 235 com [constraint.getRow()][constraint.getCol()] = component; 236 else 237 com [constraint.getRow()][constraint.getCol()] = null; 238 } 239 240 int[] colWidth = new int[cols]; 242 int[] rowHeight = new int[rows]; 243 int columnWidth = m_spaceV; 244 for (int c = 0; c < cols; c++) 245 { 246 int width = 0; 247 for (int r = 0; r < rows; r++) 248 { 249 Component component = com[r][c]; 250 if (component != null) 251 { 252 width = Math.max (width, component.getPreferredSize().width); 253 rowHeight[r] = Math.max (rowHeight[r], component.getPreferredSize().height); 254 } 255 } 256 colWidth[c] = width; 257 columnWidth += width + m_spaceV; 258 } 259 260 int parentWidth = parent.getSize().width; 262 double multiplier = (double)parentWidth / (double)columnWidth; 263 if (multiplier < .5) multiplier = .5; 265 for (int c = 0; c < cols; c++) 266 colWidth[c] = (int) (colWidth[c] * multiplier); 267 int spaceV = (int)(m_spaceV * multiplier); 268 272 Insets insets = parent.getInsets(); 274 int posH = insets.top + m_spaceH; 275 for (int r = 0; r < rows; r++) 276 { 277 int posV = insets.left + spaceV; 278 int height = 0; 279 for (int c = 0; c < cols; c++) 280 { 281 Component component = com[r][c]; 282 if (component != null) 283 { 284 Dimension ps = component.getPreferredSize(); 285 int w = ps.width; 286 if (m_colFill || w > colWidth[c]) w = colWidth[c]; 288 int h = ps.height; 289 int topSpace = 0; 290 if (h < rowHeight[r]) topSpace = (rowHeight[r] - h) / 3; 292 height = Math.max(height, h); 293 component.setBounds(posV, posH+topSpace, w, h); 294 } 297 posV += colWidth[c] + spaceV; 298 } posH += height + m_spaceH; 300 } } 303 312 public float getLayoutAlignmentX(Container target) 313 { 314 return 0f; 315 } 317 326 public float getLayoutAlignmentY(Container target) 327 { 328 return 0f; 329 } 331 336 public void invalidateLayout(Container target) 337 { 338 } 340 341 342 346 private void checkComponents (Container target) 347 { 348 int size = target.getComponentCount(); 349 for (int i = 0; i < size; i++) 350 { 351 Component comp = target.getComponent(i); 352 if (!m_data.containsValue(comp)) 353 m_data.put(null, comp); 354 } 355 } 357 361 public int getRowCount() 362 { 363 return m_data.getMaxRow()+1; 364 } 366 370 public int getColCount() 371 { 372 return m_data.getMaxCol()+1; 373 } 375 379 public void setSpaceH (int spaceH) 380 { 381 m_spaceH = spaceH; 382 } 384 388 public int getSpaceH() 389 { 390 return m_spaceH; 391 } 393 397 public void setSpaceV(int spaceV) 398 { 399 m_spaceV = spaceV; 400 } 402 406 public int getSpaceV() 407 { 408 return m_spaceV; 409 } 411 } | Popular Tags |