Browse Source

Creation of UpdateController

Edgardo Palza 7 years ago
parent
commit
1b6560780f
4 changed files with 179 additions and 133 deletions
  1. 122 0
      src/ui/controller/UpdateController.java
  2. 13 4
      src/ui/model/Model.java
  3. 39 125
      src/ui/view/GUI.java
  4. 5 4
      src/ui/view/UpperNodeCanvas.java

+ 122 - 0
src/ui/controller/UpdateController.java

@@ -0,0 +1,122 @@
+package ui.controller;
+
+import java.util.ArrayList;
+
+import classes.AbstractCpsObject;
+import classes.HolonElement;
+import classes.HolonObject;
+import ui.view.DefaulTable;
+import ui.view.Languages;
+import ui.view.PropertyTable;
+import ui.model.*;
+import ui.controller.*;
+
+/**
+ * This class is for all update methods and more ;)
+ * 
+ * @author Edgardo
+ *
+ */
+public class UpdateController {
+
+	Model model;
+	Control controller;
+
+	public UpdateController(Model model, Control control) {
+		this.model = model;
+		this.controller = control;
+	}
+
+	/**
+	 * Update the information concerning properties of the selected CpsObject.
+	 */
+	public void refreshTableProperties(DefaulTable table) {
+		if (model.getSelectedCpsObjects().size() == 1) {
+			AbstractCpsObject tempCps = model.getSelectedCpsObject();
+			if (tempCps != null && tempCps.getClass() == HolonObject.class) {
+				table.removeRow(2);
+				Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) tempCps).getCurrentEnergy() };
+				table.insertRow(2, tempEnergy);
+			}
+		}
+	}
+
+	/**
+	 * Add the Information of the given ArrayList in the HolonElement Model as
+	 * Name,Energy and Amount.
+	 * 
+	 * @param objects
+	 *            ArrayList to be displayed
+	 */
+	public void fillElementTable(ArrayList<AbstractCpsObject> objects, PropertyTable table) {
+		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.getEnergy(),
+								he.getAmount(), he.getActive() };
+						table.addRow(temp);
+					}
+				}
+			}
+		} 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.getEnergy(), he.getAmount(), he.getActive() };
+					table.addRow(temp);
+				}
+			}
+		}
+	}
+
+	/**
+	 * Update the HolonElement Table, that means erase all rows and add the new
+	 * rows with new data.
+	 */
+	public void refreshTableHolonElement(PropertyTable multiTable, PropertyTable singleTable) {
+		// Update of the Information about the HolonElements - only for
+		// HolonObjects
+
+		if (model.getSelectedCpsObjects().size() > 1) {
+			deleteRows(multiTable);
+			fillElementTable(model.getSelectedCpsObjects(), multiTable);
+			multiTable.fireTableDataChanged();
+		} else if (model.getSelectedCpsObjects().size() == 1) {
+			deleteRows(singleTable);
+			fillElementTable(model.getSelectedCpsObjects(), singleTable);
+			singleTable.fireTableDataChanged();
+		}
+	}
+
+	/**
+	 * Erase all information of the HolonElement Model.
+	 * 
+	 * @param t
+	 *            the Table
+	 */
+	public void deleteRows(PropertyTable t) {
+		if (t.getRowCount() > 0) {
+			for (int i = t.getRowCount() - 1; i > -1; i--) {
+				t.removeRow(i);
+			}
+		}
+	}
+
+	/**
+	 * Search for clicked HolonObject through the mouse's y-Coord.
+	 * 
+	 * @param yValue
+	 *            the Y Coordination
+	 * @return clicked HolonObject
+	 */
+	public HolonObject getHolonObj(int yValue, PropertyTable table) {
+		final int yTemp = (int) Math.floor(yValue / 16);
+		HolonObject obtTemp = null;
+		String temp = table.getValueAt(yTemp, 0).toString();
+		int idTemp = Integer.parseInt(temp.split(", ")[1]);
+		obtTemp = (HolonObject) controller.searchByID(idTemp);
+		return obtTemp;
+	}
+
+}

