Crossfire JXClient, Trunk  R20561
GUIItemInventory.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 
35 import java.awt.Image;
36 import javax.swing.SwingUtilities;
37 import org.jetbrains.annotations.NotNull;
38 
43 public class GUIItemInventory extends GUIItemItem {
44 
48  private static final long serialVersionUID = 1;
49 
53  @NotNull
54  private final CommandQueue commandQueue;
55 
59  @NotNull
61 
65  @NotNull
66  private final FacesManager facesManager;
67 
71  @NotNull
72  private final FloorView floorView;
73 
77  @NotNull
78  private final ItemView inventoryView;
79 
83  private final int defaultIndex;
84 
88  @NotNull
89  private final Object sync = new Object();
90 
94  private int index = -1;
95 
99  private boolean selected;
100 
105  @NotNull
107 
108  @Override
109  public void locationChanged() {
110  SwingUtilities.invokeLater(locationListenerEdt);
111  }
112 
113  };
114 
118  @NotNull
119  private final Runnable locationListenerEdt = () -> {
120  setChanged();
122  };
123 
138  public GUIItemInventory(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final CommandQueue commandQueue, final String name, @NotNull final ItemPainter itemPainter, final int index, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final FacesManager facesManager, @NotNull final FloorView floorView, @NotNull final ItemView inventoryView, final int size) {
139  super(tooltipManager, elementListener, name, itemPainter, facesManager);
140  this.commandQueue = commandQueue;
141  this.crossfireServerConnection = crossfireServerConnection;
142  this.facesManager = facesManager;
143  this.floorView = floorView;
144  defaultIndex = index;
145  this.inventoryView = inventoryView;
146  setIndex(index);
147  if (size != 0) {
148  setSize(size, size);
149  }
150  }
151 
155  @Override
156  public void dispose() {
157  super.dispose();
158  setIndex(-1);
159  }
160 
164  @Override
165  public boolean canScroll(final int distance) {
166  if (distance < 0) {
167  synchronized (sync) {
168  return index >= -distance;
169  }
170  }
171  if (distance > 0) {
172  synchronized (sync) {
173  return index+distance < inventoryView.getSize();
174  }
175  }
176  return false;
177  }
178 
182  @Override
183  public void scroll(final int distance) {
184  synchronized (sync) {
185  setIndex(index+distance);
186  }
187  setChanged();
189  }
190 
194  @Override
195  public void resetScroll() {
196  setIndex(defaultIndex);
197  }
198 
202  @Override
203  public void button1Clicked(final int modifiers) {
204  final CfItem item = getItem();
205  if (item == null) {
206  return;
207  }
208 
209  switch (modifiers&Modifiers.MASK) {
210  case Modifiers.NONE:
211  crossfireServerConnection.sendExamine(item.getTag());
212  break;
213 
214  case Modifiers.SHIFT:
215  crossfireServerConnection.sendLock(!item.isLocked(), item.getTag());
216  break;
217 
218  case Modifiers.CTRL:
219  crossfireServerConnection.sendApply(item.getTag());
220  break;
221 
223  crossfireServerConnection.sendMark(item.getTag());
224  break;
225  }
226  }
227 
231  @Override
232  public void button2Clicked(final int modifiers) {
233  final CfItem item = getItem();
234  if (item == null) {
235  return;
236  }
237 
238  switch (modifiers&Modifiers.MASK) {
239  case Modifiers.NONE:
240  crossfireServerConnection.sendApply(item.getTag());
241  break;
242 
243  case Modifiers.SHIFT:
244  crossfireServerConnection.sendMark(item.getTag());
245  break;
246  }
247  }
248 
252  @Override
253  public void button3Clicked(final int modifiers) {
254  final CfItem item = getItem();
255  if (item == null) {
256  return;
257  }
258 
259  switch (modifiers&Modifiers.MASK) {
260  case Modifiers.NONE:
261  if (item.isLocked()) {
262  crossfireServerConnection.drawInfo("This item is locked. To drop it, first unlock by SHIFT+left-clicking on it.", 3);
263  return;
264  }
265 
266  commandQueue.sendMove(floorView.getCurrentFloor(), item.getTag());
267  break;
268 
269  case Modifiers.SHIFT:
270  crossfireServerConnection.sendApply(item.getTag());
271  break;
272  }
273  }
274 
278  @Override
279  public void setSelected(final boolean selected) {
280  if (this.selected == selected) {
281  return;
282  }
283 
284  this.selected = selected;
285  setChanged();
286  }
287 
291  @Override
292  protected boolean isSelected() {
293  return selected || isActive();
294  }
295 
299  @Override
300  public int getIndex() {
301  synchronized (sync) {
302  return index;
303  }
304  }
305 
310  private void setIndex(final int index) {
311  synchronized (sync) {
312  if (this.index == index) {
313  return;
314  }
315 
316  if (this.index >= 0) {
317  inventoryView.removeLocationListener(this.index, locationListener);
318  }
319  this.index = index;
320  if (this.index >= 0) {
321  inventoryView.addLocationListener(this.index, locationListener);
322  }
323  }
324 
325  setItem(inventoryView.getItem(this.index));
326  }
327 
331  @Override
332  public void setIndexNoListeners(final int index) {
333  synchronized (sync) {
334  this.index = index;
335  }
336 
337  setItemNoListeners(inventoryView.getItem(this.index));
338  }
339 
343  @NotNull
344  @Override
345  protected Image getFace(@NotNull final CfItem item) {
346  return facesManager.getOriginalImageIcon(item.getFace().getFaceNum(), null).getImage();
347  }
348 
352  @Override
353  public void setChanged() {
354  super.setChanged();
355  setItem(inventoryView.getItem(index));
356  }
357 
358 }
boolean isLocked()
Returns whether this item is locked.
Definition: CfItem.java:390
ImageIcon getOriginalImageIcon(int faceNum, @Nullable boolean[] isUnknownImage)
Returns the "original" face for a face ID.
void sendMove(final int to, final int tag)
Sends a "move" command to the server.
final TooltipManager tooltipManager
The TooltipManager to update.
final ItemView inventoryView
The inventory view to watch.
A GUIItem for displaying inventory objects.
Interface for listeners interested in changed items.
final GUIElementListener elementListener
The GUIElementListener to notify.
boolean canScroll(final int distance)
Returns whether scrolling is possible.the distance to scroll whether scrolling is possible ...
static final int SHIFT
The mask for "shift".
Definition: Modifiers.java:40
void sendLock(boolean val, int tag)
Sends a "lock" command to the server.
final Object sync
The object used for synchronization on index.
GUIItemInventory(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final CommandQueue commandQueue, final String name, @NotNull final ItemPainter itemPainter, final int index, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final FacesManager facesManager, @NotNull final FloorView floorView, @NotNull final ItemView inventoryView, final int size)
Creates a new instance.
void resetScroll()
Resets the scroll index to the default value.
final Runnable locationListenerEdt
Called from locationListener but runs on the EDT.
Interface for listeners for changes of item locations.
void sendMark(int tag)
Sends a "mark" command to the server.
void setItemNoListeners(@Nullable final CfItem item)
Sets the current item instance without registering listeners for updates.
Manages image information ("faces") needed to display the map view, items, and spell icons...
A GUIElement instance representing an in-game item.
void scroll(final int distance)
Scrolls the element.the distance to scroll
void drawInfo(@NotNull String message, int color)
Pretends that a drawinfo message has been received.
void dispose()
Releases all allocated resources.
Helper functions for keyboard modifiers.
Definition: Modifiers.java:30
final CrossfireServerConnection crossfireServerConnection
The server instance.
boolean selected
If set, paint the element in "selected" state.
CfItem getItem()
Returns the current item instance.
void setChanged()
Records that the contents have changed and must be repainted.
void setIndex(final int index)
Set the inventory slot to display.
int getSize()
Returns the number of items.
void addLocationListener(int index, @NotNull LocationListener locationListener)
Adds a LocationListener to be notified when the item displayed in a floor slot has changed...
static final int CTRL
The mask for "ctrl".
Definition: Modifiers.java:45
void removeLocationListener(int index, @NotNull LocationListener locationListener)
Removes a LocationListener to be notified when the item displayed in a floor slot has changed...
final LocationListener locationListener
The ItemListener used to detect items added to or removed from this inventory slot.
static final int NONE
The mask for "no modifier".
Definition: Modifiers.java:35
void setItem(@Nullable final CfItem item)
Sets the current item instance.
static final int MASK
The mask for all used modifiers.
Definition: Modifiers.java:55
final ItemPainter itemPainter
The ItemPainter for painting the icon.
static final long serialVersionUID
The serial version UID.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
CfItem getItem(int index)
Returns the CfItem in a given slot.
Maintains the pending (ncom) commands sent to the server.
void sendApply(int tag)
Sends an "apply" command to the server.
void sendExamine(int tag)
Sends an "examine" command to the server.
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
final FacesManager facesManager
The FacesManager instance to use.
final CommandQueue commandQueue
The command queue for sending commands.
int getCurrentFloor()
Returns the current floor location.
Definition: FloorView.java:135
boolean isActive()
Returns whether a GUI element is active.
Provides a view to all items comprising the current floor view.
Definition: FloorView.java:35
void updateTooltipText()
Updates the tooltip text for the current item.
Maintains a mapping of face numbers to face data.