AbstractCpsObject.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package classes;
  2. import com.google.gson.annotations.Expose;
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5. /**
  6. * The abstract class "CpsObject" represents any possible object in the system
  7. * (except Edges). The representation of any object contains following
  8. * variables: see description of variables
  9. *
  10. * @author Gruppe14
  11. *
  12. */
  13. public abstract class AbstractCpsObject {
  14. /* Type of the Object. */
  15. @Expose
  16. String objName;
  17. /* Name given by the user. */
  18. @Expose
  19. String name;
  20. /* ID of the Obj. */
  21. @Expose
  22. int id;
  23. /* Path of the image for the Obj. */
  24. @Expose
  25. String image;
  26. /* Array of neighbors */
  27. ArrayList<CpsEdge> connections;
  28. /* Position with a X and Y value */
  29. @Expose
  30. Position position;
  31. /*
  32. * Energy input and output of each object in the grid Where the Object is
  33. * Stored
  34. */
  35. @Expose
  36. String sav;
  37. /* borderColor the user sets */
  38. @Expose
  39. Color borderColor = Color.WHITE;
  40. /* a Tag that can be used */
  41. ArrayList<Integer> tags;
  42. /* a Tag that can be used */
  43. ArrayList<Integer> pseudoTags;
  44. /**
  45. * Constructor for a CpsObejct with an unique ID.
  46. *
  47. * @param objName
  48. * of the Object
  49. */
  50. public AbstractCpsObject(String objName) {
  51. setObjName(objName);
  52. setName(objName);
  53. setImage("/Images/Dummy_House.png");
  54. tags = new ArrayList<>();
  55. pseudoTags = new ArrayList<>();
  56. }
  57. /**
  58. * Constructor for a new CpsObject with an unique ID (This constructor
  59. * correspond to the interaction between the Categories and Canvas)-->
  60. * actually the "new" Object is a copy.
  61. *
  62. * @param obj
  63. * Object to be copied
  64. */
  65. public AbstractCpsObject(AbstractCpsObject obj) {
  66. setObjName(obj.getObjName());
  67. setName(obj.getObjName());
  68. setConnections(new ArrayList<>());
  69. setPosition(new Position());
  70. setId(IdCounter.nextId());
  71. setImage(obj.getImage());
  72. }
  73. /**
  74. * Getter for the type of the Object.
  75. *
  76. * @return String
  77. */
  78. public String getObjName() {
  79. return objName;
  80. }
  81. /**
  82. * Set the type of Object.
  83. *
  84. * @param objName
  85. * String
  86. */
  87. public void setObjName(String objName) {
  88. this.objName = objName;
  89. }
  90. /**
  91. * Getter for the user-defined name (no unique).
  92. *
  93. * @return String
  94. */
  95. public String getName() {
  96. return name;
  97. }
  98. /**
  99. * Set the name.
  100. *
  101. * @param name
  102. * String
  103. */
  104. public void setName(String name) {
  105. this.name = name;
  106. }
  107. /**
  108. * Getter of the unique ID.
  109. *
  110. * @return int
  111. */
  112. public int getId() {
  113. return id;
  114. }
  115. /**
  116. * Set the ID to a new one.
  117. *
  118. * @param id
  119. * the iD to set
  120. */
  121. public void setId(int id) {
  122. this.id = id;
  123. }
  124. /**
  125. * Get the path of the image for the selected Object.
  126. *
  127. * @return String
  128. */
  129. public String getImage() {
  130. return image;
  131. }
  132. /**
  133. * Set the path of the image.
  134. *
  135. * @param image
  136. * the Image to set
  137. */
  138. public void setImage(String image) {
  139. this.image = image;
  140. }
  141. /**
  142. * List of all existing connections.
  143. *
  144. * @return the connections ArrayList
  145. */
  146. public ArrayList<CpsEdge> getConnections() {
  147. return connections;
  148. }
  149. /**
  150. * Set a new ArrayList of connections (Update).
  151. *
  152. * @param arrayList
  153. * the connections to set
  154. */
  155. public void setConnections(ArrayList<CpsEdge> arrayList) {
  156. this.connections = arrayList;
  157. }
  158. /**
  159. * List of all existing connections.
  160. *
  161. * @return the connections ArrayList
  162. */
  163. public ArrayList<CpsEdge> getConnectedTo() {
  164. return connections;
  165. }
  166. /**
  167. * Add a new connection to the selected Object.
  168. *
  169. * @param toConnect
  170. * Edge
  171. */
  172. public void addConnection(CpsEdge toConnect) {
  173. connections.add(toConnect);
  174. }
  175. /**
  176. * Set the position of the Object in the canvas.
  177. *
  178. * @param x
  179. * X-Coord
  180. * @param y
  181. * Y-Coord
  182. */
  183. public void setPosition(int x, int y) {
  184. setPosition(new Position(x, y));
  185. }
  186. /**
  187. * Get the actual position of the Object.
  188. *
  189. * @return Position Position of this Object
  190. */
  191. public Position getPosition() {
  192. return position;
  193. }
  194. /**
  195. * Set the position of the Object in the canvas.
  196. *
  197. * @param pos Coordinates
  198. */
  199. public void setPosition(Position pos) {
  200. this.position = pos;
  201. }
  202. /**
  203. * For save purpose.
  204. *
  205. * @return the stored
  206. */
  207. public String getSav() {
  208. return sav;
  209. }
  210. /**
  211. * For save purpose.
  212. *
  213. * @param sav
  214. * the stored to set
  215. */
  216. public void setSav(String sav) {
  217. this.sav = sav;
  218. }
  219. /**
  220. * Get the color of the border.
  221. *
  222. * @return the BorderColor
  223. */
  224. public Color getBorderColor() {
  225. return borderColor;
  226. }
  227. /**
  228. * Set the Border Color of this CpsObject.
  229. *
  230. * @param c
  231. * the BorderColor
  232. */
  233. public void setBorderColor(Color c) {
  234. this.borderColor = c;
  235. }
  236. /**
  237. * For internal purpose (energy flow).
  238. *
  239. * @param tag
  240. * for internal purpose
  241. */
  242. public void addTag(int tag) {
  243. if (!(tags.contains(tag))) {
  244. this.tags.add(tag);
  245. }
  246. }
  247. public void addAllTags(ArrayList<Integer> tags) {
  248. for (Integer tag : tags) {
  249. addTag(tag);
  250. }
  251. }
  252. /**
  253. * Get the actual tags.
  254. *
  255. * @return ArrayList
  256. */
  257. public ArrayList<Integer> getTag() {
  258. return tags;
  259. }
  260. /**
  261. * Rest the tags to Null.
  262. */
  263. public void resetTags() {
  264. this.tags = new ArrayList<>();
  265. this.pseudoTags = new ArrayList<>();
  266. }
  267. /**
  268. * For internal purpose (energy flow).
  269. *
  270. * @param tags
  271. * for internal purpose
  272. */
  273. public void setTags(ArrayList<Integer> tags) {
  274. this.tags = tags;
  275. }
  276. /**
  277. * Get the pseudo tags.
  278. *
  279. * @return ArrayList
  280. */
  281. public ArrayList<Integer> getPseudoTags() {
  282. return this.pseudoTags;
  283. }
  284. /**
  285. * For internal purpose (energy flow).
  286. *
  287. * @param tags for internal purpose
  288. */
  289. public void setPseudoTags(ArrayList<Integer> tags) {
  290. this.pseudoTags = tags;
  291. }
  292. /**
  293. * add a pseudo tag.
  294. *
  295. * @return ArrayList
  296. */
  297. public void addPseudoTag(int tag) {
  298. if (!pseudoTags.contains(tag)) {
  299. pseudoTags.add(tag);
  300. }
  301. }
  302. /**
  303. * adds all given pseudotags to the tags
  304. *
  305. * @param pseudoTags
  306. */
  307. public void addAllPseudoTags(ArrayList<Integer> pseudoTags) {
  308. for (Integer tag : pseudoTags) {
  309. addPseudoTag(tag);
  310. }
  311. }
  312. /**
  313. * adds All pseudoTags to tags without duplicates
  314. */
  315. public void recalculateTags() {
  316. for (Integer tag : pseudoTags) {
  317. if (!tags.contains(tag)) {
  318. tags.add(tag);
  319. }
  320. }
  321. }
  322. }