Crossfire JXClient, Trunk  R20561
InventoryView.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.items;
23 
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.List;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
37 public class InventoryView extends AbstractItemView {
38 
42  @NotNull
43  private final ItemSet itemSet;
44 
48  @NotNull
49  private final Comparator<CfItem> comparator;
50 
54  private int currentPlayerTag = -1;
55 
59  @NotNull
60  private final List<CfItem> items = new ArrayList<>();
61 
65  @NotNull
66  private final DoubleMapping mapping = new DoubleMapping();
67 
72  @NotNull
73  @SuppressWarnings("FieldCanBeLocal")
75 
76  @Override
77  public void itemAdded(@NotNull final CfItem item) {
78  // ignore
79  }
80 
81  @Override
82  public void itemMoved(@NotNull final CfItem item) {
83  // ignore
84  }
85 
86  @Override
87  public void itemChanged(@NotNull final CfItem item) {
88  // ignore
89  }
90 
91  @Override
92  public void itemRemoved(@NotNull final CfItem item) {
93  // ignore
94  }
95 
96  @Override
97  public void playerChanged(@Nullable final CfItem player) {
98  setCurrentPlayerTag(player == null ? -1 : player.getTag());
99  }
100 
101  @Override
102  public void openContainerChanged(final int tag) {
103  // ignore
104  }
105 
106  };
107 
111  @NotNull
113 
114  @Override
115  public void itemChanged(final int tag) {
116  // ignore
117  }
118 
119  @Override
120  public void itemRemoved(final int tag) {
121  // ignore; will be detected by itemSetListener
122  }
123 
124  @Override
125  public void inventoryAdded(final int tag, final int index, @NotNull final CfItem item) {
126  assert tag == currentPlayerTag;
127 
128  final int index2 = findInsertionIndex(item);
129  mapping.insert(index2, index);
130  items.add(index2, item);
131  addModifiedRange(index2, itemSet.getNumberOfItemsByLocation(tag)-1);
132  }
133 
134  @Override
135  public void inventoryRemoved(final int tag, final int index) {
136  assert tag == currentPlayerTag;
137 
138  final int index2 = mapping.getSrc(index);
139  mapping.remove(index2);
140  items.remove(index2);
141  addModifiedRange(index2, itemSet.getNumberOfItemsByLocation(tag));
142  }
143 
144  };
145 
151  public InventoryView(@NotNull final ItemSet itemSet, @NotNull final Comparator<CfItem> comparator) {
152  this.itemSet = itemSet;
153  this.comparator = comparator;
154  itemSet.addItemSetListener(itemSetListener);
155  final CfItem player = itemSet.getPlayer();
156  currentPlayerTag = player == null ? -1 : player.getTag();
157  }
158 
163  private void setCurrentPlayerTag(final int currentPlayerTag) {
164  final int prevSize;
165  if (this.currentPlayerTag == -1) {
166  prevSize = 0;
167  } else {
168  itemSet.removeInventoryListener(this.currentPlayerTag, playerInventoryListener);
169  prevSize = itemSet.getNumberOfItemsByLocation(currentPlayerTag);
170  }
171  this.currentPlayerTag = currentPlayerTag;
172  final int nextSize;
173  if (this.currentPlayerTag == -1) {
174  nextSize = 0;
175  } else {
176  itemSet.addInventoryListener(this.currentPlayerTag, playerInventoryListener);
177  nextSize = itemSet.getNumberOfItemsByLocation(currentPlayerTag);
178  }
179 
180  items.clear();
181  mapping.clear();
182  if (currentPlayerTag != -1) {
183  for (int i = 0; i < nextSize; i++) {
184  final CfItem item = itemSet.getInventoryItem(currentPlayerTag, i);
185  assert item != null;
186  playerInventoryListener.inventoryAdded(currentPlayerTag, i, item);
187  }
188  }
189  addModifiedRange(nextSize, prevSize-1);
190  }
191 
195  @Override
196  public int getSize() {
197  return items.size();
198  }
199 
203  @Nullable
204  @Override
205  public CfItem getItem(final int index) {
206  if (index < 0) {
207  return null;
208  }
209 
210  try {
211  return items.get(index);
212  } catch (final IndexOutOfBoundsException ignored) {
213  return null;
214  }
215  }
216 
222  private int findInsertionIndex(@NotNull final CfItem item) {
223  int i;
224  for (i = 0; i < items.size(); i++) {
225  if (comparator.compare(items.get(i), item) >= 0) {
226  break;
227  }
228  }
229  return i;
230  }
231 
232 }
int findInsertionIndex(@NotNull final CfItem item)
Returns the insertion index of a CfItem.
CfItem getItem(final int index)
Returns the CfItem in a given slot.the slot index the item ornull if the slot is empty ...
CfItem getInventoryItem(final int tag, final int index)
Returns a CfItem from the inventory of an item.
Definition: ItemSet.java:447
Provides a view of all items in the current player&#39;s inventory.
int currentPlayerTag
The tag of the current player object or.
int getNumberOfItemsByLocation(final int location)
Returns the number of items in a given location.
Definition: ItemSet.java:144
final ItemListener playerInventoryListener
The ItemListener attached to the current player object.
Interface for listeners for changes of item locations.
final ItemSetListener itemSetListener
The ItemSetListener attached to itemSet to track the current player object.
final List< CfItem > items
The items in the inventory ordered by comparator.
int getSrc(final int dst)
Returns a mapping.
final Comparator< CfItem > comparator
The Comparator for sorting.
void removeInventoryListener(final int tag, @NotNull final ItemListener listener)
Removes an ItemListener to be notified about changes.
Definition: ItemSet.java:119
InventoryView(@NotNull final ItemSet itemSet, @NotNull final Comparator< CfItem > comparator)
Creates a new instance.
Interface for listeners in ItemSet related events.
void inventoryAdded(int tag, int index, @NotNull CfItem item)
An inventory CfItem has been added to the watched item.
void addModifiedRange(final int firstIndex, final int lastIndex)
Marks a range of slots as modified.
void addInventoryListener(final int tag, @NotNull final ItemListener listener)
Adds an ItemListener to be notified about changes.
Definition: ItemSet.java:110
final DoubleMapping mapping
Maps external index to original index.
void insert(final int src, final int dst)
Adds a mapping.
final ItemSet itemSet
The ItemSet to monitor.
Maintains a bidirectional mapping from a set of integers to the same range of integers.
int getSize()
Returns the number of items.the number of items
Abstract base class for ItemView implementing classes.
Model class maintaining the CfItems known to the player.
Definition: ItemSet.java:43
void setCurrentPlayerTag(final int currentPlayerTag)
Updates the current player object.
void remove(final int src)
Removes a mapping.
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36