Crossfire JXClient, Trunk  R20561
TextSegment.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.log;
23 
24 import java.awt.Color;
25 import java.awt.Font;
26 import java.awt.Graphics;
27 import java.awt.font.FontRenderContext;
28 import java.awt.font.LineMetrics;
29 import java.awt.geom.RectangularShape;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
38 public class TextSegment extends AbstractSegment {
39 
43  @NotNull
44  private final String text;
45 
49  private final boolean bold;
50 
54  private final boolean italic;
55 
59  private final boolean underline;
60 
64  @NotNull
65  private final FontID font;
66 
70  @Nullable
71  private final Color color;
72 
77  private int underlineOffset;
78 
88  public TextSegment(@NotNull final String text, final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID font, @Nullable final Color color) {
89  this.text = text;
90  this.bold = bold;
91  this.italic = italic;
92  this.underline = underline;
93  this.font = font;
94  this.color = color;
95  }
96 
101  @NotNull
102  public String getText() {
103  return text;
104  }
105 
111  @NotNull
112  private Font getFont(@NotNull final Fonts fonts) {
113  switch (font) {
114  case PRINT:
115  return fonts.getFontPrint();
116 
117  case FIXED:
118  return bold ? fonts.getFontFixedBold() : fonts.getFontFixed();
119 
120  case ARCANE:
121  return fonts.getFontArcane();
122 
123  case HAND:
124  return fonts.getFontPrint();
125 
126  case STRANGE:
127  return fonts.getFontPrint();
128  }
129 
130  throw new AssertionError();
131  }
132 
136  @Override
137  public void draw(@NotNull final Graphics g, final int y, @NotNull final Fonts fonts) {
138  g.setColor(color);
139  g.setFont(getFont(fonts));
140  final int segmentX = getX();
141  final int segmentY = getY();
142  g.drawString(text, segmentX, y+segmentY);
143  if (underline) {
144  g.drawLine(segmentX, y+segmentY+underlineOffset, segmentX+getWidth()-1, y+segmentY+underlineOffset);
145  }
146  }
147 
151  @Override
152  public void updateAttributes(@NotNull final Fonts fonts, @NotNull final FontRenderContext context) {
153  final LineMetrics lineMetrics = getFont(fonts).getLineMetrics(text, context);
154  underlineOffset = Math.round(lineMetrics.getUnderlineOffset());
155  }
156 
160  @NotNull
161  @Override
162  public RectangularShape getSize(@NotNull final Fonts fonts, @NotNull final FontRenderContext context) {
163  return getFont(fonts).getStringBounds(text, context);
164  }
165 
175  public boolean matches(final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID font, @Nullable final Color color) {
176  return this.bold == bold && this.italic == italic && this.underline == underline && this.font == font && this.color == color;
177  }
178 
182  @NotNull
183  @Override
184  public String toString() {
185  final StringBuilder sb = new StringBuilder();
186  sb.append("segment:");
187  if (bold) {
188  sb.append("(bold)");
189  }
190  if (italic) {
191  sb.append("(italic)");
192  }
193  if (underline) {
194  sb.append("(underline)");
195  }
196  if (font != FontID.PRINT) {
197  sb.append('(').append(font.toString().toLowerCase()).append(')');
198  }
199  if (color != null) {
200  sb.append('(').append(Parser.toString(color)).append(')');
201  }
202  sb.append(text);
203  sb.append('\n');
204  return sb.toString();
205  }
206 
207 }
String getText()
Returns the text to display.
Abstract base class for Segment implementations.
void draw(@NotNull final Graphics g, final int y, @NotNull final Fonts fonts)
Draws this segment to a Graphics instance.the graphics to draw to the y-coordinate to draw to the fon...
final boolean italic
Whether italic face is enabled.
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance...
Definition: Parser.java:37
int y
The y-coordinate to display the segment.
int getX()
Returns the x-coordinate to display the segment.
int underlineOffset
The distance of the underline to the base line.
One segment of a Line which should be displayed without changing attributes.
int getWidth()
Returns the width to display the segment.
void updateAttributes(@NotNull final Fonts fonts, @NotNull final FontRenderContext context)
Updates the cached attributes of this segment.the fonts instance to use the font render context to us...
int getY()
Returns the y-coordinate to display the segment.
Font getFont(@NotNull final Fonts fonts)
Returns the Font to use for a given Segment.
final String text
The text to display.
final boolean bold
Whether bold face is enabled.
static String toString(@NotNull final Color color)
Returns the string representation for a color.
Definition: Parser.java:304
TextSegment(@NotNull final String text, final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID font, @Nullable final Color color)
Creates a new segment.
RectangularShape getSize(@NotNull final Fonts fonts, @NotNull final FontRenderContext context)
Returns the size of this segment in pixels.the fonts instance to use the font render context to use t...
boolean matches(final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID font, @Nullable final Color color)
Returns whether this segment matches the given attributes.
final boolean underline
Whether underlining is enabled.