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 
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 }
net.sf.gridarta.model.maplocation.MapLocation.getMapPath
MapPath getMapPath()
Definition: MapLocation.java:116
net.sf.gridarta.model.maplocation.MapLocation.compareTo
int compareTo(@NotNull final MapLocation o)
Definition: MapLocation.java:233
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer
Definition: MapListCellRenderer.java:39
net.sf.gridarta
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer.getListCellRendererComponent
Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus)
Definition: MapListCellRenderer.java:62
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer.faceObjectProviders
final FaceObjectProviders faceObjectProviders
Definition: MapListCellRenderer.java:50
net.sf
net.sf.gridarta.model.face.FaceObjectProviders
Definition: FaceObjectProviders.java:46
net.sf.gridarta.model.gameobject.GameObject
Definition: GameObject.java:36
net.sf.gridarta.model.maplocation.MapLocation.getMapCoordinate
Point getMapCoordinate()
Definition: MapLocation.java:125
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer.serialVersionUID
static final long serialVersionUID
Definition: MapListCellRenderer.java:44
net.sf.gridarta.model.gameobject
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maplocation.MapLocation.newAbsoluteMapLocation
static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Definition: MapLocation.java:94
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer.MapListCellRenderer
MapListCellRenderer(@NotNull final FaceObjectProviders faceObjectProviders)
Definition: MapListCellRenderer.java:57
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer.getMapLocation
static MapLocation getMapLocation(@NotNull final GameObject<?, ?, ?> gameObject)
Definition: MapListCellRenderer.java:92
net.sf.gridarta.model.maplocation.NoExitPathException
Definition: NoExitPathException.java:29
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer.compare
int compare(final GameObject<?, ?, ?> o1, final GameObject<?, ?, ?> o2)
Definition: MapListCellRenderer.java:101
net.sf.gridarta.model.maplocation.MapLocation
Definition: MapLocation.java:42
net.sf.gridarta.model
net.sf.gridarta.model.face
Definition: AbstractFaceObjects.java:20
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.model.maplocation
Definition: MapLocation.java:20