001: package net.xoetrope.awt;
002:
003: import java.awt.Color;
004: import java.awt.Container;
005: import java.awt.Graphics;
006: import net.xoetrope.xui.XAttributedComponent;
007: import java.awt.Panel;
008:
009: /**
010: * A basic container for components. The panel can optional draw a
011: * border. By default no frame is displayed.
012: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
013: * License: see license.txt
014: * @version $Revision: 1.8 $
015: */
016:
017: public class XPanel extends Panel implements XAttributedComponent {
018: private int drawFrame = 0;
019:
020: public static int BORDER_NONE = 0;
021: public static int BORDER_BEVEL = 1;
022: public static int BORDER_FLAT = 2;
023:
024: private boolean isHeavyWeight;
025:
026: /**
027: * Constructs a new panel with a null layout
028: */
029: public XPanel() {
030: this (true);
031: }
032:
033: public XPanel(boolean heavyWeight) {
034: super ();
035: setLayout(null);
036: isHeavyWeight = heavyWeight;
037: }
038:
039: /**
040: * Repaint the component once it has been created
041: */
042: public void addNotify() {
043: invalidate();
044: if (isHeavyWeight)
045: super .addNotify();
046: }
047:
048: /**
049: * Paint the panel
050: * @param g
051: */
052: public void paint(Graphics g) {
053: int w = getSize().width;
054: int h = getSize().height;
055: g.setColor(getBackground());
056: g.fillRect(0, 0, w, h);
057: if (drawFrame > 0) {
058: if (drawFrame == 2) {
059: g.setColor(getBackground().brighter());
060: g.drawLine(0, 0, w - 1, 0);
061: g.drawLine(0, 0, 0, h - 1);
062: g.setColor(getBackground().darker());
063: g.drawLine(w - 1, 0, w - 1, h - 1);
064: g.drawLine(0, h - 1, w - 1, h - 1);
065: } else {
066: g.setColor(getBackground().darker());
067: g.drawRect(0, 0, w - 1, h - 1);
068: }
069: }
070:
071: super .paint(g);
072: }
073:
074: /**
075: * Toggle the frame display.
076: * @param val 0 for no frame, 1 for a bevel, 2 for a flat frame
077: */
078: public void setDrawFrame(int value) {
079: drawFrame = value;
080: }
081:
082: /**
083: * Get the frame display value.
084: * @return val 0 for no frame, 1 for a bevel, 2 for a flat frame
085: */
086: public int getDrawFrame() {
087: return drawFrame;
088: }
089:
090: /**
091: * Set the panel size and location and then force it to redo its layout.
092: * @param x the left coordinate
093: * @param y the top coordinate
094: * @param w the width
095: * @param h the height
096: */
097: public void setBounds(int x, int y, int w, int h) {
098: super .setBounds(x, y, w, h);
099: doLayout();
100: }
101:
102: /**
103: * Set one or more attributes of the component.
104: * <OL>
105: * <LI>border, value=(0|1)</LI>
106: * </OL>
107: * @param attribName the attribute name
108: * @param attribValue the attribute value
109: */
110: public void setAttribute(String attribName, String attribValue) {
111: String attribNameLwr = attribName.toLowerCase();
112: String attribValueLwr = attribValue.toLowerCase();
113: if (attribNameLwr.compareTo("border") == 0) {
114: if (attribValueLwr != null)
115: setDrawFrame(Integer.parseInt(attribValueLwr));
116: }
117: }
118: }
|