AbstractCanvasObject.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. /**
  7. * The abstract class "CpsObject" represents any possible object in the system
  8. * (except Edges). The representation of any object contains following
  9. * variables: see description of variables
  10. *
  11. * @author Gruppe14
  12. *
  13. */
  14. public abstract class AbstractCanvasObject {
  15. /* Type of the Object. */
  16. @Expose
  17. String objName;
  18. /* Name given by the user. */
  19. @Expose
  20. String name;
  21. /* ID of the Obj. */
  22. @Expose
  23. int id;
  24. /* Path of the image for the Obj. */
  25. @Expose
  26. String image;
  27. /* Array of neighbors */
  28. ArrayList<Edge> connections = new ArrayList<>();
  29. /* Position with a X and Y value */
  30. @Expose
  31. Position position = new Position(0,0);
  32. /*
  33. * Energy input and output of each object in the grid Where the Object is
  34. * Stored
  35. */
  36. @Expose
  37. String sav;
  38. /* tags that can be used */
  39. public Set<Integer> tags = new HashSet<Integer>();
  40. /**
  41. * Constructor for a CpsObejct with an unique ID.
  42. *
  43. * @param objName
  44. * of the Object
  45. */
  46. public AbstractCanvasObject(String objName) {
  47. setObjName(objName);
  48. setName(objName);
  49. setImage("/Images/Dummy_House.png");
  50. }
  51. /**
  52. * Constructor for a new CpsObject with an unique ID (This constructor
  53. * correspond to the interaction between the Categories and Canvas)-->
  54. * actually the "new" Object is a copy.
  55. *
  56. * @param obj
  57. * Object to be copied
  58. */
  59. public AbstractCanvasObject(AbstractCanvasObject obj) {
  60. setObjName(obj.getObjName());
  61. setName(obj.getObjName());
  62. setConnections(new ArrayList<>());
  63. setId(IdCounter.nextId());
  64. setImage(obj.getImage());
  65. }
  66. public abstract AbstractCanvasObject makeCopy();
  67. /**
  68. * Getter for the type of the Object.
  69. *
  70. * @return String
  71. */
  72. public String getObjName() {
  73. return objName;
  74. }
  75. /**
  76. * Set the type of Object.
  77. *
  78. * @param objName
  79. * String
  80. */
  81. public void setObjName(String objName) {
  82. this.objName = objName;
  83. }
  84. /**
  85. * Getter for the user-defined name (no unique).
  86. *
  87. * @return String
  88. */
  89. public String getName() {
  90. return name;
  91. }
  92. /**
  93. * Set the name.
  94. *
  95. * @param name
  96. * String
  97. */
  98. public void setName(String name) {
  99. this.name = name;
  100. }
  101. /**
  102. * Getter of the unique ID.
  103. *
  104. * @return int
  105. */
  106. public int getId() {
  107. return id;
  108. }
  109. /**
  110. * Set the ID to a new one.
  111. *
  112. * @param id
  113. * the iD to set
  114. */
  115. public void setId(int id) {
  116. this.id = id;
  117. }
  118. /**
  119. * Get the path of the image for the selected Object.
  120. *
  121. * @return String
  122. */
  123. public String getImage() {
  124. return image;
  125. }
  126. /**
  127. * Set the path of the image.
  128. *
  129. * @param image
  130. * the Image to set
  131. */
  132. public void setImage(String image) {
  133. this.image = image;
  134. }
  135. /**
  136. * List of all existing connections.
  137. *
  138. * @return the connections ArrayList
  139. */
  140. public ArrayList<Edge> getConnections() {
  141. return connections;
  142. }
  143. /**
  144. * Set a new ArrayList of connections (Update).
  145. *
  146. * @param arrayList
  147. * the connections to set
  148. */
  149. public void setConnections(ArrayList<Edge> arrayList) {
  150. this.connections = arrayList;
  151. }
  152. /**
  153. * List of all existing connections.
  154. *
  155. * @return the connections ArrayList
  156. */
  157. public ArrayList<Edge> getConnectedTo() {
  158. return connections;
  159. }
  160. public ArrayList<AbstractCanvasObject> getConnectedObjects(){
  161. ArrayList<AbstractCanvasObject> connected = new ArrayList<AbstractCanvasObject>();
  162. for(Edge e : this.connections) {
  163. AbstractCanvasObject a = e.getA();
  164. AbstractCanvasObject b = e.getB();
  165. if(!e.isBurned()) {
  166. //added power would not burn edge
  167. if(a.equals(this)) {
  168. connected.add(b);
  169. } else {
  170. connected.add(a);
  171. }
  172. }
  173. }
  174. return connected;
  175. }
  176. /**
  177. * Add a new connection to the selected Object.
  178. *
  179. * @param toConnect
  180. * Edge
  181. */
  182. public void addConnection(Edge toConnect) {
  183. AbstractCanvasObject a = toConnect.getA();
  184. AbstractCanvasObject b = toConnect.getB();
  185. if(a instanceof HolonObject && ((HolonObject)a).holon != null) {
  186. ((HolonObject) a).holon.holonControlUnit.getHierarchyController().addEdgeTo(b);
  187. }
  188. if(b instanceof HolonObject && ((HolonObject)b).holon != null) {
  189. ((HolonObject) b).holon.holonControlUnit.getHierarchyController().addEdgeTo(a);
  190. }
  191. connections.add(toConnect);
  192. }
  193. public void removeConnection(Edge edge) {
  194. AbstractCanvasObject a = edge.getA();
  195. AbstractCanvasObject b = edge.getB();
  196. if(a instanceof HolonObject) {
  197. ((HolonObject) a).holon.holonControlUnit.getHierarchyController().removeEdge(edge);
  198. }
  199. if(b instanceof HolonObject) {
  200. ((HolonObject) b).holon.holonControlUnit.getHierarchyController().removeEdge(edge);
  201. }
  202. connections.remove(edge);
  203. }
  204. /**
  205. * Set the position of the Object in the canvas.
  206. *
  207. * @param x
  208. * X-Coord
  209. * @param y
  210. * Y-Coord
  211. */
  212. public void setPosition(int x, int y) {
  213. setPosition(new Position(x, y));
  214. }
  215. /**
  216. * Get the actual position of the Object.
  217. *
  218. * @return Position Position of this Object
  219. */
  220. public Position getPosition() {
  221. return position;
  222. }
  223. /**
  224. * Set the position of the Object in the canvas.
  225. *
  226. * @param pos Coordinates
  227. */
  228. public void setPosition(Position pos) {
  229. this.position = pos;
  230. }
  231. /**
  232. * For save purpose.
  233. *
  234. * @return the stored
  235. */
  236. public String getSav() {
  237. return sav;
  238. }
  239. /**
  240. * For save purpose.
  241. *
  242. * @param sav
  243. * the stored to set
  244. */
  245. public void setSav(String sav) {
  246. this.sav = sav;
  247. }
  248. }