From: <ev...@us...> - 2011-07-04 11:13:00
|
Revision: 1599 http://rails.svn.sourceforge.net/rails/?rev=1599&view=rev Author: evos Date: 2011-07-04 11:12:54 +0000 (Mon, 04 Jul 2011) Log Message: ----------- Fixed that undoing a Coalfields right by action did not reset the UI. HashMapState now extends ModelObject. MapChange and RemoveFromMap now can be passed the ModelObject it is changing, and on execute() and undo() the model update() method is now called to update the Observers; this all works the same way as ArrayListState already did. Modified Paths: -------------- trunk/18xx/rails/game/PublicCompany.java trunk/18xx/rails/game/PublicCompanyI.java trunk/18xx/rails/game/move/MapChange.java trunk/18xx/rails/game/move/RemoveFromMap.java trunk/18xx/rails/game/state/HashMapState.java Removed Paths: ------------- trunk/18xx/rails/game/model/RightsModel.java Modified: trunk/18xx/rails/game/PublicCompany.java =================================================================== --- trunk/18xx/rails/game/PublicCompany.java 2011-07-03 20:06:38 UTC (rev 1598) +++ trunk/18xx/rails/game/PublicCompany.java 2011-07-04 11:12:54 UTC (rev 1599) @@ -280,7 +280,7 @@ /** Rights */ protected HashMapState<String, String> rights = null; - protected RightsModel rightsModel = new RightsModel(); + //protected RightsModel rightsModel = new RightsModel(); /** @@ -765,6 +765,9 @@ for (SpecialPropertyI sp : specialProperties) { if (sp instanceof SpecialRight) { gameManager.setGuiParameter (GuiDef.Parm.HAS_ANY_RIGHTS, true); + // Initialize rights here to prevent overhead if not used, + // but if rights are used, the GUI needs it from the start. + if (rights == null) rights = new HashMapState<String, String>(name+"_Rights"); } } } @@ -1986,8 +1989,9 @@ return currentLoanValue; } - public RightsModel getRightsModel () { - return rightsModel; + public ModelObject getRightsModel () { + //return rightsModel; + return rights; } public boolean canClose() { @@ -1997,10 +2001,10 @@ public void setRight (String nameOfRight, String value) { if (rights == null) { rights = new HashMapState<String, String>(name+"_Rights"); - rightsModel.init (rights); + //rightsModel.init (rights); } rights.put(nameOfRight, value); - rightsModel.update(); + //rightsModel.update(); } public boolean hasRight (String nameOfRight) { Modified: trunk/18xx/rails/game/PublicCompanyI.java =================================================================== --- trunk/18xx/rails/game/PublicCompanyI.java 2011-07-03 20:06:38 UTC (rev 1598) +++ trunk/18xx/rails/game/PublicCompanyI.java 2011-07-04 11:12:54 UTC (rev 1599) @@ -342,7 +342,7 @@ public int getMaxLoansPerRound(); public int getValuePerLoan(); public MoneyModel getLoanValueModel (); - public RightsModel getRightsModel (); + public ModelObject getRightsModel (); public int sharesOwnedByPlayers(); public String getExtraShareMarks (); Deleted: trunk/18xx/rails/game/model/RightsModel.java =================================================================== --- trunk/18xx/rails/game/model/RightsModel.java 2011-07-03 20:06:38 UTC (rev 1598) +++ trunk/18xx/rails/game/model/RightsModel.java 2011-07-04 11:12:54 UTC (rev 1599) @@ -1,37 +0,0 @@ -/* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/game/model/RightsModel.java,v 1.6 2008/06/04 19:00:37 evos Exp $*/ -package rails.game.model; - -import rails.game.state.HashMapState; -import tools.Util; - -public class RightsModel extends ModelObject { - - private HashMapState<String, String> rights; - - public RightsModel() { - } - - /** Split off from the constructor to allow the rights map to exist only if needed */ - public void init (HashMapState<String, String> rights) { - this.rights = rights; - } - - public String getText() { - - if (rights == null) return ""; - - StringBuilder buf = new StringBuilder("<html>"); - for (String name : rights.viewKeySet()) { - if (buf.length() > 6) buf.append("<br>"); - buf.append(name); - String value = rights.get(name); - if (Util.hasValue(value)) buf.append("=").append(value); - } - if (buf.length() > 6) { - buf.append("</html>"); - } - return buf.toString(); - - } - -} Modified: trunk/18xx/rails/game/move/MapChange.java =================================================================== --- trunk/18xx/rails/game/move/MapChange.java 2011-07-03 20:06:38 UTC (rev 1598) +++ trunk/18xx/rails/game/move/MapChange.java 2011-07-04 11:12:54 UTC (rev 1599) @@ -7,6 +7,8 @@ import java.util.Map; +import rails.game.model.ModelObject; + /** * This Move class handles adding an entry to a Map. * @@ -35,9 +37,22 @@ MoveSet.add(this); } + public MapChange (Map<K, V> map, K key, V newValue, ModelObject modelToUpdate) { + + this.map = map; + this.key = key; + this.newValue = newValue; + this.oldValue = map.get(key); + this.keyExisted = map.containsKey(key); + if (modelToUpdate != null) registerModelToUpdate (modelToUpdate); + + MoveSet.add(this); + } + @Override public boolean execute() { map.put(key, newValue); + updateModels(); return true; } @@ -48,6 +63,7 @@ } else { map.remove(key); } + updateModels(); return true; } Modified: trunk/18xx/rails/game/move/RemoveFromMap.java =================================================================== --- trunk/18xx/rails/game/move/RemoveFromMap.java 2011-07-03 20:06:38 UTC (rev 1598) +++ trunk/18xx/rails/game/move/RemoveFromMap.java 2011-07-04 11:12:54 UTC (rev 1599) @@ -7,6 +7,8 @@ import java.util.Map; +import rails.game.model.ModelObject; + /** * This Move class handles removable from a stateful map (collection) * @@ -34,10 +36,23 @@ MoveSet.add(this); } + public RemoveFromMap (Map<K, V> map, K key, ModelObject modelToUpdate) { + + keyExisted = map.containsKey(key); + if (!keyExisted) return; // Nothing to do + this.map = map; + this.key = key; + this.oldValue = map.get(key); + if (modelToUpdate != null) registerModelToUpdate (modelToUpdate); + + MoveSet.add(this); + } + @Override public boolean execute() { if (keyExisted) { map.remove(key); + updateModels(); } return true; } @@ -46,6 +61,7 @@ public boolean undo() { if (keyExisted) { map.put (key, oldValue); + updateModels(); } return true; } Modified: trunk/18xx/rails/game/state/HashMapState.java =================================================================== --- trunk/18xx/rails/game/state/HashMapState.java 2011-07-03 20:06:38 UTC (rev 1598) +++ trunk/18xx/rails/game/state/HashMapState.java 2011-07-04 11:12:54 UTC (rev 1599) @@ -8,8 +8,10 @@ import java.util.Map; import java.util.Set; +import rails.game.model.ModelObject; import rails.game.move.MapChange; import rails.game.move.RemoveFromMap; +import tools.Util; /** * State class that wraps a HashMap @@ -23,7 +25,7 @@ * */ -public class HashMapState<K,V>{ +public class HashMapState<K,V> extends ModelObject { private final HashMap<K,V> map = new HashMap<K,V>(); private String mapName; @@ -42,12 +44,12 @@ } public void put(K key, V value) { - new MapChange<K,V>(map, key, value); + new MapChange<K,V>(map, key, value, this); } public void putAll(Map<K,V> map) { for (K key:map.keySet()) { - new MapChange<K,V>(map, key, map.get(key)); + new MapChange<K,V>(map, key, map.get(key), this); } } @@ -56,7 +58,7 @@ } public void remove(K key) { - new RemoveFromMap<K,V>(map, key); + new RemoveFromMap<K,V>(map, key, this); } public boolean hasKey(K key) { @@ -72,6 +74,7 @@ for (K key : keys) { remove (key); } + update(); } /** @@ -93,6 +96,7 @@ new MapChange<K,V>(map, key, initMap.get(key)); } } + update(); } /** @@ -115,4 +119,24 @@ public boolean isEmpty() { return map.isEmpty(); } + + @Override + public String getText() { + + if (map == null) return ""; + + StringBuilder buf = new StringBuilder("<html>"); + for (K name : map.keySet()) { + if (buf.length() > 6) buf.append("<br>"); + buf.append(name.toString()); + Object value = map.get(name); + if (value != null && Util.hasValue(value.toString())) buf.append("=").append(value.toString()); + } + if (buf.length() > 6) { + buf.append("</html>"); + } + return buf.toString(); + + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |