HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Collections in JDK"
Volume: 3 Issue: 9, p. 8

	

Listing 1.
 
public static final String EAST = "East"; 
public static final String WEST = "West"; 
public static final String NORTH = "North"; 
public static final String SOUTH = "South"; 
public static final String CENTER = "Center"; 
public static final String NORTHEAST = "NorthEast"; 
public static final String NORTHWEST = "NorthWest"; 
public static final String SOUTHEAST = "SouthEast"; 
public static final String SOUTHWEST = "SouthWest"; 

protected Hashtable table = new Hashtable(); 

private static final int PREFERRED = 1; 
private static final int MINIMUM = 0; 

private Component getComponent(String name) 
{ 
if (!table.containsKey(name)) name = CENTER; 
return (Component)table.get(name); 
} 

private boolean isVisible(String name) 
{ 
if (!table.containsKey(name)) return false; 
return getComponent(name).isVisible(); 
} 

private void setBounds(String name, 
int x, int y, int w, int h) 
{ 
if (!isVisible(name)) return; 
getComponent(name).setBounds(x, y, w, h); 
} 

private Dimension getSize(int type, String name) 
{ 
if (!isVisible(name)) return new Dimension(0, 0); 
if (type == PREFERRED) 
return getComponent(name).getPreferredSize(); 
if (type == MINIMUM) 
return getComponent(name).getMinimumSize(); 
return new Dimension(0, 0); 
} 

Listing 2
 
private Insets calculateLayoutSize(int type) 
{ 
Dimension size = new Dimension(0, 0); 

int northHeight = 0; 
int southHeight = 0; 
int eastWidth = 0; 
int westWidth = 0; 

size = getSize(type, NORTH); 
northHeight = Math.max(northHeight, size.height); 

size = getSize(type, SOUTH); 
southHeight = Math.max(southHeight, size.height); 

size = getSize(type, EAST); 
eastWidth = Math.max(eastWidth, size.width); 

size = getSize(type, WEST); 
westWidth = Math.max(westWidth, size.width); 

size = getSize(type, NORTHWEST); 
northHeight = Math.max(northHeight, size.height); 
westWidth = Math.max(westWidth, size.width); 

size = getSize(type, SOUTHWEST); 
southHeight = Math.max(southHeight, size.height); 
westWidth = Math.max(westWidth,size.width); 

size = getSize(type, NORTHEAST); 
northHeight = Math.max(northHeight, size.height); 
eastWidth = Math.max(eastWidth, size.width); 

size = getSize(type, SOUTHEAST); 
southHeight = Math.max(southHeight, size.height); 
eastWidth = Math.max(eastWidth, size.width); 

return new Insets(northHeight, 
westWidth, southHeight, eastWidth); 
} 

public Dimension preferredLayoutSize(Container target) 
{ 
Dimension size, dim = new Dimension(0, 0); 
size = getSize(PREFERRED, CENTER); 
dim.width += size.width; 
dim.height += size.height; 

Insets edge = calculateLayoutSize(PREFERRED); 
dim.width += edge.right + hgap; 
dim.width += edge.left + hgap; 
dim.height += edge.top + hgap; 
dim.height += edge.bottom + hgap; 

Insets insets = target.getInsets(); 
dim.width += insets.left + insets.right; 
dim.height += insets.top + insets.bottom; 
return dim; 
} 

Listing 3.
 
public void layoutContainer(Container target) 
{ 
Insets insets = target.getInsets(); 
Insets edge = calculateLayoutSize(PREFERRED); 

int top = insets.top; 
int bottom = target.getSize().height - insets.bottom; 
int left = insets.left; 
int right = target.getSize().width - insets.right; 

setBounds(NORTH, 
left + edge.left + hgap, top, 
right - edge.left - hgap - edge.right - hgap, 
edge.top); 
setBounds(SOUTH, 
left + edge.left + hgap, bottom - edge.bottom, 
right - edge.left - hgap - edge.right - hgap, 
edge.bottom); 
setBounds(EAST, 
right - edge.right, top + edge.top + vgap, edge.right, 
bottom - edge.top - vgap - edge.bottom - vgap); 
setBounds(WEST, 
left, top + edge.top + vgap, edge.left, 
bottom - edge.top - vgap - edge.bottom - vgap); 
setBounds(NORTHWEST, 
left, top, edge.left, edge.top); 
setBounds(SOUTHWEST, 
left, bottom - edge.bottom, 
edge.left, edge.bottom); 
setBounds(NORTHEAST, 
right - edge.right, top, 
edge.right, edge.top); 
setBounds(SOUTHEAST, 
right - edge.right, bottom - edge.bottom, 
edge.right, edge.bottom); 

top += edge.top + vgap; 
bottom -= edge.bottom + vgap; 
left += edge.left + hgap; 
right -= edge.right + hgap; 
setBounds(CENTER, 
left, top, right - left, bottom - top); 
} 

Listing 4.
 
public void setRows(int rows) 
{ 
if (rows == 0) 
{ 
throw new IllegalArgumentException( 
"rows cannot set to zero"); 
} 
iRows = new int[rows]; 
for (int i = 0; i < rows; i++) 
{ 
iRows[i] = 1; 
} 
setRows(iRows); 
} 

