HolonSwitch.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package classes;
  2. import java.awt.Point;
  3. import java.util.LinkedList;
  4. import com.google.gson.annotations.Expose;
  5. import ui.controller.SingletonControl;
  6. import ui.view.UnitGraph;
  7. /**
  8. * The class HolonSwitch represents a Switch, which can be turned on and off.
  9. *
  10. * @author Gruppe14
  11. *
  12. */
  13. public class HolonSwitch extends AbstractCpsObject implements IGraphedElement {
  14. /**
  15. * The class HolonSwitch represents an Object in the system, that has the
  16. * capacity of manipulate the electricity flow. The switch can be manage
  17. * automatically through a graph or direct manually.
  18. *
  19. * @author Gruppe14
  20. *
  21. */
  22. /*
  23. * manual state True, if this wire is working (capable of carrying
  24. * electricity), else false
  25. */
  26. @Expose
  27. boolean manualActive;
  28. /*
  29. * active state True, if this wire is working (capable of carrying
  30. * electricity), else false
  31. */
  32. @Expose
  33. private boolean autoActive;
  34. /*
  35. * true if switch has to be used manually
  36. */
  37. @Expose
  38. boolean manualMode;
  39. @Expose
  40. int localPeriod;
  41. @Expose
  42. boolean stretch;
  43. /*
  44. * Energy at each point of the graph with 50 predefined points. At the
  45. * beginning, it starts with all values at energy
  46. */
  47. boolean[] activeAt;
  48. // Points on the UnitGraph
  49. LinkedList<Point> graphPoints = new LinkedList<>();
  50. /**
  51. * Create a new HolonSwitch with the default name ("Switch"), a default
  52. * value of automatic handle and active status.
  53. *
  54. * @param objName
  55. * String
  56. */
  57. public HolonSwitch(String objName) {
  58. super(objName);
  59. setStretching(IGraphedElement.STRETCH_BY_DEFAULT);
  60. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  61. setLocalPeriod(SingletonControl.getInstance().getControl()==null?
  62. UnitGraph.STANDARD_GRAPH_ACCURACY:
  63. SingletonControl.getInstance().getControl().getModel().getGraphIterations()
  64. );
  65. setManualState(true);
  66. setAutoState(true);
  67. setActiveAt(true);
  68. setManualMode(false);
  69. setGraphPoints(new LinkedList<Point>());
  70. }
  71. /**
  72. * Create a copy of an existing HolonSwitch.
  73. *
  74. * @param obj
  75. * the Object to copy
  76. */
  77. public HolonSwitch(AbstractCpsObject obj) {
  78. super(obj);
  79. HolonSwitch copyObj = (HolonSwitch)obj;
  80. setLocalPeriod(copyObj.getLocalPeriod());
  81. setStretching(copyObj.isStretching());
  82. activeAt=new boolean[UnitGraph.STANDARD_GRAPH_ACCURACY];
  83. super.setName(obj.getName());
  84. setManualState(copyObj.getActiveManual());
  85. setAutoState(true);
  86. setLocalPeriod(((IGraphedElement)obj).getLocalPeriod());
  87. setActiveAt(true);
  88. for (int i = 0; i < activeAt.length; i++) {
  89. activeAt[i] = copyObj.getState(i);
  90. }
  91. setGraphPoints(new LinkedList<Point>());
  92. for (Point p : copyObj.getGraphPoints()) {
  93. this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
  94. }
  95. setManualMode(copyObj.getManualMode());
  96. }
  97. /**
  98. * Calculates the state of the Switch.
  99. */
  100. public void switchState() {
  101. if (!manualMode) {
  102. setManualMode(true);
  103. }
  104. if (this.manualActive == true) {
  105. setImage("/Images/switch-off.png");
  106. } else {
  107. setImage("/Images/switch-on.png");
  108. }
  109. this.manualActive = !manualActive;
  110. }
  111. /**
  112. * Getter for the status of the Switch at a given timestep.
  113. *
  114. * @param timeStep
  115. * int
  116. * @return state value
  117. */
  118. public boolean getState(int timeStep) {
  119. if (manualMode) {
  120. return this.manualActive;
  121. } else {
  122. return activeAt[UnitGraph.getEffectiveIndex(this, timeStep)];
  123. }
  124. }
  125. /**
  126. * Overall status of the switch (manual or automatic mode).
  127. *
  128. * @return boolean the State
  129. */
  130. public boolean getState() {//TODO: not really necessary
  131. if (manualMode) {
  132. return this.manualActive;
  133. } else {
  134. return this.autoActive;
  135. }
  136. }
  137. /**
  138. * Change the state of the Switch to manual.
  139. *
  140. * @param state
  141. * the State
  142. */
  143. public void setManualState(boolean state) {
  144. this.manualActive = state;
  145. setImage();
  146. }
  147. /**
  148. * Set the state of the Switch to automatic.
  149. *
  150. * @param state
  151. * the State
  152. */
  153. public void setAutoState(boolean state) {//TODO: This should probably not be public
  154. this.autoActive = state;
  155. setImage();
  156. }
  157. /**
  158. * Set Image of the Switch.
  159. */
  160. private void setImage() {
  161. if (manualMode) {
  162. if (!this.manualActive) {
  163. setImage("/Images/switch-off.png");
  164. } else {
  165. setImage("/Images/switch-on.png");
  166. }
  167. } else {
  168. if (!this.autoActive) {
  169. setImage("/Images/switch-off.png");
  170. } else {
  171. setImage("/Images/switch-on.png");
  172. }
  173. }
  174. }
  175. /**
  176. * For automatic use only (throught the graph).
  177. *
  178. * @return the Graph Points
  179. */
  180. public LinkedList<Point> getGraphPoints() {
  181. return graphPoints;
  182. }
  183. /**
  184. * Set the values of the switch in the graph (auto. mode only).
  185. *
  186. * @param points
  187. * the Graph points
  188. */
  189. public void setGraphPoints(LinkedList<Point> points) {
  190. this.graphPoints = points;
  191. }
  192. /**
  193. * Returns the ManualState.
  194. *
  195. * @return boolean Manual State
  196. */
  197. public boolean getActiveManual() {
  198. return this.manualActive;
  199. }
  200. /**
  201. * Set the value of the Switch.
  202. *
  203. * @param active
  204. * the default value
  205. */
  206. public void setActiveAt(boolean active) {
  207. activeAt = new boolean[100];//TODO This is necessary because of thisgson rubbish.
  208. for (int i = 0; i < activeAt.length; i++) {
  209. this.activeAt[i] = active;
  210. }
  211. }
  212. public void setActiveAt(int pos, boolean active) {
  213. //activeAt = new boolean[100];
  214. this.activeAt[pos] = active;
  215. }
  216. /**
  217. * Set the overall value of the Switch (manual mode).
  218. *
  219. * @param mode
  220. * the mode (boolean)
  221. */
  222. public void setManualMode(boolean mode) {
  223. manualMode = mode;
  224. }
  225. /**
  226. * Get manualmode state.
  227. *
  228. * @return boolean manual mode state
  229. */
  230. public boolean getManualMode() {
  231. return manualMode;
  232. }
  233. @Override
  234. public void setLocalPeriod(int period) {
  235. localPeriod=period;
  236. }
  237. @Override
  238. public int getLocalPeriod() {
  239. return localPeriod;
  240. }
  241. @Override
  242. public boolean isStretching() {
  243. return stretch;
  244. }
  245. @Override
  246. public void setStretching(boolean stretch) {
  247. this.stretch=stretch;
  248. }
  249. }