Gridarta Editor
ValidationError.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.model.validation.errors;
21 
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
32 import net.sf.japi.swing.action.ActionBuilder;
33 import net.sf.japi.swing.action.ActionBuilderFactory;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
44 public abstract class ValidationError<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Serializable {
45 
49  private static final int TITLE_MAP_SQUARES = 3;
50 
54  private static final int TITLE_GAME_OBJECTS = 5;
55 
59  private static final long serialVersionUID = 1L;
60 
64  @NotNull
65  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
66 
71  @NotNull
72  private final String key;
73 
78  @NotNull
79  private final MapModel<G, A, R> mapModel;
80 
85  @NotNull
86  private final List<MapSquare<G, A, R>> mapSquares = new ArrayList<>(0);
87 
92  @NotNull
93  private final List<G> gameObjects = new ArrayList<>(0);
94 
100  @NotNull
102 
108  @NotNull
110 
118  protected ValidationError(@NotNull final MapModel<G, A, R> mapModel) throws IllegalArgumentException {
119  final String name = getClass().getSimpleName();
120  if (!name.endsWith("Error")) {
121  throw new IllegalArgumentException("Class name must end with \"Error\"");
122  }
123  key = "Validator." + name.substring(0, name.indexOf("Error"));
124  this.mapModel = mapModel;
125  }
126 
132  protected ValidationError(@NotNull final String key, @NotNull final MapModel<G, A, R> mapModel) {
133  this.key = key;
134  this.mapModel = mapModel;
135  }
136 
141  @NotNull
143  return mapModel;
144  }
145 
150  @NotNull
151  public Iterable<MapSquare<G, A, R>> getMapSquares() {
152  return Collections.unmodifiableList(mapSquares);
153  }
154 
159  protected void addMapSquare(@NotNull final MapSquare<G, A, R> mapSquare) {
160  if (!mapSquares.contains(mapSquare)) {
161  if (mapSquare.getMapModel() != mapModel) {
162  throw new IllegalArgumentException();
163  }
164  mapSquares.add(mapSquare);
165  }
166  }
167 
172  @NotNull
173  @SuppressWarnings("TypeMayBeWeakened")
174  public List<G> getGameObjects() {
175  return Collections.unmodifiableList(gameObjects);
176  }
177 
182  protected void addGameObject(@NotNull final G gameObject) {
183  if (!gameObjects.contains(gameObject)) {
184  gameObjects.add(gameObject);
185  final MapSquare<G, A, R> mapSquare = gameObject.getMapSquare();
186  if (mapSquare != null) {
187  addMapSquare(mapSquare);
188  }
189  }
190  }
191 
198  @Nullable
199  public abstract String getParameter(int id);
200 
207  @NotNull
208  public String getMessage() {
209  final int x;
210  final int y;
211  if (mapSquares.isEmpty()) {
212  x = 0;
213  y = 0;
214  } else {
215  final MapSquare<G, A, R> mapSquare = mapSquares.get(0);
216  x = mapSquare != null ? mapSquare.getMapX() : -1;
217  y = mapSquare != null ? mapSquare.getMapY() : -1;
218  }
219  final String archName = gameObjects.isEmpty() ? null : gameObjects.get(0).getBestName();
220  final String parameter0 = getParameter(0);
221  final String parameter1 = getParameter(1);
222  final String parameter2 = getParameter(2);
223  final String parameter3 = getParameter(3);
224  final StringBuilder sb = new StringBuilder();
225  if (mapSquares.size() > 1) {
226  appendMapSquares(sb);
227  }
228  appendTitle(sb);
229  appendGameObjects(sb);
230  return ACTION_BUILDER.format(key + ".msg", sb.toString(), x, y, archName, parameter0, parameter1, parameter2, parameter3);
231  }
232 
236  @Nullable
237  @Override
238  public String toString() {
239  final StringBuilder sb = new StringBuilder();
240  appendMapSquares(sb);
241  appendTitle(sb);
242  appendGameObjects(sb);
243  return sb.toString();
244  }
245 
250  private void appendMapSquares(@NotNull final StringBuilder sb) {
251  appendObjects(sb, mapSquares, mapSquareFormatter, "[", "] ", TITLE_MAP_SQUARES);
252  }
253 
258  private void appendTitle(@NotNull final StringBuilder sb) {
259  final String title = ACTION_BUILDER.getString(key + ".title");
260  if (title == null) {
261  sb.append(key);
262  } else {
263  sb.append(title);
264  }
265  }
266 
271  private void appendGameObjects(@NotNull final StringBuilder sb) {
272  appendObjects(sb, gameObjects, gameObjectFormatter, " [", "]", TITLE_GAME_OBJECTS);
273  }
274 
284  private <O> void appendObjects(@NotNull final StringBuilder sb, @NotNull final Collection<O> objects, @NotNull final Formatter<G, A, R, O> formatter, @NotNull final String prefix, @NotNull final String postfix, final int maxObjects) {
285  if (objects.isEmpty()) {
286  return;
287  }
288 
289  final List<O> sortedObjects = new ArrayList<>(objects);
290  sortedObjects.sort(formatter);
291 
292  sb.append(prefix);
293  int count = 0;
294  for (final O object : sortedObjects) {
295  if (count > 0) {
296  sb.append(' ');
297  }
298  sb.append(formatter.toString(object));
299  count++;
300  if (count >= maxObjects && sortedObjects.size() > maxObjects + 1) {
301  sb.append("...");
302  break;
303  }
304  }
305  sb.append(postfix);
306  }
307 
308 }
int getMapX()
Returns the x coordinate on the map.
Definition: MapSquare.java:107
abstract String getParameter(int id)
Returns a parameter string to be used in the error message.
String toString()
The String representation of an error is its title.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
ValidationError(@NotNull final MapModel< G, A, R > mapModel)
Creates a new instance.
Interface for formatting objects.
Definition: Formatter.java:34
final MapModel< G, A, R > mapModel
The MapModel that caused the error.
ValidationError(@NotNull final String key, @NotNull final MapModel< G, A, R > mapModel)
Creates a new instance.
String getMessage()
Returns the error message for this validation error.
void addGameObject(@NotNull final G gameObject)
Adds an erroneous GameObject.
Super class of all errors that could occur during map validation.
MapModel< G, A, R > getMapModel()
Return the map on which the error occurred.
Base package of all Gridarta classes.
Iterable< MapSquare< G, A, R > > getMapSquares()
Returns the MapSquares that caused the error.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void appendMapSquares(@NotNull final StringBuilder sb)
Appends the map squares of this error to a StringBuilder.
final Formatter< G, A, R, G > gameObjectFormatter
The MapSquareFormatter for formatting GameObject instances.
void appendTitle(@NotNull final StringBuilder sb)
Appends the title of this error to a StringBuilder.
static final int TITLE_MAP_SQUARES
The maximum number of map squares to show in the title.
static final int TITLE_GAME_OBJECTS
The maximum number of game objects to show in the title.
GameObjects are the objects based on Archetypes found on maps.
final List< MapSquare< G, A, R > > mapSquares
The MapSquares that caused the error.
List< G > getGameObjects()
Returns the GameObjects that caused the error.
void appendGameObjects(@NotNull final StringBuilder sb)
Appends the game objects of this error to a StringBuilder.
final Formatter< G, A, R, MapSquare< G, A, R > > mapSquareFormatter
The MapSquareFormatter for formatting MapSquare instances.
static final long serialVersionUID
The serial version UID.
final List< G > gameObjects
The GameObjects that caused the error.
void addMapSquare(@NotNull final MapSquare< G, A, R > mapSquare)
Adds an erroneous MapSquare.
static final ActionBuilder ACTION_BUILDER
Action Builder.
int getMapY()
Returns the y coordinate on the map.
Definition: MapSquare.java:115