User modified keybindings are persistently saved

- Works on windows computers
- KeyFactory processes its own XML saving/loading
- Does not work with Jar yet
- Changed keyFactory to static temporarily to work with existing FXML loading architecture

#story[1197]
This commit is contained in:
Jessica Syder
2017-09-03 20:44:27 +12:00
parent 3fdfbd83e0
commit cfa6fc37e0
5 changed files with 89 additions and 27 deletions
@@ -1,5 +1,9 @@
package visualiser.gameController.Keys;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
@@ -62,4 +66,43 @@ public class KeyFactory {
keyState.put(newKey, controlKey);
}
/**
* Persistently saves the keybindings the user has set.
*/
public void save(){
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(getClass().getResource(
"/visualiser/userSettings/keyFactory.xml")
.toURI().getPath())));
encoder.writeObject(this.keyState);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
/**
* Loads the persistently saved keybindings the user has set.
*/
public void load(){
try {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(getClass().getResource(
"/visualiser/userSettings/keyFactory.xml")
.toURI().getPath())));
Map<String, ControlKey> savedKeyState
= (Map<String, ControlKey>)decoder.readObject();
decoder.close();
this.keyState = savedKeyState;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}