From: Erik V. <ev...@us...> - 2012-02-03 20:12:36
|
rails/game/specific/_18EU/StartCompany_18EU.java | 20 - rails/ui/swing/GameStatus.java | 2 rails/ui/swing/GameUIManager.java | 77 +++--- rails/ui/swing/ORUIManager.java | 32 +- rails/ui/swing/StartRoundWindow.java | 5 rails/ui/swing/elements/CheckBoxDialog.java | 11 rails/ui/swing/elements/ConfirmationDialog.java | 111 +-------- rails/ui/swing/elements/MessageDialog.java | 6 rails/ui/swing/elements/NonModalDialog.java | 73 +++--- rails/ui/swing/elements/RadioButtonDialog.java | 6 rails/ui/swing/gamespecific/_1835/GameUIManager_1835.java | 46 ++-- rails/ui/swing/gamespecific/_1835/StatusWindow_1835.java | 12 - rails/ui/swing/gamespecific/_1856/StatusWindow_1856.java | 13 - rails/ui/swing/gamespecific/_18EU/GameStatus_18EU.java | 3 rails/ui/swing/gamespecific/_18EU/GameUIManager_18EU.java | 161 +++++++------- 15 files changed, 256 insertions(+), 322 deletions(-) New commits: commit c734b75e2823f0be6ede03d5d58b57e66e300b3f Author: Erik Vos <eri...@xs...> Date: Fri Feb 3 21:06:37 2012 +0100 Refactored nonmodal dialog classes II Added MessageDialog. Refactored most non-modal dialog processing to take advantage of the new dialog-key concept. The Type and Usage enums (introduced in the previous commit) have been dropped. diff --git a/rails/game/specific/_18EU/StartCompany_18EU.java b/rails/game/specific/_18EU/StartCompany_18EU.java index f8333f1..0c44e69 100644 --- a/rails/game/specific/_18EU/StartCompany_18EU.java +++ b/rails/game/specific/_18EU/StartCompany_18EU.java @@ -86,7 +86,7 @@ public class StartCompany_18EU extends StartCompany { MapHex hex = mapManager.getHex(parts[0]); selectedHomeStation = hex.getStop(Integer.parseInt(parts[1])); } - + return selectedHomeStation; } @@ -97,25 +97,23 @@ public class StartCompany_18EU extends StartCompany { @Override public String toString() { - StringBuffer text = new StringBuffer(super.toString()); - if (minorsToMergeNames != null) { - text.append(" minors=").append(minorsToMergeNames); - } + StringBuilder text = new StringBuilder(super.toString()); if (chosenMinorName != null) { - text.append(" merged minor=" + chosenMinorName); - } - if (availableHomeStationNames != null) { - text.append(" stations=" + availableHomeStationNames); + text.append(" minor=" + chosenMinorName); + } else if (minorsToMergeNames != null) { + text.append(" minors=").append(minorsToMergeNames); } if (selectedHomeStationName != null) { - text.append(" home station=" + selectedHomeStationName); + text.append(" home=" + selectedHomeStationName); + } else if (availableHomeStationNames != null) { + text.append(" homes=" + availableHomeStationNames); } return text.toString(); } /** Deserialize */ private void readObject(ObjectInputStream in) throws IOException, - ClassNotFoundException { + ClassNotFoundException { in.defaultReadObject(); diff --git a/rails/ui/swing/GameStatus.java b/rails/ui/swing/GameStatus.java index dd8e00e..a10dd80 100644 --- a/rails/ui/swing/GameStatus.java +++ b/rails/ui/swing/GameStatus.java @@ -768,7 +768,7 @@ public class GameStatus extends GridPanel implements ActionListener { if (options.size() > 1) { if (startCompany) { RadioButtonDialog dialog = new RadioButtonDialog ( - NonModalDialog.Usage.COMPANY_START_PRICE, + GameUIManager.COMPANY_START_PRICE_DIALOG, gameUIManager, parent, LocalText.getText("PleaseSelect"), diff --git a/rails/ui/swing/GameUIManager.java b/rails/ui/swing/GameUIManager.java index b081250..72a3aa7 100644 --- a/rails/ui/swing/GameUIManager.java +++ b/rails/ui/swing/GameUIManager.java @@ -89,6 +89,12 @@ public class GameUIManager implements DialogOwner { protected boolean previousResult; + /* Keys of dialogs owned by this class */ + public static final String COMPANY_START_PRICE_DIALOG = "CompanyStartPrice"; + public static final String SELECT_COMPANY_DIALOG = "SelectCompany"; + public static final String REPAY_LOANS_DIALOG = "RepayLoans"; + public static final String EXCHANGE_TOKENS_DIALOG = "ExchangeTokens"; + protected static Logger log = Logger.getLogger(GameUIManager.class.getPackage().getName()); @@ -221,7 +227,7 @@ public class GameUIManager implements DialogOwner { // define configWindow configWindow = new ConfigWindow(true); configWindow.init(); - + // notify sound manager of game initialization SoundManager.notifyOfGameInit(gameManager); } @@ -256,7 +262,7 @@ public class GameUIManager implements DialogOwner { // resulting sfx are played in the correct order (first the action // related sfx and then model-change related sfx) SoundManager.notifyOfActionProcessing(gameManager, action); - + // Process the action on the server result = previousResult = processOnServer (action); @@ -272,7 +278,7 @@ public class GameUIManager implements DialogOwner { log.info ("Relinquishing turn to "+newPlayer.getName()); } else if (!wasMyTurn && isMyTurn) { autoLoadPoller.setActive(false); - setCurrentDialog(new MessageDialog(this, + setCurrentDialog(new MessageDialog(null, this, (JFrame) activeWindow, LocalText.getText("Message"), LocalText.getText("YourTurn", localPlayerName)), @@ -341,7 +347,7 @@ public class GameUIManager implements DialogOwner { public boolean displayServerMessage() { String[] message = DisplayBuffer.get(); if (message != null) { - setCurrentDialog(new MessageDialog(this, + setCurrentDialog(new MessageDialog(null, this, (JFrame) activeWindow, LocalText.getText("Message"), "<html>" + Util.joinWithDelimiter(message, "<br>")), @@ -631,7 +637,7 @@ public class GameUIManager implements DialogOwner { orWindow.setVisible(true); orWindow.toFront(); - CheckBoxDialog dialog = new CheckBoxDialog(NonModalDialog.Usage.EXCHANGE_TOKENS, + CheckBoxDialog dialog = new CheckBoxDialog(EXCHANGE_TOKENS_DIALOG, this, orWindow, LocalText.getText("ExchangeTokens"), @@ -650,10 +656,27 @@ public class GameUIManager implements DialogOwner { if (!ready) { - if (checkGameSpecificDialogAction()) { - ; - } else if (currentDialog instanceof RadioButtonDialog - && currentDialogAction instanceof StartCompany) { + String key = ""; + if (currentDialog instanceof NonModalDialog) key = ((NonModalDialog)currentDialog).getKey(); + + if (currentDialog instanceof AutoSaveLoadDialog) { + // Not yet a NonModalDialog subclass + autoSaveLoadGame2 ((AutoSaveLoadDialog)currentDialog); + + } else if (!(currentDialog instanceof NonModalDialog)) { + + log.warn("Unknown dialog action: dialog=["+currentDialog+"] action=["+currentDialogAction+"]"); + currentDialogAction = null; + + } else if (currentDialog instanceof MessageDialog) { + // Nothing to do. + currentDialogAction = null; + // This cancels the currently incomplete user action. + // WARNING: always do this if dialog processing terminates in a context + // where an action is aborted and the UI must return to its previous state. + // This will normally be the case after a CANCEL (but not after a NO). + + } else if (COMPANY_START_PRICE_DIALOG.equals(key)) { RadioButtonDialog dialog = (RadioButtonDialog) currentDialog; StartCompany action = (StartCompany) currentDialogAction; @@ -665,12 +688,10 @@ public class GameUIManager implements DialogOwner { action.setNumberBought(action.getSharesPerCertificate()); } else { // No selection done - no action - return; + currentDialogAction = null; } - - } else if (currentDialog instanceof CheckBoxDialog - && currentDialogAction instanceof ExchangeTokens) { + } else if (EXCHANGE_TOKENS_DIALOG.equals(key)) { CheckBoxDialog dialog = (CheckBoxDialog) currentDialog; ExchangeTokens action = (ExchangeTokens) currentDialogAction; @@ -704,28 +725,20 @@ public class GameUIManager implements DialogOwner { action.getTokensToExchange().get(index).setSelected(true); } } - } else if (currentDialog instanceof RadioButtonDialog - && currentDialogAction instanceof RepayLoans) { + } else if (REPAY_LOANS_DIALOG.equals(key)) { RadioButtonDialog dialog = (RadioButtonDialog) currentDialog; RepayLoans action = (RepayLoans) currentDialogAction; int selected = dialog.getSelectedOption(); action.setNumberTaken(action.getMinNumber() + selected); - } else if (currentDialog instanceof MessageDialog) { - // Nothing to do - currentDialogAction = null; // Should already be null - - } else if (currentDialog instanceof AutoSaveLoadDialog) { - - autoSaveLoadGame2 ((AutoSaveLoadDialog)currentDialog); - } else { - return; + log.warn("Unknown NonModal dialog action: dialog=["+currentDialog+"] action=["+currentDialogAction+"]"); + currentDialogAction = null; } } - /*if (currentDialogAction != null)*/ processAction(currentDialogAction); + processAction(currentDialogAction); } @@ -988,16 +1001,6 @@ public class GameUIManager implements DialogOwner { } - /* - public boolean isMyTurn() { - return myTurn; - } - - public void setMyTurn(boolean myTurn) { - this.myTurn = myTurn; - } - */ - public void setSaveDirectory(String saveDirectory) { this.saveDirectory = saveDirectory; } @@ -1042,10 +1045,6 @@ public class GameUIManager implements DialogOwner { return gameManager.getPlayers(); } - //public List<String> getPlayerNames() { - // return gameManager.getPlayerNames(); - //} - public Player getCurrentPlayer() { return gameManager.getCurrentPlayer(); } diff --git a/rails/ui/swing/ORUIManager.java b/rails/ui/swing/ORUIManager.java index 226a8b5..ea2d561 100644 --- a/rails/ui/swing/ORUIManager.java +++ b/rails/ui/swing/ORUIManager.java @@ -90,6 +90,10 @@ public class ORUIManager implements DialogOwner { "ConfirmToken", "SetRevenue", "SelectPayout", "CorrectMap" }; + /* Keys of dialogs owned by this class */ + public static final String SELECT_DESTINATION_COMPANIES_DIALOG = "SelectDestinationCompanies"; + public static final String REPAY_LOANS_DIALOG = "RepayLoans"; + protected static Logger log = Logger.getLogger(ORUIManager.class.getPackage().getName()); @@ -494,7 +498,7 @@ public class ORUIManager implements DialogOwner { orPanel.stopRevenueUpdate(); log.debug("Set revenue amount is " + amount); action.setActualRevenue(amount); - + // notify sound manager of set revenue amount as soon as // set revenue is pressed (not waiting for the completion // of the set dividend action) @@ -554,7 +558,7 @@ public class ORUIManager implements DialogOwner { orWindow.setVisible(true); orWindow.toFront(); - CheckBoxDialog dialog = new CheckBoxDialog(NonModalDialog.Usage.SELECT_DESTINATION_COMPANIES, + CheckBoxDialog dialog = new CheckBoxDialog(SELECT_DESTINATION_COMPANIES_DIALOG, this, orWindow, LocalText.getText("DestinationsReached"), @@ -614,7 +618,7 @@ public class ORUIManager implements DialogOwner { public boolean hexClicked(GUIHex clickedHex, GUIHex selectedHex) { boolean triggerORPanelRepaint = false; - + if (mapCorrectionEnabled) { triggerORPanelRepaint = true; boolean checkClickedHex = false; @@ -696,7 +700,7 @@ public class ORUIManager implements DialogOwner { } if (triggerORPanelRepaint) orWindow.repaintORPanel(); - + return triggerORPanelRepaint; } @@ -730,7 +734,7 @@ public class ORUIManager implements DialogOwner { upgradePanel.showUpgrades(); } } - + /** * @return True if the indicated tile must be connected to some other track if * placed on indicated hex. @@ -738,26 +742,26 @@ public class ORUIManager implements DialogOwner { public boolean getMustConnectRequirement (GUIHex hex,TileI tile) { if (tile == null || hex == null) return false; return tile.getColourName().equalsIgnoreCase(Tile.YELLOW_COLOUR_NAME) - // Does not apply to the current company's home hex(es) - && !hex.getHexModel().isHomeFor(orComp) - // Does not apply to special tile lays - && !isUnconnectedTileLayTarget(hex.getHexModel()); + // Does not apply to the current company's home hex(es) + && !hex.getHexModel().isHomeFor(orComp) + // Does not apply to special tile lays + && !isUnconnectedTileLayTarget(hex.getHexModel()); } - + public void addTileUpgradeIfValid(GUIHex hex, int tileId) { addTileUpgradeIfValid (hex, gameUIManager.getGameManager().getTileManager().getTile(tileId)); } - + public void addTileUpgradeIfValid(GUIHex hex, TileI tile) { if (!tileUpgrades.contains(tile) && isTileUpgradeValid(hex,tile)) { tileUpgrades.add(tile); } } - + public boolean isTileUpgradeValid(GUIHex hex, TileI tile) { // Check if the new tile must be connected to some other track - return hex.isTileUpgradeValid(tile.getId(), + return hex.isTileUpgradeValid(tile.getId(), getMustConnectRequirement(hex,tile)); } @@ -1477,7 +1481,7 @@ public class ORUIManager implements DialogOwner { Bank.format(i * loanAmount)); } } - RadioButtonDialog currentDialog = new RadioButtonDialog (NonModalDialog.Usage.REPAY_LOANS, + RadioButtonDialog currentDialog = new RadioButtonDialog (REPAY_LOANS_DIALOG, gameUIManager, orWindow, LocalText.getText("Select"), diff --git a/rails/ui/swing/StartRoundWindow.java b/rails/ui/swing/StartRoundWindow.java index 47f0a9a..1ff36cd 100644 --- a/rails/ui/swing/StartRoundWindow.java +++ b/rails/ui/swing/StartRoundWindow.java @@ -112,6 +112,9 @@ implements ActionListener, KeyListener, ActionPerformer, DialogOwner { private boolean includeBidding; private boolean showBasePrices; + /* Keys of dialogs owned by this class */ + public static final String COMPANY_START_PRICE_DIALOG = "CompanyStartPrice"; + protected static Logger log = Logger.getLogger(StartRoundWindow.class.getPackage().getName()); @@ -653,7 +656,7 @@ implements ActionListener, KeyListener, ActionPerformer, DialogOwner { } RadioButtonDialog dialog = new RadioButtonDialog( - NonModalDialog.Usage.COMPANY_START_PRICE, + COMPANY_START_PRICE_DIALOG, this, this, LocalText.getText("PleaseSelect"), diff --git a/rails/ui/swing/elements/CheckBoxDialog.java b/rails/ui/swing/elements/CheckBoxDialog.java index 0a56e91..d2fd91a 100644 --- a/rails/ui/swing/elements/CheckBoxDialog.java +++ b/rails/ui/swing/elements/CheckBoxDialog.java @@ -24,15 +24,16 @@ public class CheckBoxDialog extends NonModalDialog { int chosenOption = -1; boolean hasCancelButton = false; - public CheckBoxDialog(Usage usage, DialogOwner owner, JFrame window, String title, String message, + public CheckBoxDialog(String key, DialogOwner owner, JFrame window, String title, String message, String[] options) { - this (usage, owner, window, title, message, options, null, false); + this (key, owner, window, title, message, options, null, false); } - public CheckBoxDialog(Usage usage, DialogOwner owner, JFrame window, String title, String message, + public CheckBoxDialog(String key, DialogOwner owner, JFrame window, String title, String message, String[] options, boolean[] selectedOptions, boolean addCancelButton) { - super (Type.CHECKBOX, usage, owner, window, title, message, addCancelButton); + super (key, owner, window, title, message); + this.hasCancelButton = addCancelButton; this.options = options; this.numOptions = options.length; @@ -42,7 +43,7 @@ public class CheckBoxDialog extends NonModalDialog { this.selectedOptions = new boolean[numOptions]; } - initialize(); + initialize(hasCancelButton); } @Override diff --git a/rails/ui/swing/elements/ConfirmationDialog.java b/rails/ui/swing/elements/ConfirmationDialog.java index c3269b9..cbcdf6b 100644 --- a/rails/ui/swing/elements/ConfirmationDialog.java +++ b/rails/ui/swing/elements/ConfirmationDialog.java @@ -1,118 +1,35 @@ /* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/ui/swing/elements/ConfirmationDialog.java,v 1.1 2010/02/28 21:38:06 evos Exp $*/ package rails.ui.swing.elements; -import java.awt.*; -import java.awt.event.*; +import java.awt.event.ActionEvent; -import javax.swing.*; - -import org.apache.log4j.Logger; - -import rails.common.LocalText; +import javax.swing.JFrame; /** - * A generic YES/NO dialog + * A generic YES/NO dialog */ -public class ConfirmationDialog extends JDialog implements ActionListener { +public class ConfirmationDialog extends NonModalDialog { private static final long serialVersionUID = 1L; - GridBagConstraints gc; - JPanel messagePane, buttonPane; - JButton okButton, cancelButton; - Dimension size, optSize; - DialogOwner owner; - String message; boolean answer = false; - protected static Logger log = - Logger.getLogger(ConfirmationDialog.class.getPackage().getName()); - - public ConfirmationDialog(DialogOwner owner, String title, String message, - String okText, String cancelText) { - super((Frame) null, title, false); // Non-modal - this.owner = owner; - this.message = message; - - initialize(okText, cancelText); - pack(); + public ConfirmationDialog(String key, DialogOwner owner, JFrame window, String title, String message, + String okTextKey, String cancelTextKey) { - // Center on owner - /* - int x = - (int) owner.getLocationOnScreen().getX() - + (owner.getWidth() - getWidth()) / 2; - int y = - (int) owner.getLocationOnScreen().getY() - + (owner.getHeight() - getHeight()) / 2; - */ - int x = 400; - int y = 400; - setLocation(x, y); + super (key, owner, window, title, message); - setVisible(true); - setAlwaysOnTop(true); + initialize(okTextKey, cancelTextKey); } - private void initialize(String okText, String cancelText) { - - gc = new GridBagConstraints(); - - getContentPane().setLayout(new GridBagLayout()); - setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - - messagePane = new JPanel(); - - messagePane.add(new JLabel(message)); - - buttonPane = new JPanel(); - - okButton = new JButton(LocalText.getText(okText)); - // We only expect Yes/No or OK/Cancel - if (okText.startsWith("O")) - okButton.setMnemonic(KeyEvent.VK_O); - else if (okText.startsWith("Y")) - okButton.setMnemonic(KeyEvent.VK_Y); - okButton.addActionListener(this); - buttonPane.add(okButton); - - cancelButton = new JButton(LocalText.getText(cancelText)); - // We only expect Yes/No or OK/Cancel - if (cancelText.startsWith("C")) - cancelButton.setMnemonic(KeyEvent.VK_C); - else if (cancelText.startsWith("N")) - cancelButton.setMnemonic(KeyEvent.VK_N); - cancelButton.addActionListener(this); - buttonPane.add(cancelButton); - - getContentPane().add(messagePane, constraints(0, 0, 0, 0, 0, 0)); - getContentPane().add(buttonPane, constraints(0, 1, 0, 0, 0, 0)); - } - - private GridBagConstraints constraints(int gridx, int gridy, int leftinset, - int topinset, int rightinset, int bottominset) { - if (gridx >= 0) gc.gridx = gridx; - if (gridy >= 0) gc.gridy = gridy; - gc.fill = GridBagConstraints.BOTH; - gc.weightx = 0.5; - gc.weighty = 0.5; - if (leftinset >= 0) gc.insets.left = leftinset; - if (topinset >= 0) gc.insets.top = topinset; - if (rightinset >= 0) gc.insets.right = rightinset; - if (bottominset >= 0) gc.insets.bottom = bottominset; - - return gc; + @Override + protected void processOK (ActionEvent actionEvent) { + answer = true; } - public void actionPerformed(ActionEvent arg0) { - if (arg0.getSource().equals(okButton)) { - answer = true; - } else if (arg0.getSource().equals(cancelButton)) { - answer = false; - } - this.setVisible(false); - this.dispose(); - owner.dialogActionPerformed(); + @Override + protected void processCancel (ActionEvent actionEvent) { + answer = false; } public synchronized boolean getAnswer() { diff --git a/rails/ui/swing/elements/MessageDialog.java b/rails/ui/swing/elements/MessageDialog.java index e4bfe66..9a6dad3 100644 --- a/rails/ui/swing/elements/MessageDialog.java +++ b/rails/ui/swing/elements/MessageDialog.java @@ -10,11 +10,11 @@ public class MessageDialog extends NonModalDialog { private static final long serialVersionUID = 1L; - public MessageDialog(DialogOwner owner, JFrame window, String title, String message) { + public MessageDialog(String key, DialogOwner owner, JFrame window, String title, String message) { - super (Type.MESSAGE, null, owner, window, title, message, false); + super (key, owner, window, title, message); - initialize(); + initialize(false); } diff --git a/rails/ui/swing/elements/NonModalDialog.java b/rails/ui/swing/elements/NonModalDialog.java index 0fe730c..72718d2 100644 --- a/rails/ui/swing/elements/NonModalDialog.java +++ b/rails/ui/swing/elements/NonModalDialog.java @@ -8,55 +8,47 @@ import javax.swing.*; import org.apache.log4j.Logger; import rails.common.LocalText; +import rails.util.Util; public abstract class NonModalDialog extends JDialog implements ActionListener { private static final long serialVersionUID = 1L; - protected Type type; - protected Usage usage; + protected String key; protected DialogOwner owner = null; protected JFrame window = null; protected String message; - protected boolean hasCancelButton = false; + protected boolean hasCancelButton = true; GridBagConstraints gc; JPanel optionsPane, buttonPane; JButton okButton, cancelButton; - - public static enum Usage { - REPAY_LOANS, - DESTINATION_REACHED, - BUY_WHICH_TRAIN, - COMPANY_START_PRICE, - EXCHANGE_TOKENS, - SELECT_FOLDING_COMPANIES, - SELECT_DESTINATION_COMPANIES, - SELECT_COMPANY, - SELECT_HOME_STATION - } - - public static enum Type { - CHECKBOX, - RADIO, - MESSAGE, - MIXED - } + String okTextKey = "OK"; + String cancelTextKey = "Cancel"; protected static Logger log = Logger.getLogger(NonModalDialog.class.getPackage().getName()); - public NonModalDialog(Type type, Usage usage, - DialogOwner owner, JFrame window, String title, String message, - boolean addCancelButton) { + public NonModalDialog(String key, + DialogOwner owner, JFrame window, String title, String message) { super((Frame) null, title, false); // Non-modal - this.type = type; - this.usage = usage; + this.key = key; this.owner = owner; this.window = window; this.message = message; - hasCancelButton = addCancelButton; + } + + protected final void initialize (String okTextKey, String cancelTextKey) { + this.okTextKey = okTextKey; + this.cancelTextKey = cancelTextKey; + this.hasCancelButton = Util.hasValue(cancelTextKey); + initialize(); + } + + protected final void initialize (boolean hasCancelButton) { + this.hasCancelButton = hasCancelButton; + initialize(); } protected final void initialize() { @@ -66,14 +58,14 @@ public abstract class NonModalDialog extends JDialog implements ActionListener { optionsPane = new JPanel(); buttonPane = new JPanel(); - okButton = new JButton(LocalText.getText("OK")); - okButton.setMnemonic(KeyEvent.VK_O); + okButton = new JButton(LocalText.getText(okTextKey)); + okButton.setMnemonic(okTextKey.startsWith("Y") ? KeyEvent.VK_Y : KeyEvent.VK_O); okButton.addActionListener(this); buttonPane.add(okButton); if (hasCancelButton) { - cancelButton = new JButton(LocalText.getText("Cancel")); - cancelButton.setMnemonic(KeyEvent.VK_C); + cancelButton = new JButton(LocalText.getText(cancelTextKey)); + cancelButton.setMnemonic(cancelTextKey.startsWith("N") ? KeyEvent.VK_N : KeyEvent.VK_C); cancelButton.addActionListener(this); buttonPane.add(cancelButton); } @@ -102,8 +94,7 @@ public abstract class NonModalDialog extends JDialog implements ActionListener { setAlwaysOnTop(true); } - protected void initializeInput() { - } + protected void initializeInput() {} protected GridBagConstraints constraints(int gridx, int gridy, int leftinset, int topinset, int rightinset, int bottominset) { @@ -126,12 +117,22 @@ public abstract class NonModalDialog extends JDialog implements ActionListener { } else if (actionEvent.getSource().equals(cancelButton)) { processCancel (actionEvent); } + log.debug("Processed: dialog="+getClass().getSimpleName()+" key="+key); setVisible(false); dispose(); owner.dialogActionPerformed (); } - protected void processOK (ActionEvent actionEvent) {}; + protected void processOK (ActionEvent actionEvent) {} + + protected void processCancel (ActionEvent actionEvent) {} - protected void processCancel (ActionEvent actionEvent) {}; + public String getKey() { + return key; + } + + @Override + public String toString() { + return "Dialog type="+getClass().getSimpleName()+" key="+key; + } } diff --git a/rails/ui/swing/elements/RadioButtonDialog.java b/rails/ui/swing/elements/RadioButtonDialog.java index 1d7cc59..cbd4306 100644 --- a/rails/ui/swing/elements/RadioButtonDialog.java +++ b/rails/ui/swing/elements/RadioButtonDialog.java @@ -22,16 +22,16 @@ public class RadioButtonDialog extends NonModalDialog { int selectedOption; int chosenOption = -1; - public RadioButtonDialog(Usage usage, DialogOwner owner, JFrame window, String title, String message, + public RadioButtonDialog(String key, DialogOwner owner, JFrame window, String title, String message, String[] options, int selectedOption) { - super (Type.RADIO, usage, owner, window, title, message, selectedOption < 0); + super (key, owner, window, title, message); this.options = options; this.numOptions = options.length; this.selectedOption = selectedOption; - initialize(); + initialize(selectedOption < 0); } @Override diff --git a/rails/ui/swing/gamespecific/_1835/GameUIManager_1835.java b/rails/ui/swing/gamespecific/_1835/GameUIManager_1835.java index 22654a6..192bd50 100644 --- a/rails/ui/swing/gamespecific/_1835/GameUIManager_1835.java +++ b/rails/ui/swing/gamespecific/_1835/GameUIManager_1835.java @@ -6,45 +6,51 @@ import java.util.List; import rails.game.CompanyI; import rails.game.specific._1835.FoldIntoPrussian; import rails.ui.swing.GameUIManager; -import rails.ui.swing.elements.CheckBoxDialog; -import rails.ui.swing.elements.ConfirmationDialog; +import rails.ui.swing.elements.*; public class GameUIManager_1835 extends GameUIManager { - protected boolean checkGameSpecificDialogAction() { - - if (currentDialog instanceof ConfirmationDialog - && currentDialogAction instanceof FoldIntoPrussian) { - + // Keys of dialogs owned by this class. + public static final String START_PRUSSIAN_DIALOG = "StartPrussian"; + public static final String MERGE_INTO_PRUSSIAN_DIALOG = "MergeIntoPrussian"; + + @Override + public void dialogActionPerformed() { + + String key = ""; + if (currentDialog instanceof NonModalDialog) key = ((NonModalDialog)currentDialog).getKey(); + + // Check for the dialogs that are postprocessed in this class. + if (START_PRUSSIAN_DIALOG.equals(key)) { + ConfirmationDialog dialog = (ConfirmationDialog) currentDialog; FoldIntoPrussian action = (FoldIntoPrussian) currentDialogAction; if (dialog.getAnswer()) { action.setFoldedCompanies(action.getFoldableCompanies()); } - - return true; - - } else if (currentDialog instanceof CheckBoxDialog - && currentDialogAction instanceof FoldIntoPrussian) { - + + } else if (MERGE_INTO_PRUSSIAN_DIALOG.equals(key)) { + CheckBoxDialog dialog = (CheckBoxDialog) currentDialog; FoldIntoPrussian action = (FoldIntoPrussian) currentDialogAction; boolean[] exchanged = dialog.getSelectedOptions(); String[] options = dialog.getOptions(); - + List<CompanyI> foldedCompanies = new ArrayList<CompanyI>(); for (int index=0; index < options.length; index++) { if (exchanged[index]) { foldedCompanies.add(action.getFoldableCompanies().get(index)); - } + } } action.setFoldedCompanies(foldedCompanies); - - return true; - + } else { - return false; + // Current dialog not found yet, try the superclass. + super.dialogActionPerformed(false); + return; } - } + // Dialog action found and processed, let the superclass initiate processing. + super.dialogActionPerformed(true); + } } diff --git a/rails/ui/swing/gamespecific/_1835/StatusWindow_1835.java b/rails/ui/swing/gamespecific/_1835/StatusWindow_1835.java index aed46cc..75924af 100644 --- a/rails/ui/swing/gamespecific/_1835/StatusWindow_1835.java +++ b/rails/ui/swing/gamespecific/_1835/StatusWindow_1835.java @@ -2,8 +2,6 @@ package rails.ui.swing.gamespecific._1835; import java.util.List; -import javax.swing.JDialog; - import rails.common.LocalText; import rails.game.*; import rails.game.action.DiscardTrain; @@ -63,17 +61,19 @@ public class StatusWindow_1835 extends StatusWindow { protected void fold (FoldIntoPrussian action) { List<CompanyI> foldables = action.getFoldableCompanies(); - JDialog currentDialog; + NonModalDialog currentDialog; if (foldables.get(0).getName().equals("M2")) { // Ask if the Prussian should be started - currentDialog = new ConfirmationDialog (gameUIManager, + currentDialog = new ConfirmationDialog (GameUIManager_1835.START_PRUSSIAN_DIALOG, + gameUIManager, this, LocalText.getText("Select"), LocalText.getText("MergeMinorConfirm", getCurrentPlayer().getName(), GameManager_1835.M2_ID, GameManager_1835.PR_ID), "Yes", - "No"); + "No" + ); } else { // Ask if any other prePrussians should be folded String[] options = new String[foldables.size()]; @@ -87,7 +87,7 @@ public class StatusWindow_1835 extends StatusWindow { ((ExchangeForShare)(company.getSpecialProperties().get(0))).getShare() ); } - currentDialog = new CheckBoxDialog (NonModalDialog.Usage.SELECT_FOLDING_COMPANIES, + currentDialog = new CheckBoxDialog (GameUIManager_1835.MERGE_INTO_PRUSSIAN_DIALOG, gameUIManager, this, LocalText.getText("Select"), diff --git a/rails/ui/swing/gamespecific/_1856/StatusWindow_1856.java b/rails/ui/swing/gamespecific/_1856/StatusWindow_1856.java index b48d117..4a4b7e7 100644 --- a/rails/ui/swing/gamespecific/_1856/StatusWindow_1856.java +++ b/rails/ui/swing/gamespecific/_1856/StatusWindow_1856.java @@ -8,8 +8,8 @@ import rails.game.Bank; import rails.game.RoundI; import rails.game.action.*; import rails.game.specific._1856.CGRFormationRound; +import rails.ui.swing.GameUIManager; import rails.ui.swing.StatusWindow; -import rails.ui.swing.elements.NonModalDialog; import rails.ui.swing.elements.RadioButtonDialog; import rails.util.Util; @@ -52,15 +52,6 @@ public class StatusWindow_1856 extends StatusWindow { repayLoans (nextAction); return true; - /* Moved up - } else if (immediateAction instanceof DiscardTrain) { - // Make a local copy and discard the original, - // so that it's not going to loop. - DiscardTrain nextAction = (DiscardTrain) immediateAction; - immediateAction = null; - gameUIManager.discardTrains (nextAction); - return true; - */ } else if (immediateAction instanceof ExchangeTokens) { // Make a local copy and discard the original, // so that it's not going to loop. @@ -100,7 +91,7 @@ public class StatusWindow_1856 extends StatusWindow { + "<br>" + message; } - RadioButtonDialog currentDialog = new RadioButtonDialog (NonModalDialog.Usage.REPAY_LOANS, + RadioButtonDialog currentDialog = new RadioButtonDialog (GameUIManager.REPAY_LOANS_DIALOG, gameUIManager, this, LocalText.getText("1856MergerDialog", action.getCompanyName()), diff --git a/rails/ui/swing/gamespecific/_18EU/GameStatus_18EU.java b/rails/ui/swing/gamespecific/_18EU/GameStatus_18EU.java index 81e816d..f3378b3 100644 --- a/rails/ui/swing/gamespecific/_18EU/GameStatus_18EU.java +++ b/rails/ui/swing/gamespecific/_18EU/GameStatus_18EU.java @@ -8,7 +8,6 @@ import rails.game.PublicCompanyI; import rails.game.action.MergeCompanies; import rails.game.action.PossibleAction; import rails.ui.swing.GameStatus; -import rails.ui.swing.elements.NonModalDialog; import rails.ui.swing.elements.RadioButtonDialog; /** @@ -71,7 +70,7 @@ public class GameStatus_18EU extends GameStatus { } RadioButtonDialog dialog = new RadioButtonDialog ( - NonModalDialog.Usage.SELECT_COMPANY, + GameUIManager_18EU.SELECT_MERGING_MAJOR, gameUIManager, parent, LocalText.getText("PleaseSelect"), diff --git a/rails/ui/swing/gamespecific/_18EU/GameUIManager_18EU.java b/rails/ui/swing/gamespecific/_18EU/GameUIManager_18EU.java index 5c17748..0c6cc86 100644 --- a/rails/ui/swing/gamespecific/_18EU/GameUIManager_18EU.java +++ b/rails/ui/swing/gamespecific/_18EU/GameUIManager_18EU.java @@ -15,123 +15,138 @@ import rails.ui.swing.elements.RadioButtonDialog; public class GameUIManager_18EU extends GameUIManager { + // Keys of dialogs owned by this class. + public static final String SELECT_CONVERTING_MINOR = "SelectConvertingMinor"; + public static final String SELECT_MERGING_MAJOR = "SelectMergingMajor"; + public static final String SELECT_HOME_STATION_DIALOG = "SelectHomeStation"; + @Override public void dialogActionPerformed () { - if (currentDialog instanceof RadioButtonDialog - && currentDialogAction instanceof MergeCompanies) { + String key = ""; + if (currentDialog instanceof NonModalDialog) key = ((NonModalDialog)currentDialog).getKey(); + + // Check for the dialogs that are postprocessed in this class. + if (SELECT_MERGING_MAJOR.equals(key)) { + // A major company has been selected (or not) to merge a minor into. RadioButtonDialog dialog = (RadioButtonDialog) currentDialog; MergeCompanies action = (MergeCompanies) currentDialogAction; PublicCompanyI minor = action.getMergingCompany(); - if (action.getSelectedTargetCompany() == null) { - // Step 1: selection of the major company to merge into - int choice = dialog.getSelectedOption(); - if (choice < 0) return; + int choice = dialog.getSelectedOption(); + if (choice < 0) return; - PublicCompanyI major = action.getTargetCompanies().get(choice); - action.setSelectedTargetCompany(major); + PublicCompanyI major = action.getTargetCompanies().get(choice); + action.setSelectedTargetCompany(major); - if (major != null && action.canReplaceToken(choice)) { + if (major != null && action.canReplaceToken(choice)) { - boolean replaceToken = - JOptionPane.showConfirmDialog(statusWindow, LocalText.getText( - "WantToReplaceToken", - minor.getName(), - major.getName() ), - LocalText.getText("PleaseSelect"), - JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; - action.setReplaceToken(replaceToken); - } + boolean replaceToken = + JOptionPane.showConfirmDialog(statusWindow, LocalText.getText( + "WantToReplaceToken", + minor.getName(), + major.getName() ), + LocalText.getText("PleaseSelect"), + JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; + action.setReplaceToken(replaceToken); + } + + } else if (SELECT_CONVERTING_MINOR.equals(key)) { + + // A minor has been selected (or not) to merge into a starting company before phase 6. + RadioButtonDialog dialog = (RadioButtonDialog) currentDialog; + StartCompany_18EU action = (StartCompany_18EU) currentDialogAction; + int choice = dialog.getSelectedOption(); + if (choice < 0) { + currentDialogAction = null; } else { - // To be added later when ReplaceToken dialog is modeless + PublicCompanyI minor = action.getMinorsToMerge().get(choice); + action.setChosenMinor(minor); } - } else if (currentDialog instanceof RadioButtonDialog + } else if (COMPANY_START_PRICE_DIALOG.equals(key) && currentDialogAction instanceof StartCompany_18EU) { + // A start price has been selected (or not) for a stating major company. RadioButtonDialog dialog = (RadioButtonDialog) currentDialog; StartCompany_18EU action = (StartCompany_18EU) currentDialogAction; - if (action.getPrice() == 0) { + int index = dialog.getSelectedOption(); + if (index < 0) { + currentDialogAction = null; + return; + } + action.setStartPrice(action.getStartPrices()[index]); + + // Set up another dialog for the next step + List<PublicCompanyI> minors = action.getMinorsToMerge(); + + if (minors != null && !minors.isEmpty()) { + // Up to phase 6, a minor must be exchanged + String[] options = new String[minors.size()]; + int i = 0; + for (PublicCompanyI minor : minors) { + options[i++] = + "Minor " + minor.getName() + " " + + minor.getLongName(); + } + dialog = new RadioButtonDialog (SELECT_CONVERTING_MINOR, + this, + statusWindow, + LocalText.getText("PleaseSelect"), + LocalText.getText( + "SelectMinorToMerge", + action.getCompanyName()), + options, -1); + setCurrentDialog(dialog, action); + statusWindow.disableButtons(); + return; + } else { - // The price will be set first - int index = dialog.getSelectedOption(); - if (index < 0) return; - action.setStartPrice(action.getStartPrices()[index]); - - // Set up another dialog for the next step - List<PublicCompanyI> minors = action.getMinorsToMerge(); - - if (minors != null && !minors.isEmpty()) { - // Up to phase 6, a minor must be exchanged - String[] options = new String[minors.size()]; - int i = 0; - for (PublicCompanyI minor : minors) { - options[i++] = - "Minor " + minor.getName() + " " - + minor.getLongName(); + // From phase 6, no minors are involved, but a home station must be chosen + List<Stop> cities = action.getAvailableHomeStations(); + if (cities != null && !cities.isEmpty()) { + String[] options = new String[cities.size()]; + for (int i = 0; i < options.length; i++) { + options[i] = cities.get(i).toString(); } - dialog = new RadioButtonDialog (NonModalDialog.Usage.SELECT_FOLDING_COMPANIES, + dialog = new RadioButtonDialog (SELECT_HOME_STATION_DIALOG, this, statusWindow, LocalText.getText("PleaseSelect"), LocalText.getText( - "SelectMinorToMerge", + "SelectHomeStation", action.getCompanyName()), options, -1); setCurrentDialog(dialog, action); statusWindow.disableButtons(); return; - } else { - - // From phase 6, no minors are involved, but a home station must be chosen - List<Stop> cities = action.getAvailableHomeStations(); - if (cities != null && !cities.isEmpty()) { - String[] options = new String[cities.size()]; - for (int i = 0; i < options.length; i++) { - options[i] = cities.get(i).toString(); - } - dialog = new RadioButtonDialog (NonModalDialog.Usage.SELECT_HOME_STATION, - this, - statusWindow, - LocalText.getText("PleaseSelect"), - LocalText.getText( - "SelectHomeStation", - action.getCompanyName()), - options, -1); - setCurrentDialog(dialog, action); - statusWindow.disableButtons(); - return; - - } - } - } else if (action.getMinorsToMerge() != null) { - // Up to phase 5: a minor to merge has been selected (or not) - int choice = dialog.getSelectedOption(); - if (choice < 0) { - // Also reset price - action.setStartPrice(0); - return; } - action.setChosenMinor(action.getMinorsToMerge().get(choice)); + } + + } else if (SELECT_HOME_STATION_DIALOG.equals(key)) { + + RadioButtonDialog dialog = (RadioButtonDialog) currentDialog; + StartCompany_18EU action = (StartCompany_18EU) currentDialogAction; - } else if (action.getAvailableHomeStations() != null) { + if (action.getAvailableHomeStations() != null) { // From phase 6: a home station has been selected (or not) int index = dialog.getSelectedOption(); if (index < 0) { - // Also reset price - action.setStartPrice(0); + currentDialogAction = null; return; } action.setHomeStation(action.getAvailableHomeStations().get(index)); } } else { + // Current dialog not found yet, try the superclass. super.dialogActionPerformed(false); + return; } + // Dialog action found and processed, let the superclass initiate processing. super.dialogActionPerformed(true); } |