Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.items;
24 
27 import java.util.Collection;
28 import java.util.HashSet;
29 import org.jetbrains.annotations.NotNull;
30 
35 public abstract class AbstractItemView implements ItemView {
36 
41  @NotNull
43 
48  @NotNull
50 
54  @NotNull
55  private final Collection<Integer> modifiedSlots = new HashSet<>();
56 
60  @NotNull
61  private final Object sync = new Object();
62 
68  @NotNull
69  private final EventScheduler fireEventScheduler = new EventScheduler(100, 1, this::deliverEvents);
70 
74  protected AbstractItemView() {
76  }
77 
78  @Override
79  public void addLocationsListener(@NotNull final LocationsListener locationsListener) {
80  locationsListeners.add(locationsListener);
81  }
82 
83  @Override
84  public void removeLocationsListener(@NotNull final LocationsListener locationsListener) {
85  locationsListeners.remove(locationsListener);
86  }
87 
88  @Override
89  public void addLocationListener(final int index, @NotNull final LocationListener locationListener) {
90  locationListeners.add(index, locationListener);
91  }
92 
93  @Override
94  public void removeLocationListener(final int index, @NotNull final LocationListener locationListener) {
95  locationListeners.remove(index, locationListener);
96  }
97 
103  protected void addModifiedRange(final int firstIndex, final int lastIndex) {
104  synchronized (sync) {
105  for (int i = firstIndex; i <= lastIndex; i++) {
106  modifiedSlots.add(i);
107  }
108  }
109  fireEvents();
110  }
111 
116  protected void addModified(final int index) {
117  synchronized (sync) {
118  modifiedSlots.add(index);
119  }
120  fireEvents();
121  }
122 
126  private void deliverEvents() {
127  final Integer[] tmpModifiedSlots;
128  synchronized (sync) {
129  tmpModifiedSlots = modifiedSlots.toArray(new Integer[modifiedSlots.size()]);
130  modifiedSlots.clear();
131  }
132  if (tmpModifiedSlots.length > 0) {
133  for (LocationsListener locationsListener : locationsListeners) {
134  locationsListener.locationsModified(tmpModifiedSlots);
135  }
136  for (int index : tmpModifiedSlots) {
137  for (LocationListener locationListener : locationListeners.getListeners(index)) {
138  locationListener.locationChanged();
139  }
140  }
141  }
142  }
143 
147  private void fireEvents() {
149  }
150 
151 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.items.AbstractItemView.deliverEvents
void deliverEvents()
Definition: AbstractItemView.java:126
com.realtime.crossfire.jxclient.items.LocationsListener
Definition: LocationsListener.java:33
com.realtime.crossfire.jxclient.items.AbstractItemView
Definition: AbstractItemView.java:35
com.realtime.crossfire.jxclient.items.AbstractItemView.removeLocationsListener
void removeLocationsListener(@NotNull final LocationsListener locationsListener)
Definition: AbstractItemView.java:84
com.realtime.crossfire.jxclient.items.AbstractItemView.fireEvents
void fireEvents()
Definition: AbstractItemView.java:147
com.realtime.crossfire.jxclient.items.AbstractItemView.fireEventScheduler
final EventScheduler fireEventScheduler
Definition: AbstractItemView.java:69
com.realtime.crossfire.jxclient.util.EventListenerList2
Definition: EventListenerList2.java:37
com.realtime.crossfire.jxclient.items.AbstractItemView.removeLocationListener
void removeLocationListener(final int index, @NotNull final LocationListener locationListener)
Definition: AbstractItemView.java:94
com.realtime.crossfire.jxclient.items.AbstractItemView.addModifiedRange
void addModifiedRange(final int firstIndex, final int lastIndex)
Definition: AbstractItemView.java:103
com.realtime.crossfire.jxclient.items.AbstractItemView.addModified
void addModified(final int index)
Definition: AbstractItemView.java:116
com.realtime.crossfire.jxclient.items.AbstractItemView.addLocationsListener
void addLocationsListener(@NotNull final LocationsListener locationsListener)
Definition: AbstractItemView.java:79
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.items.EventScheduler.start
void start()
Definition: EventScheduler.java:139
com.realtime.crossfire.jxclient.items.EventScheduler
Definition: EventScheduler.java:35
com.realtime.crossfire.jxclient.items.AbstractItemView.locationsListeners
final EventListenerList2< LocationsListener > locationsListeners
Definition: AbstractItemView.java:42
com.realtime.crossfire.jxclient.items.EventScheduler.trigger
void trigger()
Definition: EventScheduler.java:146
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.util.IndexedEventListenerList
Definition: IndexedEventListenerList.java:30
com.realtime.crossfire.jxclient.items.AbstractItemView.addLocationListener
void addLocationListener(final int index, @NotNull final LocationListener locationListener)
Definition: AbstractItemView.java:89
com.realtime.crossfire.jxclient.items.ItemView
Definition: ItemView.java:32
com.realtime.crossfire.jxclient.items.AbstractItemView.sync
final Object sync
Definition: AbstractItemView.java:61
com.realtime.crossfire.jxclient.items.AbstractItemView.modifiedSlots
final Collection< Integer > modifiedSlots
Definition: AbstractItemView.java:55
com.realtime.crossfire.jxclient.items.AbstractItemView.AbstractItemView
AbstractItemView()
Definition: AbstractItemView.java:74
com.realtime.crossfire.jxclient.items.AbstractItemView.locationListeners
final IndexedEventListenerList< LocationListener > locationListeners
Definition: AbstractItemView.java:49
com.realtime.crossfire.jxclient.items.LocationListener
Definition: LocationListener.java:31