Crossfire JXClient, Trunk  R20561
FloorView.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 
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26 
35 public class FloorView extends AbstractItemView {
36 
40  @NotNull
41  private final ItemSet itemSet;
42 
47  private int currentFloor;
48 
52  @NotNull
53  @SuppressWarnings("FieldCanBeLocal")
55 
56  @Override
57  public void itemAdded(@NotNull final CfItem item) {
58  // ignore
59  }
60 
61  @Override
62  public void itemMoved(@NotNull final CfItem item) {
63  // ignore
64  }
65 
66  @Override
67  public void itemChanged(@NotNull final CfItem item) {
68  // ignore
69  }
70 
71  @Override
72  public void itemRemoved(@NotNull final CfItem item) {
73  // ignore
74  }
75 
76  @Override
77  public void playerChanged(@Nullable final CfItem player) {
78  // ignore
79  }
80 
81  @Override
82  public void openContainerChanged(final int tag) {
83  setCurrentFloor(tag);
84  }
85 
86  };
87 
91  @NotNull
92  private final ItemListener itemListener = new ItemListener() {
93 
94  @Override
95  public void itemChanged(final int tag) {
96  if (currentFloor != 0) {
97  addModified(0);
98  }
99  }
100 
101  @Override
102  public void itemRemoved(final int tag) {
103  setCurrentFloor(0);
104  }
105 
106  @Override
107  public void inventoryAdded(final int tag, final int index, @NotNull final CfItem item) {
108  final int offset = getOffset();
109  addModifiedRange(index+offset, itemSet.getNumberOfItemsByLocation(tag)-1+offset);
110  }
111 
112  @Override
113  public void inventoryRemoved(final int tag, final int index) {
114  final int offset = getOffset();
115  addModifiedRange(index+offset, itemSet.getNumberOfItemsByLocation(tag)+offset);
116  }
117 
118  };
119 
124  public FloorView(@NotNull final ItemSet itemSet) {
125  this.itemSet = itemSet;
126  itemSet.addInventoryListener(currentFloor, itemListener);
127  setCurrentFloor(itemSet.getOpenContainer());
128  itemSet.addItemSetListener(itemSetListener);
129  }
130 
135  public int getCurrentFloor() {
136  return currentFloor;
137  }
138 
144  private void setCurrentFloor(final int currentFloor) {
145  if (this.currentFloor == currentFloor) {
146  return;
147  }
148 
149  final int prevLastIndex = getSize()-1;
150  itemSet.removeInventoryListener(this.currentFloor, itemListener);
151  this.currentFloor = currentFloor;
152  itemSet.addInventoryListener(this.currentFloor, itemListener);
153  final int nextLastIndex = getSize()-1;
154  addModifiedRange(0, Math.max(prevLastIndex, nextLastIndex));
155  }
156 
160  @Override
161  public int getSize() {
162  return itemSet.getNumberOfItemsByLocation(currentFloor)+getOffset();
163  }
164 
168  @Nullable
169  @Override
170  public CfItem getItem(final int index) {
171  final int index2;
172  if (currentFloor == 0) {
173  index2 = index;
174  } else {
175  if (index == 0) {
176  return itemSet.getItemByTag(currentFloor);
177  }
178 
179  index2 = index-1;
180  }
181 
182  return itemSet.getInventoryItem(currentFloor, index2);
183  }
184 
191  private int getOffset() {
192  return currentFloor == 0 ? 0 : 1;
193  }
194 
195 }
int getSize()
Returns the number of items.the number of items
Definition: FloorView.java:161
final ItemSetListener itemSetListener
The ItemSetListener for detecting opened or closed containers.
Definition: FloorView.java:54
CfItem getInventoryItem(final int tag, final int index)
Returns a CfItem from the inventory of an item.
Definition: ItemSet.java:447
int currentFloor
The tag of the currently shown container or.
Definition: FloorView.java:47
int getNumberOfItemsByLocation(final int location)
Returns the number of items in a given location.
Definition: ItemSet.java:144
Interface for listeners for changes of item locations.
void addModified(final int index)
Marks a slot as modified.
CfItem getItemByTag(final int tag)
Returns an item by tag.
Definition: ItemSet.java:275
void removeInventoryListener(final int tag, @NotNull final ItemListener listener)
Removes an ItemListener to be notified about changes.
Definition: ItemSet.java:119
void setCurrentFloor(final int currentFloor)
Updates the currently opened container that's contents are shown in the floor view.
Definition: FloorView.java:144
CfItem getItem(final int index)
Returns the CfItem in a given slot.the slot index the item ornull if the slot is empty ...
Definition: FloorView.java:170
Interface for listeners in ItemSet related events.
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 ItemListener itemListener
The ItemListener attached to the current floor object.
Definition: FloorView.java:92
final ItemSet itemSet
The ItemSet to monitor.
Definition: FloorView.java:41
Abstract base class for ItemView implementing classes.
Model class maintaining the CfItems known to the player.
Definition: ItemSet.java:43
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
int getCurrentFloor()
Returns the current floor location.
Definition: FloorView.java:135
FloorView(@NotNull final ItemSet itemSet)
Creates a new instance.
Definition: FloorView.java:124
Provides a view to all items comprising the current floor view.
Definition: FloorView.java:35
int getOffset()
Returns the number of non-inventory items to be displayed on the floor.
Definition: FloorView.java:191