소스 검색

categories will be stored permanently

Teh-Hai Julian Zheng 7 년 전
부모
커밋
51d04b9687
5개의 변경된 파일147개의 추가작업 그리고 70개의 파일을 삭제
  1. 42 20
      src/ui/controller/Control.java
  2. 16 10
      src/ui/controller/LoadController.java
  3. 31 11
      src/ui/controller/SaveController.java
  4. 13 7
      src/ui/view/AddObjectPopUp.java
  5. 45 22
      src/ui/view/GUI.java

+ 42 - 20
src/ui/controller/Control.java

@@ -41,7 +41,7 @@ public class Control {
 	private final ObjectController objectController;
 	private final CanvasController canvasController;
 	private final GlobalController globalController;
-	private final SaveController storeController;
+	private final SaveController saveController;
 	private final LoadController loadController;
 	private final AutoSaveController autoSaveController;
 	private SimulationManager simulationManager;
@@ -49,7 +49,8 @@ public class Control {
 	private final NodeController nodeController;
 	private final ClipboardController clipboardController;
 	private final HolonCanvasController holonCanvasController;
-	private String autoPath = "";
+	private String autosaveDir = "";
+	private String categoryDir = "";
 
 	/**
 	 * Constructor.
@@ -65,7 +66,7 @@ public class Control {
 		this.objectController = new ObjectController(model, multiPurposeController);
 		this.canvasController = new CanvasController(model, multiPurposeController);
 		this.globalController = new GlobalController(model);
-		this.storeController = new SaveController(model);
+		this.saveController = new SaveController(model);
 		this.nodeController = new NodeController(model, canvasController, multiPurposeController);
 		this.loadController = new LoadController(model, categoryController, canvasController, objectController,
 				nodeController, multiPurposeController);
@@ -73,14 +74,17 @@ public class Control {
 		this.autoSaveController = new AutoSaveController(model);
 		this.consoleController = new ConsoleController(model);
 		this.statsController = new StatsController(model);
-		this.clipboardController = new ClipboardController(model, storeController, loadController, canvasController,
+		this.clipboardController = new ClipboardController(model, saveController, loadController, canvasController,
 				objectController, nodeController, multiPurposeController);
 		this.holonCanvasController = new HolonCanvasController(model);
 
-		autoPath = System.getProperty("user.home") + "/HolonGUI/Autosave/";
-		File dest = new File(autoPath);
+		autosaveDir = System.getProperty("user.home") + "/HolonGUI/Autosave/";
+		categoryDir = System.getProperty("user.home") + "/HolonGUI/Category/";
+		File autoSave = new File(autosaveDir);
+		File category = new File(categoryDir);
 		// deleteDirectory(dest);
-		dest.mkdirs();
+		autoSave.mkdirs();
+		category.mkdirs();
 		try {
 			autoSave();
 		} catch (IOException e) {
@@ -161,9 +165,11 @@ public class Control {
 
 	/**
 	 * init default category and objects.
+	 * @throws IOException 
 	 */
-	public void resetCategorys() {
+	public void resetCategorys() throws IOException {
 		categoryController.initCategories();
+		saveCategory();
 	}
 
 	/**
@@ -171,9 +177,11 @@ public class Control {
 	 * 
 	 * @param cat
 	 *            name of the new Category
+	 * @throws IOException 
 	 */
-	public void addCategory(String cat) {
+	public void addCategory(String cat) throws IOException {
 		categoryController.addNewCategory(cat);
+		saveCategory();
 	}
 
 	/**
@@ -187,9 +195,11 @@ public class Control {
 	 *            Array of Elements
 	 * @param img
 	 *            the image Path
+	 * @throws IOException 
 	 */
-	public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) {
+	public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
 		categoryController.addNewHolonObject(cat, obj, ele, img);
+		saveCategory();
 	}
 
 	/**
@@ -211,9 +221,11 @@ public class Control {
 	 *            Category
 	 * @param obj
 	 *            New Object Name
+	 * @throws IOException 
 	 */
-	public void addSwitch(Category cat, String obj) {
+	public void addSwitch(Category cat, String obj) throws IOException {
 		categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
+		saveCategory();
 	}
 
 	/**
@@ -221,9 +233,11 @@ public class Control {
 	 * 
 	 * @param cat
 	 *            the Category
+	 * @throws IOException 
 	 */
-	public void deleteCategory(String cat) {
+	public void deleteCategory(String cat) throws IOException {
 		categoryController.deleteCategory(cat);
+		saveCategory();
 	}
 
 	/**
@@ -233,9 +247,11 @@ public class Control {
 	 *            the Category
 	 * @param obj
 	 *            the Object
+	 * @throws IOException 
 	 */
-	public void delObjectCategory(String cat, String obj) {
+	public void delObjectCategory(String cat, String obj) throws IOException {
 		categoryController.deleteObject(cat, obj);
+		saveCategory();
 	}
 
 	/**
@@ -471,7 +487,7 @@ public class Control {
 	 *             exception
 	 */
 	public void saveFile(String path) throws IOException, ArchiveException {
-		storeController.writeSave(path);
+		saveController.writeSave(path);
 	}
 
 	/**
@@ -485,6 +501,8 @@ public class Control {
 	 */
 	public void loadFile(String path) throws IOException, ArchiveException, ZipException {
 		loadController.readSave(path);
+		saveCategory();
+		autoSave();
 	}
 	
 	/**
@@ -552,11 +570,15 @@ public class Control {
 	 */
 	public void autoSave() throws IOException {
 		autoSaveController.increaseAutoSaveNr();
-		storeController.writeAutosave(autoPath + autoSaveController.getAutoSaveNr());
+		saveController.writeAutosave(autosaveDir + autoSaveController.getAutoSaveNr());
 		if (autoSaveController.allowed()) {
-			new File(autoPath + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
+			new File(autosaveDir + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
 		}
 	}
+	
+	public void saveCategory() throws IOException {
+		saveController.writeCategory(categoryDir + "Category.json");
+	}
 
 	/**
 	 * Returns the undo save.
@@ -565,10 +587,10 @@ public class Control {
 	 */
 	public String getUndoSave() {
 		autoSaveController.decreaseAutoSaveNr();
-		if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
+		if (!new File(autosaveDir + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.increaseAutoSaveNr();
 		}
-		return autoPath + (autoSaveController.getAutoSaveNr());
+		return autosaveDir + (autoSaveController.getAutoSaveNr());
 	}
 
 	/**
@@ -578,10 +600,10 @@ public class Control {
 	 */
 	public String getRedoSave() {
 		autoSaveController.increaseAutoSaveNr();
-		if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
+		if (!new File(autosaveDir + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.decreaseAutoSaveNr();
 		}
-		return autoPath + (autoSaveController.getAutoSaveNr());
+		return autosaveDir + (autoSaveController.getAutoSaveNr());
 	}
 
 

+ 16 - 10
src/ui/controller/LoadController.java

@@ -57,7 +57,7 @@ public class LoadController {
 	 * enum Mode.
 	 */
 	public enum MODE {
-		COMPLETE, PARTIAL
+		COMPLETE, PARTIAL, CATEGORY
 	}
 
 	public enum EDGETYPE {
@@ -137,9 +137,10 @@ public class LoadController {
 		forwardEdges(edges, json, objDispatch);
 
 	}
-	
+
 	/**
 	 * Loads the Files from the Savefile
+	 * 
 	 * @param folder
 	 * @param trim
 	 * @throws IOException
@@ -148,7 +149,7 @@ public class LoadController {
 		// TODO Auto-generated method stub
 		for (File file : folder.listFiles()) {
 			File dst = new File(System.getProperty("user.home") + "/HolonGUI/" + file.getPath().replace(trim, ""));
-			
+
 			if (file.getName().contains(".json"))
 				readJson(file.getPath());
 			else if (file.isDirectory())
@@ -157,7 +158,7 @@ public class LoadController {
 				dst.getParentFile().mkdirs();
 				Files.copy(file.toPath(), dst.toPath(), StandardCopyOption.REPLACE_EXISTING);
 			}
-	
+
 		}
 	}
 
@@ -230,23 +231,24 @@ public class LoadController {
 
 		switch (MODE.valueOf(json.get("MODE").getAsString())) {
 		case COMPLETE:
-			model.setCgIdx(new HashMap<String, Integer>());
-			model.setCategories(new ArrayList<Category>());
 			model.setCvsObjIdx(new HashMap<Integer, Integer>());
 			model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
 			model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
-			model.setCanvasX(json.get("CANVAS_SIZE_X").getAsInt());
-			model.setCanvasX(json.get("CANVAS_SIZE_Y").getAsInt());
 			break;
 		case PARTIAL:
 			model.setCvsObjIdx(new HashMap<Integer, Integer>());
 			model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
 			model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
 			break;
+		case CATEGORY:
+			model.setCgIdx(new HashMap<String, Integer>());
+			model.setCategories(new ArrayList<Category>());
 
 		default:
 			break;
 		}
+		model.setCanvasX(json.get("CANVAS_SIZE_X").getAsInt());
+		model.setCanvasX(json.get("CANVAS_SIZE_Y").getAsInt());
 		IdCounter.setCounter(json.get("IDCOUNTER").getAsInt());
 		IdCounterElem.setCounter(json.get("IDCOUNTERELEMENT").getAsInt());
 	}
@@ -258,7 +260,8 @@ public class LoadController {
 	 */
 	private void loadCategory(JsonElement jsonElement) {
 		// TODO Auto-generated method stub
-		cgC.addCategory(new Category(jsonElement.getAsString()));
+		if (mpC.searchCat(jsonElement.getAsString()) == null)
+			cgC.addCategory(new Category(jsonElement.getAsString()));
 	}
 
 	/**
@@ -268,10 +271,13 @@ public class LoadController {
 	 */
 	private void loadCategoryObject(JsonElement jsonElement) {
 		// TODO Auto-generated method stub
+
 		AbstractCpsObject temp = gson.fromJson(jsonElement.getAsJsonObject(), AbstractCpsObject.class);
 		initObjects(temp);
-
+		if (mpC.searchCatObj(mpC.searchCat(temp.getSav()), temp.getObjName()) != null)
+			cgC.deleteObject(temp.getSav(), temp.getObjName());
 		cgC.addObject(mpC.searchCat(temp.getSav()), temp);
+
 	}
 
 	/**

+ 31 - 11
src/ui/controller/SaveController.java

@@ -48,7 +48,7 @@ import ui.model.Model;
 public class SaveController {
 
 	public enum MODE {
-		COMPLETE, PARTIAL
+		COMPLETE, PARTIAL, CATEGORY
 	}
 
 	public enum TYPE {
@@ -144,6 +144,27 @@ public class SaveController {
 		writer.close();
 	}
 
+	/**
+	 * Writes the Category File in Case of Changes
+	 * 
+	 * @param path
+	 * @throws IOException
+	 */
+	public void writeCategory(String path) throws IOException {
+
+		initNumeration();
+		JsonObject file = new JsonObject();
+		initialize(MODE.CATEGORY, file);
+		storeCategory(file);
+
+		FileWriter writer = new FileWriter(path);
+		writer.write(gson.toJson(file));
+
+		writer.flush();
+		writer.close();
+
+	}
+
 	/**
 	 * Write needed default parameter into the JsonObject. Can be extended later
 	 * on
@@ -156,20 +177,19 @@ public class SaveController {
 		switch (mode) {
 		case COMPLETE:
 			file.add("MODE", new JsonPrimitive(mode.name()));
-			file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
-			file.add("IDCOUNTERELEMENT", new JsonPrimitive(IdCounterElem.getCounter()));
-			file.add("CANVAS_SIZE_X", new JsonPrimitive(model.getCanvasX()));
-			file.add("CANVAS_SIZE_Y", new JsonPrimitive(model.getCanvasY()));
 			break;
 		case PARTIAL:
 			file.add("MODE", new JsonPrimitive(mode.name()));
-			file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
-			file.add("IDCOUNTERELEMENT", new JsonPrimitive(IdCounterElem.getCounter()));
 			break;
-
+		case CATEGORY:
+			file.add("MODE", new JsonPrimitive(mode.name()));
 		default:
 			break;
 		}
+		file.add("IDCOUNTER", new JsonPrimitive(IdCounter.getCounter()));
+		file.add("IDCOUNTERELEMENT", new JsonPrimitive(IdCounterElem.getCounter()));
+		file.add("CANVAS_SIZE_X", new JsonPrimitive(model.getCanvasX()));
+		file.add("CANVAS_SIZE_Y", new JsonPrimitive(model.getCanvasY()));
 	}
 
 	/**
@@ -193,7 +213,7 @@ public class SaveController {
 			}
 		}
 	}
-	
+
 	/**
 	 * Travers through all Objects via BFS and Stores everything relevant
 	 * 
@@ -240,7 +260,7 @@ public class SaveController {
 	}
 
 	private void storeStatistics(JsonObject file) {
-		
+
 	}
 
 	/**
@@ -409,7 +429,7 @@ public class SaveController {
 	private void addFileToSave(File src, ArchiveOutputStream stream, boolean dir) throws IOException {
 		String entryName = (dir == true ? src.getCanonicalPath() : src.getName());
 		entryName = entryName.replace(System.getProperty("user.home") + "/HolonGUI/", "");
-		
+
 		ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
 		stream.putArchiveEntry(entry);
 		BufferedInputStream input = new BufferedInputStream(new FileInputStream(src));

+ 13 - 7
src/ui/view/AddObjectPopUp.java

@@ -266,14 +266,20 @@ public class AddObjectPopUp extends JDialog {
 								// HolonObject(objectName.getText());
 								// theObject.setElements(hElements);
 								// theObject.setImage(imagePath);
-								if (editState) {
-									controller.delObjectCategory(givenCategory, toEdit.getName());
-									controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
-											hElements, imagePath);
-								} else {
-									controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
-											hElements, imagePath);
+								try {
+									if (editState) {
+										controller.delObjectCategory(givenCategory, toEdit.getName());
+										controller.addObject(controller.searchCategory(givenCategory),
+												objectName.getText(), hElements, imagePath);
+									} else {
+										controller.addObject(controller.searchCategory(givenCategory),
+												objectName.getText(), hElements, imagePath);
+									}
+								} catch (Exception e2) {
+									// TODO: handle exception
+									
 								}
+
 								// controller.addObjectCategory(controller.searchCategory(givenCategory),
 								// theObject);
 								//

+ 45 - 22
src/ui/view/GUI.java

@@ -652,11 +652,16 @@ public class GUI<E> implements CategoryListener {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				ArrayList<Category> cat = model.getCategories();
-				while (!cat.isEmpty()) {
-					controller.deleteCategory(cat.get(0).getName());
-					;
+				try {
+					while (!cat.isEmpty()) {
+						controller.deleteCategory(cat.get(0).getName());
+
+					}
+					controller.resetCategorys();
+				} catch (Exception e2) {
+					// TODO: handle exception
 				}
-				controller.resetCategorys();
+
 				tree.repaint();
 			}
 		});
@@ -1578,23 +1583,27 @@ public class GUI<E> implements CategoryListener {
 					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
 					String nodeName = selectedNode.getUserObject().toString();
 					int depthOfNode = selectedNode.getLevel();
+					try {
+						switch (depthOfNode) {
+						case 1:
+							int dialogResult = JOptionPane.showConfirmDialog(null, eraseCategory + nodeName + "?",
+									warningText, JOptionPane.YES_NO_OPTION);
+							if (dialogResult == JOptionPane.YES_OPTION) {
+								controller.deleteCategory(nodeName);
+							}
+							break;
+						case 2:
+							DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
+							controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
+							break;
 
-					switch (depthOfNode) {
-					case 1:
-						int dialogResult = JOptionPane.showConfirmDialog(null, eraseCategory + nodeName + "?",
-								warningText, JOptionPane.YES_NO_OPTION);
-						if (dialogResult == JOptionPane.YES_OPTION) {
-							controller.deleteCategory(nodeName);
+						default:
+							JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
 						}
-						break;
-					case 2:
-						DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
-						controller.delObjectCategory(parent.getUserObject().toString(), nodeName);
-						break;
-
-					default:
-						JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
+					} catch (Exception e2) {
+						// TODO: handle exception
 					}
+
 				} else {
 					JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
 				}
@@ -1655,6 +1664,7 @@ public class GUI<E> implements CategoryListener {
 						tree.repaint();
 					} catch (IOException | ArchiveException e) {
 						// TODO Auto-generated catch block
+						e.printStackTrace();
 						JLabel message = new JLabel("The savefile is corrupt and cannot be opened.");
 						JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
 					}
@@ -1890,6 +1900,12 @@ public class GUI<E> implements CategoryListener {
 		tableHolonElementScrollPane.setBorder(null);
 
 		frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
+		
+		try {
+			controller.loadAutoSave(System.getProperty("user.home") + "/HolonGUI/Category/Category.json");
+		} catch (IOException e1) {
+			// TODO Auto-generated catch block
+		}
 
 		String autoPath = System.getProperty("user.home") + "/HolonGUI/Autosave/";
 		File dest = new File(autoPath);
@@ -1909,6 +1925,8 @@ public class GUI<E> implements CategoryListener {
 				setUpAutoSave(dest);
 			}
 		}
+		
+		
 		canvasSP.addComponentListener(new ComponentAdapter() {
 			@Override
 			public void componentResized(ComponentEvent e) {
@@ -1957,12 +1975,17 @@ public class GUI<E> implements CategoryListener {
 				Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
 
 				if (objname.length() != 0) {
-					switch (objType) {
+					try {
+						switch (objType) {
 
-					case "Switch":
-						controller.addSwitch(cat, objname);
-						break;
+						case "Switch":
+							controller.addSwitch(cat, objname);
+							break;
+						}
+					} catch (Exception e) {
+						// TODO: handle exception
 					}
+
 				}
 			} else {
 				JOptionPane.showMessageDialog(new JFrame(),