AbstractCanvasObject.java 6.6 KB

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