소스 검색

little changes for loading

Teh-Hai Julian Zheng 7 년 전
부모
커밋
d39d0fd144
4개의 변경된 파일110개의 추가작업 그리고 67개의 파일을 삭제
  1. 5 4
      src/ui/controller/Control.java
  2. 37 1
      src/ui/controller/LoadController.java
  3. 56 55
      src/ui/controller/StoreController.java
  4. 12 7
      src/ui/view/GUI.java

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

@@ -466,7 +466,7 @@ public class Control {
 	 *             exception
 	 */
 	public void saveFile(String path) throws IOException, ArchiveException {
-		storeController.writeSaveFile(path);
+		storeController.writeSave(path);
 	}
 
 	/**
@@ -476,9 +476,10 @@ public class Control {
 	 *            the Path
 	 * @throws IOException
 	 *             exception
+	 * @throws ArchiveException 
 	 */
-	public void loadFile(String path) throws IOException {
-		loadController.readJson(path);
+	public void loadFile(String path) throws IOException, ArchiveException {
+		loadController.readSave(path);
 	}
 
 	/**
@@ -537,7 +538,7 @@ public class Control {
 	 */
 	public void autoSave() throws IOException {
 		autoSaveController.increaseAutoSaveNr();
-		storeController.writeAutosaveFile(autoPath + autoSaveController.getAutoSaveNr());
+		storeController.writeAutosave(autoPath + autoSaveController.getAutoSaveNr());
 		if (autoSaveController.allowed()) {
 			new File(autoPath + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
 		}

+ 37 - 1
src/ui/controller/LoadController.java

@@ -2,14 +2,23 @@ package ui.controller;
 
 import java.awt.Color;
 import java.awt.Point;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonElement;
@@ -93,8 +102,20 @@ public class LoadController {
 	 * @throws IOException
 	 *             exception
 	 */
-	public void readJson(String path) throws IOException {
+	public void readSave(String path) throws IOException, ArchiveException {
+
+		if (path.contains("json")) {
+			readJson(path);
+			return;
+		}
 
+		File src = new File(path);
+		File folder = readArchive(path, src);
+		folder.deleteOnExit();
+
+	}
+
+	public void readJson(String path) throws IOException {
 		JsonObject json = (JsonObject) parser.parse(new FileReader(path));
 		// get all keys via stream
 		List<String> keys = getKeys(json);
@@ -110,6 +131,21 @@ public class LoadController {
 
 	}
 
+	private File readArchive(String path, File src) throws IOException, ArchiveException {
+		File tmp = Files.createTempDirectory("tmpHolon").toFile();
+
+		InputStream input = new FileInputStream(src);
+		ArchiveInputStream stream = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP,
+				input);
+
+		ArchiveEntry entry = stream.getNextEntry();
+		while (entry != null) {
+
+		}
+
+		return tmp;
+	}
+
 	/**
 	 * distribute the Edges
 	 * 

+ 56 - 55
src/ui/controller/StoreController.java

@@ -18,8 +18,11 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.stream.Stream;
 
 import javax.imageio.ImageIO;
 
@@ -51,6 +54,7 @@ import classes.HolonSwitch;
 import classes.IdCounter;
 import classes.IdCounterElem;
 import classes.Position;
+import sun.reflect.misc.FieldUtil;
 import ui.model.Model;
 
 /**
@@ -105,15 +109,16 @@ public class StoreController {
 	 * @throws IOException
 	 *             exception
 	 */
-	public void writeSaveFile(String path) throws IOException, ArchiveException {
+	public void writeSave(String path) throws IOException, ArchiveException {
 
 		File dst = new File(path);
 		File holonFile = File.createTempFile("tmp", ".json");
+		holonFile.deleteOnExit();
 		dst.delete();
 
-		OutputStream out = new FileOutputStream(dst);
+		OutputStream output = new FileOutputStream(dst);
 		ArchiveOutputStream stream = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
-				out);
+				output);
 
 		initNumeration();
 		JsonObject file = new JsonObject();
@@ -127,10 +132,10 @@ public class StoreController {
 		writer.flush();
 		writer.close();
 
-		addFileToSave(holonFile, stream);
+		addFilesToSave(holonFile, stream);
 
 		stream.finish();
-		out.close();
+		output.close();
 
 	}
 
@@ -142,7 +147,7 @@ public class StoreController {
 	 * @throws IOException
 	 *             Exception
 	 */
-	public void writeAutosaveFile(String path) throws IOException {
+	public void writeAutosave(String path) throws IOException {
 
 		initNumeration();
 		JsonObject file = new JsonObject();
@@ -251,6 +256,21 @@ public class StoreController {
 
 	}
 
+	/**
+	 * Save wanted Data
+	 * @param stream
+	 * @throws IOException
+	 */
+	private void storeData(ArchiveOutputStream stream) throws IOException {
+		// TODO Auto-generated method stub
+		
+		File images = new File(System.getProperty("user.home") + "/HolonGUI/Images");
+		File background = new File(System.getProperty("user.home") + "/HolonGUI/BackgroundImages");
+		addFilesToSave(images, stream);
+		addFilesToSave(background, stream);
+	
+	}
+
 	/**
 	 * Stores Category or Canvas Elements into Json
 	 * 
@@ -367,66 +387,47 @@ public class StoreController {
 
 	}
 
-	private void storeData(ArchiveOutputStream stream) throws IOException {
-		// TODO Auto-generated method stub
-		ArrayList<String> images = new ArrayList<>();
-		Files.newDirectoryStream(Paths.get(System.getProperty("user.home") + "/HolonGUI/Images"),
-				p -> p.toFile().isFile()).forEach(p -> images.add(p.toString()));
-		for (String str : images) {
-			File src = new File(str);
-			addFileToSave(src, stream);
+	/**
+	 * Differs Between Single file or whole Directory to Store
+	 * 
+	 * @param src
+	 * @param stream
+	 * @throws IOException
+	 */
+	private void addFilesToSave(File src, ArchiveOutputStream stream) throws IOException {
+		if (!src.exists())
+			return;
+		
+		if (!src.isDirectory()) {
+			addFileToSave(src, stream, false);
+			return;
+		}
+		ArrayList<File> files = new ArrayList<>();
+		files.addAll(Arrays.asList(src.listFiles()));
+		for (File file : files) {
+			addFileToSave(file, stream, true);
 		}
 
 	}
 
-	private void imageToJson(JsonObject file) {
-		// ArrayList<String> images = new ArrayList<>();
-		// BufferedImage buffered = null;
-		// WritableRaster raster = null;
-		// DataBufferByte data = null;
-		// String k = null;
-		// JsonObject temp = new JsonObject();
-		//
-		// try {
-		// Files.newDirectoryStream(Paths.get(System.getProperty("user.home") +
-		// "/HolonGUI/Images"),
-		// p -> p.toFile().isFile()).forEach(p -> images.add(p.toString()));
-		//
-		// for (String str : images) {
-		// System.out.println("aaaaa");
-		// k = "IMAGE" + getNumerator(NUMTYPE.IMAGE);
-		// buffered = ImageIO.read(new File(str));
-		// raster = buffered.getRaster();
-		// data = (DataBufferByte) raster.getDataBuffer();
-		//
-		// temp.add("Image", new
-		// JsonPrimitive(str.substring(str.indexOf("/HolonGUI/Images"),
-		// str.length())));
-		// for (int i = 0; i < data.getData().length; i++) {
-		// temp.add("" + i, new JsonPrimitive(data.getData()[i]));
-		// System.out.println(i);
-		// }
-		//
-		// file.add(k, gson.toJsonTree(temp));
-		// }
-		// } catch (IOException e) {
-		// // TODO Auto-generated catch block
-		// e.printStackTrace();
-		// }
-	}
-
-	private void addFileToSave(File src, ArchiveOutputStream stream) throws IOException {
-
-		String entryName = src.getName();
+	/**
+	 * Add a File into the Archive
+	 * 
+	 * @param src
+	 * @param stream
+	 * @param dir
+	 * @throws IOException
+	 */
+	private void addFileToSave(File src, ArchiveOutputStream stream, boolean dir) throws IOException {
+		String entryName = (dir == true ? src.getParentFile().getName() + File.separator + src.getName()
+				: src.getName());
 		ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
 		stream.putArchiveEntry(entry);
-
 		BufferedInputStream input = new BufferedInputStream(new FileInputStream(src));
 
 		IOUtils.copy(input, stream);
 		input.close();
 		stream.closeArchiveEntry();
-
 	}
 
 	/**

+ 12 - 7
src/ui/view/GUI.java

@@ -74,6 +74,7 @@ import classes.HolonSwitch;
 import classes.HolonTransformer;
 import classes.IdCounter;
 import classes.IdCounterElem;
+import exceptions.CorruptSaveFileException;
 import interfaces.CategoryListener;
 import ui.controller.Control;
 import ui.controller.UpdateController;
@@ -344,7 +345,7 @@ public class GUI<E> implements CategoryListener {
 						}
 					}
 					unitGraph.empty();
-				} catch (IOException eex) {
+				} catch (IOException | ArchiveException eex) {
 					// TODO Auto-generated catch block
 					eex.printStackTrace();
 				}
@@ -377,7 +378,7 @@ public class GUI<E> implements CategoryListener {
 						}
 					}
 					unitGraph.empty();
-				} catch (IOException ex) {
+				} catch (IOException | ArchiveException ex) {
 					// TODO Auto-generated catch block
 					ex.printStackTrace();
 				}
@@ -1494,8 +1495,11 @@ public class GUI<E> implements CategoryListener {
 			private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
 				JFileChooser fileChooser = new JFileChooser();
 				JFrame test = new JFrame();
-				FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter("*.json", "json");
-				fileChooser.setFileFilter(jsonFilter);
+				FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(.holon)", "holon");
+				FileNameExtensionFilter ownFilter = new FileNameExtensionFilter("Own Extension(.ext)", " ");
+				fileChooser.addChoosableFileFilter(holonFilter);
+				fileChooser.addChoosableFileFilter(ownFilter);
+				fileChooser.setFileFilter(holonFilter);
 
 				if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
 					File file = fileChooser.getSelectedFile();
@@ -1522,6 +1526,8 @@ public class GUI<E> implements CategoryListener {
 					} catch (IOException e) {
 						// TODO Auto-generated catch block
 						e.printStackTrace();
+					} catch (ArchiveException e) {
+						e.printStackTrace();
 					}
 				}
 			}
@@ -1544,7 +1550,6 @@ public class GUI<E> implements CategoryListener {
 				fileChooser.addChoosableFileFilter(ownFilter);
 				fileChooser.setFileFilter(holonFilter);
 
-
 				if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
 					String file = fileChooser.getSelectedFile().getPath();
 					if (fileChooser.getFileFilter().equals(holonFilter)) {
@@ -1635,7 +1640,7 @@ public class GUI<E> implements CategoryListener {
 						}
 					}
 					unitGraph.empty();
-				} catch (IOException e) {
+				} catch (IOException | ArchiveException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}
@@ -1671,7 +1676,7 @@ public class GUI<E> implements CategoryListener {
 						}
 					}
 					unitGraph.empty();
-				} catch (IOException e) {
+				} catch (IOException | ArchiveException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}