AbstractCpsObject.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. public abstract AbstractCpsObject makeCopy();
  74. /**
  75. * Getter for the type of the Object.
  76. *
  77. * @return String
  78. */
  79. public String getObjName() {
  80. return objName;
  81. }
  82. /**
  83. * Set the type of Object.
  84. *
  85. * @param objName
  86. * String
  87. */
  88. public void setObjName(String objName) {
  89. this.objName = objName;
  90. }
  91. /**
  92. * Getter for the user-defined name (no unique).
  93. *
  94. * @return String
  95. */
  96. public String getName() {
  97. return name;
  98. }
  99. /**
  100. * Set the name.
  101. *
  102. * @param name
  103. * String
  104. */
  105. public void setName(String name) {
  106. this.name = name;
  107. }
  108. /**
  109. * Getter of the unique ID.
  110. *
  111. * @return int
  112. */
  113. public int getId() {
  114. return id;
  115. }
  116. /**
  117. * Set the ID to a new one.
  118. *
  119. * @param id
  120. * the iD to set
  121. */
  122. public void setId(int id) {
  123. this.id = id;
  124. }
  125. /**
  126. * Get the path of the image for the selected Object.
  127. *
  128. * @return String
  129. */
  130. public String getImage() {
  131. return image;
  132. }
  133. /**
  134. * Set the path of the image.
  135. *
  136. * @param image
  137. * the Image to set
  138. */
  139. public void setImage(String image) {
  140. this.image = image;
  141. }
  142. /**
  143. * List of all existing connections.
  144. *
  145. * @return the connections ArrayList
  146. */
  147. public ArrayList<CpsEdge> getConnections() {
  148. return connections;
  149. }
  150. /**
  151. * Set a new ArrayList of connections (Update).
  152. *
  153. * @param arrayList
  154. * the connections to set
  155. */
  156. public void setConnections(ArrayList<CpsEdge> arrayList) {
  157. this.connections = arrayList;
  158. }
  159. /**
  160. * List of all existing connections.
  161. *
  162. * @return the connections ArrayList
  163. */
  164. public ArrayList<CpsEdge> getConnectedTo() {
  165. return connections;
  166. }
  167. /**
  168. * Add a new connection to the selected Object.
  169. *
  170. * @param toConnect
  171. * Edge
  172. */
  173. public void addConnection(CpsEdge toConnect) {
  174. connections.add(toConnect);
  175. }
  176. /**
  177. * Set the position of the Object in the canvas.
  178. *
  179. * @param x
  180. * X-Coord
  181. * @param y
  182. * Y-Coord
  183. */
  184. public void setPosition(int x, int y) {
  185. setPosition(new Position(x, y));
  186. }
  187. /**
  188. * Get the actual position of the Object.
  189. *
  190. * @return Position Position of this Object
  191. */
  192. public Position getPosition() {
  193. return position;
  194. }
  195. /**
  196. * Set the position of the Object in the canvas.
  197. *
  198. * @param pos Coordinates
  199. */
  200. public void setPosition(Position pos) {
  201. this.position = pos;
  202. }
  203. /**
  204. * For save purpose.
  205. *
  206. * @return the stored
  207. */
  208. public String getSav() {
  209. return sav;
  210. }
  211. /**
  212. * For save purpose.
  213. *
  214. * @param sav
  215. * the stored to set
  216. */
  217. public void setSav(String sav) {
  218. this.sav = sav;
  219. }
  220. /**
  221. * Get the color of the border.
  222. *
  223. * @return the BorderColor
  224. */
  225. public Color getBorderColor() {
  226. return borderColor;
  227. }
  228. /**
  229. * Set the Border Color of this CpsObject.
  230. *
  231. * @param c
  232. * the BorderColor
  233. */
  234. public void setBorderColor(Color c) {
  235. this.borderColor = c;
  236. }
  237. /**
  238. * For internal purpose (energy flow).
  239. *
  240. * @param tag
  241. * for internal purpose
  242. */
  243. public void addTag(int tag) {
  244. if (!(tags.contains(tag))) {
  245. this.tags.add(tag);
  246. }
  247. }
  248. public void addAllTags(ArrayList<Integer> tags) {
  249. for (Integer tag : tags) {
  250. addTag(tag);
  251. }
  252. }
  253. /**
  254. * Get the actual tags.
  255. *
  256. * @return ArrayList
  257. */
  258. public ArrayList<Integer> getTag() {
  259. return tags;
  260. }
  261. /**
  262. * Rest the tags to Null.
  263. */
  264. public void resetTags() {
  265. this.tags = new ArrayList<>();
  266. this.pseudoTags = new ArrayList<>();
  267. }
  268. /**
  269. * For internal purpose (energy flow).
  270. *
  271. * @param tags
  272. * for internal purpose
  273. */
  274. public void setTags(ArrayList<Integer> tags) {
  275. this.tags = tags;
  276. }
  277. /**
  278. * Get the pseudo tags.
  279. *
  280. * @return ArrayList
  281. */
  282. public ArrayList<Integer> getPseudoTags() {
  283. return this.pseudoTags;
  284. }
  285. /**
  286. * For internal purpose (energy flow).
  287. *
  288. * @param tags for internal purpose
  289. */
  290. public void setPseudoTags(ArrayList<Integer> tags) {
  291. this.pseudoTags = tags;
  292. }
  293. /**
  294. * add a pseudo tag.
  295. *
  296. * @return ArrayList
  297. */
  298. public void addPseudoTag(int tag) {
  299. if (!pseudoTags.contains(tag)) {
  300. pseudoTags.add(tag);
  301. }
  302. }
  303. /**
  304. * adds all given pseudotags to the tags
  305. *
  306. * @param pseudoTags
  307. */
  308. public void addAllPseudoTags(ArrayList<Integer> pseudoTags) {
  309. for (Integer tag : pseudoTags) {
  310. addPseudoTag(tag);
  311. }
  312. }
  313. /**
  314. * adds All pseudoTags to tags without duplicates
  315. */
  316. public void recalculateTags() {
  317. for (Integer tag : pseudoTags) {
  318. if (!tags.contains(tag)) {
  319. tags.add(tag);
  320. }
  321. }
  322. }
  323. }