Gridarta Editor
MapListCellRenderer.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.dialog.goexit;
21 
22 import java.awt.Component;
23 import java.awt.Point;
24 import java.util.Comparator;
25 import javax.swing.DefaultListCellRenderer;
26 import javax.swing.JList;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
39 public class MapListCellRenderer extends DefaultListCellRenderer implements Comparator<GameObject<?, ?, ?>> {
40 
44  private static final long serialVersionUID = 1L;
45 
49  @NotNull
51 
57  public MapListCellRenderer(@NotNull final FaceObjectProviders faceObjectProviders) {
58  this.faceObjectProviders = faceObjectProviders;
59  }
60 
61  @Override
62  public Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
63  super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
64 
65  final GameObject<?, ?, ?> gameObject = (GameObject<?, ?, ?>) value;
66  final MapLocation mapLocation = getMapLocation(gameObject);
67  setIcon(faceObjectProviders.getFace(gameObject));
68  final StringBuilder sb = new StringBuilder();
69  sb.append(gameObject.getBestName());
70  sb.append(" [");
71  if (mapLocation == null) {
72  sb.append("?");
73  } else {
74  sb.append(mapLocation.getMapPath());
75  sb.append("@");
76  final Point mapCoordinate = mapLocation.getMapCoordinate();
77  sb.append(mapCoordinate.x);
78  sb.append("/");
79  sb.append(mapCoordinate.y);
80  }
81  sb.append("]");
82  setText(sb.toString());
83  return this;
84  }
85 
91  @Nullable
92  private static MapLocation getMapLocation(@NotNull final GameObject<?, ?, ?> gameObject) {
93  try {
94  return MapLocation.newAbsoluteMapLocation(gameObject, true);
95  } catch (final NoExitPathException ignored) {
96  return null;
97  }
98  }
99 
100  @Override
101  public int compare(final GameObject<?, ?, ?> o1, final GameObject<?, ?, ?> o2) {
102  final String name1 = o1.getBestName();
103  final String name2 = o2.getBestName();
104  final int cmp1 = name1.compareToIgnoreCase(name2);
105  if (cmp1 != 0) {
106  return cmp1;
107  }
108 
109  final MapLocation mapLocation1 = getMapLocation(o1);
110  final MapLocation mapLocation2 = getMapLocation(o2);
111  if (mapLocation1 == null) {
112  if (mapLocation2 != null) {
113  return -1;
114  }
115  } else if (mapLocation2 == null) {
116  return +1;
117  } else {
118  final int cmp2 = mapLocation1.compareTo(mapLocation2);
119  if (cmp2 != 0) {
120  return cmp2;
121  }
122  }
123 
124  final String faceObjName1 = o1.getFaceObjName();
125  final String faceObjName2 = o2.getFaceObjName();
126  if (faceObjName1 == null) {
127  if (faceObjName2 != null) {
128  return -1;
129  }
130  } else if (faceObjName2 == null) {
131  return +1;
132  } else {
133  final int cmp3 = faceObjName1.compareTo(faceObjName2);
134  if (cmp3 != 0) {
135  return cmp3;
136  }
137  }
138 
139  return 0;
140  }
141 
142 }
Exception thrown if a game object does not specify a valid exit path.
static final long serialVersionUID
The serial version UID.
int compareTo(@NotNull final MapLocation o)
MapListCellRenderer(@NotNull final FaceObjectProviders faceObjectProviders)
Creates a new instance.
int compare(final GameObject<?, ?, ?> o1, final GameObject<?, ?, ?> o2)
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus)
MapPath getMapPath()
Returns the map path.
final FaceObjectProviders faceObjectProviders
The FaceObjectProviders for looking up faces.
GameObjects are the objects based on Archetypes found on maps.
A net.sf.gridarta.gui.panel.connectionview.CellRenderer for the locked items view.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
static MapLocation getMapLocation(@NotNull final GameObject<?, ?, ?> gameObject)
Returns the MapLocation for a GameObject.
static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
Represents a location on a map consisting of a map path and a map coordinate.
Point getMapCoordinate()
Returns the map coordinate.