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 com.realtime.crossfire.jxclient.gui.gui.GUIElementListener;
00025 import com.realtime.crossfire.jxclient.gui.gui.Gui;
00026 import com.realtime.crossfire.jxclient.gui.gui.GuiUtils;
00027 import com.realtime.crossfire.jxclient.gui.gui.TooltipManager;
00028 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireDrawextinfoListener;
00029 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireDrawinfoListener;
00030 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection;
00031 import java.awt.Color;
00032 import java.awt.Font;
00033 import org.jetbrains.annotations.NotNull;
00034 import org.jetbrains.annotations.Nullable;
00035
00040 public class GUILabelMessage extends GUIMultiLineLabel {
00041
00045 private static final int MAX_LINE_LENGTH = 75;
00046
00050 private static final long serialVersionUID = 1;
00051
00055 @NotNull
00056 private final CrossfireServerConnection crossfireServerConnection;
00057
00061 @NotNull
00062 private final JXCWindowRenderer windowRenderer;
00063
00068 @NotNull
00069 private final CrossfireDrawinfoListener crossfireDrawinfoListener = new CrossfireDrawinfoListener() {
00070
00071 @Override
00072 public void commandDrawinfoReceived(@NotNull final String text, final int type) {
00073 final Gui gui = GuiUtils.getGui(GUILabelMessage.this);
00074 if (gui == null || !windowRenderer.isDialogOpen(gui)) {
00075 setText(text);
00076 }
00077 }
00078
00079 };
00080
00085 @NotNull
00086 private final CrossfireDrawextinfoListener crossfireDrawextinfoListener = new CrossfireDrawextinfoListener() {
00087
00088 @Override
00089 public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
00090 final Gui gui = GuiUtils.getGui(GUILabelMessage.this);
00091 if (gui == null || !windowRenderer.isDialogOpen(gui)) {
00092 setText(message);
00093 }
00094 }
00095
00096 @Override
00097 public void setDebugMode(final boolean printMessageTypes) {
00098
00099 }
00100
00101 };
00102
00114 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) {
00115 super(tooltipManager, elementListener, name, null, font, color, backgroundColor, Alignment.LEFT, "");
00116 this.crossfireServerConnection = crossfireServerConnection;
00117 this.windowRenderer = windowRenderer;
00118 this.crossfireServerConnection.addCrossfireDrawinfoListener(crossfireDrawinfoListener);
00119 this.crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
00120 }
00121
00125 @Override
00126 public void dispose() {
00127 super.dispose();
00128 crossfireServerConnection.removeCrossfireDrawinfoListener(crossfireDrawinfoListener);
00129 crossfireServerConnection.removeCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
00130 }
00131
00135 @Override
00136 public void setText(@NotNull final String text) {
00137 final StringBuilder sb = new StringBuilder();
00138 int pos = 0;
00139 while (pos < text.length()) {
00140 sb.append('\n');
00141
00142 int endPos = pos;
00143 while (endPos < pos+MAX_LINE_LENGTH && endPos < text.length() && text.charAt(endPos) != '\n') {
00144 endPos++;
00145 }
00146
00147 if (endPos >= pos+MAX_LINE_LENGTH) {
00148
00149 int endPos2 = endPos;
00150 while (endPos2 > pos) {
00151 endPos2--;
00152 final int ch = text.charAt(endPos2);
00153 if (ch == ' ') {
00154 break;
00155 }
00156 }
00157 if (endPos2 > pos) {
00158 sb.append(text.substring(pos, endPos2));
00159 pos = endPos2+1;
00160 } else {
00161 sb.append(text.substring(pos, endPos));
00162 pos = endPos;
00163 }
00164 } else if (endPos >= text.length()) {
00165
00166 sb.append(text.substring(pos));
00167 pos = endPos;
00168 } else {
00169 assert text.charAt(endPos) == '\n';
00170
00171 sb.append(text.substring(pos, endPos));
00172 pos = endPos+1;
00173 }
00174 }
00175 super.setText(sb.length() > 0 ? sb.substring(1) : "");
00176 }
00177
00178 }