001 package aima.gui.framework; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import aima.basic.BasicEnvironmentView; 007 008 /** 009 * Facade hiding application specific design decisions from 010 * the {@link AgentAppFrame}. Subclasses should add methods to access 011 * informations about the environment's and the agent's state. Note that any 012 * data shown by an agent view must be provided by a corresponding model. 013 * The model-view-controller architecture forbids any component of the 014 * application except the controller to directly access the view components 015 * and it forbids the views to access information without consulting the model. 016 * @author R. Lunde 017 */ 018 public class AgentAppModel extends BasicEnvironmentView { 019 /** Maintains all views. Typically, just one agent view is maintained here. */ 020 protected List<ModelChangedListener> listeners = new ArrayList<ModelChangedListener>(); 021 022 /** Adds a new view to the model. */ 023 public void addModelChangedListener(ModelChangedListener listener) { 024 listeners.add(listener); 025 } 026 027 /** Signals to all registered listeners that the model has changed. */ 028 public void fireModelChanged() { 029 for (AgentAppModel.ModelChangedListener listener : listeners) 030 listener.modelChanged(); 031 } 032 033 /** 034 * Reacts on environment changes. This implementation informs 035 * all registered model change listeners and provides their log 036 * with the given command. 037 */ 038 public void envChanged(String command) { 039 for (ModelChangedListener listener : listeners) { 040 listener.logMessage(command); 041 listener.modelChanged(); 042 } 043 } 044 045 046 /////////////////////////////////////////////////////////// 047 // inner classes 048 049 /** 050 * Observer interface for views which need to be informed 051 * about model state changes and log messages to be printed out. 052 */ 053 public static interface ModelChangedListener { 054 public void modelChanged(); 055 public void logMessage(String message); 056 } 057 } 058