001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.properties;
031:
032: import java.awt.Font;
033: import java.io.IOException;
034:
035: import com.jeta.forms.store.AbstractJETAPersistable;
036: import com.jeta.forms.store.JETAObjectInput;
037: import com.jeta.forms.store.JETAObjectOutput;
038:
039: /**
040: * Defines the attributes for a Font object. A <code>FontProperty</code> must
041: * be used instead of serializing a <code>Font</code> object directly. The
042: * reason is because some look and feels have problems with fonts that were
043: * serialized with a different look and feel. For example, saving a font on
044: * Linux and then rendering that de-serialized font on OSX causes problems.
045: *
046: * @author Jeff Tassin
047: */
048: public class FontProperty extends AbstractJETAPersistable {
049: static final long serialVersionUID = -1878354825497611389L;
050:
051: public static final int VERSION = 1;
052:
053: /**
054: * The family name for the font.
055: */
056: private String m_family;
057:
058: /**
059: * The font style. One of the following constants defined in
060: * <code>Font</code>: PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
061: */
062: private int m_style;
063:
064: /**
065: * The point size of the font.
066: */
067: private int m_size;
068:
069: /**
070: * A cached font object based on the attributes defined in this property.
071: */
072: private transient Font m_font;
073:
074: /**
075: * Creates an unitialized <code>FontProperty</code> instance.
076: */
077: public FontProperty() {
078:
079: }
080:
081: /**
082: * Creates a <code>FontProperty</code> instance with the attributes from
083: * the specified font.
084: *
085: * @param f
086: * the font whose attributes will define this property.
087: */
088: public FontProperty(Font f) {
089: if (f != null) {
090: m_family = f.getFamily();
091: m_style = f.getStyle();
092: m_size = f.getSize();
093: m_font = f;
094: }
095: }
096:
097: /**
098: * Returns a font instance that is defined by the attributes defined by this
099: * property.
100: *
101: * @return a font instance with the attributes defined by this property.
102: */
103: public Font getFont() {
104: if (m_font == null) {
105: if (m_family == null)
106: m_family = "Dialog";
107:
108: if (m_size == 0)
109: m_size = 12;
110:
111: m_font = new Font(m_family, m_style, m_size);
112: }
113: return m_font;
114: }
115:
116: /**
117: * JETAPersistable Implementation
118: */
119: public void read(JETAObjectInput in) throws ClassNotFoundException,
120: IOException {
121: int version = in.readVersion();
122: m_family = (String) in.readObject("family");
123: m_style = in.readInt("style");
124: m_size = in.readInt("size");
125: }
126:
127: /**
128: * JETAPersistable Implementation
129: */
130: public void write(JETAObjectOutput out) throws IOException {
131: out.writeVersion(VERSION);
132: out.writeObject("family", m_family);
133: out.writeInt("style", m_style);
134: out.writeInt("size", m_size);
135: }
136:
137: }
|