Parcourir la source

Implements sorting of PropertyTableRows

Toni Schneider il y a 7 ans
Parent
commit
62ee30445b

+ 17 - 0
src/ElementComparator/ElemCompOnEleName.java

@@ -0,0 +1,17 @@
+package ElementComparator;
+
+import classes.HolonElement;
+
+public class ElemCompOnEleName extends ElementComparator {
+
+	@Override
+	public int compare(HolonElement a, HolonElement b) {
+
+		String eleNameA = a.getEleName();
+		String eleNameB = b.getEleName();
+		
+		return eleNameA.compareTo(eleNameB);
+
+	}
+
+}

+ 25 - 0
src/ElementComparator/ElemCompOnEnergy.java

@@ -0,0 +1,25 @@
+package ElementComparator;
+
+import classes.HolonElement;
+
+/**
+ * @author Toni
+ * Bei dieser Implementierung wird nach 
+ * Energieverbrauch pro HolonElement sortiert
+ */
+public class ElemCompOnEnergy extends ElementComparator {
+
+	@Override
+	public int compare(HolonElement a, HolonElement b) {
+
+		float verbrauchA = a.getEnergyPerElement();
+		float verbrauchB = b.getEnergyPerElement();
+		if (verbrauchA < verbrauchB)
+			return -1;
+		if (verbrauchA > verbrauchB)
+			return 1;
+		return 0;
+
+	}
+
+}

+ 27 - 0
src/ElementComparator/ElemCompOnId.java

@@ -0,0 +1,27 @@
+/**
+ * 
+ */
+package ElementComparator;
+
+import classes.HolonElement;
+
+/**
+ * @author Toni
+ *Comparator zum sortieren nach HolonElementId
+ */
+public class ElemCompOnId extends ElementComparator {
+
+	@Override
+	public int compare(HolonElement a, HolonElement b) {
+
+		float idA = a.getId();
+		float idB = b.getId();
+		if (idA < idB)
+			return -1;
+		if (idA > idB)
+			return 1;
+		return 0;
+
+	}
+
+}

+ 17 - 0
src/ElementComparator/ElementComparator.java

@@ -0,0 +1,17 @@
+package ElementComparator;
+
+import java.util.Comparator;
+
+import classes.HolonElement;
+
+/**
+ * @author Toni
+ *Eine abstrakte Klasse um bei HolonObjekten den Comparator 
+ *der die HolonElemente sortiert welchseln zu können
+ */
+public abstract class ElementComparator implements Comparator<HolonElement> {
+	
+	public abstract int compare(HolonElement a, HolonElement b);
+	
+}
+	

+ 17 - 2
src/classes/HolonElement.java

