home *** CD-ROM | disk | FTP | other *** search
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.FontMetrics;
- import java.awt.Insets;
- import java.awt.Label;
- import java.awt.LayoutManager;
- import java.awt.Rectangle;
- import java.util.Hashtable;
-
- public class DialogLayout implements LayoutManager {
- protected Hashtable m_map = new Hashtable();
- protected int m_width;
- protected int m_height;
-
- protected void Construct(Container parent, int width, int height) {
- Rectangle rect = new Rectangle(0, 0, width, height);
- this.mapRectangle(rect, this.getCharWidth(parent), this.getCharHeight(parent));
- this.m_width = rect.width;
- this.m_height = rect.height;
- }
-
- public Dimension preferredLayoutSize(Container parent) {
- return new Dimension(this.m_width, this.m_height);
- }
-
- public void setShape(Component comp, int x, int y, int width, int height) {
- this.m_map.put(comp, new Rectangle(x, y, width, height));
- }
-
- public void setShape(Component comp, Rectangle rect) {
- this.m_map.put(comp, new Rectangle(rect.x, rect.y, rect.width, rect.height));
- }
-
- public Rectangle getShape(Component comp) {
- Rectangle rect = (Rectangle)this.m_map.get(comp);
- return new Rectangle(rect.x, rect.y, rect.width, rect.height);
- }
-
- public void addLayoutComponent(String name, Component comp) {
- }
-
- public DialogLayout(Container parent, int width, int height) {
- this.Construct(parent, width, height);
- }
-
- public DialogLayout(Container parent, Dimension d) {
- this.Construct(parent, d.width, d.height);
- }
-
- protected int getCharHeight(Container parent) {
- FontMetrics m = ((Component)parent).getFontMetrics(((Component)parent).getFont());
- int height = m.getHeight();
- return height;
- }
-
- public Dimension getDialogSize() {
- return new Dimension(this.m_width, this.m_height);
- }
-
- public void removeLayoutComponent(Component comp) {
- }
-
- public Dimension minimumLayoutSize(Container parent) {
- return new Dimension(this.m_width, this.m_height);
- }
-
- public void layoutContainer(Container parent) {
- int count = parent.countComponents();
- Rectangle rect = new Rectangle();
- int charHeight = this.getCharHeight(parent);
- int charWidth = this.getCharWidth(parent);
- Insets insets = parent.insets();
- FontMetrics m = ((Component)parent).getFontMetrics(((Component)parent).getFont());
-
- for(int i = 0; i < count; ++i) {
- Component c = parent.getComponent(i);
- Rectangle r = (Rectangle)this.m_map.get(c);
- if (r != null) {
- rect.x = r.x;
- rect.y = r.y;
- rect.height = r.height;
- rect.width = r.width;
- this.mapRectangle(rect, charWidth, charHeight);
- if (c instanceof Label) {
- rect.x -= 12;
- rect.width += 12;
- }
-
- rect.x += insets.left;
- rect.y += insets.top;
- c.reshape(rect.x, rect.y, rect.width, rect.height);
- }
- }
-
- }
-
- protected int getCharWidth(Container parent) {
- FontMetrics m = ((Component)parent).getFontMetrics(((Component)parent).getFont());
- String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int width = m.stringWidth(s) / s.length();
- if (width <= 0) {
- width = 1;
- }
-
- return width;
- }
-
- protected void mapRectangle(Rectangle rect, int charWidth, int charHeight) {
- rect.x = rect.x * charWidth / 4;
- rect.y = rect.y * charHeight / 8;
- rect.width = rect.width * charWidth / 4;
- rect.height = rect.height * charHeight / 8;
- }
- }
-