Gridarta Editor
CellRenderer.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.selectedsquare;
21 
22 import java.awt.Component;
23 import javax.swing.BorderFactory;
24 import javax.swing.DefaultListCellRenderer;
25 import javax.swing.Icon;
26 import javax.swing.JList;
31 import org.jetbrains.annotations.NotNull;
32 
37 public class CellRenderer<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends DefaultListCellRenderer {
38 
42  private static final int INDENT_PIXELS = 16;
43 
47  private static final long serialVersionUID = 1L;
48 
52  @NotNull
54 
58  @NotNull
59  private final Icon unknownSquareIcon;
60 
67  public CellRenderer(@NotNull final FaceObjectProviders faceObjectProviders, @NotNull final Icon unknownSquareIcon) {
68  this.faceObjectProviders = faceObjectProviders;
69  this.unknownSquareIcon = unknownSquareIcon;
70  }
71 
72  @Override
73  public Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
74  super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
75 
76  //DefaultListCellRenderer does not use type parameters
77  @SuppressWarnings("unchecked") GameObject<G, A, R> arch = (GameObject<G, A, R>) value;
78 
79  // arch == null should not happen, but it *can* happen when the active
80  // window gets changed by user and java is still blitting here
81  if (arch != null) {
82  arch = arch.getHead();
83  setIcon(faceObjectProviders.getFace(arch));
84  setText(arch.getBestName());
85  } else {
86  setIcon(unknownSquareIcon);
87  setText("?");
88  }
89 
90  int indent = 0;
91  if (arch != null) {
92  while (true) {
94  if (env == null) {
95  break;
96  }
97  arch = env;
98  indent++;
99  }
100  }
101  if (indent > 0) {
102  setBorder(BorderFactory.createEmptyBorder(0, indent * INDENT_PIXELS, 0, 0)); // indentation
103  }
104 
105  return this;
106  }
107 
108 }
T getHead()
Return the head part of a multi-part object.
Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus)
G getContainerGameObject()
Returns the environment game object if this game object is in the inventory or.
CellRenderer for rendering ArchObjects on a certain map square in a list.
final FaceObjectProviders faceObjectProviders
The FaceObjectProviders for looking up faces.
Base package of all Gridarta classes.
static final long serialVersionUID
Serial Version UID.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
final Icon unknownSquareIcon
The Icon for unknown squares.
static final int INDENT_PIXELS
The number of pixels to indent inventory items.
String getBestName()
Returns the name which is best appropriate to describe this GameObject.
CellRenderer(@NotNull final FaceObjectProviders faceObjectProviders, @NotNull final Icon unknownSquareIcon)
Creates a new instance.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.