home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP17 / DIALOGWINDOW / DIALOGLAYOUT.JAVA < prev    next >
Encoding:
Java Source  |  1996-06-13  |  5.3 KB  |  188 lines

  1. // This is a part of the Microsoft Jakarta library.
  2. // Copyright (C) 1996 Microsoft Corporation
  3. // All rights reserved.
  4.  
  5. //package java.awt;
  6.  
  7. import java.util.Hashtable;
  8. import java.awt.LayoutManager;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.Dimension;
  12. import java.awt.Rectangle;
  13. import java.awt.FontMetrics;
  14. import java.awt.Insets;
  15. import java.awt.Label;
  16.  
  17. //
  18. // class DialogLayout
  19. // 
  20. // DialogLayout is a simple layout manager which works with what the Win32
  21. // API calls "dialog logical units" (DLUs).  DLUs are resolution independent 
  22. // coordinates which work well for laying out controls on a dialog box.  The 
  23. // mapping from DLUs to pixels is based on the font in use in the dialog box.  
  24. // An x-coordinate DLU is described as 1/4 (.25) of the of the average character 
  25. // width of the font used in the dialog.  A y-coordinate DLU is described as
  26. // 1/8 (.125) of the character height used in the dialog.  One tricky issue to 
  27. // note: The average character width is not the average of all characters -- 
  28. // rather it is the average of all alpha characters both uppercase and 
  29. // lowercase. That is, it is the extent of the string "a...zA...Z" divided 
  30. // by 52.
  31. //
  32. // This class allows you to associate a Rectangle (x, y, width, height) with a
  33. // Component in a Container.  If called upon to layout the container, this 
  34. // layout manager will layout based on the translation of dialog units to 
  35. // pixels.
  36. //
  37.  
  38. public class DialogLayout 
  39.     implements LayoutManager 
  40. {
  41.     protected Hashtable m_map = new Hashtable();
  42.     protected int m_width;
  43.     protected int m_height;
  44.  
  45.     // DialogLayout methods
  46.  
  47.     public DialogLayout(Container parent, int width, int height)
  48.     {
  49.         //assert parent != null;
  50.         Construct(parent, width, height);
  51.     }
  52.  
  53.     public DialogLayout(Container parent, Dimension d)
  54.     {
  55.         //assert parent != null;
  56.         Construct(parent, d.width, d.height);
  57.     }
  58.  
  59.     public void setShape(Component comp, int x, int y, int width, int height)
  60.     {
  61.         //assert m_map != null;
  62.         //assert comp != null;
  63.         m_map.put(comp, new Rectangle(x, y, width, height));
  64.     }
  65.  
  66.     public void setShape(Component comp, Rectangle rect)
  67.     {
  68.         //assert m_map != null;
  69.         //assert comp != null;
  70.         m_map.put(comp, new Rectangle(rect.x, rect.y, rect.width, rect.height));
  71.     }
  72.  
  73.     public Rectangle getShape(Component comp)
  74.     {
  75.         //assert comp != null;
  76.         Rectangle rect = (Rectangle)m_map.get(comp);
  77.         //assert rect != null;
  78.         return new Rectangle(rect.x, rect.y, rect.width, rect.height);
  79.     }
  80.  
  81.     public Dimension getDialogSize()
  82.     {
  83.         return new Dimension(m_width, m_height);
  84.     }
  85.  
  86.     // LayoutManager Methods
  87.  
  88.     public void addLayoutComponent(String name, Component comp) { }
  89.     public void removeLayoutComponent(Component comp) { }
  90.  
  91.     public Dimension preferredLayoutSize(Container parent) 
  92.     {
  93.         //assert parent != null;
  94.         return new Dimension(m_width, m_height);
  95.     }
  96.  
  97.     public Dimension minimumLayoutSize(Container parent) 
  98.     {
  99.         //assert parent != null;
  100.         return new Dimension(m_width, m_height);
  101.     }
  102.  
  103.     public void layoutContainer(Container parent) 
  104.     {
  105.         //assert parent != null;
  106.         int count = parent.countComponents();
  107.         Rectangle rect = new Rectangle();
  108.         int charHeight = getCharHeight(parent);
  109.         int charWidth = getCharWidth(parent);
  110.         Insets insets = parent.insets();
  111.         FontMetrics m = parent.getFontMetrics(parent.getFont());
  112.         
  113.         for (int i = 0; i < count; i++)
  114.         {
  115.             Component c = parent.getComponent(i);
  116.             //assert c != null;
  117.             Rectangle r = (Rectangle)m_map.get(c);
  118.             if (r != null)
  119.             {
  120.                 rect.x = r.x;
  121.                 rect.y = r.y;
  122.                 rect.height = r.height;
  123.                 rect.width = r.width;
  124.                 mapRectangle(rect, charWidth, charHeight);
  125.                 if (c instanceof Label)
  126.                 {
  127.                     // REVIEW: Adjusts for space at left of Java labels.
  128.                     //         Not sure what the space is for, but it seems 
  129.                     //         to be hard coded at 12 pixels, at least on Win95/NT,
  130.                     //           and not related to the font.
  131.                     rect.x     -= 12;
  132.                     rect.width += 12;
  133.                 }
  134.  
  135.                 rect.x += insets.left;
  136.                 rect.y += insets.top;
  137.                 c.reshape(rect.x, rect.y, rect.width, rect.height);
  138.             }
  139.         }
  140.     }
  141.  
  142.     // Implementation Helpers
  143.  
  144.     protected void Construct(Container parent, int width, int height)
  145.     {
  146.         //assert m_map != null;
  147.         Rectangle rect = new Rectangle(0, 0, width, height);
  148.         mapRectangle(rect, getCharWidth(parent), getCharHeight(parent));
  149.         m_width = rect.width;
  150.         m_height = rect.height;
  151.     }
  152.  
  153.     protected int getCharWidth(Container parent)
  154.     {
  155.         //assert parent != null;
  156.         FontMetrics m = parent.getFontMetrics(parent.getFont());
  157.         //assert m != null;
  158.         String s     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  159.         int    width = m.stringWidth(s) / s.length();
  160.  
  161.         if (width <= 0)
  162.             width = 1;
  163.         return width;
  164.     }
  165.  
  166.     protected int getCharHeight(Container parent)
  167.     {
  168.         //assert parent != null;
  169.         FontMetrics m = parent.getFontMetrics(parent.getFont());
  170.         //assert m != null;
  171.         int height = m.getHeight();
  172.         //assert height > 0;
  173.         return height;
  174.     }
  175.  
  176.     protected void mapRectangle(Rectangle rect, int charWidth, int charHeight)
  177.     {
  178.         //assert rect != null;
  179.         //assert charWidth > 0;
  180.         //assert charHeight > 0;
  181.         rect.x      = (rect.x      * charWidth)  / 4;
  182.         rect.y      = (rect.y      * charHeight) / 8;
  183.         rect.width  = (rect.width  * charWidth)  / 4;
  184.         rect.height = (rect.height * charHeight) / 8;
  185.     }
  186. }
  187.  
  188.