@@ -15,7 +15,10 @@ public class HolonElement {
 
     /* Points on the UnitGraph */
     private LinkedList<Point> graphPoints;
-    /* Name of the gadget */
+    /* Name of the Object: House, 1 */
+    @Expose
+    private String objName;
+    /* Name of the gadget: TV */
     @Expose
     private String eleName;
     /* Quantity */
@@ -111,7 +114,19 @@ public class HolonElement {
         setFlexible(false);
     }
 
-    /**
+
+
+	
+
+	public String getObjName() {
+		return objName;
+	}
+
+	public void setObjName(String objName) {
+		this.objName = objName;
+	}
+
+	/**
      * Get the Array of energyPerElement (100 values).
      *
      * @return availableEnergyPerElementAt Array of Floats

+ 30 - 6
src/ui/controller/UpdateController.java

@@ -8,7 +8,14 @@ import ui.view.PropertyTable;
 import ui.view.UpperNodeCanvas;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.HashMap;
+import java.util.LinkedList;
+
+import ElementComparator.ElemCompOnEleName;
+import ElementComparator.ElemCompOnEnergy;
+import ElementComparator.ElemCompOnId;
+import ElementComparator.ElementComparator;
 
 /**
  * This class is for all update methods and more ;)
@@ -46,6 +53,7 @@ public class UpdateController {
 		}
 	}
 
+	    
 	/**
 	 * Add the Information of the given ArrayList in the HolonElement Model as
 	 * Name,Energy and Amount.
@@ -54,23 +62,39 @@ public class UpdateController {
 	 *            ArrayList to be displayed
 	 */
 	public void fillElementTable(ArrayList<AbstractCpsObject> objects, PropertyTable table) {
+		LinkedList<HolonElement> elemList = new LinkedList<HolonElement>();
+		ElementComparator compEnergy = new ElemCompOnEnergy();
+		//ElementComparator compId = new ElemCompOnId();
+		ElementComparator compEleName = new ElemCompOnEleName();
 		if (objects.size() > 1) {
 			for (AbstractCpsObject o : objects) {
 				if (o instanceof HolonObject) {
 					for (HolonElement he : ((HolonObject) o).getElements()) {
-						Object[] temp = {o.getName() + ", " + o.getId(), he.getId(), he.getEleName(), he.getEnergyPerElement(),
-								he.getFlexibleEnergyAvailablePerElement(), he.getAmount(), he.isActive(), he.isFlexible()};
-						table.addRow(temp);
+						he.setObjName(o.getName() + ", " + o.getId());
+						elemList.add(he);
+						elemList.sort(compEleName);
+						elemList.sort(compEnergy);
 					}
 				}
 			}
+			for (HolonElement e1 : elemList) {
+				Object[] temp2 = { e1.getObjName(), e1.getId(), e1.getEleName(), e1.getEnergyPerElement(),
+						e1.getFlexibleEnergyAvailablePerElement(), e1.getAmount(), e1.isActive(), e1.isFlexible() };
+				table.addRow(temp2);
+			}
+
 		} else if (objects.size() == 1) {
 			AbstractCpsObject o = objects.get(0);
 			if (o instanceof HolonObject) {
 				for (HolonElement he : ((HolonObject) o).getElements()) {
-					Object[] temp = {he.getId(), he.getEleName(), he.getEnergyPerElement(), he.getFlexibleEnergyAvailablePerElement(), he.getAmount(),
-							he.isActive(), he.isFlexible()};
-                    table.addRow(temp);
+					elemList.add(he);
+					elemList.sort(compEleName);
+					elemList.sort(compEnergy);
+				}
+				for (HolonElement e1 : elemList) {
+					Object[] temp2 = { e1.getId(), e1.getEleName(), e1.getEnergyPerElement(),
+							e1.getFlexibleEnergyAvailablePerElement(), e1.getAmount(), e1.isActive(), e1.isFlexible() };
+					table.addRow(temp2);
 				}
 			}
 		}

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

@@ -134,7 +134,7 @@ public class GUI implements CategoryListener {
     private final JButton btnAdd = new JButton("+");
     private final JButton btnDel = new JButton("-");
     private final JButton btnAddHolEL = new JButton("+");
-    private final JButton btnDelHolEL = new JButton("-");
+    private final JButton btnDelHolEL = new JButton("-");;
     private final JButton resetGraphBtn = new JButton("Reset");
     private final JToolBar toolBar = new JToolBar();
     private final JToolBar toolBarHolonEl = new JToolBar();
@@ -145,6 +145,7 @@ public class GUI implements CategoryListener {
     private final JMenuItem germanBtn = new JMenuItem("DE");
     private final JMenuItem czechBtn = new JMenuItem("CZ");
     private final JMenuItem chineseBtn = new JMenuItem("ZH");
+  
     // private final JComboBox comboBoxGraph = new JComboBox();
     private final Console console = new Console();
     private final MyCanvas canvas;
@@ -164,6 +165,7 @@ public class GUI implements CategoryListener {
             "Quantity", "Activated", "Flexible"};
     private final String[] columnNamesSingle = {"Nr.", "Device", "Energy", "Flexible Energy Available", "Quantity",
             "Activated", "Flexible"};
+
     private final ArrayList<PropertyTable> tables = new ArrayList<>();
     private final String[] comboBoxCat = {"Category", "Object", "Switch"};
     private final UpdateController updCon;
@@ -597,6 +599,10 @@ public class GUI implements CategoryListener {
         mnLanguage.add(chineseBtn);
         chineseBtn.setIcon(new ImageIcon(new ImageIcon("res/Button_Images/FlagZH.png").getImage().getScaledInstance(20,
                 20, java.awt.Image.SCALE_SMOOTH)));
+        
+
+
+        
 
         mntmFindReplace.addActionListener(actionEvent -> {
             try {
@@ -608,6 +614,11 @@ public class GUI implements CategoryListener {
                 ex.printStackTrace();
             }
         });
+        
+        
+        
+
+        
 
         mnNewMenuEdit.add(mntmFindReplace);
         mntmEditShowedInformation.addActionListener(actionEvent -> {
@@ -813,6 +824,7 @@ public class GUI implements CategoryListener {
         // single-selection mode (CPS-Object)
         model.getMultiTable().setColumnIdentifiers(columnNamesMulti);
         model.getSingleTable().setColumnIdentifiers(columnNamesSingle);
+        //TODO
         model.getTableHolonElement().setBorder(null);
         model.getTableHolonElement().setFillsViewportHeight(true);
         model.getTableHolonElement().setCellSelectionEnabled(true);
@@ -1707,6 +1719,8 @@ public class GUI implements CategoryListener {
             refreshLanguages();
             updCon.refreshTableProperties(model.getPropertyTable());
         });
+        
+        
 
         mntmUndo.addActionListener(new ActionListener() {
             @Override