UpdateController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package ui.controller;
  2. import classes.*;
  3. import classes.comparator.elementComparator.ElemCompOnEleName;
  4. import classes.comparator.elementComparator.ElemCompOnEnergy;
  5. import classes.comparator.elementComparator.ElemCompOnId;
  6. import classes.comparator.elementComparator.ElemCompOnIsActivated;
  7. import classes.comparator.elementComparator.ElemCompOnObj;
  8. import classes.comparator.elementComparator.ElemCompOnQuantity;
  9. import ui.model.DecoratedGroupNode;
  10. import ui.model.DecoratedHolonObject.HolonObjectState;
  11. import ui.model.Model;
  12. import ui.view.DefaulTable;
  13. import ui.view.PropertyTable;
  14. import ui.view.GroupNodeCanvas;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.LinkedList;
  18. /**
  19. * This class is for all update methods and more ;)
  20. *
  21. * @author Gruppe14
  22. *
  23. */
  24. public class UpdateController {
  25. Model model;
  26. Control controller;
  27. private int sortBy;
  28. public UpdateController(Model model, Control control) {
  29. this.model = model;
  30. this.controller = control;
  31. sortBy= 1;
  32. }
  33. /**
  34. * Update the information concerning properties of the selected CpsObject.
  35. */
  36. public void refreshTableProperties(DefaulTable table) {
  37. if (model.getSelectedCpsObjects().size() == 1) {
  38. AbstractCanvasObject tempCps = model.getSelectedCpsObjects().get(0);
  39. if (tempCps != null && tempCps.getClass() == HolonObject.class) {
  40. if (table.getRowCount() != 0) {
  41. table.removeRow(2);
  42. Object[] tempEnergy = { "Total Energy", ((HolonObject) tempCps).getEnergyNeededFromConsumingElements(model.getCurIteration()) };
  43. table.insertRow(2, tempEnergy);
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * Add the Information of the given ArrayList in the HolonElement Model as
  50. * Name,Energy and Amount.
  51. *
  52. * @param objects
  53. * ArrayList to be displayed
  54. */
  55. public void fillElementTable(ArrayList<AbstractCanvasObject> objects, PropertyTable table) {
  56. LinkedList<HolonElement> elemList = new LinkedList<HolonElement>();
  57. if (objects.size() > 1) {
  58. for (AbstractCanvasObject o : objects) {
  59. if (o instanceof HolonObject) {
  60. for (HolonElement he : ((HolonObject) o).getElements()) {
  61. he.setObjName(o.getName() + ", " + o.getId());
  62. elemList.add(he);
  63. elemList=sortElemList(elemList);
  64. }
  65. }
  66. }
  67. for (HolonElement e1 : elemList) {
  68. Object[] temp2 = { e1.getObjName(), e1.getId(), e1.getName(), e1.getEnergyPerElement(),
  69. e1.holon.holonControlUnit.getFlexMan().getFlexManager(controller.getModel().getCurIteration()).isAFlexInUseOfHolonElement(e1),
  70. // controller.getSimManager().getActualFlexManager().isAFlexInUseOfHolonElement(e1),
  71. e1.getAmount(), e1.isActive()};
  72. table.addRow(temp2);
  73. }
  74. } else if (objects.size() == 1) {
  75. AbstractCanvasObject o = objects.get(0);
  76. if (o instanceof HolonObject) {
  77. for (HolonElement he : ((HolonObject) o).getElements()) {
  78. elemList.add(he);
  79. elemList=sortElemList(elemList);
  80. }
  81. for (HolonElement e1 : elemList) {
  82. Object[] temp2 = { e1.getId(), e1.getName(), e1.getEnergyPerElement(),
  83. e1.holon.holonControlUnit.getFlexMan().getFlexManager(controller.getModel().getCurIteration()).isAFlexInUseOfHolonElement(e1),
  84. // controller.getSimManager().getActualFlexManager().isAFlexInUseOfHolonElement(e1),
  85. e1.getAmount(), e1.isActive()};
  86. table.addRow(temp2);
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * @param elemList - HolonElement-List (unsorted)
  93. * @return sorted HolonElement-List!
  94. */
  95. public LinkedList<HolonElement> sortElemList(LinkedList<HolonElement> elemList) {
  96. switch(sortBy) {
  97. case 0://"Object":
  98. elemList.sort(new ElemCompOnObj());
  99. break;
  100. case 1://ID
  101. elemList.sort(new ElemCompOnId());
  102. break;
  103. case 2://"Device":
  104. elemList.sort(new ElemCompOnEleName());
  105. break;
  106. case 3://"Energy":
  107. elemList.sort(new ElemCompOnEnergy());
  108. break;
  109. case 4://"Flexible Energy Available":
  110. elemList.sort(null);
  111. break;
  112. case 5://"Quantity":
  113. elemList.sort(new ElemCompOnQuantity());
  114. break;
  115. case 6://"Activated":
  116. elemList.sort(new ElemCompOnIsActivated());
  117. break;
  118. case 7://"Flexible":
  119. elemList.sort(null);
  120. break;
  121. default:
  122. elemList.sort(new ElemCompOnId());
  123. break;
  124. }
  125. return elemList;
  126. };
  127. /**
  128. * Update the HolonElement Table, that means erase all rows and add the new
  129. * rows with new data.
  130. */
  131. public void refreshTableHolonElement(PropertyTable multiTable, PropertyTable singleTable) {
  132. // Update of the Information about the HolonElements - only for
  133. // HolonObjects
  134. if(multiTable == null || singleTable == null ) return;
  135. if (model.getSelectedCpsObjects().size() > 1) {
  136. deleteRows(multiTable);
  137. fillElementTable(model.getSelectedCpsObjects(), multiTable);
  138. multiTable.fireTableDataChanged();
  139. } else if (model.getSelectedCpsObjects().size() == 1) {
  140. deleteRows(singleTable);
  141. fillElementTable(model.getSelectedCpsObjects(), singleTable);
  142. singleTable.fireTableDataChanged();
  143. }
  144. }
  145. /**
  146. * Erase all information of the HolonElement Model.
  147. *
  148. * @param t
  149. * the Table
  150. */
  151. public void deleteRows(PropertyTable t) {
  152. if (t.getRowCount() > 0) {
  153. for (int i = t.getRowCount() - 1; i > -1; i--) {
  154. t.removeRow(i);
  155. }
  156. }
  157. }
  158. /**
  159. * Search for clicked HolonObject through the mouse's y-Coord.
  160. *
  161. * @param yValue
  162. * the Y Coordination
  163. * @return clicked HolonObject
  164. */
  165. public HolonObject getHolonObj(int yValue, PropertyTable table) {
  166. final int yTemp = (int) Math.floor(yValue / 16);
  167. HolonObject obtTemp = null;
  168. String temp = table.getValueAt(yTemp, 0).toString();
  169. int idTemp = Integer.parseInt(temp.split(", ")[1]);
  170. obtTemp = (HolonObject) controller.searchByID(idTemp);
  171. return obtTemp;
  172. }
  173. /**
  174. * Search for actual selected HolonElement.
  175. *
  176. * @param obj
  177. * selected HolonObject, if obj==null means multi-selection
  178. * active
  179. * @param yValue
  180. * Y-Coord in the HolonElementsTable
  181. * @param toMultiHash
  182. * 0 means no MultiSelection, 1 means MultiSelection without
  183. * Control, 2 means MultiSelection with Control
  184. * @return the selected HolonElement
  185. */
  186. public HolonElement getActualHolonElement(HolonObject obj, int yValue, int toMultiHash,
  187. ArrayList<PropertyTable> tables) {
  188. final int yTemp = (int) Math.floor(yValue / 16);
  189. int rowsTotal = 0;
  190. // Filter for search --> single and multi selection
  191. if (obj == null) {
  192. rowsTotal = tables.get(1).getRowCount();
  193. } else {
  194. rowsTotal = tables.get(0).getRowCount();
  195. }
  196. // search for the clicked HolonObject and HolonElement --> in the
  197. // HolonElementTable
  198. HolonObject obtTemp = null;
  199. HolonElement toReturnEle = null;
  200. int id = 0;
  201. if (rowsTotal != 0 && rowsTotal > yTemp) {
  202. // Multi-Selection search
  203. if (obj == null) {
  204. String tempStringObj = tables.get(1).getValueAt(yTemp, 0).toString();
  205. int idTempObj = Integer.parseInt(tempStringObj.split(", ")[1]);
  206. if (model.getSelectedCpsObjects() != null) {
  207. obtTemp = (HolonObject) getHolonObjSelected(idTempObj);
  208. }
  209. id = Integer.parseInt(tables.get(1).getValueAt(yTemp, 1).toString());
  210. ArrayList<HolonElement> eleTemp = new ArrayList<HolonElement>();
  211. if (model.getEleToDelete().containsKey(idTempObj) && toMultiHash == 2) {
  212. eleTemp = model.getEleToDelete().get(idTempObj);
  213. if (!eleTemp.contains(obtTemp.searchElementById(id))) {
  214. eleTemp.add(obtTemp.searchElementById(id));
  215. model.getEleToDelete().replace(idTempObj, eleTemp);
  216. }
  217. } else if (toMultiHash == 2) {
  218. eleTemp.add(obtTemp.searchElementById(id));
  219. model.getEleToDelete().put(idTempObj, eleTemp);
  220. } else if (toMultiHash == 1) {
  221. model.setEleToDelete(new HashMap<Integer, ArrayList<HolonElement>>());
  222. eleTemp.add(obtTemp.searchElementById(id));
  223. model.getEleToDelete().put(idTempObj, eleTemp);
  224. } else if (toMultiHash == 0) {
  225. toReturnEle = obtTemp.searchElementById(id);
  226. }
  227. } // Single-Selection search
  228. else {
  229. model.setEleToDelete(new HashMap<Integer, ArrayList<HolonElement>>());
  230. id = Integer.parseInt(tables.get(0).getValueAt(yTemp, 0).toString());
  231. toReturnEle = obj.searchElementById(id);
  232. }
  233. model.setSelectedHolonElement(toReturnEle);
  234. return toReturnEle;
  235. } // If no HolonObject selected
  236. else {
  237. model.setEleToDelete(new HashMap<Integer, ArrayList<HolonElement>>());
  238. model.setSelectedHolonElement(null);
  239. return null;
  240. }
  241. }
  242. /**
  243. * Getter for selected CpsObject.
  244. *
  245. * @return selected CpsObject
  246. */
  247. public AbstractCanvasObject getActualCps() {
  248. AbstractCanvasObject tempCps;
  249. if (model.getSelectedCpsObjects().size() == 1) {
  250. tempCps = model.getSelectedCpsObjects().get(0);
  251. } else {
  252. int tempID = model.getSelectedObjectID();
  253. tempCps = controller.searchByID(tempID);
  254. }
  255. return tempCps;
  256. }
  257. /**
  258. * Getter for selected CpsObject.
  259. *
  260. * @return selected CpsObject
  261. */
  262. public AbstractCanvasObject getActualCpsUpperNode(GroupNodeCanvas canvas) {
  263. int tempID = model.getSelectedObjectID();
  264. AbstractCanvasObject tempCps = controller.searchByIDUpperNode(tempID, canvas.upperNode);
  265. return tempCps;
  266. }
  267. public void paintProperties(AbstractCanvasObject obj) {
  268. if (obj != null) {
  269. // Name of the CpsObject
  270. Object[] tempName = { "Name", obj.getName() };
  271. model.getPropertyTable().addRow(tempName);
  272. // Id of the CpsObject
  273. Object[] tempId = { "ID", obj.getId() };
  274. model.getPropertyTable().addRow(tempId);
  275. // For HolonObjects the Total Energy (production or
  276. // consumption) is calculated
  277. if (obj instanceof HolonObject) {
  278. refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  279. Object[] tempEnergy = { "Total Energy", ((HolonObject) obj).getEnergyNeededFromConsumingElements(model.getCurIteration()) };
  280. model.getPropertyTable().addRow(tempEnergy);
  281. model.getPropertyTable().setCellEditable(0, 1, false);
  282. model.getPropertyTable().setCellEditable(2, 1, false);
  283. model.getPropertyTable().setCellEditable(3, 1, false);
  284. model.getPropertyTable().setCellEditable(4, 1, false);
  285. } // For HolonSwitches is showed the actual status (active
  286. // or inactive)
  287. else if (obj instanceof HolonSwitch) {
  288. deleteRows(model.getSingleTable());
  289. deleteRows(model.getMultiTable());
  290. Object[] tempMode = { "Manual", ((HolonSwitch) obj).getManualMode() };
  291. model.getPropertyTable().addRow(tempMode);
  292. if (((HolonSwitch) obj).getManualMode()) {
  293. Object[] tempActive = { "Active", ((HolonSwitch) obj).getManualState() };
  294. model.getPropertyTable().addRow(tempActive);
  295. model.getPropertyTable().setCellEditable(3, 1, true);
  296. } else {
  297. Object[] tempActive = { "Active",
  298. ((HolonSwitch) obj).getState(model.getCurIteration()) };
  299. model.getPropertyTable().addRow(tempActive);
  300. model.getPropertyTable().setCellEditable(3, 1, false);
  301. }
  302. // unitGraph.repaintWithNewSwitch((HolonSwitch) obj);
  303. // elementGraph.setText(obj.getName());
  304. model.getPropertyTable().setCellEditable(0, 1, true);
  305. model.getPropertyTable().setCellEditable(2, 1, true);
  306. } else if (obj instanceof GroupNode) {
  307. deleteRows(model.getSingleTable());
  308. deleteRows(model.getMultiTable());
  309. //short fix please make me new
  310. DecoratedGroupNode dGroupNode = controller.getSimManager().getVisualRepresentationalState(model.getCurIteration()).getCreatedGroupNodes().get((GroupNode) obj);
  311. Object[] info = { "Statistics:", "" };
  312. Object[] numEle = { "Number of Objects (total)", ((GroupNode) obj).getNodes().size() };
  313. Object[] numObj = { "Number of HolonObjects", ((GroupNode) obj).getNumHolonObj().size() };
  314. Object[] numSwi = { "Number of HolonSwitches", ((GroupNode) obj).getNumSwitches().size() };
  315. Object[] numUpp = { "Number of GroupNodes", ((GroupNode) obj).getNumUpperNodes().size() };
  316. model.getPropertyTable().addRow(info);
  317. model.getPropertyTable().addRow(numEle);
  318. model.getPropertyTable().addRow(numObj);
  319. model.getPropertyTable().addRow(numSwi);
  320. model.getPropertyTable().addRow(numUpp);
  321. if(dGroupNode != null) {
  322. int numerator, denominator;
  323. Object[] title = { "HolonObject Statistics:", "" };
  324. denominator = dGroupNode.getAmountOfConsumer() + dGroupNode.getAmountOfSupplier() + dGroupNode.getAmountOfPassiv();
  325. numerator = dGroupNode.getAmountOfSupplier();
  326. Object[] producer = { "Producer:", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  327. numerator = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  328. Object[] notSupplied = { "UnSupplied:", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  329. numerator = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  330. Object[] partiallySupplied = { "PartriallySupplied:", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  331. numerator = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  332. Object[] supplied = { "Supplied:", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  333. numerator = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  334. Object[] overSupplied = { "OverSupplied:", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  335. numerator = dGroupNode.getAmountOfPassiv();
  336. Object[] passiv = { "Passiv(%):", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  337. Object[] nothing= {"", ""};
  338. numerator = dGroupNode.getAmountOfAktiveElemntsFromHolonObjects();
  339. denominator = dGroupNode.getAmountOfElemntsFromHolonObjects();
  340. Object[] aktiv = { "Active HolonElements:", numerator + "/" + denominator + "("+ (float)numerator/(float)denominator * 100 + "%)"};
  341. float consumption = dGroupNode.getConsumptionFromConsumer();
  342. float production = dGroupNode.getProductionFromSupplier();
  343. Object[] consumptionObj = { "Total Consumption:", consumption};
  344. Object[] productionObj = { "Total Production:", production};
  345. Object[] difference = { "Difference:", production - consumption};
  346. model.getPropertyTable().addRow(title);
  347. model.getPropertyTable().addRow(producer);
  348. model.getPropertyTable().addRow(notSupplied);
  349. model.getPropertyTable().addRow(partiallySupplied);
  350. model.getPropertyTable().addRow(supplied);
  351. model.getPropertyTable().addRow(overSupplied);
  352. model.getPropertyTable().addRow(passiv);
  353. model.getPropertyTable().addRow(nothing);
  354. model.getPropertyTable().addRow(aktiv);
  355. model.getPropertyTable().addRow(nothing);
  356. model.getPropertyTable().addRow(consumptionObj);
  357. model.getPropertyTable().addRow(productionObj);
  358. model.getPropertyTable().addRow(difference);
  359. }
  360. } else {
  361. deleteRows(model.getSingleTable());
  362. deleteRows(model.getMultiTable());
  363. }
  364. // For Objects the only editable cell is the name
  365. ArrayList<Edge> tempArray = obj.getConnections();
  366. // If the clicked object has connections
  367. if (!tempArray.isEmpty()) {
  368. boolean first = true;
  369. for (Edge temp2 : tempArray) {
  370. if (first) {
  371. first = false;
  372. if (obj.getId() != temp2.getA().getId()) {
  373. Object[] tempConnection = { obj.getName() + " is connected to",
  374. temp2.getA().getName() + " with ID: " + temp2.getA().getId() };
  375. model.getPropertyTable().addRow(tempConnection);
  376. } else {
  377. Object[] tempConnection = { obj.getName() + " is connected to",
  378. temp2.getB().getName() + " with ID: " + temp2.getB().getId() };
  379. model.getPropertyTable().addRow(tempConnection);
  380. }
  381. } else {
  382. if (obj.getId() != temp2.getA().getId()) {
  383. Object[] tempConnection = { "",
  384. temp2.getA().getName() + " with ID: " + temp2.getA().getId() };
  385. model.getPropertyTable().addRow(tempConnection);
  386. } else {
  387. Object[] tempConnection = { "",
  388. temp2.getB().getName() + " with ID: " + temp2.getB().getId() };
  389. model.getPropertyTable().addRow(tempConnection);
  390. }
  391. }
  392. }
  393. }
  394. } // If the clicked Object is an edge
  395. else if (model.getSelectedEdge() != null) {
  396. // Name displayed
  397. Object[] tempName = { "Name",
  398. "Edge: " + model.getSelectedEdge().getA().getName() + " to "
  399. + model.getSelectedEdge().getB().getName() };
  400. model.getPropertyTable().addRow(tempName);
  401. // Current Flow displayed
  402. Object[] tempFlow = { "Current flow", "" };
  403. model.getPropertyTable().addRow(tempFlow);
  404. // Max Capacity displayed
  405. Object[] tempCapacity = { "Max. Capacity", model.getSelectedEdge().getCapacity() };
  406. model.getPropertyTable().addRow(tempCapacity);
  407. // Status displayed
  408. Object[] tempStatus = {"Length", model.getSelectedEdge().getLength() };
  409. model.getPropertyTable().addRow(tempStatus);
  410. // For edges, the only possible editable cell is the max
  411. // flow
  412. model.getPropertyTable().setCellEditable(0, 1, false);
  413. model.getPropertyTable().setCellEditable(2, 1, true);
  414. model.getPropertyTable().setCellEditable(3, 1, true);
  415. } else if (getActualCps() == null) {
  416. deleteRows(model.getSingleTable());
  417. deleteRows(model.getMultiTable());
  418. }
  419. // Update of the HolonElementTable (Single- or Multi-Selection)
  420. if (model.getSelectedCpsObjects().size() > 1) {
  421. model.getTableHolonElement().setModel(model.getMultiTable());
  422. } else if (model.getSelectedCpsObjects().size() == 1) {
  423. model.getTableHolonElement().setModel(model.getSingleTable());
  424. }
  425. }
  426. public AbstractCanvasObject getHolonObjSelected(int id) {
  427. AbstractCanvasObject obj = null;
  428. for (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
  429. if (o.getId() == id) {
  430. obj = o;
  431. break;
  432. }
  433. }
  434. return obj;
  435. }
  436. /**
  437. *
  438. * @return id of column that should be sorted
  439. */
  440. public int getSortBy() {
  441. return sortBy;
  442. }
  443. /**
  444. * sets the column id that should be sorted
  445. * @param column
  446. */
  447. public void setSortBy(int column) {
  448. /**
  449. * if there is no "Object" column, assume coloumn 0 is
  450. */
  451. if(model.getTableHolonElement().getColumnCount()==7)
  452. this.sortBy = column+1;
  453. else
  454. this.sortBy = column;
  455. }
  456. }