From: <ev...@us...> - 2011-02-06 19:29:12
|
Revision: 1482 http://rails.svn.sourceforge.net/rails/?rev=1482&view=rev Author: evos Date: 2011-02-06 19:29:06 +0000 (Sun, 06 Feb 2011) Log Message: ----------- Added "Load Recent Game" option to game setup screen. This option offers a selection of the 20 most recent saved files in and below the configured save directory. Modified Paths: -------------- trunk/18xx/LocalisedText.properties trunk/18xx/rails/ui/swing/GameSetupWindow.java trunk/18xx/rails/ui/swing/GameUIManager.java Modified: trunk/18xx/LocalisedText.properties =================================================================== --- trunk/18xx/LocalisedText.properties 2011-02-05 20:00:15 UTC (rev 1481) +++ trunk/18xx/LocalisedText.properties 2011-02-06 19:29:06 UTC (rev 1482) @@ -352,6 +352,7 @@ LeaveAuctionOnPass=Leave private auction on pass LoadFailed=Load failed, reason: {0} LoadGame=Load Game +LoadRecentGame=Load Recent LOAD=Load LoadInterrupted=Load interrupted at this point, you can continue play from here LoansNotAllowed={0} may not take any loans Modified: trunk/18xx/rails/ui/swing/GameSetupWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/GameSetupWindow.java 2011-02-05 20:00:15 UTC (rev 1481) +++ trunk/18xx/rails/ui/swing/GameSetupWindow.java 2011-02-06 19:29:06 UTC (rev 1482) @@ -4,6 +4,7 @@ import java.awt.*; import java.awt.event.*; import java.io.File; +import java.io.IOException; import java.util.*; import java.util.List; @@ -13,8 +14,7 @@ import rails.common.GuiDef; import rails.game.*; -import rails.util.Config; -import rails.util.LocalText; +import rails.util.*; /** * The Game Setup Window displays the first window presented to the user. This @@ -25,7 +25,7 @@ private static final long serialVersionUID = 1L; GridBagConstraints gc; JPanel gameListPane, playersPane, buttonPane, optionsPane; - JButton newButton, loadButton, recoveryButton, quitButton, optionButton, infoButton, + JButton newButton, loadButton, recentButton, recoveryButton, quitButton, optionButton, infoButton, creditsButton, randomizeButton, configureButton; JComboBox gameNameBox = new JComboBox(); JComboBox[] playerBoxes = new JComboBox[Player.MAX_PLAYERS]; @@ -39,6 +39,9 @@ List<GameOption> availableOptions = new ArrayList<GameOption>(); String gameName; Game game; + + SortedSet<File> recentFiles; + String savedFileExtension; private ConfigWindow configWindow; @@ -68,6 +71,7 @@ newButton = new JButton(LocalText.getText("NewGame")); loadButton = new JButton(LocalText.getText("LoadGame")); + recentButton = new JButton(LocalText.getText("LoadRecentGame")); recoveryButton = new JButton(LocalText.getText("RecoverGame")); quitButton = new JButton(LocalText.getText("QUIT")); optionButton = new JButton(LocalText.getText("OPTIONS")); @@ -77,6 +81,7 @@ newButton.setMnemonic(KeyEvent.VK_N); loadButton.setMnemonic(KeyEvent.VK_L); + recentButton.setMnemonic(KeyEvent.VK_D); recoveryButton.setMnemonic(KeyEvent.VK_R); quitButton.setMnemonic(KeyEvent.VK_Q); optionButton.setMnemonic(KeyEvent.VK_O); @@ -99,6 +104,7 @@ newButton.addActionListener(this); loadButton.addActionListener(this); + recentButton.addActionListener(this); recoveryButton.addActionListener(this); quitButton.addActionListener(this); optionButton.addActionListener(this); @@ -109,6 +115,7 @@ buttonPane.add(newButton); buttonPane.add(loadButton); + buttonPane.add(recentButton); recoveryButton.setEnabled(Config.get("save.recovery.active", "no").equalsIgnoreCase("yes")); buttonPane.add(recoveryButton); @@ -116,7 +123,7 @@ buttonPane.add(quitButton); buttonPane.add(creditsButton); - buttonPane.setLayout(new GridLayout(3, 2)); + buttonPane.setLayout(new GridLayout(0, 2)); buttonPane.setBorder(BorderFactory.createLoweredBevelBorder()); optionsPane.setLayout(new FlowLayout()); @@ -252,6 +259,42 @@ } else { // cancel pressed return; } + } else if (arg0.getSource().equals(recentButton)) { + File saveDirectory = new File(Config.get("save.directory")); + if (saveDirectory == null) return; + + recentFiles = new TreeSet<File>(new Comparator<File> (){ + public int compare (File a, File b) { + return Math.round(b.lastModified() - a.lastModified()); + } + }); + savedFileExtension = Config.get("save.filename.extension"); + if (!Util.hasValue(savedFileExtension)) { + savedFileExtension = GameUIManager.DEFAULT_SAVE_EXTENSION; + } + savedFileExtension = "."+savedFileExtension; + + getRecentFiles(saveDirectory); + if (recentFiles == null || recentFiles.size() == 0) return; + File[] files = recentFiles.toArray(new File[]{}); + int numOptions = 20; + String[] options = new String[numOptions]; + int dirPathLength = saveDirectory.getPath().length(); + for (int i=0; i<numOptions;i++) { + // Get path relative to saveDirectory + options[i] = files[i].getPath().substring(dirPathLength+1); + } + String text = LocalText.getText("Select"); + String result = (String) JOptionPane.showInputDialog(this, text, text, + JOptionPane.OK_CANCEL_OPTION, + null, options, options[0]); + if (result == null) return; + File selectedFile = files[Arrays.asList(options).indexOf(result)]; + if (selectedFile != null) { + loadAndStartGame(selectedFile.getPath(), selectedFile.getParent()); + } else { // cancel pressed + return; + } } else if (arg0.getSource().equals(recoveryButton)) { String filePath = Config.get("save.recovery.filepath", "18xx_autosave.rails"); loadAndStartGame(filePath, null); @@ -317,6 +360,17 @@ } } } + + private void getRecentFiles (File dir) { + if (!dir.exists() || !dir.isDirectory()) return; + for (File entry : dir.listFiles()) { + if (entry.isFile() && entry.getName().endsWith(savedFileExtension)) { + recentFiles.add(entry); + } else if (entry.isDirectory()){ + getRecentFiles(entry); + } + } + } private void toggleOptions() { if (optionsPane.isVisible()) { Modified: trunk/18xx/rails/ui/swing/GameUIManager.java =================================================================== --- trunk/18xx/rails/ui/swing/GameUIManager.java 2011-02-05 20:00:15 UTC (rev 1481) +++ trunk/18xx/rails/ui/swing/GameUIManager.java 2011-02-06 19:29:06 UTC (rev 1482) @@ -54,7 +54,7 @@ protected static final String DEFAULT_SAVE_DIRECTORY = "save"; protected static final String DEFAULT_SAVE_PATTERN = "yyyyMMdd_HHmm"; - protected static final String DEFAULT_SAVE_EXTENSION = "rails"; + public static final String DEFAULT_SAVE_EXTENSION = "rails"; protected static final String NEXT_PLAYER_SUFFIX = "NEXT_PLAYER"; protected String saveDirectory; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |