From: Erik V. <ev...@us...> - 2010-01-05 20:54:15
|
Update of /cvsroot/rails/18xx/rails/ui/swing In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv4302/rails/ui/swing Modified Files: GameStatus.java MessagePanel.java ORPanel.java Added Files: GridPanel.java Log Message: Created common superclass GridPanel for GameStatus and ORPanel to abstract common features. Index: MessagePanel.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/MessagePanel.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** MessagePanel.java 29 Dec 2009 21:58:11 -0000 1.6 --- MessagePanel.java 5 Jan 2010 20:54:05 -0000 1.7 *************** *** 2,9 **** package rails.ui.swing; ! import java.awt.*; ! import javax.swing.*; ! import rails.util.*; public class MessagePanel extends JPanel { --- 2,8 ---- package rails.ui.swing; ! import java.awt.Color; ! import javax.swing.*; public class MessagePanel extends JPanel { Index: GameStatus.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/GameStatus.java,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** GameStatus.java 3 Jan 2010 20:31:29 -0000 1.34 --- GameStatus.java 5 Jan 2010 20:54:05 -0000 1.35 *************** *** 1,9 **** package rails.ui.swing; ! import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; - import java.util.List; import javax.swing.*; --- 1,9 ---- package rails.ui.swing; ! import java.awt.GridBagConstraints; ! import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.*; *************** *** 14,19 **** import rails.game.*; import rails.game.action.*; - import rails.game.model.ModelObject; - import rails.game.state.BooleanState; import rails.ui.swing.elements.*; import rails.util.LocalText; --- 14,17 ---- *************** *** 23,46 **** * rails.game status information. */ ! public class GameStatus extends JPanel implements ActionListener, RowHideable { private static final long serialVersionUID = 1L; - private static final int NARROW_GAP = 1; - private static final int WIDE_GAP = 3; - private static final int WIDE_LEFT = 1; - private static final int WIDE_RIGHT = 2; - private static final int WIDE_TOP = 4; - private static final int WIDE_BOTTOM = 8; - private static final String BUY_FROM_IPO_CMD = "BuyFromIPO"; private static final String BUY_FROM_POOL_CMD = "BuyFromPool"; private static final String SELL_CMD = "Sell"; - private static GameStatus gameStatus; protected StatusWindow parent; - private GridBagConstraints gbc; - private Color buttonHighlight = new Color(255, 160, 80); - // Grid elements per function protected Field certPerPlayer[][]; --- 21,33 ---- * rails.game status information. */ ! public class GameStatus extends GridPanel implements ActionListener { private static final long serialVersionUID = 1L; private static final String BUY_FROM_IPO_CMD = "BuyFromIPO"; private static final String BUY_FROM_POOL_CMD = "BuyFromPool"; private static final String SELL_CMD = "Sell"; protected StatusWindow parent; // Grid elements per function protected Field certPerPlayer[][]; *************** *** 90,98 **** protected int futureTrainsXOffset, futureTrainsYOffset, futureTrainsWidth; protected int rightCompCaptionXOffset; - - /** 2D-array of fields to enable show/hide per row or column */ - protected JComponent[][] fields; - /** Array of Observer objects to set row visibility */ - protected RowVisibility[] rowVisibilityObservers; private Caption[] upperPlayerCaption; --- 77,80 ---- *************** *** 100,110 **** private Caption treasurySharesCaption; - protected int np; // Number of players - protected GridBagLayout gb; - - protected int nc; // Number of companies - protected Player[] players; - protected PublicCompanyI[] companies; - //protected CompanyManagerI cm; protected Portfolio ipo, pool; --- 82,85 ---- *************** *** 120,126 **** protected boolean hasCompanyLoans = false; - private PublicCompanyI c; - private JComponent f; - // Current actor. // Players: 0, 1, 2, ... --- 95,98 ---- *************** *** 144,152 **** public void init(StatusWindow parent, GameUIManager gameUIManager) { - gameStatus = this; this.parent = parent; this.gameUIManager = gameUIManager; bank = gameUIManager.getGameManager().getBank(); gb = new GridBagLayout(); this.setLayout(gb); --- 116,126 ---- public void init(StatusWindow parent, GameUIManager gameUIManager) { this.parent = parent; this.gameUIManager = gameUIManager; bank = gameUIManager.getGameManager().getBank(); + gridPanel = this; + parentFrame = parent; + gb = new GridBagLayout(); this.setLayout(gb); *************** *** 256,260 **** fields = new JComponent[1+lastX][2+lastY]; rowVisibilityObservers = new RowVisibility[nc]; ! addField(new Caption(LocalText.getText("COMPANY")), 0, 0, 1, 2, WIDE_RIGHT + WIDE_BOTTOM, true); --- 230,234 ---- fields = new JComponent[1+lastX][2+lastY]; rowVisibilityObservers = new RowVisibility[nc]; ! addField(new Caption(LocalText.getText("COMPANY")), 0, 0, 1, 2, WIDE_RIGHT + WIDE_BOTTOM, true); *************** *** 317,324 **** c = companies[i]; companyIndex.put(c, new Integer(i)); ! rowVisibilityObservers[i] = new RowVisibility (this, certPerPlayerYOffset + i, c.getClosedModel()); boolean visible = !c.isClosed(); ! f = new Caption(c.getName()); f.setForeground(c.getFgColour()); --- 291,298 ---- c = companies[i]; companyIndex.put(c, new Integer(i)); ! rowVisibilityObservers[i] = new RowVisibility (this, certPerPlayerYOffset + i, c.getClosedModel()); boolean visible = !c.isClosed(); ! f = new Caption(c.getName()); f.setForeground(c.getFgColour()); *************** *** 521,556 **** } - private void addField(JComponent comp, int x, int y, int width, int height, - int wideGapPositions, boolean visible) { - - int padTop, padLeft, padBottom, padRight; - gbc.gridx = x; - gbc.gridy = y; - gbc.gridwidth = width; - gbc.gridheight = height; - gbc.weightx = gbc.weighty = 0.5; - gbc.fill = GridBagConstraints.BOTH; - padTop = (wideGapPositions & WIDE_TOP) > 0 ? WIDE_GAP : NARROW_GAP; - padLeft = (wideGapPositions & WIDE_LEFT) > 0 ? WIDE_GAP : NARROW_GAP; - padBottom = - (wideGapPositions & WIDE_BOTTOM) > 0 ? WIDE_GAP : NARROW_GAP; - padRight = (wideGapPositions & WIDE_RIGHT) > 0 ? WIDE_GAP : NARROW_GAP; - gbc.insets = new Insets(padTop, padLeft, padBottom, padRight); - - add(comp, gbc); - - if (fields[x][y] == null) fields[x][y] = comp; - comp.setVisible(visible); - } - - public static GameStatus getInstance() { - return gameStatus; - } - - @Override - public void repaint() { - super.repaint(); - } - public void actionPerformed(ActionEvent actor) { JComponent source = (JComponent) actor.getSource(); --- 495,498 ---- *************** *** 937,949 **** certInTreasuryButton[i].setVisible(clickable); } ! ! public void setRowVisibility (int rowIndex, boolean value) { ! for (int j=0; j < fields.length; j++) { ! if (fields[j][rowIndex] != null) { ! fields[j][rowIndex].setVisible(value); ! } ! } ! parent.pack(); ! } ! } --- 879,882 ---- certInTreasuryButton[i].setVisible(clickable); } ! } --- NEW FILE: GridPanel.java --- /* $Header: /cvsroot/rails/18xx/rails/ui/swing/GridPanel.java,v 1.1 2010/01/05 20:54:05 evos Exp $*/ package rails.ui.swing; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; import org.apache.log4j.Logger; import rails.game.*; import rails.game.model.ModelObject; import rails.game.state.BooleanState; import rails.ui.swing.elements.ViewObject; public abstract class GridPanel extends JPanel implements ActionListener, KeyListener { protected static final int NARROW_GAP = 1; protected static final int WIDE_GAP = 3; protected static final int WIDE_LEFT = 1; protected static final int WIDE_RIGHT = 2; protected static final int WIDE_TOP = 4; protected static final int WIDE_BOTTOM = 8; protected JPanel gridPanel; protected JFrame parentFrame; protected GridBagLayout gb; protected GridBagConstraints gbc; protected static Color buttonHighlight = new Color(255, 160, 80); protected int np; protected Player[] players; protected int nc; protected PublicCompanyI[] companies; protected RoundI round; protected PublicCompanyI c; protected JComponent f; protected List<ViewObject> observers = new ArrayList<ViewObject>(); /** 2D-array of fields to enable show/hide per row or column */ protected JComponent[][] fields; /** Array of Observer objects to set row visibility */ protected RowVisibility[] rowVisibilityObservers; protected List<JMenuItem> menuItemsToReset = new ArrayList<JMenuItem>(); protected static Logger log = Logger.getLogger(GridPanel.class.getPackage().getName()); public void redisplay() { revalidate(); } protected void deRegisterObservers() { log.debug("Deregistering observers"); for (ViewObject vo : observers) { vo.deRegister(); } } protected void addField(JComponent comp, int x, int y, int width, int height, int wideGapPositions) { addField (comp, x, y, width, height, wideGapPositions, true); } protected void addField(JComponent comp, int x, int y, int width, int height, int wideGapPositions, boolean visible) { int padTop, padLeft, padBottom, padRight; gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = width; gbc.gridheight = height; gbc.weightx = gbc.weighty = 0.5; gbc.fill = GridBagConstraints.BOTH; padTop = (wideGapPositions & WIDE_TOP) > 0 ? WIDE_GAP : NARROW_GAP; padLeft = (wideGapPositions & WIDE_LEFT) > 0 ? WIDE_GAP : NARROW_GAP; padBottom = (wideGapPositions & WIDE_BOTTOM) > 0 ? WIDE_GAP : NARROW_GAP; padRight = (wideGapPositions & WIDE_RIGHT) > 0 ? WIDE_GAP : NARROW_GAP; gbc.insets = new Insets(padTop, padLeft, padBottom, padRight); gridPanel.add(comp, gbc); if (comp instanceof ViewObject && ((ViewObject) comp).getModel() != null) { observers.add((ViewObject) comp); } if (fields != null && fields[x][y] == null) fields[x][y] = comp; comp.setVisible(visible); } public void displayPopup(String text) { JOptionPane.showMessageDialog(this, text); } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_F1) { HelpWindow.displayHelp(GameManager.getInstance().getHelp()); e.consume(); } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void setRowVisibility (int rowIndex, boolean value) { for (int j=0; j < fields.length; j++) { if (fields[j][rowIndex] != null) { fields[j][rowIndex].setVisible(value); } } parentFrame.pack(); } class RowVisibility implements ViewObject { private GridPanel parent; private ModelObject modelObject; private int rowIndex; private boolean lastValue; public RowVisibility (GridPanel parent, int rowIndex, ModelObject model) { this.parent = parent; this.modelObject = model; this.rowIndex = rowIndex; modelObject.addObserver(this); lastValue = !((BooleanState)modelObject).booleanValue(); } public boolean lastValue () { return lastValue; } /** Needed to satisfy the ViewObject interface. */ public ModelObject getModel() { return modelObject; } /** Needed to satisfy the Observer interface. * The closedObject model will send true if the company is closed. */ public void update(Observable o1, Object o2) { if (o2 instanceof Boolean) { lastValue = !(Boolean)o2; parent.setRowVisibility(rowIndex, lastValue); } } /** Needed to satisfy the ViewObject interface. Currently not used. */ public void deRegister() { if (modelObject != null) modelObject.deleteObserver(this); } } } Index: ORPanel.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/ORPanel.java,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** ORPanel.java 3 Jan 2010 20:31:29 -0000 1.37 --- ORPanel.java 5 Jan 2010 20:54:05 -0000 1.38 *************** *** 4,8 **** import java.awt.*; import java.awt.event.*; - import java.util.ArrayList; import java.util.List; --- 4,7 ---- *************** *** 17,30 **** import rails.util.LocalText; ! public class ORPanel extends JPanel ! implements ActionListener, KeyListener, RowHideable { private static final long serialVersionUID = 1L; - private static final int NARROW_GAP = 1; - private static final int WIDE_GAP = 3; - private static final int WIDE_LEFT = 1; - private static final int WIDE_RIGHT = 2; - private static final int WIDE_TOP = 4; - private static final int WIDE_BOTTOM = 8; public static final String BUY_PRIVATE_CMD = "BuyPrivate"; --- 16,23 ---- import rails.util.LocalText; ! public class ORPanel extends GridPanel ! implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; public static final String BUY_PRIVATE_CMD = "BuyPrivate"; *************** *** 58,64 **** private ActionMenuItem repayLoans; - private GridBagLayout gb; - private GridBagConstraints gbc; - // Grid elements per function private Caption leftCompName[]; --- 51,54 ---- *************** *** 106,124 **** private ActionButton redoButton; - private int nc = 0; // Number of companies - - private Player[] players; - private PublicCompanyI[] companies; - private RoundI round; - private PublicCompanyI c; - private JComponent f; - - private List<ViewObject> observers = new ArrayList<ViewObject>(); - - /** 2D-array of fields to enable show/hide per row or column */ - protected JComponent[][] fields; - /** Array of Observer objects to set row visibility */ - protected RowVisibility[] rowVisibilityObservers; - // Current state private int playerIndex = -1; --- 96,99 ---- *************** *** 127,132 **** private PublicCompanyI orComp = null; - private List<JMenuItem> menuItemsToReset = new ArrayList<JMenuItem>(); - protected static Logger log = Logger.getLogger(ORPanel.class.getPackage().getName()); --- 102,105 ---- *************** *** 145,148 **** --- 118,124 ---- statusPanel.setOpaque(true); + gridPanel = statusPanel; + parentFrame = parent; + round = gameUIManager.getCurrentRound(); privatesCanBeBought = gameUIManager.getGameParameterAsBoolean(Defs.Parm.CAN_ANY_COMPANY_BUY_PRIVATES); *************** *** 224,228 **** log.debug("ORPanel.recreate() called"); ! companies = (or).getOperatingCompanies(); nc = companies.length; --- 200,204 ---- log.debug("ORPanel.recreate() called"); ! companies = or.getOperatingCompanies(); nc = companies.length; *************** *** 236,250 **** } - public void redisplay() { - revalidate(); - } - - private void deRegisterObservers() { - log.debug("Deregistering observers"); - for (ViewObject vo : observers) { - vo.deRegister(); - } - } - private void initButtonPanel() { buttonPanel = new JPanel(); --- 212,215 ---- *************** *** 316,320 **** /* Top titles */ ! addField(new Caption("Company"), 0, 0, lastXWidth = 1, 2, WIDE_BOTTOM + WIDE_RIGHT); --- 281,285 ---- /* Top titles */ ! addField(new Caption("Company"), 0, 0, lastXWidth = 1, 2, WIDE_BOTTOM + WIDE_RIGHT); *************** *** 397,404 **** fields = new JComponent[1+currentXOffset][2+nc]; rowVisibilityObservers = new RowVisibility[nc]; ! for (int i = 0; i < nc; i++) { c = companies[i]; ! rowVisibilityObservers[i] = new RowVisibility (this, leftCompNameYOffset + i, c.getClosedModel()); observers.add(rowVisibilityObservers[i]); --- 362,369 ---- fields = new JComponent[1+currentXOffset][2+nc]; rowVisibilityObservers = new RowVisibility[nc]; ! for (int i = 0; i < nc; i++) { c = companies[i]; ! rowVisibilityObservers[i] = new RowVisibility (this, leftCompNameYOffset + i, c.getClosedModel()); observers.add(rowVisibilityObservers[i]); *************** *** 493,528 **** } - private void addField(JComponent comp, int x, int y, int width, int height, - int wideGapPositions) { - addField (comp, x, y, width, height, wideGapPositions, true); - } - - private void addField(JComponent comp, int x, int y, int width, int height, - int wideGapPositions, boolean visible) { - - int padTop, padLeft, padBottom, padRight; - gbc.gridx = x; - gbc.gridy = y; - gbc.gridwidth = width; - gbc.gridheight = height; - gbc.weightx = gbc.weighty = 0.5; - gbc.fill = GridBagConstraints.BOTH; - padTop = (wideGapPositions & WIDE_TOP) > 0 ? WIDE_GAP : NARROW_GAP; - padLeft = (wideGapPositions & WIDE_LEFT) > 0 ? WIDE_GAP : NARROW_GAP; - padBottom = - (wideGapPositions & WIDE_BOTTOM) > 0 ? WIDE_GAP : NARROW_GAP; - padRight = (wideGapPositions & WIDE_RIGHT) > 0 ? WIDE_GAP : NARROW_GAP; - gbc.insets = new Insets(padTop, padLeft, padBottom, padRight); - - statusPanel.add(comp, gbc); - - if (comp instanceof ViewObject - && ((ViewObject) comp).getModel() != null) { - observers.add((ViewObject) comp); - } - - if (fields != null && fields[x][y] == null) fields[x][y] = comp; - comp.setVisible(visible); - } public void finish() { --- 458,461 ---- *************** *** 806,838 **** } - public void displayPopup(String text) { - JOptionPane.showMessageDialog(this, text); - } - - public void keyPressed(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_F1) { - HelpWindow.displayHelp(GameManager.getInstance().getHelp()); - e.consume(); - } - } - - public void keyReleased(KeyEvent e) {} - - public void keyTyped(KeyEvent e) {} - public PublicCompanyI[] getOperatingCompanies() { return companies; } - public void setRowVisibility (int rowIndex, boolean value) { - - for (int j=0; j < fields.length; j++) { - if (fields[j][rowIndex] != null) { - fields[j][rowIndex].setVisible(value); - } - } - orWindow.pack(); - } - - } --- 739,745 ---- |