home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
ReportLayout.java
< prev
next >
Wrap
Text File
|
1998-11-05
|
8KB
|
202 lines
// Copyright (c) 1997, 1998 Symantec, Inc. All Rights Reserved.
import java.awt.LayoutManager;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.util.Vector;
import java.util.Enumeration;
/*
This class implements a simple LayoutManager.
It is used to lay out the report elements in the ReportViewFrame.
There are three component placement strings:
PLACE_SAME - Place the component in the same spot as the previous component.
Used for the first component to lay out, too.
PLACE_NEW_LINE - Place the component on the next line with regular line spacing.
PLACE_NEW_LINE_BREAK- Place the component on the next line with little or no spacing.
Note: Often layout managers call the component's getPreferredSize() method to
get the size of the component, but here we just use the size as originally
laid out by the user.
*/
public class ReportLayout implements LayoutManager {
// Add types
public static final String PLACE_NEW_LINE = "New";
public static final String PLACE_NEW_LINE_BREAK = "Break";
public static final String PLACE_SAME = "Spot";
// Tuning consts
static final int SPACE_BETWEEN_LINES = 10;
static final int SPACE_BETWEEN_LINE_BREAKS = 0;
Container container; // the Container being laid out
int insetH; // amount to inset the components by, Horz
int insetV; // amount to inset the components by, Vert
Vector components; // (of ComponentRecord) the components to lay out
/*
Constructs a ReportLayout object for the given container with the
specified component insets.
*/
public ReportLayout(Container container, int insetHorz, int insetVert) {
this.container = container;
insetH = insetHorz;
insetV = insetVert;
components = new Vector();
//{{INIT_CONTROLS
//}}
}
// Standard LayoutManger Inteface Method
public void addLayoutComponent( String placement, // PLACE_
Component comp) {
// maintain my list of components to lay out
components.addElement(new ComponentRecord(comp, placement));
}
// Standard LayoutManger Inteface Method
public void removeLayoutComponent(Component comp) {
// find the ComponentRecord with the given component...
for(Enumeration e = components.elements(); e.hasMoreElements();) {
ComponentRecord c = (ComponentRecord)e.nextElement();
if(c.comp == comp) {
// ...and remove it
components.removeElement(this);
return;
}
}
}
// Standard LayoutManger Inteface Method
public void layoutContainer(Container parent) {
// determine position of managed components
Dimension dim = determineLayout(parent);
// loop through managed components, laying them out
for(Enumeration e = components.elements(); e.hasMoreElements();) {
ComponentRecord c = (ComponentRecord)e.nextElement();
// only handle visible ones
if(c.comp.isVisible()) {
// move to previously determined position
c.setBounds();
}
}
// resize the parent container, as needed
Dimension curDim = parent.getSize();
if(true || dim.height != curDim.height || dim.width != curDim.width) {
parent.setSize(dim.width, dim.height);
}
}
// Standard LayoutManger Inteface Method
public Dimension minimumLayoutSize(Container parent) {
// determine position of managed components
return determineLayout(parent);
}
// Standard LayoutManger Inteface Method
public Dimension preferredLayoutSize(Container parent) {
// determine position of managed components
return determineLayout(parent);
}
// Determines position of managed components
private Dimension determineLayout(Container parent) {
Insets insets = parent.getInsets();
int lineX = insetH + insets.left;
int lineY = insetV + insets.top;
int lineWidth = 0;
int lineHeight = 0;
int maxWidth = lineX; // max width required to contain components
int maxHeight = lineY; // max height required to contain components
ComponentRecord c = null; // component currently being laid out
// loop through managed components, determining where each should be
for(Enumeration e = components.elements(); e.hasMoreElements();) {
c = (ComponentRecord)e.nextElement();
// if this component is invisible, skip and move to next one
if(c.comp.isVisible() == false) {
continue;
}
// size the component
c.determineSize();
// lay out the component
if(c.placement.equalsIgnoreCase(PLACE_SAME)) {
// Placement: Same Position
c.x = lineX;
if(c.width > lineWidth) {
lineWidth = c.width;
}
c.y = lineY;
if(c.height > lineHeight) {
lineHeight = c.height;
}
} else {
if(c.placement.equalsIgnoreCase(PLACE_NEW_LINE_BREAK)) {
// Placement: Line Break
lineY += lineHeight + SPACE_BETWEEN_LINE_BREAKS;
} else {
// Placement: New Line (also default action if unrecognized placement)
lineY += lineHeight + SPACE_BETWEEN_LINES;
}
c.y = lineY;
c.x = lineX;
lineWidth = c.width;
lineHeight = c.height;
}
// maintain maxWidth and maxHeight
maxWidth = Math.max(maxWidth, lineX + lineWidth);
maxHeight = Math.max(maxHeight, lineY + lineHeight);
}
// calc final container size, accounting for layout inset and container inset
maxWidth += insetH + insets.right;
maxHeight += insetV + insets.bottom;
// return with required container size
return new Dimension(maxWidth, maxHeight);
}
/*
This class tracks the folling info on one component:
1) The component
2) The component's placement
3) The component's position and size
*/
class ComponentRecord {
Component comp; // the component
String placement; // the component's placement
int x = 0; // the component's position, X
int y = 0; // the component's position, Y
int width = 0; // the component's size, width
int height = 0; // the component's size, height
/*
Constructs a ComponentRecord for the given component with the
given placement.
*/
public ComponentRecord(Component comp, String placement) {
this.comp = comp;
this.placement = placement;
}
/*
Determines the recommended size of this component.
Often layout managers call the component's getPreferredSize() method,
but here we just use the size as originally laid out by the user.
*/
public void determineSize() {
// This layout manager just uses the actual sizes of the components
// doesn't call the components getPreferredSize, etc routine
Dimension dim = comp.getSize();
width = dim.width;
height = dim.height;
}
/*
Sets the component's bounds to those specified in this class.
*/
public void setBounds() {
comp.setBounds(x, y, width, height);
}
}
//{{DECLARE_CONTROLS
//}}
}