001: /*=============================================================================
002: * Copyright Texas Instruments 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.chimera.pref;
020:
021: import java.awt.*;
022:
023: /**
024: * A {@link NodeContract} which constrains the value to be a font matching the
025: * specified style. Note that some of the styles are mutually exclusive, so
026: * if you specify, for example a style <code>BOLD | NOT_BOLD</code>, then no
027: * possible font will match.
028: *
029: * @author ;Rob Clark;a0873619;San Diego;;
030: * @version 0.1
031: */
032: public class FontNodeContract extends
033: ti.chimera.registry.TypeNodeContract {
034: /**
035: * The bit mask of parameters to futher limit the font choice.
036: */
037: private int style;
038:
039: /**
040: * A style flag indicating that only monospace fonts are acceptible.
041: */
042: public static final int MONOSPACE = 0x00000001;
043:
044: /**
045: * A style flag indicating that only non-monospace fonts are acceptible.
046: */
047: public static final int NOT_MONOSPACE = 0x00000002;
048:
049: /**
050: * A style flag indicating that only bold fonts are acceptible.
051: */
052: public static final int BOLD = 0x00000004;
053:
054: /**
055: * A style flag indicating that only non-bold fonts are acceptible.
056: */
057: public static final int NOT_BOLD = 0x00000008;
058:
059: /**
060: * A style flag indicating that only italic fonts are acceptible.
061: */
062: public static final int ITALIC = 0x00000010;
063:
064: /**
065: * A style flag indicating that only non-italic fonts are acceptible.
066: */
067: public static final int NOT_ITALIC = 0x00000020;
068:
069: // XXX part of a hack to get at a FontMetrics for a given Font
070: private static Component c;
071:
072: /**
073: * Class Constructor.
074: */
075: public FontNodeContract() {
076: this (0);
077: }
078:
079: /**
080: * Class Constructor.
081: *
082: * @param style a bitmask of constraints on the acceptible font
083: */
084: public FontNodeContract(int style) {
085: super (Font.class);
086: this .style = style;
087: }
088:
089: private final static String ALPHANUMERIC = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
090:
091: /**
092: * Determine if the specified <code>value</code> meets this contract.
093: *
094: * @param value the value to check
095: * @return <code>true</code> if meets contract
096: */
097: public boolean accepts(Object value) {
098: if (!super .accepts(value))
099: return false;
100:
101: Font font = (Font) value;
102:
103: // check that the font can display regular alphanumeric characters:
104: int idx = font.canDisplayUpTo(ALPHANUMERIC);
105: if ((idx != -1) && (idx < ALPHANUMERIC.length()))
106: return false;
107:
108: if (((style & MONOSPACE) != 0)
109: || ((style & NOT_MONOSPACE) != 0)) {
110: // XXX is there a cleaner way to get a FontMetrics for this font??
111: if (c == null)
112: c = new javax.swing.JPanel();
113:
114: FontMetrics fm = c.getFontMetrics(font);
115:
116: boolean isMonospace = fm.charWidth('l') == fm
117: .charWidth('m');
118:
119: if (((style & MONOSPACE) != 0) && !isMonospace)
120: return false;
121:
122: if (((style & NOT_MONOSPACE) != 0) && isMonospace)
123: return false;
124: }
125:
126: if (((style & BOLD) != 0) && !font.isBold())
127: return false;
128:
129: if (((style & NOT_BOLD) != 0) && font.isBold())
130: return false;
131:
132: if (((style & ITALIC) != 0) && !font.isItalic())
133: return false;
134:
135: if (((style & NOT_ITALIC) != 0) && font.isItalic())
136: return false;
137:
138: return true;
139: }
140:
141: /**
142: * Get the style
143: */
144: public int getStyle() {
145: return style;
146: }
147:
148: /**
149: * The contract implementation should overload <code>toString</code> so
150: * the contract can be displayed to the user in a sane format, for use
151: * in error messages, etc.
152: */
153: public String toString() {
154: return "(" + super .toString() + " && (style is " + style + "))";
155: }
156: }
157:
158: /*
159: * Local Variables:
160: * tab-width: 2
161: * indent-tabs-mode: nil
162: * mode: java
163: * c-indentation-style: java
164: * c-basic-offset: 2
165: * eval: (c-set-offset 'substatement-open '0)
166: * eval: (c-set-offset 'case-label '+)
167: * eval: (c-set-offset 'inclass '+)
168: * eval: (c-set-offset 'inline-open '0)
169: * End:
170: */
|