00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.items;
00023
00024 import com.realtime.crossfire.jxclient.util.EventListenerList2;
00025 import com.realtime.crossfire.jxclient.util.IndexedEventListenerList;
00026 import java.util.Collection;
00027 import java.util.HashSet;
00028 import org.jetbrains.annotations.NotNull;
00029
00034 public abstract class AbstractItemView implements ItemView {
00035
00040 @NotNull
00041 private final EventListenerList2<LocationsListener> locationsListeners = new EventListenerList2<LocationsListener>(LocationsListener.class);
00042
00047 @NotNull
00048 private final IndexedEventListenerList<LocationListener> locationListeners = new IndexedEventListenerList<LocationListener>(LocationListener.class);
00049
00053 @NotNull
00054 private final Collection<Integer> modifiedSlots = new HashSet<Integer>();
00055
00059 @NotNull
00060 private final Object sync = new Object();
00061
00067 @NotNull
00068 private final Runnable fireEventCallback = new Runnable() {
00069
00070 @Override
00071 public void run() {
00072 deliverEvents();
00073 }
00074
00075 };
00076
00080 protected AbstractItemView() {
00081 fireEventScheduler.start();
00082 }
00083
00087 @NotNull
00088 private final EventScheduler fireEventScheduler = new EventScheduler(100, 1, fireEventCallback);
00089
00093 @Override
00094 public void addLocationsListener(@NotNull final LocationsListener locationsListener) {
00095 locationsListeners.add(locationsListener);
00096 }
00097
00101 @Override
00102 public void removeLocationsListener(@NotNull final LocationsListener locationsListener) {
00103 locationsListeners.remove(locationsListener);
00104 }
00105
00109 @Override
00110 public void addLocationListener(final int index, @NotNull final LocationListener locationListener) {
00111 locationListeners.add(index, locationListener);
00112 }
00113
00117 @Override
00118 public void removeLocationListener(final int index, @NotNull final LocationListener locationListener) {
00119 locationListeners.remove(index, locationListener);
00120 }
00121
00127 protected void addModifiedRange(final int firstIndex, final int lastIndex) {
00128 synchronized (sync) {
00129 for (int i = firstIndex; i <= lastIndex; i++) {
00130 modifiedSlots.add(i);
00131 }
00132 }
00133 fireEvents();
00134 }
00135
00140 protected void addModified(final int index) {
00141 synchronized (sync) {
00142 modifiedSlots.add(index);
00143 }
00144 fireEvents();
00145 }
00146
00150 private void deliverEvents() {
00151 final Integer[] tmpModifiedSlots;
00152 synchronized (sync) {
00153 tmpModifiedSlots = modifiedSlots.toArray(new Integer[modifiedSlots.size()]);
00154 modifiedSlots.clear();
00155 }
00156 if (tmpModifiedSlots.length > 0) {
00157 for (final LocationsListener locationsListener : locationsListeners.getListeners()) {
00158 locationsListener.locationsModified(tmpModifiedSlots);
00159 }
00160 for (final int index : tmpModifiedSlots) {
00161 for (final LocationListener locationListener : locationListeners.getListeners(index)) {
00162 locationListener.locationChanged();
00163 }
00164 }
00165 }
00166 }
00167
00171 private void fireEvents() {
00172 fireEventScheduler.trigger();
00173 }
00174
00175 }