Crossfire JXClient, Trunk  R20561
GUIHTMLLabel.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.label;
23 
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.Font;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
32 import java.awt.image.BufferedImage;
33 import java.io.IOException;
34 import java.io.Reader;
35 import java.io.StringReader;
36 import java.util.regex.Pattern;
37 import javax.swing.text.html.HTMLEditorKit.ParserCallback;
38 import javax.swing.text.html.parser.ParserDelegator;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
46 public class GUIHTMLLabel extends AbstractLabel {
47 
51  private static final long serialVersionUID = 1;
52 
56  private static final int AUTO_BORDER_SIZE = 2;
57 
61  @NotNull
62  private static final Pattern PATTERN_LINE_BREAK = Pattern.compile("<br>");
63 
67  private boolean autoResize;
68 
81  public GUIHTMLLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final BufferedImage backgroundPicture, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor, @NotNull final String text) {
82  super(tooltipManager, elementListener, name, text, font, color, backgroundPicture, backgroundColor);
83  }
84 
88  @Override
89  protected void textChanged() {
90  autoResize();
91  setChanged();
92  }
93 
100  public void setAutoResize(final boolean autoResize) {
101  if (this.autoResize != autoResize) {
102  this.autoResize = autoResize;
103  autoResize();
104  }
105  }
106 
110  @Override
111  public void paintComponent(@NotNull final Graphics g) {
112  super.paintComponent(g);
113 
114  final Font font = getTextFont();
115  final Color color = getTextColor();
116  g.setFont(font);
117  g.setColor(color);
118 
119  final Reader reader = new StringReader(getText());
120  final ParserCallback renderer = new InternalHTMLRenderer(font, color, g, 0, font.getSize(), autoResize ? AUTO_BORDER_SIZE : 0);
121  final ParserDelegator parserDelegator = new ParserDelegator();
122  try {
123  parserDelegator.parse(reader, renderer, false);
124  } catch (final IOException ex) {
125  System.err.println("GUIHTMLLabel: cannot render HTML: "+ex.getMessage());
126  System.err.println(getText());
127  }
128  }
129 
133  private void autoResize() {
134  if (!autoResize) {
135  return;
136  }
137 
138  final FontMetrics fontMetrics = getFontMetrics(getTextFont());
139 
140  int width = 0;
141  int height = 0;
142  for (final String str : PATTERN_LINE_BREAK.split(getText(), -1)) {
143  final Dimension size = GuiUtils.getTextDimension(str, fontMetrics);
144  width = Math.max(width, size.width);
145  height += size.height;
146  }
147  setSize(Math.max(1, width+2*AUTO_BORDER_SIZE), Math.max(1, height+2*AUTO_BORDER_SIZE));
148  }
149 
153  @Nullable
154  @Override
155  public Dimension getPreferredSize() {
156  return new Dimension(100, 32); // XXX
157  }
158 
162  @Nullable
163  @Override
164  public Dimension getMinimumSize() {
165  return new Dimension(100, 32); // XXX
166  }
167 
168 }
Abstract base class for all label classes.
final TooltipManager tooltipManager
The TooltipManager to update.
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Returns the extents of a string when rendered in a given Font on this component.
Definition: GuiUtils.java:51
void setChanged()
Records that the contents have changed and must be repainted.
void setAutoResize(final boolean autoResize)
Enable or disable auto-resizing.
static final int AUTO_BORDER_SIZE
Size of border around text in auto-resize mode.
final GUIElementListener elementListener
The GUIElementListener to notify.
final Color backgroundColor
If set, the opaque background color.
boolean autoResize
If set, auto-resize this element to the extent of text.
void autoResize()
If auto-resizing is enabled, calculate the new width and height.
static final Pattern PATTERN_LINE_BREAK
The pattern used to split a string into lines.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
static final long serialVersionUID
The serial version UID.
Implements an AbstractLabel that displays HTML contents.
GUIHTMLLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final BufferedImage backgroundPicture, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor, @NotNull final String text)
Creates a new instance.