public void setRows(int[] rows) 
{ 
if ((rows == null) || (rows.length == 0)) 
{ 
throw new IllegalArgumentException( 
"rows cannot be null or zero length"); 
} 
float total = 0; 
for (int i = 0; i < rows.length; i++) 
{ 
total += rows[i]; 
} 
iRows = rows; 
fRows = new float[rows.length]; 
for (int i = 0; i < rows.length; i++) 
{ 
fRows[i] = (float)iRows[i] / total; 
} 
} 

Listing 5.
 
public Dimension preferredLayoutSize(Container parent) 
{ 
Insets insets = parent.getInsets(); 
int ncomponents = parent.getComponentCount(); 
int nrows = iRows.length; 
int ncols = iCols.length; 

Dimension dim; 
Component comp; 
int count = 0; 
float xUnit = 0; 
float yUnit = 0; 
float unit; 

for (int row = 0; row < nrows; row++) 
{ 
for (int col = 0; col < ncols; col++) 
{ 
if (count > ncomponents) break; 
else 
{ 
comp = parent.getComponent(count); 
dim = comp.getPreferredSize(); 
unit = (float)dim.width / (float)iCols[col]; 
if (unit > xUnit) xUnit = unit; 
unit = (float)dim.height / (float)iRows[row]; 
if (unit > yUnit) yUnit = unit; 
count++; 
} 
} 
} 
int height = 0; 
for (int row = 0; row < nrows; row++) 
{ 
height += yUnit * iRows[row]; 
} 
int width = 0; 
for (int col = 0; col < ncols; col++) 
{ 
width += xUnit * iCols[col]; 
} 

return new Dimension( 
insets.left + insets.right + width, 
insets.top + insets.bottom + height); 
} 

Listing 6.
 
public void layoutContainer(Container parent) 
{ 
int ncomponents = parent.getComponentCount(); 
if (ncomponents == 0) return; 

Insets insets = parent.getInsets(); 
int nrows = iRows.length; 
int ncols = iCols.length; 
int w = parent.getSize().width - 
(insets.left + insets.right); 
int h = parent.getSize().height - 
(insets.top + insets.bottom); 

int x = insets.left; 
for (int c = 0; c < ncols; c++) 
{ 
int ww = (int)((float)w * fCols[c]) - hgap; 
int y = insets.top; 
for (int r = 0; r < nrows; r++) 
{ 
int i = r * ncols + c; 
int hh = (int)((float)h * fRows[r]) - vgap; 
if (i < ncomponents) 
{ 
parent.getComponent(i).setBounds(x, y, ww, hh); 
} 
y += hh + vgap; 
} 
x += ww + hgap; 
} 
} 

Listing 7.
 
public void addLayoutComponent(Component comp, Object rect) 
{ 
if (!(rect instanceof Rectangle)) 
{ 
throw new IllegalArgumentException( 
"constraint object must be a Rectangle"); 
} 
table.put(comp, rect); 
} 

public void removeLayoutComponent(Component comp) 
{ 
table.remove(comp); 
} 

Listing 8.
 
public Dimension preferredLayoutSize(Container parent) 
{ 
Insets insets = parent.getInsets(); 
int ncomponents = parent.getComponentCount(); 
int nrows = rows; 
int ncols = cols; 

Dimension dim; 
Component comp; 
int count = 0; 
float xUnit = 0; 
float yUnit = 0; 
float unit; 

int w = 0; 
int h = 0; 
for (int i = 0; i < ncomponents; i++) 
{ 
comp = parent.getComponent(count); 
dim = comp.getPreferredSize(); 
Rectangle rect = (Rectangle)table.get(comp); 
unit = (float)dim.width / (float)rect.width; 
if (unit > xUnit) xUnit = unit; 
unit = (float)dim.height / (float)rect.height; 
if (unit > yUnit) yUnit = unit; 
count++; 
} 

int height = (int)(yUnit * nrows); 
int width = (int)(xUnit * ncols); 

return new Dimension( 
insets.left + insets.right + width, 
insets.top + insets.bottom + height); 
} 

Listing 9.
 
public void layoutContainer(Container parent) 
{ 
Insets insets = parent.getInsets(); 
int ncomponents = parent.getComponentCount(); 
int nrows = rows; 
int ncols = cols; 

if (ncomponents == 0) return; 

int w = parent.getSize().width - 
(insets.left + insets.right); 
int h = parent.getSize().height - 
(insets.top + insets.bottom); 
float ww = (float)(w - (ncols - 1) * hgap) / (float)ncols; 
float hh = (float)(h - (nrows - 1) * vgap) / (float)nrows; 

int left = insets.left; 
int top = insets.top; 
Component comp; 
Rectangle rect; 
int x, y, width, height; 

for (int i = 0; i < ncomponents; i++) 
{ 
comp = parent.getComponent(i); 
rect = (Rectangle)table.get(comp); 
x = (int)(ww * (float)rect.x) + left; 
y = (int)(hh * (float)rect.y) + top; 
// Calculate by substraction to a void rounding errors 
width = (int)(ww * (float)(rect.x + rect.width)) - x; 
height = (int)(hh * (float)(rect.y + rect.height)) - y; 
comp.setBounds(new Rectangle(x, y, width, height)); 
} 
} 
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.