ObjectController.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package ui.controller;
  2. import classes.Category;
  3. import classes.CpsObject;
  4. import classes.HolonElement;
  5. import classes.HolonObject;
  6. import ui.model.iDCounter;
  7. import ui.model.Model;
  8. import java.util.ArrayList;
  9. import Interfaces.ObjectListener;
  10. public class ObjectController {
  11. public enum Type {
  12. CONSUMER, PRODUCER
  13. }
  14. private Model MODEL;
  15. private int ID;
  16. public ObjectController(Model model) {
  17. this.MODEL = model;
  18. this.ID = iDCounter.nextId();
  19. initHolonElements();
  20. }
  21. /**
  22. * init default Power supply of the Power Plant
  23. */
  24. public void initHolonElements(){
  25. addNewElementIntoCategoryObject("Energy", "Power Plant", "Power", 1, 100);
  26. }
  27. /**
  28. * Adds Element into a Object
  29. */
  30. public void addElement(HolonObject object, HolonElement element) {
  31. object.getElements().add(element);
  32. }
  33. /**
  34. * Adds Element into a Object on the Canvas
  35. *
  36. * @param object
  37. * @param element
  38. * @param type
  39. */
  40. public void addElementIntoCanvasObject(String object, HolonElement element) {
  41. addElement(searchHolonObject(object, MODEL.getObjectsOnCanvas()), element);
  42. }
  43. /**
  44. * Add a new Element into a Object on the Canvas
  45. *
  46. * @param object
  47. * @param eleName
  48. * @param amount
  49. * @param energy
  50. * @param type
  51. */
  52. public void addNewElementIntoCanvasObject(String object, String eleName, int amount, float energy) {
  53. addElementIntoCanvasObject(object, new HolonElement(eleName, energy, amount));
  54. }
  55. /**
  56. * Add Element into a Object in Category
  57. *
  58. * @param object
  59. * @param element
  60. * @param type
  61. */
  62. public void addElementIntoCategoryObject(String category, String object, HolonElement element) {
  63. Category cat = null;
  64. for (Category cats : MODEL.getCategories()) {
  65. if (cats.getName().equals(category)) {
  66. cat = cats;
  67. break;
  68. }
  69. }
  70. addElement(searchHolonObject(object, cat.getObjects()), element);
  71. }
  72. /**
  73. * Add a new Element into a Object in Category
  74. *
  75. * @param category
  76. * @param object
  77. * @param eleName
  78. * @param amount
  79. * @param energy
  80. * @param type
  81. */
  82. public void addNewElementIntoCategoryObject(String category, String object, String eleName, int amount,
  83. float energy) {
  84. addElementIntoCategoryObject(category, object, new HolonElement(eleName, energy, amount));
  85. }
  86. /**
  87. * Search for Object
  88. *
  89. * @param object
  90. * @param list
  91. * @return
  92. */
  93. public HolonObject searchHolonObject(String object, ArrayList<CpsObject> list) {
  94. HolonObject obj = null;
  95. for (CpsObject objects : list) {
  96. if (objects.getObjName().equals(object)) {
  97. obj = (HolonObject) objects;
  98. break;
  99. }
  100. }
  101. return obj;
  102. }
  103. /**
  104. * Returns the ID of the selected Object 0 = no Object is selected
  105. *
  106. * @return ID
  107. */
  108. public void setSelectedObjectID(int id) {
  109. MODEL.setSelectedObjectID(id);
  110. }
  111. }