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-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.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.Iterator;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.Vector;
30 import javax.swing.DefaultListModel;
31 import javax.swing.JEditorPane;
32 import javax.swing.JList;
33 import javax.swing.JPanel;
34 import javax.swing.JScrollPane;
35 import javax.swing.JSplitPane;
36 import javax.swing.event.ListSelectionEvent;
37 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
83 
88  @Nullable
90 
94  @Nullable
95  private Vector<ValidationError<G, A, R>> errors;
96 
101  @Nullable
103 
107  private final ListSelectionListener listSelectionListener = new ListSelectionListener() {
108 
109  @Override
110  public void valueChanged(final ListSelectionEvent e) {
111  if (e.getValueIsAdjusting()) {
112  return;
113  }
114  highlightEntries(((JList<?>) e.getSource()).getSelectedIndex());
115  }
116 
117  };
118 
124 
125  @Override
126  public void mapSizeChanged(@NotNull final Size2D newSize) {
127  // ignore
128  }
129 
130  @Override
131  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
132  // ignore
133  }
134 
135  @Override
136  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
137  // ignore
138  }
139 
140  @Override
141  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
142  updateErrors(errors);
143  }
144 
145  @Override
146  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
147  // ignore
148  }
149 
150  @Override
151  public void modifiedChanged() {
152  // ignore
153  }
154 
155  };
156 
157  @NotNull
159 
160  @Override
161  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
162  if (currentMapModel != null) {
163  currentMapModel.removeMapModelListener(mapModelListener);
164  }
165  currentMapModel = mapView == null ? null : mapView.getMapControl().getMapModel();
166  if (currentMapModel != null) {
167  currentMapModel.addMapModelListener(mapModelListener);
168  }
169 
171  updateErrors();
172  }
173 
174  @Override
175  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
176  // ignore
177  }
178 
179  @Override
180  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
181  // ignore
182  }
183 
184  };
185 
190  public ErrorListView(@NotNull final MapViewManager<G, A, R> mapViewManager) {
191  setLayout(new BorderLayout());
192  final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(errorList), new JScrollPane(errorMsg));
193  splitPane.setOneTouchExpandable(true);
194  add(splitPane, BorderLayout.CENTER);
195  errorMsg.setEditable(false);
196  errorList.addListSelectionListener(listSelectionListener);
197  //noinspection RefusedBequest
198  errorList.addMouseListener(new MouseAdapter() {
199 
200  @Override
201  public void mousePressed(final MouseEvent e) {
202  highlightEntries(errorList.getSelectedIndex());
203  }
204  });
205  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
206  mapView = mapViewManager.getActiveMapView();
207  mapViewManagerListener.activeMapViewChanged(mapView);
208  updateErrors();
209  }
210 
215  public void addErrorListViewListener(@NotNull final ErrorListViewListener listener) {
216  listeners.add(listener);
217  }
218 
223  public void removeErrorListViewListener(@NotNull final ErrorListViewListener listener) {
224  listeners.remove(listener);
225  }
226 
231  public boolean hasWarnings() {
232  return errors != null && !errors.isEmpty();
233  }
234 
240  private void highlightEntries(final int index) {
241  if (errors == null) {
242  errorMsg.setText(null);
243  errorMsg.setCaretPosition(0);
244  return;
245  }
246  final ValidationError<G, A, R> error;
247  try {
248  error = errors.elementAt(index);
249  } catch (final ArrayIndexOutOfBoundsException ignored) {
250  errorMsg.setText(null);
251  errorMsg.setCaretPosition(0);
252  return;
253  }
254  errorMsg.setText(error.getMessage());
255 
256  final MapView<G, A, R> tmpMapView = mapView;
257  if (tmpMapView != null) {
258  final Iterator<G> gameObjectIterator = error.getGameObjects().iterator();
259  if (gameObjectIterator.hasNext()) {
260  final G gameObject = gameObjectIterator.next();
261  tmpMapView.getMapCursor().setGameObject(gameObject);
262  } else {
263  final Iterator<MapSquare<G, A, R>> mapSquareIterator = error.getMapSquares().iterator();
264  if (mapSquareIterator.hasNext()) {
265  final MapSquare<G, A, R> mapSquare = mapSquareIterator.next();
266  tmpMapView.getMapCursor().setMapSquare(mapSquare);
267  }
268  }
269  }
270  errorMsg.setCaretPosition(0);
271  }
272 
277  private void fireErrorsUpdated(final boolean hasWarnings) {
278  for (final ErrorListViewListener listener : listeners.getListeners()) {
279  listener.warningsChanged(hasWarnings);
280  }
281  }
282 
286  private void updateErrors() {
287  if (mapView != null) {
289  return;
290  }
291 
292  errors = null;
293  errorList.setModel(new DefaultListModel<>());
294  fireErrorsUpdated(false);
295  }
296 
301  public void updateErrors(@NotNull final ErrorCollector<G, A, R> errors) {
302  assert mapView != null;
303  final List<ValidationError<G, A, R>> errorVector = new ArrayList<>();
304  for (final ValidationError<G, A, R> validationError : errors.getErrors()) {
305  errorVector.add(validationError);
306  }
307  this.errors = new Vector<>(errorVector);
308  errorList.setListData(this.errors);
309 
311  }
312 
313 }
final EventListenerList2< ErrorListViewListener > listeners
The registered listeners to notify.
void highlightEntries(final int index)
Display an error message.
final ListSelectionListener listSelectionListener
The list selection listener to detect selected list entries.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
T [] getListeners()
Returns an array of all the listeners.
This package contains the framework for validating maps.
Interface for listeners listening on MapModel events.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
void fireErrorsUpdated(final boolean hasWarnings)
Notifies all listeners that the warnings may have changed.
String getMessage()
Returns the error message for this validation error.
MapModel< G, A, R > getMapModel()
Returns the map model.
Super class of all errors that could occur during map validation.
ErrorListView(@NotNull final MapViewManager< G, A, R > mapViewManager)
Create a ConnectionPanel.
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 activeMapViewChanged(@Nullable MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
Interface for listeners interested in ErrorListView related events.
MapView< G, A, R > mapView
The MapView for displaying map errors.
void updateErrors(@NotNull final ErrorCollector< G, A, R > errors)
Updates the errors for the current map.
final JEditorPane errorMsg
The JLabel for displaying the error text.
void remove(@NotNull final T listener)
Removes a listener.
GameObjects are the objects based on Archetypes found on maps.
final MapModelListener< G, A, R > mapModelListener
The MapModelListener which is attached to currentMapModel.
void add(@NotNull final T listener)
Adds a listener.
boolean hasWarnings()
Returns whether any warnings are shown.
Interface for listeners interested in events related to MapViewManager instances. ...
final JList< ValidationError< G, A, R > > errorList
The list for displaying the errors.
Base classes for rendering maps.
void setGameObject(@Nullable final G gameObject)
Sets the selected GameObject.
Definition: MapCursor.java:446
void addErrorListViewListener(@NotNull final ErrorListViewListener listener)
Adds an ErrorListViewListener to be notified about changes.
Vector< ValidationError< G, A, R > > errors
The currently displayed errors.
List< G > getGameObjects()
Returns the GameObjects that caused the error.
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
MapModel< G, A, R > currentMapModel
Last known active map.
Type-safe version of EventListenerList.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
An interface for classes that collect errors.
An ErrorPanel displays errors to the user.
final MapViewManagerListener< G, A, R > mapViewManagerListener
ErrorCollector< G, A, R > getErrors()
Gets the errors in this map.
static final long serialVersionUID
The serial version UID.
void removeErrorListViewListener(@NotNull final ErrorListViewListener listener)
Removes an ErrorListViewListener to be notified about changes.
void setMapSquare(@Nullable final MapSquare< G, A, R > mapSquare)
Sets the selected MapSquare.
Definition: MapCursor.java:469
The location of a map file with a map directory.
Definition: MapFile.java:31
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30