From: Erik V. <ev...@us...> - 2009-05-04 20:29:22
|
Update of /cvsroot/rails/18xx/rails/ui/swing In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv29969/rails/ui/swing Modified Files: ORUIManager.java GameSetupWindow.java GameUIManager.java Log Message: 1856 CGR formation round Index: ORUIManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/ORUIManager.java,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** ORUIManager.java 21 Jan 2009 20:18:23 -0000 1.26 --- ORUIManager.java 4 May 2009 20:29:15 -0000 1.27 *************** *** 897,929 **** if (dt == null) break; ! PublicCompanyI c = dt.getCompany(); ! String playerName = dt.getPlayerName(); ! List<TrainI> trains = dt.getOwnedTrains(); ! List<String> trainOptions = ! new ArrayList<String>(trains.size()); ! String[] options = new String[trains.size()]; ! ! for (int i = 0; i < options.length; i++) { ! options[i] = ! LocalText.getText("N_Train", ! trains.get(i).getName()); ! trainOptions.add(options[i]); ! } ! String discardedTrainName = ! (String) JOptionPane.showInputDialog(orWindow, ! LocalText.getText("HAS_TOO_MANY_TRAINS", ! playerName, ! c.getName() ), ! LocalText.getText("WhichTrainToDiscard"), ! JOptionPane.QUESTION_MESSAGE, null, ! options, options[0]); ! if (discardedTrainName != null) { ! TrainI discardedTrain = ! trains.get(trainOptions.indexOf(discardedTrainName)); ! ! dt.setDiscardedTrain(discardedTrain); ! ! orWindow.process(dt); ! } } } --- 897,901 ---- if (dt == null) break; ! gameUIManager.discardTrains(dt); } } Index: GameSetupWindow.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/GameSetupWindow.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** GameSetupWindow.java 4 Jun 2008 19:00:32 -0000 1.12 --- GameSetupWindow.java 4 May 2009 20:29:15 -0000 1.13 *************** *** 8,15 **** --- 8,17 ---- import org.apache.log4j.Logger; + import rails.common.Defs; import rails.game.*; import rails.ui.swing.GameUIManager; import rails.util.*; + import java.io.File; import java.util.*; import java.util.List; *************** *** 37,40 **** --- 39,43 ---- List<GameOption> availableOptions = new ArrayList<GameOption>(); String gameName; + Game game; // Used by the player selection combo box. *************** *** 46,53 **** Logger.getLogger(GameSetupWindow.class.getPackage().getName()); ! public GameSetupWindow(GameUIManager gameUIManager) { super(); ! this.gameUIManager = gameUIManager; initialize(); --- 49,56 ---- Logger.getLogger(GameSetupWindow.class.getPackage().getName()); ! public GameSetupWindow(/*GameUIManager gameUIManager*/) { super(); ! //this.gameUIManager = gameUIManager; initialize(); *************** *** 193,198 **** toggleOptions(); this.pack(); ! } else if (arg0.getSource().equals(loadButton) ! && gameUIManager.loadGame()) { setVisible(false); } else if (arg0.getSource().equals(infoButton)) { --- 196,223 ---- toggleOptions(); this.pack(); ! } else if (arg0.getSource().equals(loadButton)) { ! String saveDirectory = Config.get("save.directory"); ! JFileChooser jfc = new JFileChooser(); ! //if (providedName != null) { ! // jfc.setSelectedFile(new File(providedName)); ! //} else { ! jfc.setCurrentDirectory(new File(saveDirectory)); ! //} ! ! if (jfc.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION) { ! File selectedFile = jfc.getSelectedFile(); ! String filepath = selectedFile.getPath(); ! saveDirectory = selectedFile.getParent(); ! if ((game = Game.load(filepath)) == null) { ! JOptionPane.showMessageDialog(this, ! DisplayBuffer.get(), "", JOptionPane.ERROR_MESSAGE); ! return; ! } ! DisplayBuffer.clear(); ! ! } ! ! startGameUIManager(game); ! gameUIManager.startLoadedGame(); setVisible(false); } else if (arg0.getSource().equals(infoButton)) { *************** *** 344,348 **** } ! Game game = new Game(gameName, playerNames, selectedOptions); if (!game.setup()) { JOptionPane.showMessageDialog(this, DisplayBuffer.get(), "", --- 369,373 ---- } ! game = new Game(gameName, playerNames, selectedOptions); if (!game.setup()) { JOptionPane.showMessageDialog(this, DisplayBuffer.get(), "", *************** *** 355,358 **** --- 380,384 ---- } else { game.start(); + startGameUIManager (game); gameUIManager.gameUIInit(); } *************** *** 361,364 **** --- 387,403 ---- // XXX: object rather than just making it invisible } + + private void startGameUIManager(Game game) { + String gameUIManagerClassName = game.getGameManager().getClassName(Defs.ClassName.GAME_UI_MANAGER); + try { + Class<? extends GameUIManager> gameUIManagerClass = + Class.forName(gameUIManagerClassName).asSubclass(GameUIManager.class); + gameUIManager = gameUIManagerClass.newInstance(); + gameUIManager.init(this); + } catch (Exception e) { + log.fatal("Cannot instantiate class " + gameUIManagerClassName, e); + System.exit(1); + } + } private void fillPlayersPane() { Index: GameUIManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/GameUIManager.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** GameUIManager.java 4 Feb 2009 20:36:40 -0000 1.16 --- GameUIManager.java 4 May 2009 20:29:15 -0000 1.17 *************** *** 3,8 **** import java.io.File; import java.text.SimpleDateFormat; ! import java.util.Date; ! import java.util.List; import javax.swing.JFileChooser; --- 3,7 ---- import java.io.File; import java.text.SimpleDateFormat; ! import java.util.*; import javax.swing.JFileChooser; *************** *** 13,20 **** import rails.common.Defs; import rails.game.*; ! import rails.game.action.GameAction; ! import rails.game.action.PossibleAction; ! import rails.util.Config; ! import rails.util.Util; /** --- 12,18 ---- import rails.common.Defs; import rails.game.*; ! import rails.game.action.*; ! import rails.ui.swing.elements.CheckBoxDialog; ! import rails.util.*; /** *************** *** 55,59 **** --- 53,63 ---- public GameUIManager() { + + } + + public void init (GameSetupWindow gameSetupWindow) { + instance = this; + this.gameSetupWindow = gameSetupWindow; saveDirectory = Config.get("save.directory"); *************** *** 71,75 **** } ! gameSetupWindow = new GameSetupWindow(this); } --- 75,79 ---- } ! //gameSetupWindow = new GameSetupWindow(this); } *************** *** 97,100 **** --- 101,110 ---- } + + public void startLoadedGame() { + gameUIInit(); + processOnServer(null); + statusWindow.setGameActions(); + } public boolean processOnServer(PossibleAction action) { *************** *** 102,106 **** // In some cases an Undo requires a different follow-up lastAction = action; ! log.debug("==Passing to server: " + action); --- 112,117 ---- // In some cases an Undo requires a different follow-up lastAction = action; ! if (action != null) action.setActed(); ! log.debug("==Passing to server: " + action); *************** *** 109,113 **** --- 120,128 ---- action.setPlayerName(player.getName()); } + + // Process the action on the server boolean result = gameManager.process(action); + + // Follow-up the result log.debug("==Result from server: " + result); if (DisplayBuffer.getAutoDisplay()) activeWindow.displayServerMessage(); *************** *** 149,152 **** --- 164,171 ---- // Process consequences of a round type change to the UI + Class<? extends RoundI> previousRoundType + = previousRound == null ? null : previousRound.getClass(); + Class<? extends RoundI> currentRoundType + = currentRound.getClass(); Class<? extends RoundI> previousRoundUItype = previousRound == null ? null : previousRound.getRoundTypeForUI(); *************** *** 154,184 **** = currentRound.getRoundTypeForUI(); if (previousRound == null || !previousRound.equals(currentRound)) { if (previousRound != null) { ! // Finish the previous round UI aspects ! //if (previousRound instanceof StockRound) { ! if (StockRound.class.isAssignableFrom(previousRoundUItype)) { ! log.debug("Finishing Stock Round UI"); statusWindow.finishRound(); ! //} else if (previousRound instanceof StartRound) { ! } else if (StartRound.class.isAssignableFrom(previousRoundUItype)) { ! log.debug("Finishing Start Round UI"); if (startRoundWindow != null) { startRoundWindow.close(); startRoundWindow = null; } ! //} else if (previousRound instanceof OperatingRound) { ! } else if (OperatingRound.class.isAssignableFrom(previousRoundUItype)) { ! log.debug("Finishing Operating Round UI"); orUIManager.finish(); } } ! // Start the new round UI aspects ! //if (currentRound instanceof StartRound) { ! if (StartRound.class.isAssignableFrom(currentRoundUItype)) { ! log.debug("Entering Start Round UI"); startRound = (StartRound) currentRound; if (startRoundWindow == null) { --- 173,210 ---- = currentRound.getRoundTypeForUI(); + /* Distinguish actual round type from visible round type. + * Actual round type is the class of the active Round subclass. + * Visible round type is the class of one of the three 'basic' + * round types: Start, Stock or Operating. + * The latter type determines what UI windows will become visible. + */ + + /* Process actual round type changes */ if (previousRound == null || !previousRound.equals(currentRound)) { + /* Finish previous round UI processing */ if (previousRound != null) { ! ! if (StockRound.class.isAssignableFrom(previousRoundType)) { ! log.debug("UI leaving Stock Round"); statusWindow.finishRound(); ! } else if (StartRound.class.isAssignableFrom(previousRoundType)) { ! log.debug("UI leaving Start Round"); if (startRoundWindow != null) { startRoundWindow.close(); startRoundWindow = null; } ! } else if (OperatingRound.class.isAssignableFrom(previousRoundType)) { ! log.debug("UI leaving Operating Round"); orUIManager.finish(); + } else if (SwitchableUIRound.class.isAssignableFrom(previousRoundType) ) { + log.debug("UI leaving switchable round type"); } } ! // Start the new round UI processing ! if (StartRound.class.isAssignableFrom(currentRoundType)) { ! log.debug("UI entering Start Round"); startRound = (StartRound) currentRound; if (startRoundWindow == null) { *************** *** 186,204 **** } stockChart.setVisible(false); - //} else if (currentRound instanceof StockRound) { } else if (StockRound.class.isAssignableFrom(currentRoundUItype)) { ! log.debug("Entering Stock Round UI"); stockChart.setVisible(true); statusWindow.setVisible(true); - //} else if (currentRound instanceof OperatingRound) { } else if (OperatingRound.class.isAssignableFrom(currentRoundUItype)) { ! log.debug("Entering Operating Round UI"); stockChart.setVisible(false); ! orUIManager.initOR((OperatingRound) currentRound); } } --- 212,263 ---- } + } else if (StockRound.class.isAssignableFrom(currentRoundType)) { + + log.debug("UI entering Stock Round"); + + } else if (OperatingRound.class.isAssignableFrom(currentRoundType)) { + + log.debug("UI entering Operating Round"); + orUIManager.initOR((OperatingRound) currentRound); + } else if (SwitchableUIRound.class.isAssignableFrom(currentRoundType) ) { + log.debug("UI entering switchable round type"); + } + } + + /* Process visible round type changes */ + if (previousRoundUItype == null || !previousRoundUItype.equals(currentRoundUItype)) { + + if (previousRoundUItype != null) { + // Finish the previous round UI aspects + if (StockRound.class.isAssignableFrom(previousRoundUItype)) { + log.debug("Leaving Stock Round UI type"); + } else if (StartRound.class.isAssignableFrom(previousRoundUItype)) { + log.debug("Leaving Start Round UI type"); + } else if (OperatingRound.class.isAssignableFrom(previousRoundUItype)) { + log.debug("Leaving Operating Round UI type"); + orWindow.setVisible(false); + } + } + + // Start the new round UI aspects + if (StartRound.class.isAssignableFrom(currentRoundUItype)) { + + log.debug("Entering Start Round UI type"); + activeWindow = startRoundWindow; stockChart.setVisible(false); } else if (StockRound.class.isAssignableFrom(currentRoundUItype)) { ! log.debug("Entering Stock Round UI type"); ! activeWindow = statusWindow; stockChart.setVisible(true); statusWindow.setVisible(true); } else if (OperatingRound.class.isAssignableFrom(currentRoundUItype)) { ! log.debug("Entering Operating Round UI type "); ! activeWindow = orWindow; stockChart.setVisible(false); ! orWindow.setVisible(true); } } *************** *** 207,230 **** previousRound = currentRound; ! // Update the current round window ! //if (currentRound instanceof StartRound) { if (StartRound.class.isAssignableFrom(currentRoundUItype)) { - activeWindow = startRoundWindow; startRoundWindow.updateStatus(); startRoundWindow.setSRPlayerTurn(startRound.getCurrentPlayerIndex()); - //} else if (currentRound instanceof StockRound) { } else if (StockRound.class.isAssignableFrom(currentRoundUItype)) { - activeWindow = statusWindow; statusWindow.updateStatus(); - //} else if (currentRound instanceof OperatingRound) { } else if (OperatingRound.class.isAssignableFrom(currentRoundUItype)) { - activeWindow = orUIManager.getORWindow(); orUIManager.updateStatus(); } } --- 266,411 ---- previousRound = currentRound; ! // Update the currently visible round window ! // "Switchable" rounds will be handled from subclasses of this class. if (StartRound.class.isAssignableFrom(currentRoundUItype)) { + log.debug("Updating Start round window"); startRoundWindow.updateStatus(); startRoundWindow.setSRPlayerTurn(startRound.getCurrentPlayerIndex()); } else if (StockRound.class.isAssignableFrom(currentRoundUItype)) { + log.debug("Updating Stock (status) round window"); statusWindow.updateStatus(); } else if (OperatingRound.class.isAssignableFrom(currentRoundUItype)) { + log.debug("Updating Operating round window"); orUIManager.updateStatus(); + } else { + // Handle special rounds that do not fall in a standard category + // The round must indicate which main window to raise + if (StockRound.class.isAssignableFrom(currentRoundUItype)) { + log.debug("Updating switched Stock (status) round window"); + activeWindow = statusWindow; + } else if (OperatingRound.class.isAssignableFrom(currentRoundUItype)) { + log.debug("Updating switched Operating round window"); + activeWindow = orWindow; + } + updateStatus(activeWindow); + } + } + + /** Stub, to be overridden in subclasses for special round types */ + protected void updateStatus(ActionPerformer activeWindow) { + + } + + public void discardTrains (DiscardTrain dt) { + + PublicCompanyI c = dt.getCompany(); + String playerName = dt.getPlayerName(); + List<TrainI> trains = dt.getOwnedTrains(); + int size = trains.size() + (dt.isForced() ? 0 : 1); + List<String> trainOptions = + new ArrayList<String>(size); + String[] options = new String[size]; + String prompt = null; + + int j = 0; + if (!dt.isForced()) { + trainOptions.add( + options[j++] = LocalText.getText("None") + ); + prompt = LocalText.getText("MayDiscardTrain", + c.getName()); + } + int offset = j; + for (int i = 0; i < trains.size(); i++) { + trainOptions.add( + options[j++] = LocalText.getText("N_Train", + trains.get(i).getName()) + ); + } + if (prompt == null) prompt = LocalText.getText( + "HAS_TOO_MANY_TRAINS", + playerName, + c.getName() ); + String discardedTrainName = + (String) JOptionPane.showInputDialog(orWindow, + prompt, + LocalText.getText("WhichTrainToDiscard"), + JOptionPane.QUESTION_MESSAGE, null, + options, options[0]); + if (discardedTrainName != null) { + int index = trainOptions.indexOf(discardedTrainName); + if (index >= offset) { + TrainI discardedTrain = + trains.get(trainOptions.indexOf(discardedTrainName)-offset); + dt.setDiscardedTrain(discardedTrain); + } + + orWindow.process(dt); + } + } + + public void exchangeTokens (ExchangeTokens action) { + + int index, cityNumber; + String prompt, cityName, hexName, oldCompName; + String[] ct; + MapHex hex; + List<String> options = new ArrayList<String>(); + City city; + List<ExchangeableToken> oldTokens = action.getTokensToExchange(); + + for (ExchangeableToken t : oldTokens) { + cityName = t.getCityName(); + ct = cityName.split("/"); + hexName = ct[0]; + try { + cityNumber = Integer.parseInt(ct[1]); + } catch (NumberFormatException e) { + cityNumber = 1; + } + hex = orWindow.getMapPanel().getMap().getHexByName (hexName).getHexModel(); + city = hex.getCity(cityNumber); + oldCompName = t.getOldCompanyName(); + options.add(LocalText.getText("ExchangeableToken", + oldCompName, + hexName, + hex.getCityName(), + cityNumber, + city.getTrackEdges())); + } + + + int minNumber = action.getMinNumberToExchange(); + int maxNumber = action.getMaxNumberToExchange(); + if (minNumber == maxNumber) { + prompt = LocalText.getText("ExchangeTokensPrompt1", + minNumber, + action.getCompanyName()); + } else { + prompt = LocalText.getText("ExchangeTokensPrompt2", + minNumber, maxNumber, + action.getCompanyName()); + } + + if (options.size() > 0) { + orWindow.setVisible(true); + boolean[] exchanged = + new CheckBoxDialog(orWindow.getORPanel(), + LocalText.getText("ExchangeTokens"), + prompt, + options.toArray(new String[0])) + .getSelectedOptions(); + for (index=0; index < options.size(); index++) { + if (exchanged[index]) { + oldTokens.get(index).setSelected(true); + } + } + orWindow.process(action); } } *************** *** 257,260 **** --- 438,442 ---- } + /* public boolean loadGame() { *************** *** 285,288 **** --- 467,471 ---- return true; } + */ public PossibleAction getLastAction() { |