Crossfire JXClient, Trunk  R20561
GUILabelMessage.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.misc;
23 
34 import java.awt.Color;
35 import java.awt.Font;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public class GUILabelMessage extends GUIMultiLineLabel {
44 
48  private static final int MAX_LINE_LENGTH = 75;
49 
53  private static final long serialVersionUID = 1;
54 
58  @NotNull
60 
64  @NotNull
66 
71  @NotNull
73 
74  @Override
75  public void commandDrawinfoReceived(@NotNull final String text, final int type) {
76  final Gui gui = GuiUtils.getGui(GUILabelMessage.this);
77  if (gui == null || !windowRenderer.isDialogOpen(gui)) {
78  setText(text);
79  }
80  }
81 
82  };
83 
88  @NotNull
90 
91  @Override
92  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
93  final Gui gui = GuiUtils.getGui(GUILabelMessage.this);
94  if (gui == null || !windowRenderer.isDialogOpen(gui)) {
95  setText(message);
96  }
97  }
98 
99  @Override
100  public void setDebugMode(final boolean printMessageTypes) {
101  // ignore
102  }
103 
104  };
105 
117  public GUILabelMessage(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor) {
118  super(tooltipManager, elementListener, name, null, font, color, backgroundColor, Alignment.LEFT, "");
119  this.crossfireServerConnection = crossfireServerConnection;
120  this.windowRenderer = windowRenderer;
121  this.crossfireServerConnection.addCrossfireDrawinfoListener(crossfireDrawinfoListener);
122  this.crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
123  }
124 
128  @Override
129  public void dispose() {
130  super.dispose();
131  crossfireServerConnection.removeCrossfireDrawinfoListener(crossfireDrawinfoListener);
132  crossfireServerConnection.removeCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
133  }
134 
138  @Override
139  public void setText(@NotNull final String text) {
140  final StringBuilder sb = new StringBuilder();
141  int pos = 0;
142  while (pos < text.length()) {
143  sb.append('\n');
144 
145  int endPos = pos;
146  while (endPos < pos+MAX_LINE_LENGTH && endPos < text.length() && text.charAt(endPos) != '\n') {
147  endPos++;
148  }
149 
150  if (endPos >= pos+MAX_LINE_LENGTH) {
151  // whitespace split
152  int endPos2 = endPos;
153  while (endPos2 > pos) {
154  endPos2--;
155  final int ch = text.charAt(endPos2);
156  if (ch == ' ') {
157  break;
158  }
159  }
160  if (endPos2 > pos) {
161  sb.append(text.substring(pos, endPos2));
162  pos = endPos2+1;
163  } else {
164  sb.append(text.substring(pos, endPos));
165  pos = endPos;
166  }
167  } else if (endPos >= text.length()) {
168  // append all
169  sb.append(text.substring(pos));
170  pos = endPos;
171  } else {
172  assert text.charAt(endPos) == '\n';
173  // append segment
174  sb.append(text.substring(pos, endPos));
175  pos = endPos+1;
176  }
177  }
178  super.setText(sb.length() > 0 ? sb.substring(1) : "");
179  }
180 
181 }
static Gui getGui(@NotNull final Component element)
Returns the Gui an element is part of.
Definition: GuiUtils.java:91
A AbstractLabel that renders the text as a list of plain strings.
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
final TooltipManager tooltipManager
The TooltipManager to update.
final CrossfireServerConnection crossfireServerConnection
The CrossfireServerConnection to monitor.
void dispose()
Releases all allocated resources.
Interface for listeners interested in drawinfo messages received from the Crossfire server...
final JXCWindowRenderer windowRenderer
The JXCWindowRenderer this element belongs to.
boolean isDialogOpen(@NotNull final Gui dialog)
Returns whether a given dialog is currently visible.
final GUIElementListener elementListener
The GUIElementListener to notify.
final Color backgroundColor
If set, the opaque background color.
final CrossfireDrawinfoListener crossfireDrawinfoListener
The CrossfireDrawinfoListener registered to receive drawinfo messages.
static final long serialVersionUID
The serial version UID.
static final int MAX_LINE_LENGTH
The maximum line length in characters.
GUILabelMessage(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final Font font, @NotNull final Color color, @Nullable final Color backgroundColor)
Creates a new instance.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
A GUIHTMLLabel that displays the last received "drawinfo" message.
Interface for listeners interested in drawextinfo messages received from the Crossfire server...
Implements an AbstractLabel that displays HTML contents.
final CrossfireDrawextinfoListener crossfireDrawextinfoListener
The CrossfireDrawextinfoListener registered to receive drawextinfo messages.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
void removeCrossfireDrawinfoListener(@NotNull CrossfireDrawinfoListener listener)
Removes the given listener from the list of objects listening to the drawinfo S->C messages...
void removeCrossfireDrawextinfoListener(@NotNull CrossfireDrawextinfoListener listener)
Removes the given listener from the list of objects listening to the drawextinfo S->C messages...