home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / Ms_vj / Samples / JavaNow / Chap17 / DialogWindow / DialogLayout.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-07-29  |  4.1 KB  |  116 lines

  1. import java.awt.Component;
  2. import java.awt.Container;
  3. import java.awt.Dimension;
  4. import java.awt.FontMetrics;
  5. import java.awt.Insets;
  6. import java.awt.Label;
  7. import java.awt.LayoutManager;
  8. import java.awt.Rectangle;
  9. import java.util.Hashtable;
  10.  
  11. public class DialogLayout implements LayoutManager {
  12.    protected Hashtable m_map = new Hashtable();
  13.    protected int m_width;
  14.    protected int m_height;
  15.  
  16.    protected void Construct(Container parent, int width, int height) {
  17.       Rectangle rect = new Rectangle(0, 0, width, height);
  18.       this.mapRectangle(rect, this.getCharWidth(parent), this.getCharHeight(parent));
  19.       this.m_width = rect.width;
  20.       this.m_height = rect.height;
  21.    }
  22.  
  23.    public Dimension preferredLayoutSize(Container parent) {
  24.       return new Dimension(this.m_width, this.m_height);
  25.    }
  26.  
  27.    public void setShape(Component comp, int x, int y, int width, int height) {
  28.       this.m_map.put(comp, new Rectangle(x, y, width, height));
  29.    }
  30.  
  31.    public void setShape(Component comp, Rectangle rect) {
  32.       this.m_map.put(comp, new Rectangle(rect.x, rect.y, rect.width, rect.height));
  33.    }
  34.  
  35.    public Rectangle getShape(Component comp) {
  36.       Rectangle rect = (Rectangle)this.m_map.get(comp);
  37.       return new Rectangle(rect.x, rect.y, rect.width, rect.height);
  38.    }
  39.  
  40.    public void addLayoutComponent(String name, Component comp) {
  41.    }
  42.  
  43.    public DialogLayout(Container parent, int width, int height) {
  44.       this.Construct(parent, width, height);
  45.    }
  46.  
  47.    public DialogLayout(Container parent, Dimension d) {
  48.       this.Construct(parent, d.width, d.height);
  49.    }
  50.  
  51.    protected int getCharHeight(Container parent) {
  52.       FontMetrics m = ((Component)parent).getFontMetrics(((Component)parent).getFont());
  53.       int height = m.getHeight();
  54.       return height;
  55.    }
  56.  
  57.    public Dimension getDialogSize() {
  58.       return new Dimension(this.m_width, this.m_height);
  59.    }
  60.  
  61.    public void removeLayoutComponent(Component comp) {
  62.    }
  63.  
  64.    public Dimension minimumLayoutSize(Container parent) {
  65.       return new Dimension(this.m_width, this.m_height);
  66.    }
  67.  
  68.    public void layoutContainer(Container parent) {
  69.       int count = parent.countComponents();
  70.       Rectangle rect = new Rectangle();
  71.       int charHeight = this.getCharHeight(parent);
  72.       int charWidth = this.getCharWidth(parent);
  73.       Insets insets = parent.insets();
  74.       FontMetrics m = ((Component)parent).getFontMetrics(((Component)parent).getFont());
  75.  
  76.       for(int i = 0; i < count; ++i) {
  77.          Component c = parent.getComponent(i);
  78.          Rectangle r = (Rectangle)this.m_map.get(c);
  79.          if (r != null) {
  80.             rect.x = r.x;
  81.             rect.y = r.y;
  82.             rect.height = r.height;
  83.             rect.width = r.width;
  84.             this.mapRectangle(rect, charWidth, charHeight);
  85.             if (c instanceof Label) {
  86.                rect.x -= 12;
  87.                rect.width += 12;
  88.             }
  89.  
  90.             rect.x += insets.left;
  91.             rect.y += insets.top;
  92.             c.reshape(rect.x, rect.y, rect.width, rect.height);
  93.          }
  94.       }
  95.  
  96.    }
  97.  
  98.    protected int getCharWidth(Container parent) {
  99.       FontMetrics m = ((Component)parent).getFontMetrics(((Component)parent).getFont());
  100.       String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  101.       int width = m.stringWidth(s) / s.length();
  102.       if (width <= 0) {
  103.          width = 1;
  104.       }
  105.  
  106.       return width;
  107.    }
  108.  
  109.    protected void mapRectangle(Rectangle rect, int charWidth, int charHeight) {
  110.       rect.x = rect.x * charWidth / 4;
  111.       rect.y = rect.y * charHeight / 8;
  112.       rect.width = rect.width * charWidth / 4;
  113.       rect.height = rect.height * charHeight / 8;
  114.    }
  115. }
  116.