Browse Source

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

dominik.rieder 8 years ago
parent
commit
b26908901f

+ 38 - 0
src/TypeAdapter/PairAdapter.java

@@ -0,0 +1,38 @@
+package TypeAdapter;
+
+import java.lang.reflect.Type;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonPrimitive;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+import javafx.util.Pair;
+
+public class PairAdapter implements JsonSerializer<Pair<String, String>>, JsonDeserializer<Pair<String, String>>{
+
+	@Override
+	public Pair<String, String> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
+			throws JsonParseException {
+		// TODO Auto-generated method stub
+		JsonObject object = json.getAsJsonObject();
+		
+		return new Pair<String, String>(object.get("KEY").getAsString(), object.get("VALUE").getAsString());
+	}
+
+	@Override
+	public JsonElement serialize(Pair<String, String> src, Type typeOfSrc, JsonSerializationContext context) {
+		// TODO Auto-generated method stub
+		
+		JsonObject object = new JsonObject();
+		object.add("KEY", new JsonPrimitive(src.getKey()));
+		object.add("VALUE", new JsonPrimitive(src.getValue()));
+		return object;
+
+	}
+
+}

+ 22 - 43
src/classes/HolonElement.java

@@ -5,6 +5,8 @@ import java.util.LinkedList;
 
 import com.google.gson.annotations.Expose;
 
+import javafx.util.Pair;
+
 /**
  * The class "HolonElement" represents any possible element that can be added to
  * a CpsObject (such as TV (consumer) or any energy source/producer).
@@ -34,11 +36,10 @@ public class HolonElement {
 	private char sign;
 	/* Place where the Object is Stored */
 	@Expose
-	private String sav;
-	/* Object where the Element is Stored */
-	@Expose
-	private String obj;
-	/* Unique Id of the Element */
+	private Pair<String, String> saving;
+	/*
+	 * ID 
+	 */
 	@Expose
 	private int id;
 	/*
@@ -104,8 +105,7 @@ public class HolonElement {
 		for (Point p : element.getGraphPoints()) {
 			this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
 		}
-		setSav("CVS");
-		setObj(element.getObj());
+		setSaving(null);
 		setId(IdCounterElem.nextId());
 		setFlexibility(0);
 		setActiveFlex(false);
@@ -276,42 +276,6 @@ public class HolonElement {
 			this.sign = '+';
 	}
 
-	/**
-	 * @return the stored.
-	 */
-	public String getSav() {
-		return sav;
-	}
-
-	/**
-	 * for save purposes.
-	 * 
-	 * @param sav
-	 *            the stored to set
-	 */
-	public void setSav(String sav) {
-		this.sav = sav;
-	}
-
-	/**
-	 * Get the actual object.
-	 * 
-	 * @return the obj
-	 */
-	public String getObj() {
-		return obj;
-	}
-
-	/**
-	 * Set the object to a new one.
-	 * 
-	 * @param obj
-	 *            the obj to set
-	 */
-	public void setObj(String obj) {
-		this.obj = obj;
-	}
-
 	/**
 	 * Get the points (values) in the graph.
 	 * 
@@ -377,4 +341,19 @@ public class HolonElement {
 	public int getId() {
 		return id;
 	}
+
+	/**
+	 * @return the saving
+	 */
+	public Pair<String, String> getSaving() {
+		return saving;
+	}
+
+	/**
+	 * @param saving
+	 *            the saving to set
+	 */
+	public void setSaving(Pair<String, String> saving) {
+		this.saving = saving;
+	}
 }

+ 2 - 2
src/tests/PraktikumHolonsTestClasses.java

@@ -196,8 +196,8 @@ public class PraktikumHolonsTestClasses {
 		assertTrue("Amount not correct", ele2.getAmount() == 5);
 		assertTrue("Total Energy not Correct", ele2.getTotalEnergy() == ele2.getAmount() * ele2.getEnergy());
 		assertTrue("Sign not correct", ele2.getSign() == '-');
-		ele3.setSav("CVS");
-		assertTrue("SAV not correct", ele3.getSav().equals("CVS"));
+//		ele3.setSav("CVS");
+//		assertTrue("SAV not correct", ele3.getSav().equals("CVS"));
 	}
 
 	/**

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

@@ -12,6 +12,7 @@ import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -290,7 +291,7 @@ public class LoadController {
 		HolonElement temp = model.getGson().fromJson(jsonElement.getAsJsonObject().get("properties").getAsJsonObject(),
 				HolonElement.class);
 		initElements(temp);
-		objC.addElementIntoCategoryObject(temp.getSav(), temp.getObj(), temp);
+		objC.addElementIntoCategoryObject(temp.getSaving().getKey(), temp.getSaving().getValue(), temp);
 	}
 
 	/**
@@ -444,6 +445,7 @@ public class LoadController {
 	private void loadStatisticGraph(JsonElement jsonElement, HashMap<Integer, AbstractCpsObject> objDispatch) {
 		// TODO Auto-generated method stub
 		JsonObject object = jsonElement.getAsJsonObject();
+		model.setGraphTable(new Hashtable<>());
 		model.getStatisticData().add(object);
 	}
 

+ 3 - 4
src/ui/controller/ObjectController.java

@@ -7,6 +7,7 @@ import classes.CpsUpperNode;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import javafx.util.Pair;
 import ui.model.Model;
 
 /**
@@ -68,8 +69,7 @@ public class ObjectController {
 	 *            the Element
 	 */
 	public void addElementIntoCanvasObject(HolonObject object, HolonElement element) {
-		element.setSav("CVS");
-		element.setObj(Integer.toString(object.getId()));
+		element.setSaving(null);
 		addElement(object, element);
 	}
 
