Gridarta Editor
ErrorListView.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 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.panel.gameobjectattributes;
21 
22 import java.awt.BorderLayout;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.Vector;
31 import java.util.concurrent.CopyOnWriteArrayList;
32 import javax.swing.DefaultListModel;
33 import javax.swing.JEditorPane;
34 import javax.swing.JList;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.JSplitPane;
38 import javax.swing.event.ListSelectionListener;
51 import net.sf.gridarta.utils.Size2D;
52 import org.jetbrains.annotations.NotNull;
53 import org.jetbrains.annotations.Nullable;
54 
59 public class ErrorListView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JPanel {
60 
64  private static final long serialVersionUID = 1L;
65 
70  private final JList<ValidationError<G, A, R>> errorList = new JList<>();
71 
76  private final JEditorPane errorMsg = new JEditorPane("text/html", "");
77 
81  @NotNull
82  private final Collection<ErrorListViewListener> listeners = new CopyOnWriteArrayList<>();
83 
88  @Nullable
90 
94  @Nullable
95  private Vector<ValidationError<G, A, R>> errors;
96 
101  @Nullable
103 
107  private final ListSelectionListener listSelectionListener = e -> {
108  if (e.getValueIsAdjusting()) {
109  return;
110  }
111  highlightEntries(((JList<?>) e.getSource()).getSelectedIndex());
112  };
113 
119 
120  @Override
121  public void mapSizeChanged(@NotNull final Size2D newSize) {
122  // ignore
123  }
124 
125  @Override
126  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
127  // ignore
128  }
129 
130  @Override
131  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
132  // ignore
133  }
134 
135  @Override
136  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
138  }
139 
140  @Override
141  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
142  // ignore
143  }
144 
145  @Override
146  public void modifiedChanged() {
147  // ignore
148  }
149 
150  };
151 
152  @NotNull
154 
155  @Override
156  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
157  if (currentMapModel != null) {
159  }
161  if (currentMapModel != null) {
163  }
164 
166  updateErrors();
167  }
168 
169  @Override
170  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
171  // ignore
172  }
173 
174  @Override
175  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
176  // ignore
177  }
178 
179  };
180 
185  public ErrorListView(@NotNull final MapViewManager<G, A, R> mapViewManager) {
186  setLayout(new BorderLayout());
187  final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(errorList), new JScrollPane(errorMsg));
188  splitPane.setOneTouchExpandable(true);
189  add(splitPane, BorderLayout.CENTER);
190  errorMsg.setEditable(false);
191  errorList.addListSelectionListener(listSelectionListener);
192  //noinspection RefusedBequest
193  errorList.addMouseListener(new MouseAdapter() {
194 
195  @Override
196  public void mousePressed(final MouseEvent e) {
197  highlightEntries(errorList.getSelectedIndex());
198  }
199  });
200  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
201  mapView = mapViewManager.getActiveMapView();
203  updateErrors();
204  }
205 
210  public void addErrorListViewListener(@NotNull final ErrorListViewListener listener) {
211  listeners.add(listener);
212  }
213 
218  public void removeErrorListViewListener(@NotNull final ErrorListViewListener listener) {
219  listeners.remove(listener);
220  }
221 
226  public boolean hasWarnings() {
227  return errors != null && !errors.isEmpty();
228  }
229 
235  private void highlightEntries(final int index) {
236  if (errors == null) {
237  errorMsg.setText(null);
238  errorMsg.setCaretPosition(0);
239  return;
240  }
241  final ValidationError<G, A, R> error;
242  try {
243  error = errors.elementAt(index);
244  } catch (final ArrayIndexOutOfBoundsException ignored) {
245  errorMsg.setText(null);
246  errorMsg.setCaretPosition(0);
247  return;
248  }
249  errorMsg.setText(error.getMessage());
250 
251  final MapView<G, A, R> tmpMapView = mapView;
252  if (tmpMapView != null) {
253  final Iterator<G> gameObjectIterator = error.getGameObjects().iterator();
254  if (gameObjectIterator.hasNext()) {
255  final G gameObject = gameObjectIterator.next();
256  tmpMapView.getMapCursor().setGameObject(gameObject);
257  } else {
258  final Iterator<MapSquare<G, A, R>> mapSquareIterator = error.getMapSquares().iterator();
259  if (mapSquareIterator.hasNext()) {
260  final MapSquare<G, A, R> mapSquare = mapSquareIterator.next();
261  tmpMapView.getMapCursor().setMapSquare(mapSquare);
262  }
263  }
264  }
265  errorMsg.setCaretPosition(0);
266  }
267 
272  private void fireErrorsUpdated(final boolean hasWarnings) {
273  for (final ErrorListViewListener listener : listeners) {
274  listener.warningsChanged(hasWarnings);
275  }
276  }
277 
281  private void updateErrors() {
282  if (mapView != null) {
284  return;
285  }
286 
287  errors = null;
288  errorList.setModel(new DefaultListModel<>());
289  fireErrorsUpdated(false);
290  }
291 
296  private void updateErrors(@NotNull final ErrorCollector<G, A, R> errors) {
297  assert mapView != null;
298  final List<ValidationError<G, A, R>> errorVector = new ArrayList<>();
299  for (final ValidationError<G, A, R> validationError : errors.getErrors()) {
300  errorVector.add(validationError);
301  }
302  this.errors = new Vector<>(errorVector);
303  errorList.setListData(this.errors);
304 
306  }
307 
308 }
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.listSelectionListener
final ListSelectionListener listSelectionListener
The list selection listener to detect selected list entries.
Definition: ErrorListView.java:107
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListViewListener
Interface for listeners interested in ErrorListView related events.
Definition: ErrorListViewListener.java:28
net.sf.gridarta.gui.map.mapview.MapView.getMapControl
MapControl< G, A, R > getMapControl()
Return the controller of this view.
net.sf.gridarta.model.validation.errors.ValidationError.getMapSquares
Iterable< MapSquare< G, A, R > > getMapSquares()
Returns the MapSquares that caused the error.
Definition: ValidationError.java:152
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.removeErrorListViewListener
void removeErrorListViewListener(@NotNull final ErrorListViewListener listener)
Removes an ErrorListViewListener to be notified about changes.
Definition: ErrorListView.java:218
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.validation.errors.ValidationError
Super class of all errors that could occur during map validation.
Definition: ValidationError.java:45
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.hasWarnings
boolean hasWarnings()
Returns whether any warnings are shown.
Definition: ErrorListView.java:226
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.gui.map.mapview.MapViewManager
Maintains all map views.
Definition: MapViewManager.java:38
net.sf
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.fireErrorsUpdated
void fireErrorsUpdated(final boolean hasWarnings)
Notifies all listeners that the warnings may have changed.
Definition: ErrorListView.java:272
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.mapview.MapViewManagerListener
Interface for listeners interested in events related to {} instances.
Definition: MapViewManagerListener.java:33
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.currentMapModel
MapModel< G, A, R > currentMapModel
Last known active map.
Definition: ErrorListView.java:89
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.updateErrors
void updateErrors()
Updates the displayed errors.
Definition: ErrorListView.java:281
net.sf.gridarta.model.mapmodel.MapModel.addMapModelListener
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
net.sf.gridarta.model.mapcursor.MapCursor.setGameObject
void setGameObject(@Nullable final G gameObject)
Sets the selected GameObject.
Definition: MapCursor.java:446
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView
An ErrorPanel displays errors to the user.
Definition: ErrorListView.java:59
net.sf.gridarta.gui.map.mapview.MapViewManagerListener.activeMapViewChanged
void activeMapViewChanged(@Nullable MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.map.mapview.MapView.getMapCursor
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
net.sf.gridarta.model.mapmodel.MapModel.getErrors
ErrorCollector< G, A, R > getErrors()
Gets the errors in this map.
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.mapModelListener
final MapModelListener< G, A, R > mapModelListener
The MapModelListener which is attached to {}.
Definition: ErrorListView.java:118
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.listeners
final Collection< ErrorListViewListener > listeners
The registered listeners to notify.
Definition: ErrorListView.java:82
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.mapViewManagerListener
final MapViewManagerListener< G, A, R > mapViewManagerListener
Definition: ErrorListView.java:153
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.errorMsg
final JEditorPane errorMsg
The JLabel for displaying the error text.
Definition: ErrorListView.java:76
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.ErrorListView
ErrorListView(@NotNull final MapViewManager< G, A, R > mapViewManager)
Creates a new instance.
Definition: ErrorListView.java:185
net.sf.gridarta.model.mapmodel.MapModel.removeMapModelListener
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.updateErrors
void updateErrors(@NotNull final ErrorCollector< G, A, R > errors)
Updates the errors for the current map.
Definition: ErrorListView.java:296
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.mapView
MapView< G, A, R > mapView
The MapView for displaying map errors.
Definition: ErrorListView.java:102
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.mapmodel.MapModelListener
Interface for listeners listening on MapModel events.
Definition: MapModelListener.java:36
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.errorList
final JList< ValidationError< G, A, R > > errorList
The list for displaying the errors.
Definition: ErrorListView.java:70
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: ErrorListView.java:64
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.validation.errors.ValidationError.getGameObjects
List< G > getGameObjects()
Returns the GameObjects that caused the error.
Definition: ValidationError.java:174
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.errors
Vector< ValidationError< G, A, R > > errors
The currently displayed errors.
Definition: ErrorListView.java:95
net.sf.gridarta.model.mapcursor.MapCursor.setMapSquare
void setMapSquare(@Nullable final MapSquare< G, A, R > mapSquare)
Sets the selected MapSquare.
Definition: MapCursor.java:469
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.highlightEntries
void highlightEntries(final int index)
Display an error message.
Definition: ErrorListView.java:235
net.sf.gridarta.model.validation.errors
Definition: AttributeRangeError.java:20
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.validation.errors.ValidationError.getMessage
String getMessage()
Returns the error message for this validation error.
Definition: ValidationError.java:205
net.sf.gridarta.gui.panel.gameobjectattributes.ErrorListView.addErrorListViewListener
void addErrorListViewListener(@NotNull final ErrorListViewListener listener)
Adds an ErrorListViewListener to be notified about changes.
Definition: ErrorListView.java:210
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20