home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.awt;
-
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.LayoutManager;
- import java.awt.Panel;
-
- public strictfp class BorderPanel extends Panel implements AlignStyle, BevelStyle {
- protected static final int labelpadx = 10;
- protected static final int labelipadx = 4;
- protected Color borderColor;
- protected Color labelColor;
- protected int padtop;
- protected int padbottom;
- protected int padleft;
- protected int padright;
- protected int ixPad;
- protected int iyPadTop;
- protected int iyPadBottom;
- protected int style;
- protected String label;
- protected int labelAlignment;
- protected Insets internalInsets;
- protected Panel panel;
- Dimension oldSize;
-
- public BorderPanel() {
- this((String)null, 1, 2);
- }
-
- public BorderPanel(int style) {
- this((String)null, 1, style);
- }
-
- public BorderPanel(String s) {
- this(s, 1, 2);
- }
-
- public BorderPanel(String s, int alignment) {
- this(s, alignment, 2);
- }
-
- public BorderPanel(String s, int alignment, int style) {
- this.oldSize = new Dimension();
- this.borderColor = Color.black;
- this.labelColor = Color.black;
- this.padleft = 6;
- this.padright = 6;
- this.padtop = 10;
- this.padbottom = 6;
- this.ixPad = 4;
- this.iyPadTop = 2;
- this.iyPadBottom = 7;
- this.label = s != null && s.length() == 0 ? null : s;
- this.labelAlignment = alignment;
- this.internalInsets = new Insets(10, 10, 10, 10);
- this.style = style;
- this.setLayout((LayoutManager)null);
- super.add(this.panel = new Panel());
- this.sizepanel(true);
- }
-
- public void addNotify() {
- super.addNotify();
- this.sizepanel(true);
- }
-
- public void setPaddingTop(int p) {
- this.padtop = p;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getPaddingTop() {
- return this.padtop;
- }
-
- public void setPaddingBottom(int p) {
- this.padbottom = p;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getPaddingBottom() {
- return this.padbottom;
- }
-
- public void setPaddingLeft(int p) {
- this.padleft = p;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getPaddingLeft() {
- return this.padleft;
- }
-
- public void setPaddingRight(int p) {
- this.padright = p;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getPaddingRight() {
- return this.padright;
- }
-
- public void setIPadTop(int t) {
- this.iyPadTop = t;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public void setIPadBottom(int b) {
- this.iyPadBottom = b;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getIPadTop() {
- return this.iyPadTop;
- }
-
- public void setIPadSides(int s) {
- this.ixPad = s;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getIPadBottom() {
- return this.iyPadBottom;
- }
-
- public void setPadding(int t, int b, int l, int r) {
- this.padtop = t;
- this.padbottom = b;
- this.padleft = l;
- this.padright = r;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getIPadSides() {
- return this.ixPad;
- }
-
- public void setLabel(String s) {
- this.label = s != null && s.length() == 0 ? null : s;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public String getLabel() {
- return this.label;
- }
-
- public void setBorderColor(Color clr, boolean useForLabel) {
- this.borderColor = clr;
- if (useForLabel) {
- this.labelColor = clr;
- }
-
- ((Container)this).invalidate();
- }
-
- public void setBorderColor(Color clr) {
- this.setBorderColor(clr, false);
- }
-
- public Color getBorderColor() {
- return this.borderColor;
- }
-
- public void setAlignStyle(int alignment) {
- this.labelAlignment = alignment;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public int getAlignStyle() {
- return this.labelAlignment;
- }
-
- public void setLabelColor(Color clr) {
- this.labelColor = clr;
- ((Container)this).invalidate();
- }
-
- public Color getLabelColor() {
- return this.labelColor;
- }
-
- public void setBevelStyle(int s) {
- this.style = s;
- ((Container)this).invalidate();
- }
-
- public int getBevelStyle() {
- return this.style;
- }
-
- public void setInternalInsets(Insets insets) {
- this.internalInsets = insets;
- this.sizepanel(true);
- ((Container)this).invalidate();
- }
-
- public Insets getInternalInsets() {
- return this.internalInsets;
- }
-
- public void layout() {
- this.sizepanel(false);
- this.panel.layout();
- }
-
- public Dimension minimumSize() {
- return this.preferredSize();
- }
-
- public Dimension preferredSize() {
- Dimension s = this.panel.preferredSize();
- s.width = Math.max(s.width, this.getLabelWidthMargin());
- s.width += this.padleft + this.padright + this.ixPad * 2 + 1;
- s.height += this.getLabelTopMargin() + this.padbottom + this.iyPadTop + this.iyPadBottom + 1;
- return s;
- }
-
- public void setLayout(LayoutManager l) {
- if (this.panel != null) {
- this.panel.setLayout(l);
- }
-
- }
-
- public LayoutManager getLayout() {
- return this.panel.getLayout();
- }
-
- public Component add(Component c) {
- return this.panel.add(c);
- }
-
- public Component add(String s, Component c) {
- return this.panel.add(s, c);
- }
-
- public Component add(Component c, int pos) {
- return c == this.panel ? super.add(c, pos) : this.panel.add(c, pos);
- }
-
- public void remove(Component c) {
- this.panel.remove(c);
- }
-
- public void removeAll() {
- this.panel.removeAll();
- }
-
- public void reshape(int x, int y, int width, int height) {
- super.reshape(x, y, width, height);
- this.sizepanel(false);
- }
-
- public void update(Graphics g) {
- Dimension s = ((Component)this).size();
- Insets insets = this.insets();
- g.setColor(((Component)this).getBackground());
- if (insets.left > 0) {
- g.fillRect(0, 0, insets.left, s.height);
- }
-
- if (insets.top > 0) {
- g.fillRect(0, 0, s.width, insets.top);
- }
-
- if (insets.bottom > 0) {
- g.fillRect(0, s.height - insets.bottom, s.width, insets.bottom);
- }
-
- if (insets.right > 0) {
- g.fillRect(s.width - insets.right, 0, insets.right, s.height);
- }
-
- this.paint(g);
- this.panel.repaint();
- }
-
- public int countComponents() {
- return this.panel.countComponents();
- }
-
- public Component getComponent(int i) {
- return this.panel.getComponent(i);
- }
-
- public Component[] getComponents() {
- return this.panel.getComponents();
- }
-
- public Insets insets() {
- int h = this.getLabelTopMargin();
- Insets insets = this.getInternalInsets();
- return new Insets(h + insets.top, insets.left, insets.bottom, insets.right);
- }
-
- void sizepanel(boolean force) {
- Dimension s = ((Component)this).size();
- if (force || this.oldSize.width != s.width || this.oldSize.height != s.height) {
- this.oldSize = ((Component)this).size();
- 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);
- }
-
- }
-
- public void paint(Graphics g) {
- this.sizepanel(false);
- g.setColor(((Component)this).getBackground());
- this.draw(g);
- }
-
- void draw(Graphics g) {
- Dimension s = ((Component)this).size();
- int delta = this.padtop;
- FontMetrics fm = ((Component)this).getFontMetrics(((Component)this).getFont());
- g.clipRect(0, 0, s.width, s.height);
- if (this.label != null && fm != null) {
- delta = (fm.getAscent() + fm.getDescent() + this.padtop) / 2;
- }
-
- int x = this.padleft;
- int w = s.width - this.padleft - this.padright - 1;
- int h = s.height - 1 - delta - this.padbottom;
- this.drawBorder(g, x, delta, w, h);
- this.drawLabel(g, fm);
- }
-
- void drawBorder(Graphics g, int x, int y, int w, int h) {
- switch (this.style) {
- case 0:
- g.setColor(Color.gray);
- g.drawLine(x, y, x + w, y);
- g.drawLine(x, y, x, y + h);
- g.setColor(Color.white);
- g.drawLine(x, y + h, x + w, y + h);
- g.drawLine(x + w, y, x + w, y + h);
- return;
- case 1:
- g.setColor(Color.white);
- g.drawLine(x, y, x + w, y);
- g.drawLine(x, y, x, y + h);
- g.setColor(Color.gray);
- g.drawLine(x, y + h, x + w, y + h);
- g.drawLine(x + w, y, x + w, y + h);
- return;
- case 2:
- g.setColor(this.borderColor);
- g.drawRect(x, y, w, h);
- return;
- default:
- g.setColor(this.borderColor);
- g.drawRect(x, y, w, h);
- case 3:
- }
- }
-
- void drawLabel(Graphics g, FontMetrics fm) {
- if (this.label != null && fm != null) {
- int fWidth = 10;
- Dimension s = ((Component)this).size();
- if (((Component)this).getFont().getSize() > fWidth) {
- fWidth += ((Component)this).getFont().getSize() / 2;
- }
-
- int stringWidth = fm.stringWidth(this.label);
- int ascent = fm.getAscent();
- int descent = fm.getDescent();
- int x;
- switch (this.labelAlignment) {
- case 0:
- default:
- x = fWidth + 7;
- break;
- case 1:
- x = (s.width - stringWidth) / 2;
- break;
- case 2:
- x = s.width - fWidth - (stringWidth + 7);
- }
-
- int h = ascent + descent + this.padtop;
- int y = (fWidth - h) / 2 + this.padtop + ascent;
- g.setColor(((Component)this).getBackground());
- g.fillRect(x - 2, y - 1 - ascent - this.padtop / 2, stringWidth + 4, h);
- g.setColor(this.labelColor);
- g.drawString(this.label, x, y - 1);
- }
-
- }
-
- protected int getLabelTopMargin() {
- if (this.label == null) {
- return this.padtop;
- } else {
- int top = this.padtop;
- Font font = ((Component)this).getFont();
- if (font != null) {
- FontMetrics fm = ((Component)this).getFontMetrics(font);
- top = fm.getAscent() + fm.getDescent() + this.padtop;
- }
-
- return top;
- }
- }
-
- int getLabelWidthMargin() {
- if (this.label == null) {
- return 0;
- } else {
- int w = 2 + this.internalInsets.left + this.internalInsets.right;
- Font font = ((Component)this).getFont();
- if (font != null) {
- FontMetrics fm = ((Component)this).getFontMetrics(font);
- w = Math.max(w, 2 + fm.stringWidth(this.label) + 10 + 4);
- }
-
- return w;
- }
- }
-
- public void setBackground(Color c) {
- super.setBackground(c);
- this.panel.setBackground(c);
- }
- }
-