001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Font;
022: import java.io.Serializable;
023:
024: public class FontInfo implements Cloneable, Serializable {
025: private static final long serialVersionUID = 1L;
026:
027: public interface IPropertyNames {
028: String FAMILY = "family";
029: String IS_BOLD = "isBold";
030: String IS_ITALIC = "isItalic";
031: String SIZE = "size";
032: }
033:
034: private static String DEFAULT_FAMILY = "Monospaced";
035:
036: private String _familyName;
037: private boolean _isBold;
038: private boolean _isItalic;
039: private int _size;
040:
041: public FontInfo() {
042: super ();
043: setFamily(DEFAULT_FAMILY);
044: setSize(12);
045: }
046:
047: public FontInfo(Font font) {
048: super ();
049: if (font == null) {
050: throw new IllegalArgumentException("Null Font passed");
051: }
052: setFont(font);
053: }
054:
055: /**
056: * Return a copy of this object.
057: */
058: public Object clone() {
059: try {
060: return super .clone();
061: } catch (CloneNotSupportedException ex) {
062: throw new InternalError(ex.getMessage()); // Impossible.
063: }
064: }
065:
066: public String getFamily() {
067: return _familyName;
068: }
069:
070: public void setFamily(String value) {
071: _familyName = value != null ? value : DEFAULT_FAMILY;
072: }
073:
074: public boolean isBold() {
075: return _isBold;
076: }
077:
078: public void setIsBold(boolean value) {
079: _isBold = value;
080: }
081:
082: public boolean isItalic() {
083: return _isItalic;
084: }
085:
086: public void setIsItalic(boolean value) {
087: _isItalic = value;
088: }
089:
090: public int getSize() {
091: return _size;
092: }
093:
094: public void setSize(int value) {
095: _size = value;
096: }
097:
098: public void setFont(Font font) throws IllegalArgumentException {
099: if (font == null) {
100: throw new IllegalArgumentException("Null Font passed");
101: }
102: _familyName = font.getFamily();
103: _isBold = font.isBold();
104: _isItalic = font.isItalic();
105: _size = font.getSize();
106: }
107:
108: public boolean doesFontMatch(Font font) {
109: if (font == null) {
110: return false;
111: }
112: return font.getFamily().equals(_familyName)
113: && font.getSize() == getSize()
114: && font.getStyle() == generateStyle();
115: }
116:
117: public int generateStyle() {
118: int style = 0;
119: if (!_isBold && !_isItalic) {
120: style = Font.PLAIN;
121: } else {
122: if (_isBold) {
123: style |= Font.BOLD;
124: }
125: if (_isItalic) {
126: style |= Font.ITALIC;
127: }
128: }
129: return style;
130: }
131:
132: public Font createFont() {
133: return new Font(_familyName, generateStyle(), _size);
134: }
135:
136: // i18n ? What is this used for?
137: public String toString() {
138: StringBuffer buf = new StringBuffer();
139: buf.append(_familyName).append(", " + _size);
140: if (_isBold) {
141: buf.append(", bold");
142: }
143: if (_isItalic) {
144: buf.append(", italic");
145: }
146: return buf.toString();
147: }
148:
149: /**
150: * @see java.lang.Object#hashCode()
151: */
152: @Override
153: public int hashCode() {
154: final int PRIME = 31;
155: int result = 1;
156: result = PRIME * result
157: + ((_familyName == null) ? 0 : _familyName.hashCode());
158: result = PRIME * result + (_isBold ? 1231 : 1237);
159: result = PRIME * result + (_isItalic ? 1231 : 1237);
160: result = PRIME * result + _size;
161: return result;
162: }
163:
164: /**
165: * @see java.lang.Object#equals(java.lang.Object)
166: */
167: @Override
168: public boolean equals(Object obj) {
169: if (this == obj)
170: return true;
171: if (obj == null)
172: return false;
173: if (getClass() != obj.getClass())
174: return false;
175: final FontInfo other = (FontInfo) obj;
176: if (_familyName == null) {
177: if (other._familyName != null)
178: return false;
179: } else if (!_familyName.equals(other._familyName))
180: return false;
181: if (_isBold != other._isBold)
182: return false;
183: if (_isItalic != other._isItalic)
184: return false;
185: if (_size != other._size)
186: return false;
187: return true;
188: }
189: }
|