001: /*=============================================================================
002: * Copyright Texas Instruments, Inc., 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.swing.console;
020:
021: import java.awt.Color;
022:
023: import ti.exceptions.ProgrammingErrorException;
024:
025: /**
026: * An attribute to modify the foreground (ie text) color.
027: *
028: * @author Rob Clark
029: * @version 0.0
030: */
031: public class FgColorAttribute implements Attribute {
032: public static final Attribute RED = new FgColorAttribute(Color.red);
033: public static final Attribute GREEN = new FgColorAttribute(
034: Color.green);
035: public static final Attribute BLUE = new FgColorAttribute(
036: Color.blue);
037:
038: private Color color;
039:
040: /**
041: * Class Constructor.
042: *
043: * @param color the color
044: */
045: public FgColorAttribute(Color color) {
046: this .color = color;
047: }
048:
049: /**
050: * Get a region to apply this region.
051: *
052: * @param offset the offset to beginning of region
053: * @param length the length of the region
054: * @return a region
055: */
056: public Region getRegion(int offset, int length) {
057: return new Region(offset, length) {
058:
059: private Color oldColor = null;
060:
061: public void enter(ConsoleGraphics g) {
062: if (oldColor != null)
063: throw new ProgrammingErrorException(this
064: + ": haven't left!");
065:
066: oldColor = g.getColor();
067: g.setColor(color);
068: }
069:
070: public void leave(ConsoleGraphics g) {
071: if (oldColor == null)
072: throw new ProgrammingErrorException(this
073: + ": haven't entered!");
074:
075: g.setColor(oldColor);
076: oldColor = null;
077: }
078:
079: public String toString() {
080: return ("[FgColorRegion: offset=" + getStart()
081: + ", length=" + getLength() + ", color="
082: + color + ", oldColor=" + oldColor + "]");
083: }
084:
085: };
086: }
087: }
088:
089: /*
090: * Local Variables:
091: * tab-width: 2
092: * indent-tabs-mode: nil
093: * mode: java
094: * c-indentation-style: java
095: * c-basic-offset: 2
096: * eval: (c-set-offset 'substatement-open '0)
097: * eval: (c-set-offset 'case-label '+)
098: * eval: (c-set-offset 'inclass '+)
099: * eval: (c-set-offset 'inline-open '0)
100: * End:
101: */
|