Crossfire JXClient, Trunk  R20561
AbstractItemView.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 
26 import java.util.Collection;
27 import java.util.HashSet;
28 import org.jetbrains.annotations.NotNull;
29 
34 public abstract class AbstractItemView implements ItemView {
35 
40  @NotNull
42 
47  @NotNull
49 
53  @NotNull
54  private final Collection<Integer> modifiedSlots = new HashSet<>();
55 
59  @NotNull
60  private final Object sync = new Object();
61 
67  @NotNull
68  private final Runnable fireEventCallback = this::deliverEvents;
69 
73  protected AbstractItemView() {
75  }
76 
80  @NotNull
81  private final EventScheduler fireEventScheduler = new EventScheduler(100, 1, fireEventCallback);
82 
86  @Override
87  public void addLocationsListener(@NotNull final LocationsListener locationsListener) {
88  locationsListeners.add(locationsListener);
89  }
90 
94  @Override
95  public void removeLocationsListener(@NotNull final LocationsListener locationsListener) {
96  locationsListeners.remove(locationsListener);
97  }
98 
102  @Override
103  public void addLocationListener(final int index, @NotNull final LocationListener locationListener) {
104  locationListeners.add(index, locationListener);
105  }
106 
110  @Override
111  public void removeLocationListener(final int index, @NotNull final LocationListener locationListener) {
112  locationListeners.remove(index, locationListener);
113  }
114 
120  protected void addModifiedRange(final int firstIndex, final int lastIndex) {
121  synchronized (sync) {
122  for (int i = firstIndex; i <= lastIndex; i++) {
123  modifiedSlots.add(i);
124  }
125  }
126  fireEvents();
127  }
128 
133  protected void addModified(final int index) {
134  synchronized (sync) {
135  modifiedSlots.add(index);
136  }
137  fireEvents();
138  }
139 
143  private void deliverEvents() {
144  final Integer[] tmpModifiedSlots;
145  synchronized (sync) {
146  tmpModifiedSlots = modifiedSlots.toArray(new Integer[modifiedSlots.size()]);
147  modifiedSlots.clear();
148  }
149  if (tmpModifiedSlots.length > 0) {
150  for (final LocationsListener locationsListener : locationsListeners) {
151  locationsListener.locationsModified(tmpModifiedSlots);
152  }
153  for (final int index : tmpModifiedSlots) {
154  for (final LocationListener locationListener : locationListeners.getListeners(index)) {
155  locationListener.locationChanged();
156  }
157  }
158  }
159  }
160 
164  private void fireEvents() {
165  fireEventScheduler.trigger();
166  }
167 
168 }
final Collection< Integer > modifiedSlots
The pending modified floor slots to be reported to listeners.
void fireEvents()
Delivers outstanding change events.
final Runnable fireEventCallback
The event scheduler callback for delaying event generation.
Interface for listeners interested in changed items.
void remove(final int index, @NotNull final T listener)
Removes a listener.
void removeLocationsListener(@NotNull final LocationsListener locationsListener)
Removes a LocationsListener to be notified when any displayed item has changed.the locations listener...
void addModified(final int index)
Marks a slot as modified.
final IndexedEventListenerList< LocationListener > locationListeners
The registered ItemListeners to be notified about changes.
final Object sync
The synchronization object for accesses to modifiedSlots.
void removeLocationListener(final int index, @NotNull final LocationListener locationListener)
Removes a LocationListener to be notified when the item displayed in a floor slot has changed...
final EventListenerList2< LocationsListener > locationsListeners
The registered LocationsListeners to be notified about changes.
A scheduler for synchronous event notifications.
Interface for listeners interested in changed item locations.
void addModifiedRange(final int firstIndex, final int lastIndex)
Marks a range of slots as modified.
Iterable< T > getListeners(final int index)
Returns all the listeners by index.
void add(@NotNull final T listener)
Adds a listener.
void addLocationsListener(@NotNull final LocationsListener locationsListener)
Adds a LocationsListener to be notified when any displayed item has changed.the locations listener to...
void addLocationListener(final int index, @NotNull final LocationListener locationListener)
Adds a LocationListener to be notified when the item displayed in a floor slot has changed...
void add(final int index, @NotNull final T listener)
Adds a listener.
Abstract base class for ItemView implementing classes.
void remove(@NotNull final T listener)
Removes a listener.
final EventScheduler fireEventScheduler
The EventScheduler for delaying event generation.