CategoryController.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import Interfaces.CategoryListener;
  4. import ui.model.*;
  5. import ui.view.*;
  6. public class CategoryController {
  7. private IdCounter ID;
  8. private Model M;
  9. public CategoryController(Model model, IdCounter id) {
  10. this.ID = id;
  11. this.M = model;
  12. initCategories();
  13. }
  14. /**
  15. * init default category and objects
  16. */
  17. public void initCategories() {
  18. Category energy = new Category("Energy");
  19. Category building = new Category("Building");
  20. Category component = new Category("Component");
  21. HolonObject powerp = new HolonObject("Power Plant");
  22. HolonObject house = new HolonObject("House");
  23. HolonTransformer transformer = new HolonTransformer("Transformer");
  24. HolonSwitch sw = new HolonSwitch("Switch");
  25. addObject(energy, powerp);
  26. addObject(building, house);
  27. addObject(component, transformer);
  28. addObject(component, sw);
  29. addCategory(energy);
  30. addCategory(building);
  31. addCategory(component);
  32. }
  33. /**
  34. * Adds Category into Model
  35. *
  36. * @param toAdd
  37. * neue Kategorie
  38. */
  39. public void addCategory(Category toAdd) {
  40. M.getCategories().add(toAdd);
  41. notifyCatListeners();
  42. }
  43. /**
  44. * Adds New Category into Model
  45. *
  46. * @param name
  47. * Bezeichnung der neuen Kategorie
  48. */
  49. public void addNewCategory(String name) {
  50. addCategory(new Category(name));
  51. }
  52. /**
  53. * Add Object into a Category
  54. *
  55. * @param cat
  56. * Category
  57. * @param obj
  58. * Object
  59. */
  60. public void addObject(Category cat, CpsObject obj) {
  61. cat.getObjects().add(obj);
  62. notifyCatListeners();
  63. }
  64. /**
  65. * Add new Holon Object
  66. *
  67. * @param cat
  68. * Category
  69. * @param obj
  70. * New Object Name
  71. */
  72. public void addNewHolonObject(Category cat, String obj) {
  73. addObject(cat, new HolonObject(obj));
  74. }
  75. /**
  76. * Add new Holon Transformer
  77. *
  78. * @param cat
  79. * Category
  80. * @param obj
  81. * New Object Name
  82. */
  83. public void addNewHolonTransformer(Category cat, String obj) {
  84. addObject(cat, new HolonTransformer(obj));
  85. }
  86. /**
  87. * Add new Holon Switch
  88. *
  89. * @param cat
  90. * Category
  91. * @param obj
  92. * New Object Name
  93. */
  94. public void addNewHolonSwitch(Category cat, String obj) {
  95. addObject(cat, new HolonSwitch(obj));
  96. }
  97. /**
  98. *
  99. */
  100. public void notifyCatListeners() {
  101. for (CategoryListener l : M.getCategoryListeners()) {
  102. l.onChange(M.getCategories());
  103. }
  104. }
  105. /**
  106. *
  107. * @param catLis
  108. */
  109. public void addCatListener(CategoryListener catLis) {
  110. M.getCategoryListeners().add(catLis);
  111. }
  112. /**
  113. * search for category
  114. * @param name
  115. * @return
  116. */
  117. public Category searchNode(String name) {
  118. Category query = null;
  119. for (Category cat : M.getCategories()) {
  120. if (cat.getName().equals(name)) {
  121. query = cat;
  122. break;
  123. }
  124. }
  125. return query;
  126. }
  127. public void deleteCategory(int idx) {
  128. M.getCategories().remove(idx);
  129. }
  130. }