001: /**
002: * Wizard Framework
003: * Copyright 2004 - 2005 Andrew Pietsch
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * $Id: HTMLPane.java,v 1.4 2005/09/18 05:25:06 pietschy Exp $
020: */package org.pietschy.wizard;
021:
022: import javax.swing.*;
023: import javax.swing.text.html.HTMLEditorKit;
024: import javax.swing.text.EditorKit;
025: import java.awt.Color;
026: import java.awt.Font;
027: import java.awt.Graphics;
028: import java.awt.Graphics2D;
029: import java.awt.RenderingHints;
030: import java.io.IOException;
031: import java.net.URL;
032:
033: /**
034: * This class displays HTML text using an instance of {@link JEditorPane} but allows
035: * the font, foreground and background colors to be easily changed. This is accomplished by
036: * updating the documents style sheet rules when ever the font and color attributes are changed.
037: *
038: * @see #setForeground
039: */
040: public class HTMLPane extends JEditorPane {
041: private HTMLEditorKit kit;
042: private boolean antiAlias = false;
043: private boolean forceReload = false;
044:
045: /**
046: * Creates a new <code>JEditorPane</code>.
047: * The document model is set to <code>null</code>.
048: */
049: public HTMLPane() {
050: this (true);
051: }
052:
053: /**
054: * Creates a new {@link HTMLPane}.
055: */
056: public HTMLPane(boolean opaque) {
057: kit = new HTMLEditorKit();
058: setEditorKit(kit);
059: setFont(UIManager.getFont("Label.font"));
060: updateEditorColor(getForeground());
061: setEditable(false);
062: setOpaque(opaque);
063:
064: }
065:
066: public URL getPage() {
067: if (forceReload) {
068: forceReload = false;
069: return null;
070: }
071:
072: return super .getPage();
073: }
074:
075: public void setPage(URL page) throws IOException {
076: forceReload = true;
077: super .setPage(page);
078: }
079:
080: public void setFont(Font font) {
081: super .setFont(font);
082: if (kit != null)
083: updateEditorFont(font);
084: }
085:
086: public void setForeground(Color fg) {
087: super .setForeground(fg);
088: if (kit != null)
089: updateEditorColor(fg);
090: }
091:
092: public void setEditorKit(EditorKit kit) {
093: super .setEditorKit(kit);
094: updateEditorColor(getForeground());
095: updateEditorFont(getFont());
096: }
097:
098: private void updateEditorFont(Font font) {
099: StringBuffer rule = new StringBuffer("body { ");
100: rule.append("font-family: ").append(font.getFamily()).append(
101: ";");
102: rule.append(" font-size: ").append(font.getSize())
103: .append("pt;");
104: if (font.isBold()) {
105: rule.append("font-weight: 700;");
106: }
107: if (font.isItalic()) {
108: rule.append("font-style: italic;");
109: }
110:
111: rule.append("}");
112:
113: kit.getStyleSheet().addRule(rule.toString());
114: }
115:
116: private void updateEditorColor(Color fg) {
117: StringBuffer rule = new StringBuffer("body { color: #");
118:
119: if (fg.getRed() < 16) {
120: rule.append('0');
121: }
122: rule.append(Integer.toHexString(fg.getRed()));
123:
124: if (fg.getGreen() < 16) {
125: rule.append('0');
126: }
127: rule.append(Integer.toHexString(fg.getGreen()));
128:
129: if (fg.getBlue() < 16) {
130: rule.append('0');
131: }
132: rule.append(Integer.toHexString(fg.getBlue()));
133:
134: rule.append(";}");
135:
136: kit.getStyleSheet().addRule(rule.toString());
137: }
138:
139: public boolean isAntiAlias() {
140: return antiAlias;
141: }
142:
143: public void paint(Graphics g) {
144: if (antiAlias) {
145: Graphics2D g2 = (Graphics2D) g;
146: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
147: RenderingHints.VALUE_ANTIALIAS_ON);
148: super .paint(g2);
149: } else {
150: super .paint(g);
151: }
152: }
153:
154: public void setAntiAlias(boolean antiAlias) {
155: this.antiAlias = antiAlias;
156: repaint();
157: }
158:
159: }
|