001: package net.xoetrope.swing;
002:
003: import java.awt.Color;
004: import java.awt.Graphics;
005: import javax.swing.JComponent;
006: import net.xoetrope.xui.XAttributedComponent;
007:
008: /**
009: * A basic container for components. The panel can optional draw a
010: * border. By default no frame is displayed.
011: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
012: * License: see license.txt
013: * @version 1.0
014: */
015:
016: public class XPanel extends JComponent implements XAttributedComponent {
017: private int drawFrame = 0;
018:
019: public static int BORDER_NONE = 0;
020: public static int BORDER_BEVEL = 1;
021: public static int BORDER_FLAT = 2;
022:
023: /**
024: * Constructs a new panel with a null layout
025: */
026: public XPanel() {
027: super ();
028: setLayout(null);
029: }
030:
031: /**
032: * Repaint the component once it has been created
033: */
034: public void addNotify() {
035: super .addNotify();
036: invalidate();
037: }
038:
039: /**
040: * Paint the panel
041: * @param g
042: */
043: // public void paint( Graphics g )
044: // {
045: // update( g );
046: // super.paint( g );
047: // }
048: /**
049: * Update the panel by erasing and then rendering the frame if any
050: * @param g
051: */
052: public void paintComponent(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(Color.white);
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:
072: /**
073: * Toggle the frame display.
074: * @param val 0 for no frame, 1 for a bevel, 2 for a flat frame
075: */
076: public void setDrawFrame(int value) {
077: drawFrame = value;
078: }
079:
080: /**
081: * Get the frame display value.
082: * @return val 0 for no frame, 1 for a bevel, 2 for a flat frame
083: */
084: public int getDrawFrame() {
085: return drawFrame;
086: }
087:
088: /**
089: * Set one or more attributes of the component.
090: * <OL>
091: * <LI>border value=(0|1) for nor border or for a border</LI>
092: * </OL>
093: * @param attribName the attribute name
094: * @param attribValue the attribute value
095: */
096: public void setAttribute(String attribName, String attribValue) {
097: String attribNameLwr = attribName.toLowerCase();
098: String attribValueLwr = attribValue.toLowerCase();
099: if (attribNameLwr.compareTo("border") == 0) {
100: if (attribValueLwr != null)
101: setDrawFrame(Integer.parseInt(attribValueLwr));
102: }
103: }
104: }
|