Crossfire JXClient, Trunk  R20561
GUIItemQuest.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) 2011 Nicolas Weeger
19  */
20 
21 package com.realtime.crossfire.jxclient.gui.item;
22 
35 import java.awt.Dimension;
36 import java.awt.Image;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
44 public class GUIItemQuest extends GUIItemItem {
45 
49  private static final long serialVersionUID = 1;
50 
54  @NotNull
55  private final Object sync = new Object();
56 
60  @NotNull
61  private final FacesManager facesManager;
62 
66  private final int defaultIndex;
67 
71  @NotNull
73 
77  @Nullable
78  private Quest quest;
79 
84  private int index = -1;
85 
89  private boolean selected;
90 
94  @NotNull
95  private final ItemView questsView;
96 
100  @NotNull
101  private final QuestsManagerListener questsManagerListener = index1 -> {
102  if (index >= index1) {
103  setQuest();
104  }
105  };
106 
110  @NotNull
111  private final QuestListener questListener = this::setQuest;
112 
116  @NotNull
118 
119  @Override
120  public void faceUpdated(@NotNull final Face face) {
121  if (quest != null && quest.getFace() == face.getFaceNum()) {
122  setChanged();
123  }
124  }
125 
126  };
127 
140  public GUIItemQuest(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ItemPainter itemPainter, final int defaultIndex, @NotNull final FacesManager facesManager, @NotNull final QuestsManager questsManager, @NotNull final ItemView questsView, final int size) {
141  super(tooltipManager, elementListener, name, itemPainter, facesManager);
142  this.facesManager = facesManager;
143  this.defaultIndex = defaultIndex;
144  this.questsManager = questsManager;
145  setIndex(defaultIndex);
146  this.questsManager.addCrossfireQuestChangedListener(questsManagerListener);
147  this.facesManager.addFacesManagerListener(facesManagerListener);
148  this.questsView = questsView;
149  if (size != 0) {
150  setSize(size, size);
151  }
152  }
153 
157  @Override
158  public void dispose() {
159  super.dispose();
160  questsManager.removeCrossfireQuestChangedListener(questsManagerListener);
161  facesManager.removeFacesManagerListener(facesManagerListener);
162  if (quest != null) {
163  quest.removeQuestListener(questListener);
164  }
165  }
166 
170  @Override
171  public boolean canScroll(final int distance) {
172  if (distance < 0) {
173  return index >= -distance;
174  }
175  //noinspection SimplifiableIfStatement
176  if (distance > 0) {
177  return index+distance < questsManager.getQuests();
178  }
179  return false;
180  }
181 
185  @Override
186  public void scroll(final int distance) {
187  setIndex(index+distance);
188  setChanged();
189  }
190 
194  @Override
195  public void resetScroll() {
196  setIndex(defaultIndex);
197  }
198 
202  @NotNull
203  @Override
204  public Dimension getPreferredSize() {
205  return getMinimumSizeInt();
206  }
207 
211  @NotNull
212  @Override
213  public Dimension getMinimumSize() {
214  return getMinimumSizeInt();
215  }
216 
221  @NotNull
222  private static Dimension getMinimumSizeInt() {
223  return new Dimension(32, 32);
224  }
225 
230  private void setQuest() {
231  final Quest newQuest = questsManager.getQuest(index);
232  if (quest == newQuest) {
233  return;
234  }
235 
236  if (quest != null) {
237  quest.removeQuestListener(questListener);
238  }
239 
240  quest = newQuest;
241 
242  if (quest != null) {
243  quest.addQuestListener(questListener);
244  }
245 
246  setChanged();
247 
248  setTooltipText(newQuest == null ? null : newQuest.getTooltipText());
249  }
250 
256  private void setIndex(final int index) {
257  if (this.index == index) {
258  return;
259  }
260  this.index = index;
261 
262  setQuest();
263  }
264 
268  @NotNull
269  @Override
270  protected Image getFace(@NotNull final CfItem item) {
271  return facesManager.getOriginalImageIcon(item.getFace().getFaceNum(), null).getImage();
272  }
273 
277  @Override
278  public void setSelected(final boolean selected) {
279  if (this.selected == selected) {
280  return;
281  }
282 
283  this.selected = selected;
284  setChanged();
285  }
286 
290  @Override
291  protected boolean isSelected() {
292  return selected || isActive();
293  }
294 
298  @Override
299  public int getIndex() {
300  synchronized (sync) {
301  return index;
302  }
303  }
304 
308  @Override
309  public void setIndexNoListeners(final int index) {
310  synchronized (sync) {
311  this.index = index;
312  }
313 
314  setItemNoListeners(questsView.getItem(this.index));
315  }
316 
320  @Override
321  public void button1Clicked(final int modifiers) {
322  }
323 
327  @Override
328  public void button2Clicked(final int modifiers) {
329  }
330 
334  @Override
335  public void button3Clicked(final int modifiers) {
336  }
337 
338 }
ImageIcon getOriginalImageIcon(int faceNum, @Nullable boolean[] isUnknownImage)
Returns the "original" face for a face ID.
void resetScroll()
Resets the scroll index to the default value.
final TooltipManager tooltipManager
The TooltipManager to update.
void setIndex(final int index)
Sets the index of the currently selected quest.
int index
The currently selected quest or.
Describes an in-game quest.
Definition: Quest.java:30
final GUIElementListener elementListener
The GUIElementListener to notify.
void scroll(final int distance)
Scrolls the element.the distance to scroll
void setChanged()
Records that the contents have changed and must be repainted.
A GUIItemItem that represents an entry in a GUIQuestList.
final FacesManager facesManager
The FacesManager for looking up faces.
final QuestListener questListener
The QuestListener attached to quest.
final int defaultIndex
The default scroll index.
Quest quest
The currently selected Quest, null if none.
void removeCrossfireQuestChangedListener(@NotNull final QuestsManagerListener listener)
Removes a QuestsManagerListener to notify about changes.
Interface for listeners interested in Quest related events.
void setItemNoListeners(@Nullable final CfItem item)
Sets the current item instance without registering listeners for updates.
boolean selected
Whether this element is selected in its GUIQuestList.
final QuestsManagerListener questsManagerListener
The QuestsManagerListener used to detect spell changes.
Manages image information ("faces") needed to display the map view, items, and spell icons...
static Dimension getMinimumSizeInt()
Returns the minimal size to display this component.
A GUIElement instance representing an in-game item.
int getQuests()
Returns the number of current quests.
void dispose()
Releases all allocated resources.
final FacesManagerListener facesManagerListener
The FacesManagerListener registered to detect updated faces.
final ItemView questsView
The spells view to use.
final QuestsManager questsManager
The QuestsManager instance to watch.
Interface for listeners interested in FacesManager events.
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...
boolean canScroll(final int distance)
Returns whether scrolling is possible.the distance to scroll whether scrolling is possible ...
int getFace()
Get the quest&#39;s face.
Definition: Quest.java:117
String getTooltipText()
Returns a description for this spell to be used in tooltips.
Definition: Quest.java:149
final ItemPainter itemPainter
The ItemPainter for painting the icon.
GUIItemQuest(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ItemPainter itemPainter, final int defaultIndex, @NotNull final FacesManager facesManager, @NotNull final QuestsManager questsManager, @NotNull final ItemView questsView, final int size)
Creates a new instance.
final Object sync
The object used for synchronization on index.
CfItem getItem(int index)
Returns the CfItem in a given slot.
Interface for listeners interested in QuestsManager events.
static final long serialVersionUID
The serial version UID.
void removeQuestListener(@NotNull final QuestListener listener)
Removes a QuestListener to be notified of changes.
Definition: Quest.java:186
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 addQuestListener(@NotNull final QuestListener listener)
Adds a QuestListener to be notified of changes.
Definition: Quest.java:178
Quest getQuest(final int index)
Returns a Quest instance by index.
void setQuest()
Sets the currently selected quest.
boolean isActive()
Returns whether a GUI element is active.
Maintains a mapping of face numbers to face data.