+ 13 - 4
src/ui/model/Model.java

@@ -290,6 +290,15 @@ public class Model {
 		return selectedObjects;
 	}
 
+	/**
+	 * Returns all selected Objects on the Canvas.
+	 * 
+	 * @return The selected Objects
+	 */
+	public void setSelectedCpsObjects(ArrayList<AbstractCpsObject> arr) {
+		this.selectedObjects = arr;
+	}
+
 	/**
 	 * Returns the Selected Holon Element.
 	 * 
@@ -359,10 +368,10 @@ public class Model {
 	}
 
 	private void notifyGraphListeners() {
-		for(GraphListener gl: graphListeners){
+		for (GraphListener gl : graphListeners) {
 			gl.repaintGraph();
 		}
-		
+
 	}
 
 	/**
@@ -630,8 +639,8 @@ public class Model {
 	public ArrayList<HolonObject> getTrackingObj() {
 		return trackingObj;
 	}
-	
-	public void addGraphListener(GraphListener gl){
+
+	public void addGraphListener(GraphListener gl) {
 		graphListeners.add(gl);
 	}
 

+ 39 - 125
src/ui/view/GUI.java

@@ -80,6 +80,7 @@ import classes.IdCounter;
 import classes.IdCounterElem;
 import interfaces.CategoryListener;
 import ui.controller.Control;
+import ui.controller.UpdateController;
 import ui.model.Model;
 
 /**
@@ -267,6 +268,8 @@ public class GUI<E> implements CategoryListener {
 	private String eraseCategory = "Do you really want to delete the Category ";
 	private String selectObjBeforeErase = "Please select a Category or an Object in order to delete something.";
 
+	UpdateController updCon;
+
 	/**
 	 * Create the application.
 	 * 
@@ -288,7 +291,7 @@ public class GUI<E> implements CategoryListener {
 		simMenu = new SimulationMenu(model, control);
 		initialize();
 		updateCategories(model.getCategories());
-
+		updCon = new UpdateController(model, controller);
 	}
 
 	/**
@@ -685,8 +688,8 @@ public class GUI<E> implements CategoryListener {
 							controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
 									ele.getEnergy());
 						}
-						refreshTableHolonElement();
-						refreshTableProperties();
+						updCon.refreshTableHolonElement(tableModelHolonElementMulti, tableModelHolonElementSingle);
+						updCon.refreshTableProperties(tableModelProperties);
 						controller.calculateStateForTimeStep(model.getCurIteration());
 					}
 				}
@@ -704,8 +707,8 @@ public class GUI<E> implements CategoryListener {
 						HolonObject obj = (HolonObject) getActualCps();
 						for (HolonElement e : selectedElements) {
 							controller.deleteElementCanvas(obj.getID(), e.getId());
-							refreshTableHolonElement();
-							refreshTableProperties();
+							updCon.refreshTableHolonElement(tableModelHolonElementMulti, tableModelHolonElementSingle);
+							updCon.refreshTableProperties(tableModelProperties);
 							controller.calculateStateForTimeStep(model.getCurIteration());
 							// Names displayed in graph are not updated
 						}
@@ -719,8 +722,8 @@ public class GUI<E> implements CategoryListener {
 							controller.deleteElementCanvas(i, e.getId());
 						}
 					}
-					refreshTableHolonElement();
-					refreshTableProperties();
+					updCon.refreshTableHolonElement(tableModelHolonElementMulti, tableModelHolonElementSingle);
+					updCon.refreshTableProperties(tableModelProperties);
 					eleToDelete.clear();
 					selectedElements.clear();
 				}
@@ -801,7 +804,7 @@ public class GUI<E> implements CategoryListener {
 					if (model.getSelectedCpsObjects().size() > 1) {
 						int selectedValueX = (int) Math.floor(xThis / (tableHolonElement.getWidth() / 6));
 						int selectedValueBX = (int) Math.floor(xBThis / (tableHolonElement.getWidth() / 6));
-						if (getHolonObj(yMouse) != null) {
+						if (updCon.getHolonObj(yMouse, tableModelHolonElementMulti) != null) {
 							// For last column (boolean with a checkbox)
 							if (selectedValueBX == 5) {
 								HolonElement eleBTemp = getActualHolonElement(null, yBMouse, 0);
@@ -862,7 +865,7 @@ public class GUI<E> implements CategoryListener {
 							}
 						}
 					}
-					refreshTableProperties();
+					updCon.refreshTableProperties(tableModelProperties);
 					tableModelHolonElementSingle.fireTableDataChanged();
 					controller.calculateStateForTimeStep(model.getCurIteration());
 					unitGraph.repaint();
@@ -922,9 +925,10 @@ public class GUI<E> implements CategoryListener {
 									((HolonSwitch) getActualCps()).setManualState(bTemp);
 								}
 							}
-						}// else if (getActualCps() instanceof AbstractCpsObject) {
-							getActualCps().setName(temp.toString());
-						//}
+						} // else if (getActualCps() instanceof
+							// AbstractCpsObject) {
+						getActualCps().setName(temp.toString());
+						// }
 					} else {
 						temp = tableModelProperties.getValueAt(selValueY, selValueX);
 						btemp = tableModelProperties.getValueAt(mousePos.y / tableProperties.getRowHeight(),
@@ -1085,8 +1089,8 @@ public class GUI<E> implements CategoryListener {
 							.getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
 					if (selectedNode.getLevel() == 2) {
 						controller.searchCategoryObject(selectedNode.getParent().toString(), selectedNode.toString());
-						deleteRows(tableModelHolonElementSingle);
-						deleteRows(tableModelHolonElementMulti);
+						updCon.deleteRows(tableModelHolonElementSingle);
+						updCon.deleteRows(tableModelHolonElementMulti);
 						// if (selected instanceof HolonObject && selected !=
 						// null) {
 						// selected = (HolonObject) selected;
@@ -1244,7 +1248,7 @@ public class GUI<E> implements CategoryListener {
 					// For HolonObjects the Total Energy (production or
 					// consumption) is calculated
 					if (temp instanceof HolonObject) {
-						refreshTableHolonElement();
+						updCon.refreshTableHolonElement(tableModelHolonElementMulti, tableModelHolonElementSingle);
 						Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) temp).getCurrentEnergy() };
 						tableModelProperties.addRow(tempEnergy);
 						tableModelProperties.setCellEditable(0, 1, true);
@@ -1253,8 +1257,8 @@ public class GUI<E> implements CategoryListener {
 					} // For HolonSwitches is showed the actual status (active
 						// or inactive)
 					else if (temp instanceof HolonSwitch) {
-						deleteRows(tableModelHolonElementSingle);
-						deleteRows(tableModelHolonElementMulti);
+						updCon.deleteRows(tableModelHolonElementSingle);
+						updCon.deleteRows(tableModelHolonElementMulti);
 						Object[] tempMode = { Languages.getLanguage()[74], ((HolonSwitch) temp).getManualMode() };
 						tableModelProperties.addRow(tempMode);
 						if (((HolonSwitch) temp).getManualMode()) {
@@ -1273,8 +1277,8 @@ public class GUI<E> implements CategoryListener {
 						tableModelProperties.setCellEditable(0, 1, true);
 						tableModelProperties.setCellEditable(2, 1, true);
 					} else {
-						deleteRows(tableModelHolonElementSingle);
-						deleteRows(tableModelHolonElementMulti);
+						updCon.deleteRows(tableModelHolonElementSingle);
+						updCon.deleteRows(tableModelHolonElementMulti);
 					}
 					// For Objects the only editable cell is the name
 					ArrayList<CpsEdge> tempArray = temp.getConnections();
@@ -1330,8 +1334,8 @@ public class GUI<E> implements CategoryListener {
 					tableModelProperties.setCellEditable(2, 1, true);
 					tableModelProperties.setCellEditable(3, 1, true);
 				} else if (getActualCps() == null) {
-					deleteRows(tableModelHolonElementSingle);
-					deleteRows(tableModelHolonElementMulti);
+					updCon.deleteRows(tableModelHolonElementSingle);
+					updCon.deleteRows(tableModelHolonElementMulti);
 				} // Update of the HolonElementTable (Single- or
 					// Multi-Selection)
 				if (model.getSelectedCpsObjects().size() > 1) {
@@ -1347,8 +1351,8 @@ public class GUI<E> implements CategoryListener {
 
 			@Override
 			public void mouseReleased(MouseEvent e) {
-				refreshTableHolonElement();
-				refreshTableProperties();
+				updCon.refreshTableHolonElement(tableModelHolonElementMulti, tableModelHolonElementSingle);
+				updCon.refreshTableProperties(tableModelProperties);
 				if (model.getSelectedCpsObjects().size() > 1) {
 					tableHolonElement.setModel(tableModelHolonElementMulti);
 				} else if (model.getSelectedCpsObjects().size() == 1) {
@@ -1499,7 +1503,7 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				Languages.setLanguage(0);
 				refreshLanguages();
-				refreshTableProperties();
+				updCon.refreshTableProperties(tableModelProperties);
 			}
 		});
 		spanishBtn.addActionListener(new ActionListener() {
@@ -1508,7 +1512,7 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				Languages.setLanguage(1);
 				refreshLanguages();
-				refreshTableProperties();
+				updCon.refreshTableProperties(tableModelProperties);
 			}
 		});
 		germanBtn.addActionListener(new ActionListener() {
@@ -1517,7 +1521,7 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				Languages.setLanguage(2);
 				refreshLanguages();
-				refreshTableProperties();
+				updCon.refreshTableProperties(tableModelProperties);
 			}
 		});
 		czechBtn.addActionListener(new ActionListener() {
@@ -1526,7 +1530,7 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				Languages.setLanguage(3);
 				refreshLanguages();
-				refreshTableProperties();
+				updCon.refreshTableProperties(tableModelProperties);
 			}
 		});
 		chineseBtn.addActionListener(new ActionListener() {
@@ -1535,7 +1539,8 @@ public class GUI<E> implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				Languages.setLanguage(4);
 				refreshLanguages();
-				refreshTableProperties();
+				updCon.refreshTableProperties(tableModelProperties);
+				;
 			}
 		});
 
@@ -1852,98 +1857,6 @@ public class GUI<E> implements CategoryListener {
 		}
 	}
 
-	/**
-	 * Search for clicked HolonObject through the mouse's y-Coord.
-	 * 
-	 * @param yValue
-	 *            the Y Coordination
-	 * @return clicked HolonObject
-	 */
-	private HolonObject getHolonObj(int yValue) {
-		final int yTemp = (int) Math.floor(yValue / 16);
-		HolonObject obtTemp = null;
-		String temp = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
-		int idTemp = Integer.parseInt(temp.split(", ")[1]);
-		obtTemp = (HolonObject) controller.searchByID(idTemp);
-		return obtTemp;
-	}
-
-	/**
-	 * Update the HolonElement Table, that means erase all rows and add the new
-	 * rows with new data.
-	 */
-	private void refreshTableHolonElement() {
-		// Update of the Information about the HolonElements - only for
-		// HolonObjects
-
-		if (model.getSelectedCpsObjects().size() > 1) {
-			deleteRows(tableModelHolonElementMulti);
-			fillElementTable(model.getSelectedCpsObjects());
-			tableModelHolonElementMulti.fireTableDataChanged();
-		} else if (model.getSelectedCpsObjects().size() == 1) {
-			deleteRows(tableModelHolonElementSingle);
-			fillElementTable(model.getSelectedCpsObjects());
-			tableModelHolonElementSingle.fireTableDataChanged();
-		}
-	}
-
-	/**
-	 * Erase all information of the HolonElement Model.
-	 * 
-	 * @param t
-	 *            the Table
-	 */
-	private void deleteRows(PropertyTable t) {
-		if (t.getRowCount() > 0) {
-			for (int i = t.getRowCount() - 1; i > -1; i--) {
-				t.removeRow(i);
-			}
-		}
-	}
-
-	/**
-	 * Add the Information of the given ArrayList in the HolonElement Model as
-	 * Name,Energy and Amount.
-	 * 
-	 * @param objects
-	 *            ArrayList to be displayed
-	 */
-	private void fillElementTable(ArrayList<AbstractCpsObject> objects) {
-		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.getEnergy(),
-								he.getAmount(), he.getActive() };
-						tableModelHolonElementMulti.addRow(temp);
-					}
-				}
-			}
-		} 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.getEnergy(), he.getAmount(), he.getActive() };
-					tableModelHolonElementSingle.addRow(temp);
-				}
-			}
-		}
-	}
-
-	/**
-	 * Update the information concerning properties of the selected CpsObject.
-	 */
-	private void refreshTableProperties() {
-		if (model.getSelectedCpsObjects().size() == 1) {
-			AbstractCpsObject tempCps = getActualCps();
-			if (tempCps != null && tempCps.getClass() == HolonObject.class) {
-				tableModelProperties.removeRow(2);
-				Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) tempCps).getCurrentEnergy() };
-				tableModelProperties.insertRow(2, tempEnergy);
-			}
-		}
-	}
-
 	/**
 	 * Adds a Popup.
 	 * 
@@ -2058,13 +1971,14 @@ public class GUI<E> implements CategoryListener {
 	 */
 	private void openNewUpperNodeTab() {
 		UpperNodeCanvas unc;
-		if(((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
-		.getComponent(0) instanceof MyCanvas){
-			unc = new UpperNodeCanvas(model, controller, (CpsUpperNode) temp, "" );
+		if (((JScrollPane) tabbedPane.getSelectedComponent()).getViewport().getComponent(0) instanceof MyCanvas) {
+			unc = new UpperNodeCanvas(model, controller, (CpsUpperNode) temp, "");
 		} else {
-			unc = new UpperNodeCanvas(model, controller, (CpsUpperNode) temp, ((UpperNodeCanvas)((JScrollPane) tabbedPane.getSelectedComponent()).getViewport().getComponent(0)).path+" -> " );	
+			unc = new UpperNodeCanvas(model, controller, (CpsUpperNode) temp,
+					((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+							.getComponent(0)).path + " -> ");
 		}
-		
+
 		unc.setBorder(null);
 		unc.setBackground(Color.WHITE);
 		unc.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));

+ 5 - 4
src/ui/view/UpperNodeCanvas.java

@@ -108,8 +108,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 		this.upperNode = UpperNode;
 		this.path = parentPath + upperNode.getName();
 		this.breadCrumb = new JLabel(path);
-		//this.add(breadCrumb);
-
+		// this.add(breadCrumb);
 		scalediv20 = model.getScale() / 20;
 
 		showedInformation[0] = true;
@@ -244,7 +243,7 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 	public void paintComponent(Graphics g) {
 		String maxCap;
 		super.paintComponent(g);
-		((JScrollPane)this.getParent().getParent()).setColumnHeaderView(breadCrumb);
+		((JScrollPane) this.getParent().getParent()).setColumnHeaderView(breadCrumb);
 		// Rendering
 		g2 = (Graphics2D) g;
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
@@ -479,7 +478,9 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 				}
 			}
 		}
-
+		// Selection of CpsObject
+		model.setSelectedCpsObject(tempCps);
+		
 		// Edge Selection
 		if (tempCps == null) {
 			edgeHighlight = mousePositionOnEdge(x, y);