Gridarta Editor
ToolTipAppender.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.map.renderer;
21 
22 import java.util.Collections;
23 import java.util.Map;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
36 public class ToolTipAppender<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
37 
42  @Nullable
44 
45  @NotNull
46  private final StringBuilder sb = new StringBuilder("<html>");
47 
48  private boolean empty = true;
49 
55  public ToolTipAppender(@Nullable final GameObjectParser<G, A, R> gameObjectParser) {
56  this.gameObjectParser = gameObjectParser;
57  }
58 
59  public void appendGameObject(@NotNull final G gameObject, final boolean alwaysInclude, @NotNull final String prefix) {
60  final Map<String, String> fields = gameObjectParser != null ? gameObjectParser.getModifiedFields(gameObject) : Collections.emptyMap();
61  fields.remove("x");
62  fields.remove("y");
63  if (alwaysInclude || !fields.isEmpty() || !gameObject.isEmpty()) {
64  if (empty) {
65  empty = false;
66  } else {
67  sb.append("<br><hr>");
68  }
69  sb.append(prefix);
70  sb.append("<b>").append(encode(gameObject.getBestName())).append("</b>");
71  for (final Map.Entry<String, String> field : fields.entrySet()) {
72  sb.append("<br>");
73  sb.append(prefix);
74  sb.append(StringUtils.PATTERN_NEWLINE.matcher((encode(field.getKey()) + " " + encode(field.getValue())).trim()).replaceAll("<br>" + prefix));
75  }
76  for (final G invGameObject : gameObject.reverse()) {
77  appendGameObject(invGameObject, true, prefix + "&nbsp;&nbsp;&nbsp;&nbsp;");
78  }
79  }
80  }
81 
82  public void appendValidationError(@NotNull final ValidationError<G, ?, ?> error) {
83  if (empty) {
84  empty = false;
85  } else {
86  sb.append("\n<hr>");
87  }
88  sb.append(error.getMessage().trim());
89  }
90 
91  @Nullable
92  public String finish() {
93  return empty ? null : StringUtils.PATTERN_NEWLINE.matcher(sb.toString()).replaceAll("<br>");
94  }
95 
101  @NotNull
102  private static String encode(@NotNull final String str) {
103  final StringBuilder sb = new StringBuilder(str.length());
104  for (final char ch : str.toCharArray()) {
105  switch (ch) {
106  case '<':
107  sb.append("&lt;");
108  break;
109 
110  case '>':
111  sb.append("&gt;");
112  break;
113 
114  case '&':
115  sb.append("&amp;");
116  break;
117 
118  default:
119  sb.append(ch);
120  break;
121  }
122  }
123  return sb.toString();
124  }
125 
126 }
Utility class for string manipulation.
Reading and writing of maps, handling of paths.
ToolTipAppender(@Nullable final GameObjectParser< G, A, R > gameObjectParser)
Creates a new instance.
This package contains the framework for validating maps.
Interface for classes that read or write GameObject instances.
static final Pattern PATTERN_NEWLINE
The pattern that matches a single newline ("\n").
Map< String, String > getModifiedFields(@NotNull G gameObject)
Returns the modified fields of a GameObject.
Super class of all errors that could occur during map validation.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void appendValidationError(@NotNull final ValidationError< G, ?, ?> error)
void appendGameObject(@NotNull final G gameObject, final boolean alwaysInclude, @NotNull final String prefix)
GameObjects are the objects based on Archetypes found on maps.
final GameObjectParser< G, A, R > gameObjectParser
The GameObjectParser for creating tooltip information or.
static String encode(@NotNull final String str)
Encodes a string so that the result can be embedded into HTML.