Crossfire JXClient, Trunk  R20561
GUIMultiLineLabel.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.Graphics2D;
33 import java.awt.image.BufferedImage;
34 import java.util.regex.Pattern;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
43 public class GUIMultiLineLabel extends GUILabel {
44 
48  private static final long serialVersionUID = 1;
49 
53  @NotNull
54  private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile(" *\n");
55 
59  @NotNull
60  private String[] lines;
61 
74  public GUIMultiLineLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final BufferedImage picture, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor, @NotNull final Alignment alignment, @NotNull final String text) {
75  super(tooltipManager, elementListener, name, picture, text, font, color, backgroundColor, alignment);
76  lines = LINE_SEPARATOR_PATTERN.split(getText(), -1);
77  }
78 
82  @Override
83  protected void textChanged() {
84  lines = LINE_SEPARATOR_PATTERN.split(getText(), -1);
85  super.textChanged();
86  }
87 
91  @Override
92  public void paintComponent(@NotNull final Graphics g) {
93  super.paintComponent(g);
94 
95  if (lines.length <= 0) {
96  return;
97  }
98 
99  final Graphics2D g2 = (Graphics2D)g;
100  final Dimension rectangle = GuiUtils.getTextDimension("Xg", getFontMetrics(getTextFont()));
101  final int lineHeight = rectangle.height;
102 
103  int y = 0;
104  for (final String line : lines) {
105  drawLine(g2, y, lineHeight, line);
106  y += lineHeight;
107  }
108  }
109 
113  @Nullable
114  @Override
115  public Dimension getPreferredSize() {
116  return getTextSize();
117  }
118 
122  @Nullable
123  @Override
124  public Dimension getMinimumSize() {
125  return getTextSize();
126  }
127 
133  @NotNull
134  private Dimension getTextSize() {
135  final FontMetrics fontMetrics = getFontMetrics(getTextFont());
136 
137  int width = 0;
138  for (final String line : lines) {
139  final Dimension dimension = GuiUtils.getTextDimension(line, fontMetrics);
140  if (width < dimension.width) {
141  width = dimension.width;
142  }
143  }
144 
145  final Dimension rectangle = GuiUtils.getTextDimension("Xg", fontMetrics);
146  final int height = lines.length*rectangle.height;
147 
148  return new Dimension(width, height);
149  }
150 
151 }
static final Pattern LINE_SEPARATOR_PATTERN
The pattern to split the text into lines.
A AbstractLabel that renders the text as a list of plain strings.
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 drawLine(@NotNull final Graphics2D g, final int y0, final int h0, @NotNull final String text)
Draws one line of text.
Definition: GUILabel.java:83
static final long serialVersionUID
The serial version UID.
final GUIElementListener elementListener
The GUIElementListener to notify.
Abstract base class for labels that render text.
Definition: GUILabel.java:38
final Color backgroundColor
If set, the opaque background color.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
GUIMultiLineLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final BufferedImage picture, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor, @NotNull final Alignment alignment, @NotNull final String text)
Creates a new instance.
Dimension getTextSize()
Returns the minimal size of this component to display all of lines.