Browse Source

load store

Teh-Hai Julian Zheng 8 years ago
parent
commit
0ed2942ea7
3 changed files with 41 additions and 9 deletions
  1. 4 4
      src/ui/controller/Control.java
  2. 4 4
      src/ui/controller/LoadStoreController.java
  3. 33 1
      src/ui/view/GUI.java

+ 4 - 4
src/ui/controller/Control.java

@@ -144,12 +144,12 @@ public class Control {
 	}
 	
 	/* Operations for Loading and Storing */
-	public void writeFile() throws IOException {
-		loadStoreController.writeJSONFile();
+	public void saveFile(String path) throws IOException {
+		loadStoreController.writeJSONFile(path);
 	}
 	
-	public void readFile() throws IOException {
-		loadStoreController.readJSON();
+	public void loadFile(String path) throws IOException {
+		loadStoreController.readJSON(path);
 	}
 	////////// etc
 	public void initListener(CategoryListener catLis) {

+ 4 - 4
src/ui/controller/LoadStoreController.java

@@ -44,7 +44,7 @@ public class LoadStoreController {
 	 * 
 	 * @throws IOException
 	 */
-	public void writeJSONFile() throws IOException {
+	public void writeJSONFile(String path) throws IOException {
 
 		JSONObject json = new JSONObject();
 
@@ -54,7 +54,7 @@ public class LoadStoreController {
 		writeElements(json);
 		writeEdges(json);
 
-		FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
+		FileWriter writer = new FileWriter(path);
 		writer.write(json.toJSONString());
 		writer.flush();
 		writer.close();
@@ -184,7 +184,7 @@ public class LoadStoreController {
 		}
 	}
 
-	public void readJSON() throws IOException {
+	public void readJSON(String path) throws IOException {
 		JSONParser parser = new JSONParser();
 		MODEL.setCategories(new ArrayList<>());
 		MODEL.setObjectsOnCanvas(new ArrayList<>());
@@ -195,7 +195,7 @@ public class LoadStoreController {
 
 		try {
 
-			JSONObject json = (JSONObject) parser.parse(new FileReader("//Users//zheng//Desktop//Tesst.json"));
+			JSONObject json = (JSONObject) parser.parse(new FileReader(path));
 
 			for (Object key : json.keySet()) {
 

+ 33 - 1
src/ui/view/GUI.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 
 import javax.swing.JFrame;
@@ -720,6 +721,7 @@ public class GUI implements CategoryListener {
 			@Override
 			public void actionPerformed(java.awt.event.ActionEvent evt) {
 				menuFileExitActionPerformed(evt);
+				
 			}
 
 			private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
@@ -727,7 +729,37 @@ public class GUI implements CategoryListener {
 				JFrame test = new JFrame();
 				if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
 					File file = fileChooser.getSelectedFile();
-					System.out.println("File Path is: " + file.toString());
+					
+					try {
+						controller.loadFile(file.getAbsolutePath());
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+				}
+			}
+		});
+		
+		mntmSave.addActionListener(new java.awt.event.ActionListener() {
+			@Override
+			public void actionPerformed(java.awt.event.ActionEvent evt) {
+				
+				menuSaveActionPerformed(evt);
+				
+			}
+
+			private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
+				JFileChooser fileChooser = new JFileChooser();
+				JFrame test = new JFrame();
+				if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
+					File file = fileChooser.getSelectedFile();
+					
+					try {
+						controller.saveFile(file.getAbsolutePath());
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
 				}
 			}
 		});