home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / main.bin / BorderPanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-04  |  10.0 KB  |  442 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.Insets;
  11. import java.awt.LayoutManager;
  12. import java.awt.Panel;
  13.  
  14. public strictfp class BorderPanel extends Panel implements AlignStyle, BevelStyle {
  15.    protected static final int labelpadx = 10;
  16.    protected static final int labelipadx = 4;
  17.    protected Color borderColor;
  18.    protected Color labelColor;
  19.    protected int padtop;
  20.    protected int padbottom;
  21.    protected int padleft;
  22.    protected int padright;
  23.    protected int ixPad;
  24.    protected int iyPadTop;
  25.    protected int iyPadBottom;
  26.    protected int style;
  27.    protected String label;
  28.    protected int labelAlignment;
  29.    protected Insets internalInsets;
  30.    protected Panel panel;
  31.    Dimension oldSize;
  32.  
  33.    public BorderPanel() {
  34.       this((String)null, 1, 2);
  35.    }
  36.  
  37.    public BorderPanel(int style) {
  38.       this((String)null, 1, style);
  39.    }
  40.  
  41.    public BorderPanel(String s) {
  42.       this(s, 1, 2);
  43.    }
  44.  
  45.    public BorderPanel(String s, int alignment) {
  46.       this(s, alignment, 2);
  47.    }
  48.  
  49.    public BorderPanel(String s, int alignment, int style) {
  50.       this.oldSize = new Dimension();
  51.       this.borderColor = Color.black;
  52.       this.labelColor = Color.black;
  53.       this.padleft = 6;
  54.       this.padright = 6;
  55.       this.padtop = 10;
  56.       this.padbottom = 6;
  57.       this.ixPad = 4;
  58.       this.iyPadTop = 2;
  59.       this.iyPadBottom = 7;
  60.       this.label = s != null && s.length() == 0 ? null : s;
  61.       this.labelAlignment = alignment;
  62.       this.internalInsets = new Insets(10, 10, 10, 10);
  63.       this.style = style;
  64.       this.setLayout((LayoutManager)null);
  65.       super.add(this.panel = new Panel());
  66.       this.sizepanel(true);
  67.    }
  68.  
  69.    public void addNotify() {
  70.       super.addNotify();
  71.       this.sizepanel(true);
  72.    }
  73.  
  74.    public void setPaddingTop(int p) {
  75.       this.padtop = p;
  76.       this.sizepanel(true);
  77.       ((Container)this).invalidate();
  78.    }
  79.  
  80.    public int getPaddingTop() {
  81.       return this.padtop;
  82.    }
  83.  
  84.    public void setPaddingBottom(int p) {
  85.       this.padbottom = p;
  86.       this.sizepanel(true);
  87.       ((Container)this).invalidate();
  88.    }
  89.  
  90.    public int getPaddingBottom() {
  91.       return this.padbottom;
  92.    }
  93.  
  94.    public void setPaddingLeft(int p) {
  95.       this.padleft = p;
  96.       this.sizepanel(true);
  97.       ((Container)this).invalidate();
  98.    }
  99.  
  100.    public int getPaddingLeft() {
  101.       return this.padleft;
  102.    }
  103.  
  104.    public void setPaddingRight(int p) {
  105.       this.padright = p;
  106.       this.sizepanel(true);
  107.       ((Container)this).invalidate();
  108.    }
  109.  
  110.    public int getPaddingRight() {
  111.       return this.padright;
  112.    }
  113.  
  114.    public void setIPadTop(int t) {
  115.       this.iyPadTop = t;
  116.       this.sizepanel(true);
  117.       ((Container)this).invalidate();
  118.    }
  119.  
  120.    public void setIPadBottom(int b) {
  121.       this.iyPadBottom = b;
  122.       this.sizepanel(true);
  123.       ((Container)this).invalidate();
  124.    }
  125.  
  126.    public int getIPadTop() {
  127.       return this.iyPadTop;
  128.    }
  129.  
  130.    public void setIPadSides(int s) {
  131.       this.ixPad = s;
  132.       this.sizepanel(true);
  133.       ((Container)this).invalidate();
  134.    }
  135.  
  136.    public int getIPadBottom() {
  137.       return this.iyPadBottom;
  138.    }
  139.  
  140.    public void setPadding(int t, int b, int l, int r) {
  141.       this.padtop = t;
  142.       this.padbottom = b;
  143.       this.padleft = l;
  144.       this.padright = r;
  145.       this.sizepanel(true);
  146.       ((Container)this).invalidate();
  147.    }
  148.  
  149.    public int getIPadSides() {
  150.       return this.ixPad;
  151.    }
  152.  
  153.    public void setLabel(String s) {
  154.       this.label = s != null && s.length() == 0 ? null : s;
  155.       this.sizepanel(true);
  156.       ((Container)this).invalidate();
  157.    }
  158.  
  159.    public String getLabel() {
  160.       return this.label;
  161.    }
  162.  
  163.    public void setBorderColor(Color clr, boolean useForLabel) {
  164.       this.borderColor = clr;
  165.       if (useForLabel) {
  166.          this.labelColor = clr;
  167.       }
  168.  
  169.       ((Container)this).invalidate();
  170.    }
  171.  
  172.    public void setBorderColor(Color clr) {
  173.       this.setBorderColor(clr, false);
  174.    }
  175.  
  176.    public Color getBorderColor() {
  177.       return this.borderColor;
  178.    }
  179.  
  180.    public void setAlignStyle(int alignment) {
  181.       this.labelAlignment = alignment;
  182.       this.sizepanel(true);
  183.       ((Container)this).invalidate();
  184.    }
  185.  
  186.    public int getAlignStyle() {
  187.       return this.labelAlignment;
  188.    }
  189.  
  190.    public void setLabelColor(Color clr) {
  191.       this.labelColor = clr;
  192.       ((Container)this).invalidate();
  193.    }
  194.  
  195.    public Color getLabelColor() {
  196.       return this.labelColor;
  197.    }
  198.  
  199.    public void setBevelStyle(int s) {
  200.       this.style = s;
  201.       ((Container)this).invalidate();
  202.    }
  203.  
  204.    public int getBevelStyle() {
  205.       return this.style;
  206.    }
  207.  
  208.    public void setInternalInsets(Insets insets) {
  209.       this.internalInsets = insets;
  210.       this.sizepanel(true);
  211.       ((Container)this).invalidate();
  212.    }
  213.  
  214.    public Insets getInternalInsets() {
  215.       return this.internalInsets;
  216.    }
  217.  
  218.    public void layout() {
  219.       this.sizepanel(false);
  220.       this.panel.layout();
  221.    }
  222.  
  223.    public Dimension minimumSize() {
  224.       return this.preferredSize();
  225.    }
  226.  
  227.    public Dimension preferredSize() {
  228.       Dimension s = this.panel.preferredSize();
  229.       s.width = Math.max(s.width, this.getLabelWidthMargin());
  230.       s.width += this.padleft + this.padright + this.ixPad * 2 + 1;
  231.       s.height += this.getLabelTopMargin() + this.padbottom + this.iyPadTop + this.iyPadBottom + 1;
  232.       return s;
  233.    }
  234.  
  235.    public void setLayout(LayoutManager l) {
  236.       if (this.panel != null) {
  237.          this.panel.setLayout(l);
  238.       }
  239.  
  240.    }
  241.  
  242.    public LayoutManager getLayout() {
  243.       return this.panel.getLayout();
  244.    }
  245.  
  246.    public Component add(Component c) {
  247.       return this.panel.add(c);
  248.    }
  249.  
  250.    public Component add(String s, Component c) {
  251.       return this.panel.add(s, c);
  252.    }
  253.  
  254.    public Component add(Component c, int pos) {
  255.       return c == this.panel ? super.add(c, pos) : this.panel.add(c, pos);
  256.    }
  257.  
  258.    public void remove(Component c) {
  259.       this.panel.remove(c);
  260.    }
  261.  
  262.    public void removeAll() {
  263.       this.panel.removeAll();
  264.    }
  265.  
  266.    public void reshape(int x, int y, int width, int height) {
  267.       super.reshape(x, y, width, height);
  268.       this.sizepanel(false);
  269.    }
  270.  
  271.    public void update(Graphics g) {
  272.       Dimension s = ((Component)this).size();
  273.       Insets insets = this.insets();
  274.       g.setColor(((Component)this).getBackground());
  275.       if (insets.left > 0) {
  276.          g.fillRect(0, 0, insets.left, s.height);
  277.       }
  278.  
  279.       if (insets.top > 0) {
  280.          g.fillRect(0, 0, s.width, insets.top);
  281.       }
  282.  
  283.       if (insets.bottom > 0) {
  284.          g.fillRect(0, s.height - insets.bottom, s.width, insets.bottom);
  285.       }
  286.  
  287.       if (insets.right > 0) {
  288.          g.fillRect(s.width - insets.right, 0, insets.right, s.height);
  289.       }
  290.  
  291.       this.paint(g);
  292.       this.panel.repaint();
  293.    }
  294.  
  295.    public int countComponents() {
  296.       return this.panel.countComponents();
  297.    }
  298.  
  299.    public Component getComponent(int i) {
  300.       return this.panel.getComponent(i);
  301.    }
  302.  
  303.    public Component[] getComponents() {
  304.       return this.panel.getComponents();
  305.    }
  306.  
  307.    public Insets insets() {
  308.       int h = this.getLabelTopMargin();
  309.       Insets insets = this.getInternalInsets();
  310.       return new Insets(h + insets.top, insets.left, insets.bottom, insets.right);
  311.    }
  312.  
  313.    void sizepanel(boolean force) {
  314.       Dimension s = ((Component)this).size();
  315.       if (force || this.oldSize.width != s.width || this.oldSize.height != s.height) {
  316.          this.oldSize = ((Component)this).size();
  317.          this.panel.reshape(this.padleft + this.ixPad, this.getLabelTopMargin() + this.iyPadTop, this.oldSize.width - this.padright - this.padleft - this.ixPad * 2 - 1, this.oldSize.height - this.padbottom - this.getLabelTopMargin() - this.iyPadBottom - this.iyPadTop - 1);
  318.       }
  319.  
  320.    }
  321.  
  322.    public void paint(Graphics g) {
  323.       this.sizepanel(false);
  324.       g.setColor(((Component)this).getBackground());
  325.       this.draw(g);
  326.    }
  327.  
  328.    void draw(Graphics g) {
  329.       Dimension s = ((Component)this).size();
  330.       int delta = this.padtop;
  331.       FontMetrics fm = ((Component)this).getFontMetrics(((Component)this).getFont());
  332.       g.clipRect(0, 0, s.width, s.height);
  333.       if (this.label != null && fm != null) {
  334.          delta = (fm.getAscent() + fm.getDescent() + this.padtop) / 2;
  335.       }
  336.  
  337.       int x = this.padleft;
  338.       int w = s.width - this.padleft - this.padright - 1;
  339.       int h = s.height - 1 - delta - this.padbottom;
  340.       this.drawBorder(g, x, delta, w, h);
  341.       this.drawLabel(g, fm);
  342.    }
  343.  
  344.    void drawBorder(Graphics g, int x, int y, int w, int h) {
  345.       switch (this.style) {
  346.          case 0:
  347.             g.setColor(Color.gray);
  348.             g.drawLine(x, y, x + w, y);
  349.             g.drawLine(x, y, x, y + h);
  350.             g.setColor(Color.white);
  351.             g.drawLine(x, y + h, x + w, y + h);
  352.             g.drawLine(x + w, y, x + w, y + h);
  353.             return;
  354.          case 1:
  355.             g.setColor(Color.white);
  356.             g.drawLine(x, y, x + w, y);
  357.             g.drawLine(x, y, x, y + h);
  358.             g.setColor(Color.gray);
  359.             g.drawLine(x, y + h, x + w, y + h);
  360.             g.drawLine(x + w, y, x + w, y + h);
  361.             return;
  362.          case 2:
  363.             g.setColor(this.borderColor);
  364.             g.drawRect(x, y, w, h);
  365.             return;
  366.          default:
  367.             g.setColor(this.borderColor);
  368.             g.drawRect(x, y, w, h);
  369.          case 3:
  370.       }
  371.    }
  372.  
  373.    void drawLabel(Graphics g, FontMetrics fm) {
  374.       if (this.label != null && fm != null) {
  375.          int fWidth = 10;
  376.          Dimension s = ((Component)this).size();
  377.          if (((Component)this).getFont().getSize() > fWidth) {
  378.             fWidth += ((Component)this).getFont().getSize() / 2;
  379.          }
  380.  
  381.          int stringWidth = fm.stringWidth(this.label);
  382.          int ascent = fm.getAscent();
  383.          int descent = fm.getDescent();
  384.          int x;
  385.          switch (this.labelAlignment) {
  386.             case 0:
  387.             default:
  388.                x = fWidth + 7;
  389.                break;
  390.             case 1:
  391.                x = (s.width - stringWidth) / 2;
  392.                break;
  393.             case 2:
  394.                x = s.width - fWidth - (stringWidth + 7);
  395.          }
  396.  
  397.          int h = ascent + descent + this.padtop;
  398.          int y = (fWidth - h) / 2 + this.padtop + ascent;
  399.          g.setColor(((Component)this).getBackground());
  400.          g.fillRect(x - 2, y - 1 - ascent - this.padtop / 2, stringWidth + 4, h);
  401.          g.setColor(this.labelColor);
  402.          g.drawString(this.label, x, y - 1);
  403.       }
  404.  
  405.    }
  406.  
  407.    protected int getLabelTopMargin() {
  408.       if (this.label == null) {
  409.          return this.padtop;
  410.       } else {
  411.          int top = this.padtop;
  412.          Font font = ((Component)this).getFont();
  413.          if (font != null) {
  414.             FontMetrics fm = ((Component)this).getFontMetrics(font);
  415.             top = fm.getAscent() + fm.getDescent() + this.padtop;
  416.          }
  417.  
  418.          return top;
  419.       }
  420.    }
  421.  
  422.    int getLabelWidthMargin() {
  423.       if (this.label == null) {
  424.          return 0;
  425.       } else {
  426.          int w = 2 + this.internalInsets.left + this.internalInsets.right;
  427.          Font font = ((Component)this).getFont();
  428.          if (font != null) {
  429.             FontMetrics fm = ((Component)this).getFontMetrics(font);
  430.             w = Math.max(w, 2 + fm.stringWidth(this.label) + 10 + 4);
  431.          }
  432.  
  433.          return w;
  434.       }
  435.    }
  436.  
  437.    public void setBackground(Color c) {
  438.       super.setBackground(c);
  439.       this.panel.setBackground(c);
  440.    }
  441. }
  442.