From: <ste...@us...> - 2011-07-09 12:07:30
|
Revision: 1613 http://rails.svn.sourceforge.net/rails/?rev=1613&view=rev Author: stefanfrey Date: 2011-07-09 12:07:23 +0000 (Sat, 09 Jul 2011) Log Message: ----------- Updated ListAndFixSavedFiles to use GameFileIO for saving. Added support to show and save user comments correctly for that class. Modified Paths: -------------- trunk/18xx/rails/util/GameFileIO.java trunk/18xx/rails/util/ListAndFixSavedFiles.java Modified: trunk/18xx/rails/util/GameFileIO.java =================================================================== --- trunk/18xx/rails/util/GameFileIO.java 2011-07-08 17:17:29 UTC (rev 1612) +++ trunk/18xx/rails/util/GameFileIO.java 2011-07-09 12:07:23 UTC (rev 1613) @@ -265,9 +265,12 @@ * requires initSave and setting actions and comments */ public boolean saveGame(File file, boolean displayErrorMessage, String errorMessageKey) { - if (!initSave || gameData.actions == null) return false; + if (!(initSave || dataLoadDone) || gameData.actions == null) { + log.warn("File save not possible due to missing data"); + return false; + } boolean result = false; - + log.info("Trying to save file to " + file.getAbsoluteFile()); try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); @@ -284,6 +287,7 @@ oos.close(); result = true; + log.info("File save successfull"); } catch (IOException e) { log.error(errorMessageKey, e); if (displayErrorMessage) { Modified: trunk/18xx/rails/util/ListAndFixSavedFiles.java =================================================================== --- trunk/18xx/rails/util/ListAndFixSavedFiles.java 2011-07-08 17:17:29 UTC (rev 1612) +++ trunk/18xx/rails/util/ListAndFixSavedFiles.java 2011-07-09 12:07:23 UTC (rev 1613) @@ -34,9 +34,7 @@ private StringBuffer headerText = new StringBuffer(); - private List<Object> savedObjects = new ArrayList<Object>(512); - private List<PossibleAction> executedActions; - private SortedMap<Integer,String> userComments; + private GameFileIO fileIO; private int vbarPos; @@ -152,7 +150,6 @@ } - @SuppressWarnings("unchecked") private void load() { JFileChooser jfc = new JFileChooser(); @@ -165,15 +162,13 @@ saveDirectory = selectedFile.getParent(); // use GameLoader object to load game - GameFileIO gameLoader = new GameFileIO(); + fileIO = new GameFileIO(); - gameLoader.loadGameData(filepath); - add(gameLoader.getGameDataAsText()); + fileIO.loadGameData(filepath); + add(fileIO.getGameDataAsText()); try{ - gameLoader.initGame(); - gameLoader.loadActionsAndComments(); - executedActions = gameLoader.getActions(); - userComments = gameLoader.getComments(); + fileIO.initGame(); + fileIO.loadActionsAndComments(); setReportText(true); } catch (ConfigurationException e) { @@ -200,9 +195,15 @@ reportText.setText(headerText.toString()); // append actionText int i=0; - for (PossibleAction action : executedActions) { - reportText.append("Action "+(i++)+" "+action.getPlayerName()+": "+action.toString()); + for (PossibleAction action : fileIO.getActions()) { + reportText.append("Action "+i+" "+action.getPlayerName()+": "+action.toString()); reportText.append("\n"); + // check for comments for this action + String comment = fileIO.getComments().get(i); + if (comment!= null) { + reportText.append("Comment to action " + i + ": " + comment + "\n"); + } + i++; } scrollDown(vbarPos); } @@ -228,8 +229,15 @@ if (Util.hasValue(result)) { try { int index = Integer.parseInt(result); - List<PossibleAction> toRemove = executedActions.subList(index, executedActions.size()); - toRemove.clear(); + // delete actions + int size = fileIO.getActions().size(); + fileIO.getActions().subList(index + 1, size).clear(); + // delete comments + for (int id = 0; id < size; id++) { + if (id > index) { + fileIO.getComments().remove(id); + } + } setReportText(false); } catch (NumberFormatException e) { @@ -240,9 +248,17 @@ if (Util.hasValue(result)) { try { int index = Integer.parseInt(result); - PossibleAction action = executedActions.get(index); - executedActions.remove(action); - savedObjects.remove(action); + fileIO.getActions().remove(index); + // delete and renumber in user Comments + SortedMap<Integer, String> newComments = new TreeMap<Integer, String>(); + for (Integer id:fileIO.getComments().keySet()) { + if (id < index) { + newComments.put(id, fileIO.getComments().get(id)); + } else if (id > index) { + newComments.put(id-1, fileIO.getComments().get(id)); + } + } + fileIO.setComments(newComments); setReportText(false); } catch (NumberFormatException e) { log.error("Number format exception for '"+result+"'", e); @@ -265,26 +281,7 @@ } if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { File selectedFile = jfc.getSelectedFile(); - filepath = selectedFile.getPath(); - saveDirectory = selectedFile.getParent(); - - try { - try { - ObjectOutputStream oos = - new ObjectOutputStream(new FileOutputStream(new File( - filepath))); - for (Object object : savedObjects) { - oos.writeObject(object); - } - oos.close(); - } catch (IOException e) { - log.error("Save failed", e); - DisplayBuffer.add(LocalText.getText("SaveFailed", e.getMessage())); - } - } catch (Exception e) { - System.out.println ("Error whilst writing file "+filepath); - e.printStackTrace(); - } + fileIO.saveGame(selectedFile, true, "SaveFailed"); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |