TrustGrapher
r52
A playabale simulator for modelling trust between agents
|
00001 00002 package cu.trustGrapher.eventplayer; 00003 00004 import cu.trustGrapher.OptionsWindow; 00005 import cu.trustGrapher.TrustGrapher; 00006 00007 import cu.trustGrapher.visualizer.GraphViewer; 00008 import java.awt.event.ActionEvent; 00009 import java.awt.event.ActionListener; 00010 import java.util.LinkedList; 00011 import javax.swing.Timer; 00012 import java.util.List; 00013 import aohara.utilities.ChatterBox; 00014 import cu.trustGrapher.graphs.SimAbstractGraph; 00015 00023 public final class EventPlayer implements ActionListener { 00024 00025 public static final int REVERSE = -1, PAUSE = 0, FORWARD = 1; 00026 public static final int DEFAULT_DELAY = 250, DEFUALT_EVENTS_PER_TICK = 1; // This many milliseconds between events while playing regularly 00027 private int state, currentEventIndex, eventsPerTick; 00028 protected TrustGrapher trustGrapher; 00029 protected Timer timer; //Invokes the EventPlayer actionPerormed() method every time the delay expires 00030 protected List<TrustLogEvent> events; //The list of TrustLogEvents that are played through 00031 protected List<EventPlayerListener> listeners; //These listeners listen to EventPlayer timeline updates and handle them in some way 00032 00034 public EventPlayer(TrustGrapher trustGrapher, List<TrustLogEvent> events) { 00035 this.trustGrapher = trustGrapher; 00036 this.events = events; 00037 listeners = new LinkedList<EventPlayerListener>(); 00038 currentEventIndex = 0; 00039 eventsPerTick = DEFUALT_EVENTS_PER_TICK; 00040 state = PAUSE; 00041 timer = new Timer(DEFAULT_DELAY, this); 00042 //Search for a custom delay in the properties file 00043 if (trustGrapher.getPropertyManager().containsKey(OptionsWindow.DELAY)) { 00044 try { 00045 setDelay(Integer.parseInt(trustGrapher.getPropertyManager().getProperty(OptionsWindow.DELAY))); 00046 } catch (NumberFormatException ex) { 00047 ChatterBox.alert("Invalid event delay property. Will continue, and set delay to defualt."); 00048 } 00049 } 00050 timer.start(); //Start the time counter 00051 } 00052 00054 public TrustGrapher getTrustGrapher() { 00055 return trustGrapher; 00056 } 00057 00058 public List<TrustLogEvent> getEvents() { 00059 return events; 00060 } 00061 00066 public int getPlayState() { 00067 return state; 00068 } 00069 00073 public int getCurrentEventIndex() { 00074 return currentEventIndex; 00075 } 00076 00080 public boolean atFront() { 00081 return currentEventIndex == 0; 00082 } 00083 00087 public boolean atBack() { 00088 return currentEventIndex == events.size() - 1; 00089 00090 } 00091 00095 public boolean atAnEnd() { 00096 return atFront() || atBack(); 00097 } 00098 00104 public PlaybackPanel getPlaybackPanel() { 00105 for (EventPlayerListener listener : listeners) { 00106 if (listener instanceof PlaybackPanel) { 00107 return (PlaybackPanel) listener; 00108 } 00109 } 00110 ChatterBox.error(this, "getPlaybackPanel()", "No playbackPanel was found. This should never occur."); 00111 return null; 00112 } 00113 00119 public LogPanel getLogPanel() { 00120 for (EventPlayerListener listener : listeners) { 00121 if (listener instanceof LogPanel) { 00122 return (LogPanel) listener; 00123 } 00124 } 00125 ChatterBox.error(this, "getLogPanel()", "No logPanel was found. This should never occur."); 00126 return null; 00127 } 00128 00130 00135 public void addEventPlayerListener(EventPlayerListener listener) { 00136 listeners.add(listener); 00137 } 00138 00143 public void setDelay(int value) { 00144 timer.setDelay(value); 00145 } 00146 00152 public void setEventsPerTick(int value) { 00153 eventsPerTick = value; 00154 } 00155 00160 public void reverse() { 00161 if (state != REVERSE) { 00162 int prevState = state; 00163 state = REVERSE; 00164 wakeup(prevState); 00165 } 00166 } 00167 00172 public void forward() { 00173 if (state != FORWARD) { 00174 int prevState = state; 00175 state = FORWARD; 00176 wakeup(prevState); 00177 } 00178 } 00179 00184 private synchronized void wakeup(int previousState) { 00185 if (previousState == PAUSE) { 00186 timer.start(); 00187 notify(); 00188 } 00189 } 00190 00195 public synchronized void pause() { 00196 if (state != PAUSE) { 00197 state = PAUSE; 00198 notify(); 00199 } 00200 } 00201 00208 @Override 00209 public void actionPerformed(ActionEvent event) { 00210 if (state != PAUSE) { 00211 goToEvent(currentEventIndex + (state == FORWARD ? eventsPerTick : -eventsPerTick)); 00212 } 00213 } 00214 00221 public void goToEvent(int newEventIndex) { 00222 if (currentEventIndex != newEventIndex) { 00223 if (newEventIndex < 0) { 00224 newEventIndex = 0; 00225 } else if (newEventIndex > events.size() - 1) { 00226 newEventIndex = events.size() - 1; 00227 } 00228 LinkedList<TrustLogEvent> eventsToProcess = new LinkedList<TrustLogEvent>(); 00229 boolean isForward = currentEventIndex < newEventIndex; 00230 if (isForward) { //Forward 00231 while (currentEventIndex < newEventIndex) { 00232 currentEventIndex++; 00233 eventsToProcess.add(events.get(currentEventIndex)); 00234 } 00235 } else { //Backward 00236 while (currentEventIndex > newEventIndex) { 00237 eventsToProcess.add(events.get(currentEventIndex)); 00238 currentEventIndex--; 00239 } 00240 } 00241 if (!eventsToProcess.isEmpty()) { 00242 for (TrustLogEvent event : eventsToProcess) { 00243 for (SimAbstractGraph graph : trustGrapher.getGraphs()) { 00244 graph.graphEvent(event, isForward); 00245 } 00246 } 00247 for (EventPlayerListener listener : listeners) { 00248 listener.goToIndex(currentEventIndex); 00249 } 00250 for (GraphViewer viewer : trustGrapher.getVisibleViewers()) { 00251 viewer.repaint(); 00252 } 00253 } 00254 if (state == PAUSE || atAnEnd()) { 00255 pause(); 00256 } 00257 } 00258 } 00259 00265 public void insertEvent(TrustLogEvent event) { 00266 List<TrustLogEvent> newEvents = new java.util.ArrayList<TrustLogEvent>(events.subList(0, currentEventIndex + 1)); 00267 newEvents.add(event); 00268 newEvents.addAll(events.subList(currentEventIndex + 1, events.size())); 00269 goToEvent(0); 00270 for (SimAbstractGraph graph : trustGrapher.getGraphs()) { 00271 graph.graphConstructionEvent(event); 00272 graph.graphConstructionEvent(null); 00273 } 00274 trustGrapher.startGraph(newEvents); 00275 } 00276 00282 public void removeEvent() { 00283 if (currentEventIndex != 0 && ChatterBox.yesNoDialog("Are you sure you want to remove this event?")) { //Make sure you don't remove the start event 00284 int indexToRemove = currentEventIndex; 00285 goToEvent(0); //It is necessary to go back to event 0 then remove the event so that the backwardEvent for that event is handled 00286 events.remove(indexToRemove); 00287 trustGrapher.startGraph(events); 00288 } else { 00289 ChatterBox.alert("You cannot remove the start event."); 00290 } 00291 } 00292 00299 public void modifyEvent(TrustLogEvent event) { 00300 if (currentEventIndex != 0) { 00301 int indexToModify = currentEventIndex; 00302 goToEvent(0); 00303 events.set(indexToModify, event); 00304 for (SimAbstractGraph graph : trustGrapher.getGraphs()) { 00305 graph.graphConstructionEvent(event); 00306 graph.graphConstructionEvent(null); 00307 } 00308 trustGrapher.startGraph(events); 00309 } else { 00310 ChatterBox.alert("You cannot modify the start event."); 00311 } 00312 } 00313 } 00315