00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.label;
00023
00024 import java.awt.Color;
00025 import java.awt.Font;
00026 import java.awt.FontMetrics;
00027 import java.awt.Graphics;
00028 import java.util.Stack;
00029 import javax.swing.text.MutableAttributeSet;
00030 import javax.swing.text.html.HTML;
00031 import javax.swing.text.html.HTMLEditorKit;
00032 import org.jetbrains.annotations.NotNull;
00033
00037 public class InternalHTMLRenderer extends HTMLEditorKit.ParserCallback {
00038
00039 @NotNull
00040 private final Stack<Font> fonts = new Stack<Font>();
00041
00042 @NotNull
00043 private final Stack<Color> colors = new Stack<Color>();
00044
00045 @NotNull
00046 private final Graphics gc;
00047
00048 private int x = 0;
00049
00050 private int y = 0;
00051
00052 private final int origX;
00053
00054 private final int borderSize;
00055
00056 public InternalHTMLRenderer(@NotNull final Font font, @NotNull final Color color, @NotNull final Graphics gc, final int x, final int y, final int borderSize) {
00057 fonts.push(font);
00058 colors.push(color);
00059 this.gc = gc;
00060 this.x = x;
00061 this.y = y;
00062 origX = x;
00063 this.borderSize = borderSize;
00064 }
00065
00066 @Override
00067 public void handleText(@NotNull final char[] data, final int pos) {
00068 gc.setFont(fonts.peek());
00069 gc.setColor(colors.peek());
00070 final FontMetrics m = gc.getFontMetrics();
00071 final String str = new String(data);
00072 final int w = m.stringWidth(str);
00073 gc.drawString(str, x+borderSize, y+borderSize);
00074 x += w;
00075 }
00076
00077 @Override
00078 public void handleStartTag(@NotNull final HTML.Tag t, @NotNull final MutableAttributeSet a, final int pos) {
00079 if (t.equals(HTML.Tag.A)) {
00080 fonts.push(fonts.peek());
00081 colors.push(Color.YELLOW);
00082
00083 } else if (t.equals(HTML.Tag.B)) {
00084 fonts.push(fonts.peek().deriveFont(Font.BOLD));
00085 colors.push(colors.peek());
00086 } else if (t.equals(HTML.Tag.I)) {
00087 fonts.push(fonts.peek().deriveFont(Font.ITALIC));
00088 colors.push(colors.peek());
00089 } else if (t.equals(HTML.Tag.LI)) {
00090 fonts.push(fonts.peek());
00091 colors.push(colors.peek());
00092 gc.setFont(fonts.peek());
00093 gc.setColor(colors.peek());
00094 final FontMetrics m = gc.getFontMetrics();
00095 x = origX;
00096 y += fonts.peek().getSize()+1;
00097 final String str = " - ";
00098 final int w = m.stringWidth(str);
00099 gc.drawString(str, x+borderSize, y+borderSize);
00100 x += w;
00101 } else {
00102 fonts.push(fonts.peek());
00103 colors.push(colors.peek());
00104 }
00105 }
00106
00107 @Override
00108 public void handleSimpleTag(@NotNull final HTML.Tag t, @NotNull final MutableAttributeSet a, final int pos) {
00109 if (t.equals(HTML.Tag.BR)) {
00110 y += fonts.peek().getSize()+1;
00111 x = origX;
00112 }
00113 }
00114
00115 @Override
00116 public void handleEndTag(@NotNull final HTML.Tag t, final int pos) {
00117 fonts.pop();
00118 colors.pop();
00119 }
00120
00121 }