Browse Source

All Basic Function works again.

Tom Troppmann 5 years ago
parent
commit
ce42804237

+ 25 - 66
src/classes/HolonElement.java

@@ -6,7 +6,6 @@ import interfaces.GraphEditable;
 import interfaces.IGraphedElement;
 import ui.model.Model;
 import ui.view.IndexTranslator;
-import ui.view.UnitGraph;
 
 import java.awt.*;
 import java.awt.geom.Point2D;
@@ -26,9 +25,8 @@ public class HolonElement implements IGraphedElement, GraphEditable{
      * Represent the Graph 
      * the X component from a Point is period from 0..1
      * the Y component from a Point is the percentage from 0..1
-     * currently saved in int -> TODO serelized float point class
      * */
-    private LinkedList<Point2D.Double> testGraphPoints;
+    private LinkedList<Point2D.Double> graphPoints;
     /** Name of the object, e.g. House, 1 */
     @Expose
     private String objName;
@@ -100,8 +98,8 @@ public class HolonElement implements IGraphedElement, GraphEditable{
         setEnergyPerElement(energy);
         setActive(true);
         System.out.println("heiNEW");
-        setTestGraphPoints(new LinkedList<>());
-        initTestGraphPoints(1);
+        setGraphPoints(new LinkedList<>());
+        initGraphPoints();
         sampleGraph();
         setId(id);
         setFlexibleEnergyAvailable(0);
@@ -121,13 +119,10 @@ public class HolonElement implements IGraphedElement, GraphEditable{
         setAmount(element.getAmount());
         setEnergyPerElement(element.getEnergyPerElement());
         setActive(element.isActive());
-        setTestGraphPoints(new LinkedList<>());
+        setGraphPoints(new LinkedList<>());
         System.out.println("hei");
-        for (Point2D.Double p : element.getTestGraphPoints()) {
-            this.testGraphPoints.add(new Point2D.Double(p.getX(), p.getY()));
-        }
-        for (Point2D.Double p : getTestGraphPoints()) {
-            System.out.println(testGraphPoints);
+        for (Point2D.Double p : element.getGraphPoints()) {
+            this.graphPoints.add(new Point2D.Double(p.getX(), p.getY()));
         }
         sampleGraph();
         setSaving(null);
@@ -145,18 +140,6 @@ public class HolonElement implements IGraphedElement, GraphEditable{
 	}
 
 
-    /**
-     * Set energyPerElement to any value to the whole array.
-     *
-     * @param energy the value
-     */
-    public void setAvailableEnergyPerElementAt(float energy) {
-        this.curveSample = new float[100];
-        for (int i = 0; i < curveSample.length; i++) {
-            this.curveSample[i] = energy;
-        }
-    }
-
     /**
      * Get the energyPerElement currently(at given time step) available
      */
@@ -165,16 +148,6 @@ public class HolonElement implements IGraphedElement, GraphEditable{
         return amount * energyPerElement * this.curveSample[IndexTranslator.getEffectiveIndex(this, timestep)];
     }
 
-    /**
-     * Set energyPerElement to any value at a given position.
-     *
-     * @param pos    int
-     * @param energyPerElement float
-     */
-    public void setAvailableEnergyPerElementAt(int pos, float energyPerElement) {
-        this.curveSample[pos] = energyPerElement;
-    }
-
     /**
      * Get the user-defined Name.
      *
@@ -291,26 +264,6 @@ public class HolonElement implements IGraphedElement, GraphEditable{
         }
     }
 
-
-    /**
-     * Get the points (values) in the graph.
-     *
-     * @return the Graph Points
-     */
-    public LinkedList<Point> getGraphPoints() {
-        return null;
-        //TODO DELETE
-    }
-
-    /**
-     * Set the points (values) in the graph.
-     *
-     * @param points the Graph points
-     */
-    public void setGraphPoints(LinkedList<Point> points) {
-    	//TODO DELETE
-    }
-
     /**
      * Get the flexibleEnergyAvailable of an element
      */
@@ -321,8 +274,8 @@ public class HolonElement implements IGraphedElement, GraphEditable{
     /**
      * Set the flexibleEnergyAvailable of an element
      */
-    public void setFlexibleEnergyAvailable(float f) {
-        this.flexibleEnergyAvailable = f;
+    public void setFlexibleEnergyAvailable(float energy) {
+        this.flexibleEnergyAvailable = energy;
     }
 
     /**
@@ -422,20 +375,19 @@ public class HolonElement implements IGraphedElement, GraphEditable{
 		this.stretch=stretch;
 	}
 
-//--> Test Area --> TODO Neue float Point Klasse
-	public void initTestGraphPoints(int percentage)
+	public void initGraphPoints()
 	{
-		testGraphPoints.clear();
-		testGraphPoints.add(new Point2D.Double(0,percentage));
-		testGraphPoints.add(new Point2D.Double(1,percentage));
+		graphPoints.clear();
+		graphPoints.add(new Point2D.Double(0,1.0));
+		graphPoints.add(new Point2D.Double(1,1.0));
 	}
-	public LinkedList<Point2D.Double> getTestGraphPoints() {
-		return testGraphPoints;
+	public LinkedList<Point2D.Double> getGraphPoints() {
+		return graphPoints;
 	}
 
 
-	public void setTestGraphPoints(LinkedList<Point2D.Double> testGraphPoints) {
-		this.testGraphPoints = testGraphPoints;
+	public void setGraphPoints(LinkedList<Point2D.Double> testGraphPoints) {
+		this.graphPoints = testGraphPoints;
 	}
 
 
@@ -447,7 +399,7 @@ public class HolonElement implements IGraphedElement, GraphEditable{
 
 	@Override
 	public LinkedList<Double> getStateGraph() {
-		return getTestGraphPoints();
+		return getGraphPoints();
 	}
 
 	
@@ -479,7 +431,7 @@ public class HolonElement implements IGraphedElement, GraphEditable{
 	
 	private float[] sampleGraph(int sampleLength)
 	{		
-		ListIterator<Point2D.Double> iter = this.testGraphPoints.listIterator();
+		ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
 		Point.Double before = iter.next();
 		Point.Double after = iter.next();
 		float [] sampleCurve = new float[sampleLength];	
@@ -504,4 +456,11 @@ public class HolonElement implements IGraphedElement, GraphEditable{
 	public void sampleGraph() {
 		curveSample = sampleGraph(100);
 	}
+
+
+	@Override
+	public void reset() {
+		initGraphPoints();
+		sampleGraph();
+	}
 }

+ 12 - 27
src/classes/HolonSwitch.java

@@ -78,9 +78,10 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 		);
 		setManualState(true);
 		setAutoState(true);
-		setActiveAt(true);
 		setManualMode(false);
+		setGraphPoints(new LinkedList<Point2D.Double>());
 		initGraphPoints();
+		sampleGraph();
 	}
 
 	/**
@@ -91,6 +92,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 	 */
 	public HolonSwitch(AbstractCpsObject obj) {
 		super(obj);
+		System.out.println("SwitchCopy?");
 		HolonSwitch copyObj = (HolonSwitch)obj;
 		setLocalPeriod(copyObj.getLocalPeriod());
 		setStretching(copyObj.isStretching());
@@ -99,14 +101,11 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 		setManualState(copyObj.getActiveManual());
 		setAutoState(true);
 		setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
-		setActiveAt(true);
-		for (int i = 0; i < activeAt.length; i++) {
-			activeAt[i] = copyObj.getState(i);
-		}
 		setGraphPoints(new LinkedList<Point2D.Double>());
 		for (Point2D.Double p : copyObj.getGraphPoints()) {
 			this.graphPoints.add(new Point2D.Double(p.getX(),p.getY()));
 		}
+		sampleGraph();
 		setManualMode(copyObj.getManualMode());
 	}
 
@@ -213,7 +212,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 	//TODO: javadoc
 	private void initGraphPoints()
 	{
-		graphPoints = new LinkedList<Point2D.Double>();
+		graphPoints.clear();
 		graphPoints.add(new Point2D.Double(0.0, 1.0));
 		graphPoints.add(new Point2D.Double(1.0, 1.0));
 	}
@@ -226,23 +225,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 		return this.manualActive;
 	}
 
-	/**
-	 * Set the value of the Switch.
-	 * 
-	 * @param active
-	 *            the default value
-	 */
-	public void setActiveAt(boolean active) {
-		activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish.
-		for (int i = 0; i < activeAt.length; i++) {
-			this.activeAt[i] = active;
-		}
-	}
-	
-	public void setActiveAt(int pos, boolean active) {
-		//activeAt = new boolean[100];
-		this.activeAt[pos] = active;
-	}
+
 	
 	/**
 	 * Set the overall value of the Switch (manual mode).
@@ -290,10 +273,6 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 
 	@Override
 	public LinkedList<Double> getStateGraph() {
-		for(Double p: graphPoints)
-		{
-			System.out.println(p);
-		}
 		return graphPoints;
 	}
 
@@ -319,4 +298,10 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 	public void sampleGraph() {
 		activeAt = sampleGraph(100);
 	}
+
+	@Override
+	public void reset() {
+		initGraphPoints();
+		sampleGraph();
+	}
 }

+ 6 - 0
src/interfaces/GraphEditable.java

@@ -31,4 +31,10 @@ public interface GraphEditable {
 	 * Sample the Graph on the object.
 	 */
 	void sampleGraph();
+	
+	/**
+	 * Resets the Graph two the initial start
+	 * e.g. the Point Left and Right at 100%
+	 */
+	void reset();
 }

+ 2 - 1
src/ui/controller/ClipboardController.java

@@ -278,13 +278,14 @@ public class ClipboardController {
                 sav = objIDMap.get(sav);
                 HolonSwitch sw = (HolonSwitch) objDispatch.get(sav);
                 sw.setGraphPoints(graphpointTEST);
+                sw.sampleGraph();
                 break;
             case ELEMENT:                
                 break;
             case TESTELEMENT:
                 sav = eleIDMap.get(sav);
                 HolonElement ele1 = eleDispatch.get(sav);
-                ele1.setTestGraphPoints(graphpointTEST);
+                ele1.setGraphPoints(graphpointTEST);
                 ele1.sampleGraph();
                 break;
             default:

+ 9 - 5
src/ui/controller/LoadController.java

@@ -398,13 +398,15 @@ public class LoadController {
             case SWITCH:
                 HolonSwitch sw = (HolonSwitch) objDispatch.get(sav);
                 sw.setGraphPoints(graphpointTEST);
+                sw.sampleGraph();
                 break;
             case ELEMENT:
             	System.out.println("Write me new");
             	break;
             case TESTELEMENT:
                 HolonElement ele1 = eleDispatch.get(sav);
-                ele1.setTestGraphPoints(graphpointTEST);
+                ele1.setGraphPoints(graphpointTEST);
+                ele1.sampleGraph();
                 break;
             default:
                 break;
@@ -504,8 +506,10 @@ public class LoadController {
         }
 
         if (obj instanceof HolonSwitch) {
-            ((HolonSwitch) obj).setActiveAt(true);
-            ((HolonSwitch) obj).setGraphPoints(new LinkedList<>());
+           
+            ((HolonSwitch) obj).setGraphPoints(new LinkedList<>()); 
+            ((HolonSwitch) obj).reset();
+            ((HolonSwitch) obj).sampleGraph();
         }
 
         if (obj instanceof CpsUpperNode) {
@@ -522,9 +526,9 @@ public class LoadController {
      * @param ele the element to be initialized
      */
     void initElements(HolonElement ele) {
-        ele.setAvailableEnergyPerElementAt(ele.getEnergyPerElement());
+        
         ele.setGraphPoints(new LinkedList<>());
-        ele.setTestGraphPoints(new LinkedList<>());
+        ele.reset();
     }
 
     /**

+ 2 - 2
src/ui/controller/SaveController.java

@@ -267,8 +267,8 @@ public class SaveController {
             }
             file.add(key, model.getGson().toJsonTree(temp));
             // if there are gps add them into
-            if (!ele.getTestGraphPoints().isEmpty())
-            	unitgraphTESTToJson(file, ele.getId(), ele.getTestGraphPoints());
+            if (!ele.getGraphPoints().isEmpty())
+            	unitgraphTESTToJson(file, ele.getId(), ele.getGraphPoints());
             temp = new JsonObject();
         }
 

+ 26 - 3
src/ui/view/UnitGraph.java

@@ -245,9 +245,28 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     	
     	g.setColor(editDotColor);
     	drawBoolGraphFromList(g, middle);
-     	
+    	drawSnappingHint(g);
     }
     
+    private void drawSnappingHint(Graphics2D g)
+    {
+    	//ColorHint
+    	g.setColor(Color.RED);
+    	//Threshhold Line
+    	final float dash1[] = {10.0f};
+        final BasicStroke dashed =new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
+        g.setStroke(dashed);
+        
+        
+        int halfheight =  border + heightWithBorder / 2;
+        g.drawLine(0, halfheight , widthWithBorder + 2 * border, halfheight);
+        //Threshhold Text
+        g.drawString("Snapping Threshold", 10, halfheight - 2);
+    }
+    
+    
+    
+    
 	private void drawBoolGraphFromList(Graphics2D g, LinkedList<Position> list) {
 		if(list.size() <= 1) return;
      	ListIterator<Position> iter = list.listIterator();
@@ -478,7 +497,6 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		}
 		return p;
 	}
-	
     private void insertNewGraphPoint(Position pos)
     {
     	System.out.println("insertNewGraphPoint");
@@ -594,7 +612,12 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 
     public void reset() {
        	System.out.println("reset");
-
+       	if(this.actualElement != null) {
+       		actualElement.reset();
+       		overrideUnitGraph(actualElement.getStateGraph());
+       		repaint();
+       	}
+       	
     }
 
     public void update(ArrayList<AbstractCpsObject> obj) {

+ 0 - 2
tests/tests/PraktikumHolonsTestClasses.java

@@ -186,8 +186,6 @@ public class PraktikumHolonsTestClasses {
 
         assertTrue("Array not empty", ele1.getAvailableEnergyAt(0) == -20);
         assertTrue("Array not empty", ele1.getAvailableEnergyAt(2) == -20);
-        ele1.setAvailableEnergyPerElementAt(2, -10);
-        assertTrue("Array not empty", ele1.getAvailableEnergyAt(2) == -10);
         assertTrue("Name not correct", ele1.getEleName().equals("TV"));
         ele1.setEleName(ele2.getEleName());
 		assertTrue("Name not correct", ele1.getEleName().equals("Fridge"));