From: Erik V. <ev...@us...> - 2009-12-13 16:40:05
|
Update of /cvsroot/rails/18xx/rails/game/model In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv28822/rails/game/model Modified Files: PriceModel.java ModelObject.java Added Files: ViewUpdate.java Log Message: Implemented request to show low-price colours in SR and OR panels. Added a generic Model-to-View update mechanism (used by above) All configurable colours can now be specified as RGB decimally or hexadecimally. --- NEW FILE: ViewUpdate.java --- package rails.game.model; import java.io.Serializable; import java.util.*; /** * ViewUpdate is a composite object that can be sent from a ModelObject (Observable) * to a View object (Observer). * <p> The current version has text, background colour and foreground colour. * Receiving view objects must be prepared to handle extensions. * @author VosE * */ public class ViewUpdate implements Serializable { protected Map<String, Object> updates = new HashMap<String, Object>(4); public static final String TEXT = "TEXT"; public static final String BGCOLOUR = "BGCOLOUR"; public static final long serialVersionUID = 1L; public ViewUpdate (String key, Object value) { addObject (key, value); } public ViewUpdate (String text) { addObject (TEXT, text); } /** Add an object. * Return this ViewUpdate to enable chaining. */ public ViewUpdate addObject (String key, Object value) { updates.put(key, value); return this; } public Set<String> getKeys () { return updates.keySet(); } public boolean hasKey (String key) { return updates.containsKey(key); } public Object getValue(String key) { return updates.get(key); } public String getText () { return (String) updates.get(TEXT); } } Index: PriceModel.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/model/PriceModel.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** PriceModel.java 4 Jun 2008 19:00:37 -0000 1.7 --- PriceModel.java 13 Dec 2009 16:39:48 -0000 1.8 *************** *** 2,8 **** package rails.game.model; ! import rails.game.Bank; ! import rails.game.PublicCompanyI; ! import rails.game.StockSpaceI; import rails.game.move.PriceMove; import rails.game.state.StateI; --- 2,6 ---- package rails.game.model; ! import rails.game.*; import rails.game.move.PriceMove; import rails.game.state.StateI; *************** *** 31,35 **** } ! public String getText() { if (stockPrice != null) { return Bank.format(stockPrice.getPrice()) + " (" --- 29,44 ---- } ! @Override ! public Object getUpdate() { ! if (stockPrice != null) { ! return new ViewUpdate(getText()) ! .addObject(ViewUpdate.BGCOLOUR, stockPrice.getColour()); ! } else { ! return getText(); ! } ! } ! ! @Override ! public String getText() { if (stockPrice != null) { return Bank.format(stockPrice.getPrice()) + " (" Index: ModelObject.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/model/ModelObject.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ModelObject.java 4 Jun 2008 19:00:37 -0000 1.7 --- ModelObject.java 13 Dec 2009 16:39:48 -0000 1.8 *************** *** 2,8 **** package rails.game.model; ! import java.util.HashSet; ! import java.util.Observable; ! import java.util.Set; import org.apache.log4j.Logger; --- 2,6 ---- package rails.game.model; ! import java.util.*; import org.apache.log4j.Logger; *************** *** 24,27 **** --- 22,31 ---- Logger.getLogger(ModelObject.class.getPackage().getName()); + @Override + public void addObserver (Observer o) { + super.addObserver(o); + notifyViewObjects(); + } + /** Add a dependent model object */ public void addDependent(ModelObject object) { *************** *** 39,50 **** private void notifyViewObjects() { setChanged(); ! notifyObservers(getText()); clearChanged(); } /** * Optional method, to make a subclass-dependent selection of the way the * "value" will be composed. The default value is 0. ! * * @param option The selected */ --- 43,59 ---- private void notifyViewObjects() { setChanged(); ! notifyObservers(getUpdate()); clearChanged(); } + /** Default update is just text */ + public Object getUpdate () { + return getText(); + } + /** * Optional method, to make a subclass-dependent selection of the way the * "value" will be composed. The default value is 0. ! * * @param option The selected */ *************** *** 79,83 **** * default result is the Observable's toString(), but it can be overridden * where needed. ! * * @return */ --- 88,92 ---- * default result is the Observable's toString(), but it can be overridden * where needed. ! * * @return */ |