Forráskód Böngészése

I surrender. Using a static constant now.

Ludwig Tietze 6 éve
szülő
commit
643959ad26

+ 20 - 14
src/classes/HolonElement.java

@@ -2,6 +2,8 @@ package classes;
 
 import com.google.gson.annotations.Expose;
 
+import ui.model.Model;
+
 import java.awt.*;
 import java.util.LinkedList;
 
@@ -45,6 +47,10 @@ public class HolonElement {
     /* ID */
     @Expose
     private int id;
+    
+    private static final int DEFAULT_GRAPH_LENGTH=100;
+    //private int graphLength=DEFAULT_GRAPH_LENGTH; Unimplementable due to former developer's dark magic.
+    
     /*
      * Energy at each point of the graph with 100 predefined points. At the
      * beginning, it starts with all values at energyPerElement.
@@ -59,25 +65,24 @@ public class HolonElement {
      * @param eleName String
      * @param amount  int
      * @param energy  float
+     * @param model Model
      */
+    public HolonElement(String eleName, int amount, float energy, Model model) {
+    	this(eleName, amount, energy, IdCounterElem.nextId(),model);
+    }
+    
     public HolonElement(String eleName, int amount, float energy) {
-        setEleName(eleName);
-        setAmount(amount);
-        setEnergyPerElement(energy);
-        setActive(true);
-        setSign(energy);
-        setAvailableEnergyPerElementAt(energy);
-        setGraphPoints(new LinkedList<>());
-        setId(IdCounterElem.nextId());
-        setFlexibleEnergyAvailable(0);
-        setFlexible(false);
+    	this(eleName, amount, energy, IdCounterElem.nextId(),null);//TODO: This is just for the old tests...
+    	System.out.println("HELL NO");
     }
 
     /**
      * same as standard constructor, but with already given id (so the counter is not increased twice)
      */
-    public HolonElement(String eleName, int amount, float energy, int id) {
-        setEleName(eleName);
+    public HolonElement(String eleName, int amount, float energy, int id, Model model){
+    	//if(model!=null)graphLength=model.getGraphIterations();
+    	//if(graphLength==0)graphLength=DEFAULT_GRAPH_LENGTH;//To counter their constructor bypassing...
+    	setEleName(eleName);
         setAmount(amount);
         setEnergyPerElement(energy);
         setActive(true);
@@ -95,6 +100,7 @@ public class HolonElement {
      * @param element element to copy
      */
     public HolonElement(HolonElement element) {
+    	//graphLength=element.graphLength;//Should I add a getter?
         setEleName(element.getEleName());
         setAmount(element.getAmount());
         setEnergyPerElement(element.getEnergyPerElement());
@@ -151,7 +157,7 @@ public class HolonElement {
      * Get the energyPerElement currently available
      */
     public float getAvailableEnergyAt(int timestep) {
-        return this.availableEnergyPerElementAt[timestep%100];//TODO: Since I added the modulo twice, something could be refactored here.
+        return this.availableEnergyPerElementAt[timestep%DEFAULT_GRAPH_LENGTH];//TODO:This would use the variable
     }
 
     /**
@@ -259,7 +265,7 @@ public class HolonElement {
         if (flexible) {
             return ((float) amount) * energyPerElement;
         } else {
-            return ((float) amount) * availableEnergyPerElementAt[x%100];//TODO: Adapt graph length
+            return ((float) amount) * availableEnergyPerElementAt[x%DEFAULT_GRAPH_LENGTH];//TODO: Adapt graph length
         }
     }
 

+ 2 - 0
src/classes/HolonObject.java

@@ -26,6 +26,8 @@ public class HolonObject extends AbstractCpsObject {
     public final static int PRODUCER = 3;
     public final static int PARTIALLY_SUPPLIED = 4;
     public final static int OVER_SUPPLIED = 5;
+    
+    private static final int DEFAULT_GRAPH_LENGTH = 100;//TODO
     /*
      * Color of the actual state (red = no supplied, yellow = partially supplied
      * and green = supplied)

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

@@ -71,7 +71,7 @@ public class ObjectController {
      * @param elementId the Element ID
      */
     public void addNewElementIntoCanvasObject(int objectId, String element, int amount, float energy, int elementId) {
-        HolonElement ele = new HolonElement(element, amount, energy, elementId);
+        HolonElement ele = new HolonElement(element, amount, energy, elementId, model);
         if (mpC.searchByID(objectId) == null) {
             addElementIntoCanvasObject((HolonObject) model.getSelectedCpsObjects().get(0), ele);
         } else {
@@ -103,7 +103,7 @@ public class ObjectController {
     public void addNewElementIntoCategoryObject(String category, String object, String element, int amount,
                                                 float energy) {
 
-        HolonElement ele = new HolonElement(element, amount, energy);
+        HolonElement ele = new HolonElement(element, amount, energy, model);//TODO: I added model
         addElementIntoCategoryObject(category, object, ele);
     }
 

+ 4 - 2
src/ui/view/AddElementPopUp.java

@@ -3,6 +3,7 @@ package ui.view;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import ui.model.Model;
 
 import javax.swing.*;
 import javax.swing.border.EmptyBorder;
@@ -27,6 +28,7 @@ public class AddElementPopUp extends JDialog {
 	private JTextField amount;
 	private HolonElement hl;
 	private AbstractCpsObject tempCps;
+	private Model model;
 
 //	/**
 //	 * Launch the application.
@@ -48,7 +50,7 @@ public class AddElementPopUp extends JDialog {
 	/**
 	 * Create the dialog.
 	 */
-	AddElementPopUp(JFrame parentFrame) {
+	AddElementPopUp(JFrame parentFrame, Model model) {
 		super((java.awt.Frame) null, true);
 		this.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
 		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
@@ -120,7 +122,7 @@ public class AddElementPopUp extends JDialog {
                         try {
                             float energy = Float.parseFloat(providedEnergy.getText());
                             int elementAmount = Integer.parseInt(amount.getText());
-                            hl = new HolonElement(elementName.getText(), elementAmount, energy);
+                            hl = new HolonElement(elementName.getText(), elementAmount, energy, model);
 
                             dispose();
                         } catch (NumberFormatException e) {

+ 5 - 2
src/ui/view/AddObjectPopUp.java

@@ -5,6 +5,7 @@ import classes.HolonElement;
 import classes.HolonObject;
 import classes.Pair;
 import ui.controller.Control;
+import ui.model.Model;
 
 import javax.swing.*;
 import javax.swing.border.EmptyBorder;
@@ -41,6 +42,7 @@ public class AddObjectPopUp extends JDialog {
 	private AbstractCpsObject toEdit;
 	private boolean editState;
 	private boolean imageChanged = false;
+	private Model model;
 
 //	/**
 //	 * Launch the application.
@@ -68,9 +70,10 @@ public class AddObjectPopUp extends JDialog {
 	 * @param cat
 	 *            the categorie
 	 */
-    AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
+    AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame, Model model) {
         toEdit = obj;
 		editState = edit;
+		this.model=model;
 		this.setIconImage(Util.loadImage(this, "/Images/Dummy_House.png",30,30));
 		setBounds(100, 100, 450, 342);
         setLocationRelativeTo(parentFrame);
@@ -162,7 +165,7 @@ public class AddObjectPopUp extends JDialog {
 		{
 			JButton btnAddDefaultElement = new JButton(Languages.getLanguage()[61]);
 			btnAddDefaultElement.addActionListener(actionEvent -> {
-                addElement = new AddElementPopUp(parentFrame);
+                addElement = new AddElementPopUp(parentFrame, model);
                 addElement.setActualCps(toEdit);
 				addElement.setVisible(true);
 				HolonElement hl = addElement.getElement();

+ 5 - 2
src/ui/view/CreateTemplatePopUp.java

@@ -79,8 +79,10 @@ public class CreateTemplatePopUp extends JDialog {
 	/**
 	 * parent Frame
 	 */
+	
+	private Model model;
+	
 	JFrame parent;
-
 	/**
 	 * Create the dialog.
 	 * 
@@ -105,6 +107,7 @@ public class CreateTemplatePopUp extends JDialog {
 		template = new HolonObject(obj);
 		this.parent = parentFrame;
 		this.controller = controller;
+		this.model=model;//Thankfully that was in the ctor already.
 		/*
 		 * create Frame and GUI
 		 */
@@ -312,7 +315,7 @@ public class CreateTemplatePopUp extends JDialog {
 	 * Add an Holon Element to the template
 	 */
 	private void addElement() {
-		AddElementPopUp popUp = new AddElementPopUp(parent);
+		AddElementPopUp popUp = new AddElementPopUp(parent, model);
 		popUp.setActualCps(template);
 		popUp.setVisible(true);
 		HolonElement he = popUp.getElement();

+ 3 - 3
src/ui/view/GUI.java

@@ -1081,7 +1081,7 @@ public class GUI implements CategoryListener {
 				if (tempCpsObject != null
 						&& tempCpsObject.getClass() == HolonObject.class
 						&& tempCpsObject.getId() != 0) {
-					addElementPopUp = new AddElementPopUp(frmCyberPhysical);
+					addElementPopUp = new AddElementPopUp(frmCyberPhysical, model);//TODO: I didn't even check
 					addElementPopUp.setActualCps(updCon.getActualCps());
 					addElementPopUp.setVisible(true);
 					HolonElement ele = addElementPopUp.getElement();
@@ -1727,7 +1727,7 @@ public class GUI implements CategoryListener {
 		editItem.addActionListener(actionEvent -> {
 			// Remove the selected Object object
 			addObjectPopUP = new AddObjectPopUp(true, tempCps,
-					catOfObjToBeEdited, frmCyberPhysical);
+					catOfObjToBeEdited, frmCyberPhysical, model);
 			addObjectPopUP.setCategory(catOfObjToBeEdited);
 			addObjectPopUP.setController(controller);
 			addObjectPopUP.setVisible(true);
@@ -1768,7 +1768,7 @@ public class GUI implements CategoryListener {
 					if (selectedNode != null && selectedNode.getLevel() == 1) {
 						AbstractCpsObject tmp = new HolonObject("");
 						addObjectPopUP = new AddObjectPopUp(false, tmp, null,
-								frmCyberPhysical);
+								frmCyberPhysical, model);
 						addObjectPopUP.setVisible(true);
 						addObjectPopUP.setController(controller);
 						addObjectPopUP.setCategory(selectedNode.toString());