From: Erik V. <ev...@us...> - 2009-12-13 16:40:00
|
Update of /cvsroot/rails/18xx/rails/ui/swing/elements In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv28822/rails/ui/swing/elements Modified Files: Field.java GUIStockSpace.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. Index: Field.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/elements/Field.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Field.java 7 Jan 2009 21:03:24 -0000 1.8 --- Field.java 13 Dec 2009 16:39:49 -0000 1.9 *************** *** 10,14 **** import rails.game.model.ModelObject; ! import rails.ui.swing.StatusWindow; public class Field extends JLabel implements ViewObject { --- 10,15 ---- import rails.game.model.ModelObject; ! import rails.game.model.ViewUpdate; ! import rails.util.Util; public class Field extends JLabel implements ViewObject { *************** *** 23,26 **** --- 24,28 ---- private ModelObject modelObject; + private Color normalBgColour = NORMAL_BG_COLOUR; private boolean pull = false; *************** *** 43,49 **** public Field(ModelObject modelObject) { ! this(modelObject.getText()); this.modelObject = modelObject; ! if (StatusWindow.useObserver) modelObject.addObserver(this); } --- 45,56 ---- public Field(ModelObject modelObject) { ! this(""); ! //this(modelObject.getText()); this.modelObject = modelObject; ! //Object mu = modelObject.getUpdate(); ! //if (mu instanceof ViewUpdate) { ! // updateDetails ((ViewUpdate) mu); ! //} ! modelObject.addObserver(this); } *************** *** 64,77 **** public void setModel(ModelObject m) { ! if (StatusWindow.useObserver) modelObject.deleteObserver(this); modelObject = m; ! if (StatusWindow.useObserver) { ! modelObject.addObserver(this); ! update(null, null); ! } } public void setHighlight(boolean highlight) { ! setBackground(highlight ? HIGHLIGHT_BG_COLOUR : NORMAL_BG_COLOUR); } --- 71,82 ---- public void setModel(ModelObject m) { ! modelObject.deleteObserver(this); modelObject = m; ! modelObject.addObserver(this); ! update(null, null); } public void setHighlight(boolean highlight) { ! setBackground(highlight ? HIGHLIGHT_BG_COLOUR : normalBgColour); } *************** *** 80,84 **** @Override public void paintComponent(Graphics g) { ! if (modelObject != null && (pull || !StatusWindow.useObserver)) { setText(modelObject.getText()); } --- 85,89 ---- @Override public void paintComponent(Graphics g) { ! if (modelObject != null && pull) { setText(modelObject.getText()); } *************** *** 88,103 **** /** Needed to satisfy the Observer interface. */ public void update(Observable o1, Object o2) { ! if (StatusWindow.useObserver) { ! if (o2 instanceof String) { ! setText((String) o2); ! } else { ! setText(modelObject.toString()); ! } } } /** Needed to satisfy the ViewObject interface. Currently not used. */ public void deRegister() { ! if (modelObject != null && StatusWindow.useObserver) modelObject.deleteObserver(this); } --- 93,120 ---- /** Needed to satisfy the Observer interface. */ public void update(Observable o1, Object o2) { ! if (o2 instanceof String) { ! setText((String) o2); ! } else if (o2 instanceof ViewUpdate) { ! updateDetails ((ViewUpdate)o2); ! } else { ! setText(modelObject.toString()); } } + protected void updateDetails (ViewUpdate vu) { + for (String key : vu.getKeys()) { + if (ViewUpdate.TEXT.equalsIgnoreCase(key)) { + setText (vu.getText()); + } else if (ViewUpdate.BGCOLOUR.equalsIgnoreCase(key)) { + setBackground((Color)vu.getValue(key)); + normalBgColour = getBackground(); + setForeground (Util.isDark(normalBgColour) ? Color.WHITE : Color.BLACK); + } + } + } + /** Needed to satisfy the ViewObject interface. Currently not used. */ public void deRegister() { ! if (modelObject != null) modelObject.deleteObserver(this); } Index: GUIStockSpace.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/elements/GUIStockSpace.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** GUIStockSpace.java 31 Oct 2009 17:08:27 -0000 1.7 --- GUIStockSpace.java 13 Dec 2009 16:39:49 -0000 1.8 *************** *** 2,15 **** package rails.ui.swing.elements; ! import java.awt.Color; ! import java.awt.Dimension; ! import java.awt.Point; import java.util.List; import java.util.Observable; ! import javax.swing.BorderFactory; ! import javax.swing.JLabel; ! import javax.swing.JLayeredPane; ! import javax.swing.SwingConstants; import org.apache.log4j.Logger; --- 2,10 ---- package rails.ui.swing.elements; ! import java.awt.*; import java.util.List; import java.util.Observable; ! import javax.swing.*; import org.apache.log4j.Logger; *************** *** 19,23 **** import rails.game.model.ModelObject; import rails.ui.swing.GUIToken; ! import rails.ui.swing.StatusWindow; public class GUIStockSpace extends JLayeredPane implements ViewObject { --- 14,18 ---- import rails.game.model.ModelObject; import rails.ui.swing.GUIToken; ! import rails.util.Util; public class GUIStockSpace extends JLayeredPane implements ViewObject { *************** *** 31,37 **** List<PublicCompanyI> tokenList; - private static final Color BROWN = new Color(144, 72, 0); private static final Color LIGHT_GRAY = new Color(200, 200, 200); - private static final Color ORANGE = new Color(255, 180, 0); protected static Logger log = --- 26,30 ---- *************** *** 53,57 **** priceLabel.setText(Integer.toString(model.getPrice())); ! priceLabel.setBackground(stringToColor(model.getColour())); priceLabel.setVerticalTextPosition(SwingConstants.TOP); --- 46,53 ---- priceLabel.setText(Integer.toString(model.getPrice())); ! //priceLabel.setBackground(stringToColor(model.getColour())); ! priceLabel.setBackground(model.getColour()); ! priceLabel.setForeground(Util.isDark(priceLabel.getBackground()) ! ? Color.WHITE : Color.BLACK); priceLabel.setVerticalTextPosition(SwingConstants.TOP); *************** *** 113,117 **** /* * (non-Javadoc) ! * * @see rails.ui.swing.elements.ViewObject#getModel() */ --- 109,113 ---- /* * (non-Javadoc) ! * * @see rails.ui.swing.elements.ViewObject#getModel() */ *************** *** 122,130 **** /* * (non-Javadoc) ! * * @see rails.ui.swing.elements.ViewObject#deRegister() */ public void deRegister() { ! if (model != null && StatusWindow.useObserver) ((ModelObject) model).deleteObserver(this); --- 118,126 ---- /* * (non-Javadoc) ! * * @see rails.ui.swing.elements.ViewObject#deRegister() */ public void deRegister() { ! if (model != null) ((ModelObject) model).deleteObserver(this); *************** *** 133,137 **** /* * (non-Javadoc) ! * * @see java.rails.util.Observer#update(java.rails.util.Observable, * java.lang.Object) --- 129,133 ---- /* * (non-Javadoc) ! * * @see java.rails.util.Observer#update(java.rails.util.Observable, * java.lang.Object) *************** *** 139,177 **** public void update(Observable o1, Object o2) { ! if (StatusWindow.useObserver) { ! recreate(); ! } ! ! } ! ! /** ! * Quick n' dirty method of converting strings to color objects. This has ! * been replaced by using hex colors in the XML definitions. ! * ! * @deprecated ! */ ! private static Color stringToColor(String color) { ! if (color.equalsIgnoreCase("yellow")) { ! return Color.YELLOW; ! } else if (color.equalsIgnoreCase("orange")) { ! return ORANGE; ! } else if (color.equalsIgnoreCase("brown")) { ! return BROWN; ! } else if (color.equalsIgnoreCase("red")) { ! return Color.RED; ! } else if (color.equalsIgnoreCase("green")) { ! return Color.GREEN; ! } else if (color.equalsIgnoreCase("blue")) { ! return Color.BLUE; ! } else if (color.equalsIgnoreCase("black")) { ! return Color.BLACK; ! } else if (color.equalsIgnoreCase("white")) { ! return Color.WHITE; ! } else if (color.equals("")) { ! return Color.WHITE; ! } else { ! log.warn("Unknown color: " + color + "."); ! return Color.MAGENTA; ! } } --- 135,139 ---- public void update(Observable o1, Object o2) { ! recreate(); } |