@@ -106,8 +106,7 @@ public class ObjectController {
 	 *            the Element
 	 */
 	public void addElementIntoCategoryObject(String category, String object, HolonElement element) {
-		element.setSav(category);
-		element.setObj(object);
+		element.setSaving(new Pair<String, String>(category, object));
 		addElement((HolonObject) mpC.searchCatObj(mpC.searchCat(category), object), element);
 	}
 

+ 3 - 0
src/ui/model/Model.java

@@ -16,6 +16,7 @@ import com.google.gson.JsonParser;
 
 import TypeAdapter.AbstractCpsObjectAdapter;
 import TypeAdapter.ColorAdapter;
+import TypeAdapter.PairAdapter;
 import TypeAdapter.PositionAdapter;
 import classes.AbstractCpsObject;
 import classes.Category;
@@ -27,6 +28,7 @@ import classes.Position;
 import interfaces.CategoryListener;
 import interfaces.GraphListener;
 import interfaces.ObjectListener;
+import javafx.util.Pair;
 import ui.view.Console;
 import ui.view.DefaulTable;
 import ui.view.PropertyTable;
@@ -964,6 +966,7 @@ public class Model {
 		builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
 		builder.registerTypeAdapter(Position.class, new PositionAdapter());
 		builder.registerTypeAdapter(Color.class, new ColorAdapter());
+		builder.registerTypeAdapter(Pair.class, new PairAdapter());
 		// use the builder and make a instance of the Gson
 		this.setGson(builder.create());
 

+ 6 - 4
src/ui/view/AddObjectPopUp.java

@@ -38,6 +38,7 @@ import javax.swing.filechooser.FileNameExtensionFilter;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import javafx.util.Pair;
 import ui.controller.Control;
 
 /**
@@ -199,10 +200,11 @@ public class AddObjectPopUp extends JDialog {
 					addElement.setActualCps(toEdit);
 					addElement.setVisible(true);
 					HolonElement hl = addElement.getElement();
-					if (hl != null) {
-						hl.setSav(givenCategory);
-					}
-					hl.setObj(objectName.getText());
+					hl.setSaving(new Pair<String, String>(givenCategory, objectName.getText()));
+//					if (hl != null) {
+//						hl.setSav(givenCategory);
+//					}
+//					hl.setObj(objectName.getText());
 					addElement(hl);
 				}
 			});

+ 21 - 8
src/ui/view/AlgorithmMenu.java

@@ -14,9 +14,11 @@ import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.tools.JavaCompiler;
 import javax.tools.ToolProvider;
 
+import api.CpsAlgorithm;
 import ui.controller.Control;
 import ui.model.Model;
 
@@ -129,7 +131,8 @@ public class AlgorithmMenu extends JMenu {
 	}
 
 	public void setAlgorithm(File file, String name) {
-		boolean missinConsole = false;
+		boolean missingCompiler = false;
+		boolean instantiationError = false;
 		try {
 			BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
 			String line = br.readLine();
@@ -155,7 +158,7 @@ public class AlgorithmMenu extends JMenu {
 			JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 
 			if (ToolProvider.getSystemJavaCompiler() == null) {
-				missinConsole = true;
+				missingCompiler = true;
 			}
 
 			compiler.run(null, null, null, file.getPath());
@@ -163,6 +166,7 @@ public class AlgorithmMenu extends JMenu {
 			// Load and instantiate compiled class.
 			URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
 
+			instantiationError = true;
 			Class<?> cls;
 			if (packageName.isEmpty()) {
 				cls = Class.forName(name, true, classLoader);
@@ -171,14 +175,23 @@ public class AlgorithmMenu extends JMenu {
 			}
 
 			Object t = cls.newInstance();
-
-			controller.setAlgorithm(t);
-
+			if (t instanceof CpsAlgorithm) {
+				controller.setAlgorithm(t);
+			} else {
+				JOptionPane.showMessageDialog(null, "Class does not Implement CpsAlgorithm!", "Error!",
+						JOptionPane.ERROR_MESSAGE);
+				noneItem.doClick();
+			}
 		} catch (Exception e) {
-			if (missinConsole) {
-				controller.addTextToConsole("missing Compiiler! Please install JDK");
+			if (missingCompiler) {
+				JOptionPane.showMessageDialog(null, "Missing Compiiler! Please install JDK!", "Error!",
+						JOptionPane.ERROR_MESSAGE);
+			} else if (instantiationError) {
+				JOptionPane.showMessageDialog(null, "Class does not Implement CpsAlgorithm!", "Error!",
+						JOptionPane.ERROR_MESSAGE);
+				noneItem.doClick();
 			} else {
-				controller.addTextToConsole(e.toString());
+				JOptionPane.showMessageDialog(null, e.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
 			}
 		}
 	}