Crossfire JXClient, Trunk  R20561
GUIItemItem.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.item;
23 
32 import java.awt.Dimension;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.Image;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public abstract class GUIItemItem extends GUIItem {
44 
48  private static final long serialVersionUID = 1;
49 
53  @NotNull
54  private final FacesManager facesManager;
55 
59  @NotNull
60  private final ItemPainter itemPainter;
61 
65  @Nullable
66  private CfItem item;
67 
72  @NotNull
73  private final CfItemListener itemListener = () -> {
74  setChanged();
76  };
77 
81  @NotNull
83 
84  @Override
85  public void faceUpdated(@NotNull final Face face) {
86  if (item != null && face.equals(item.getFace())) {
87  setChanged();
88  }
89  }
90 
91  };
92 
101  protected GUIItemItem(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ItemPainter itemPainter, @NotNull final FacesManager facesManager) {
102  super(tooltipManager, elementListener, name);
103  this.itemPainter = itemPainter;
104  this.facesManager = facesManager;
105  this.facesManager.addFacesManagerListener(facesManagerListener);
106  }
107 
111  @Override
112  public void dispose() {
113  super.dispose();
114  facesManager.removeFacesManagerListener(facesManagerListener);
115  setItem(null);
116  }
117 
121  @Override
122  public void paintComponent(@NotNull final Graphics g) {
123  super.paintComponent(g);
124 
125  final CfItem tmpItem = item;
126  if (tmpItem == null) {
127  return;
128  }
129 
130  final Graphics2D g2 = (Graphics2D)g;
131  itemPainter.paint(g2, tmpItem, isSelected(), getFace(tmpItem), getWidth(), getHeight());
132  }
133 
137  @Nullable
138  @Override
139  public Dimension getPreferredSize() {
140  return itemPainter.getMinimumSize();
141  }
142 
146  @Nullable
147  @Override
148  public Dimension getMinimumSize() {
149  return itemPainter.getMinimumSize();
150  }
151 
157  @NotNull
158  protected abstract Image getFace(@NotNull final CfItem item);
159 
164  @Nullable
165  public CfItem getItem() {
166  return item;
167  }
168 
173  protected void setItem(@Nullable final CfItem item) {
174  if (this.item == item) {
175  return;
176  }
177 
178  if (this.item != null) {
179  this.item.removeCfItemModifiedListener(itemListener);
180  }
181  this.item = item;
182  if (this.item != null) {
183  this.item.addCfItemModifiedListener(itemListener);
184  }
185 
186  setChanged();
188  }
189 
195  protected void setItemNoListeners(@Nullable final CfItem item) {
196  this.item = item;
197  }
198 
202  protected void updateTooltipText() {
203  setTooltipText(item == null ? null : item.getTooltipText());
204  }
205 
210  public abstract void setSelected(final boolean selected);
211 
216  protected abstract boolean isSelected();
217 
222  public abstract int getIndex();
223 
229  public abstract void setIndexNoListeners(final int index);
230 
231 }
void paint(@NotNull final Graphics2D g, @NotNull final CfItem item, final boolean selected, @NotNull final Image face, final int w, final int h)
Paints an CfItem.
final TooltipManager tooltipManager
The TooltipManager to update.
final GUIElementListener elementListener
The GUIElementListener to notify.
abstract void setSelected(final boolean selected)
Sets the selected state.
GUIItemItem(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ItemPainter itemPainter, @NotNull final FacesManager facesManager)
Creates a new instance.
void setChanged()
Records that the contents have changed and must be repainted.
void dispose()
Releases all allocated resources.
abstract int getIndex()
Returns the slot index.
final CfItemListener itemListener
The CfItemListener used to detect attribute changes of the displayed item.
A GUIElement representing an in-game object.
Definition: GUIItem.java:38
void setItemNoListeners(@Nullable final CfItem item)
Sets the current item instance without registering listeners for updates.
void addCfItemModifiedListener(@NotNull final CfItemListener listener)
Add a.
Definition: CfItem.java:466
Manages image information ("faces") needed to display the map view, items, and spell icons...
A GUIElement instance representing an in-game item.
Interface defining an abstract GUI element.
Definition: GUIElement.java:32
static final long serialVersionUID
The serial version UID.
void paintComponent(@NotNull final Graphics g)
CfItem getItem()
Returns the current item instance.
final FacesManagerListener facesManagerListener
The FacesManagerListener registered to detect updated faces.
Interface for listeners interested in FacesManager events.
final FacesManager facesManager
The FacesManager instance to use.
void setTooltipText(@Nullable final String tooltipText)
Sets the tooltip text to show when the mouse is inside this element.the text to show ornull to disab...
abstract Image getFace(@NotNull final CfItem item)
Returns the face for a CfItem instance.
abstract boolean isSelected()
Returns whether this element should drawn as "selected".
Interface for listeners for attribute changes of CfItems.
Dimension getMinimumSize()
Returns the minimal size needed to display this item.
void setItem(@Nullable final CfItem item)
Sets the current item instance.
final ItemPainter itemPainter
The ItemPainter for painting the icon.
abstract void setIndexNoListeners(final int index)
Sets the slot index to display without registering listeners for updates.
void removeFacesManagerListener(@NotNull FacesManagerListener facesManagerListener)
Removes a FacesManagerListener to be notified about updated faces.
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
void addFacesManagerListener(@NotNull FacesManagerListener facesManagerListener)
Adds a FacesManagerListener to be notified about updated faces.
void updateTooltipText()
Updates the tooltip text for the current item.
Maintains a mapping of face numbers to face data.