Browse Source

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons.git

Kevin Trometer 8 years ago
parent
commit
f6f213ce24
2 changed files with 56 additions and 40 deletions
  1. 56 39
      src/ui/controller/Control.java
  2. 0 1
      src/ui/view/GUI.java

+ 56 - 39
src/ui/controller/Control.java

@@ -53,6 +53,7 @@ public class Control {
 	private final HolonCanvasController holonCanvasController;
 	private String autosaveDir = "";
 	private String categoryDir = "";
+	private int rand;
 
 	/**
 	 * Constructor.
@@ -87,6 +88,7 @@ public class Control {
 		// deleteDirectory(dest);
 		autoSave.mkdirs();
 		category.mkdirs();
+		createAutoRandom();
 		try {
 			autoSave();
 		} catch (IOException e) {
@@ -95,6 +97,17 @@ public class Control {
 		}
 	}
 
+	/**
+	 * Generate random number, so that every instance of the program has unique
+	 * save files
+	 */
+	private void createAutoRandom() {
+		rand = (int) (Math.random() * 1000);
+		while (new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
+			rand = (int) Math.random() * 1000;
+		}
+	}
+
 	/**
 	 * Delete a Directory.
 	 * 
@@ -108,10 +121,11 @@ public class Control {
 				if (files[i].isDirectory()) {
 					deleteDirectory(files[i]);
 				} else {
-					files[i].delete();
+					if(files[i].getName().contains(""+rand))
+							files[i].delete();
 				}
 			}
-			path.delete();
+			//path.delete();
 		}
 	}
 
@@ -167,7 +181,8 @@ public class Control {
 
 	/**
 	 * init default category and objects.
-	 * @throws IOException 
+	 * 
+	 * @throws IOException
 	 */
 	public void resetCategorys() throws IOException {
 		categoryController.initCategories();
@@ -179,7 +194,7 @@ public class Control {
 	 * 
 	 * @param cat
 	 *            name of the new Category
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void addCategory(String cat) throws IOException {
 		categoryController.addNewCategory(cat);
@@ -197,7 +212,7 @@ public class Control {
 	 *            Array of Elements
 	 * @param img
 	 *            the image Path
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
 		categoryController.addNewHolonObject(cat, obj, ele, img);
@@ -223,7 +238,7 @@ public class Control {
 	 *            Category
 	 * @param obj
 	 *            New Object Name
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void addSwitch(Category cat, String obj) throws IOException {
 		categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
@@ -235,7 +250,7 @@ public class Control {
 	 * 
 	 * @param cat
 	 *            the Category
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void deleteCategory(String cat) throws IOException {
 		categoryController.deleteCategory(cat);
@@ -249,7 +264,7 @@ public class Control {
 	 *            the Category
 	 * @param obj
 	 *            the Object
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void delObjectCategory(String cat, String obj) throws IOException {
 		categoryController.deleteObject(cat, obj);
@@ -499,20 +514,21 @@ public class Control {
 	 *            the Path
 	 * @throws IOException
 	 *             exception
-	 * @throws ArchiveException 
+	 * @throws ArchiveException
 	 */
 	public void loadFile(String path) throws IOException, ArchiveException, ZipException {
 		loadController.readSave(path);
 		saveCategory();
 		autoSave();
 	}
-	
+
 	/**
 	 * Reads the Json File from Autosave
+	 * 
 	 * @param path
 	 * @throws IOException
 	 */
-	public void loadAutoSave(String path) throws IOException  {
+	public void loadAutoSave(String path) throws IOException {
 		loadController.readJson(path);
 	}
 
@@ -531,7 +547,7 @@ public class Control {
 	 * current Timestep.
 	 */
 	public void calculateStateForCurrentTimeStep() {
-		//simulationManager.reset();
+		// simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(model.getCurIteration());
 	}
 
@@ -542,15 +558,15 @@ public class Control {
 	 *            current Iteration
 	 */
 	public void calculateStateForTimeStep(int x) {
-		//simulationManager.reset();
+		// simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(x);
 	}
-	
+
 	/**
-	 * resets the whole State of the simulation 
-	 * including a reset of all Edges to the default "is working" state
+	 * resets the whole State of the simulation including a reset of all Edges
+	 * to the default "is working" state
 	 */
-	public void resetSimulation(){
+	public void resetSimulation() {
 		setIsSimRunning(false);
 		simulationManager.resetSimulation();
 	}
@@ -573,12 +589,13 @@ public class Control {
 	 */
 	public void autoSave() throws IOException {
 		autoSaveController.increaseAutoSaveNr();
-		saveController.writeAutosave(autosaveDir + autoSaveController.getAutoSaveNr());
+		saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
 		if (autoSaveController.allowed()) {
-			new File(autosaveDir + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
+			new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
+					.delete();
 		}
 	}
-	
+
 	public void saveCategory() throws IOException {
 		saveController.writeCategory(categoryDir + "Category.json");
 	}
@@ -590,10 +607,10 @@ public class Control {
 	 */
 	public String getUndoSave() {
 		autoSaveController.decreaseAutoSaveNr();
-		if (!new File(autosaveDir + (autoSaveController.getAutoSaveNr())).exists()) {
+		if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.increaseAutoSaveNr();
 		}
-		return autosaveDir + (autoSaveController.getAutoSaveNr());
+		return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
 	}
 
 	/**
@@ -603,14 +620,12 @@ public class Control {
 	 */
 	public String getRedoSave() {
 		autoSaveController.increaseAutoSaveNr();
-		if (!new File(autosaveDir + (autoSaveController.getAutoSaveNr())).exists()) {
+		if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.decreaseAutoSaveNr();
 		}
-		return autosaveDir + (autoSaveController.getAutoSaveNr());
+		return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
 	}
 
-
-
 	/**
 	 * Getter for Model.
 	 * 
@@ -881,11 +896,12 @@ public class Control {
 	public void copy(CpsUpperNode upperNode) {
 		clipboardController.copy(upperNode);
 	}
-	
-	public void paste(CpsUpperNode upperNode, Point point) throws JsonParseException, UnsupportedFlavorException, IOException {
+
+	public void paste(CpsUpperNode upperNode, Point point)
+			throws JsonParseException, UnsupportedFlavorException, IOException {
 		clipboardController.paste(upperNode, point);
 	}
-	
+
 	public void cut(CpsUpperNode upperNode) {
 		clipboardController.cut(upperNode);
 	}
@@ -903,7 +919,7 @@ public class Control {
 	}
 
 	public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
-		return holonCanvasController.getTotalElements(arrayList) ;
+		return holonCanvasController.getTotalElements(arrayList);
 	}
 
 	public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
@@ -913,6 +929,7 @@ public class Control {
 	public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
 		return holonCanvasController.getActiveElements(arrayList);
 	}
+
 	/**
 	 * Set the Background Image;
 	 * 
@@ -926,25 +943,25 @@ public class Control {
 	 *            Image custom height
 	 */
 	public void setBackgroundImage(String imagePath, int mode, int width, int height) {
-	canvasController.setBackgroundImage(imagePath, mode, width, height);
+		canvasController.setBackgroundImage(imagePath, mode, width, height);
 	}
-	
-	public void setFlexiblePane(FlexiblePane fp){
+
+	public void setFlexiblePane(FlexiblePane fp) {
 		simulationManager.setFlexiblePane(fp);
 	}
-	
-	public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT){
+
+	public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
 		model.setGraphTable(gT);
 	}
-	
-	public Hashtable<String, StatisticGraphPanel> getGraphTable(){
+
+	public Hashtable<String, StatisticGraphPanel> getGraphTable() {
 		return model.getGraphTable();
 	}
-	
+
 	/**
 	 * Sets if the Simulation is running
 	 */
-	public void setIsSimRunning(boolean isRunning){
+	public void setIsSimRunning(boolean isRunning) {
 		globalController.setIsSimRunning(isRunning);
 	}
 

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

@@ -1977,7 +1977,6 @@ public class GUI<E> implements CategoryListener {
 	 *            path to save-folder
 	 */
 	public void setUpAutoSave(File dest) {
-		controller.deleteDirectory(dest);
 		dest.mkdirs();
 		try {
 			controller.autoSave();