SettingsController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
  2. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConfigurationManager;
  3. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  5. /**
  6. * Controller which allows access to the configuration
  7. *
  8. * @author Andreas T. Meyer-Berg
  9. */
  10. public class SettingsController {
  11. /**
  12. * Model which is configured
  13. */
  14. private Model model;
  15. /**
  16. * Controller
  17. */
  18. private Controller controller;
  19. /**
  20. * Network Tree Controller
  21. */
  22. private NetworkTreeSettingsController networkTreeSettings;
  23. /**
  24. * Link ColorController
  25. */
  26. private LinkColorController linkColors;
  27. /**
  28. * @param model model which stores the configuration
  29. * @param controller Main Controller for updates etc.
  30. */
  31. public SettingsController(Model model, Controller controller) {
  32. this.model = model;
  33. this.controller = controller;
  34. networkTreeSettings = new NetworkTreeSettingsController(model,controller);
  35. linkColors = new LinkColorController(model, controller);
  36. }
  37. /**
  38. * Returns the networkTreeSettingsController
  39. *
  40. * @return NetworkTreeSettingsController
  41. */
  42. public NetworkTreeSettingsController getNetworkTreeSettingsController(){
  43. return networkTreeSettings;
  44. }
  45. /**
  46. * @return the linkColors
  47. */
  48. public LinkColorController getLinkColors() {
  49. return linkColors;
  50. }
  51. /**
  52. * Returns the ConfigurationManager
  53. *
  54. * @return ConfigurationManager of the program
  55. */
  56. public ConfigurationManager getConfigurationManager(){
  57. return model.getConfigurator();
  58. }
  59. /**
  60. * @return the showConnections
  61. */
  62. public boolean isShowConnections() {
  63. return model.getConfigurator().getVisualizationConfiguration().isShowConnections();
  64. }
  65. /**
  66. * @param showConnections the showConnections to set
  67. */
  68. public void setShowConnections(boolean showConnections) {
  69. model.getConfigurator().getVisualizationConfiguration().setShowConnections(showConnections);
  70. controller.notifyObservers();
  71. }
  72. /**
  73. * @return the showLinks
  74. */
  75. public boolean isShowLinks() {
  76. return model.getConfigurator().getVisualizationConfiguration().isShowLinks();
  77. }
  78. /**
  79. * @param showLinks the showLinks to set
  80. */
  81. public void setShowLinks(boolean showLinks) {
  82. model.getConfigurator().getVisualizationConfiguration().setShowLinks(showLinks);
  83. controller.notifyObservers();
  84. }
  85. /**
  86. * @return the showSmartDevices
  87. */
  88. public boolean isShowSmartDevices() {
  89. return model.getConfigurator().getVisualizationConfiguration().isShowSmartDevices();
  90. }
  91. /**
  92. * @param showSmartDevices the showSmartDevices to set
  93. */
  94. public void setShowSmartDevices(boolean showSmartDevices) {
  95. model.getConfigurator().getVisualizationConfiguration().setShowSmartDevices(showSmartDevices);
  96. controller.notifyObservers();
  97. }
  98. /**
  99. * @return the showSmartDeviceNames
  100. */
  101. public boolean isShowSmartDeviceNames() {
  102. return model.getConfigurator().getVisualizationConfiguration().isShowSmartDeviceNames();
  103. }
  104. /**
  105. * @param showSmartDeviceNames the showSmartDeviceNames to set
  106. */
  107. public void setShowSmartDeviceNames(boolean showSmartDeviceNames) {
  108. model.getConfigurator().getVisualizationConfiguration().setShowSmartDeviceNames(showSmartDeviceNames);
  109. controller.notifyObservers();
  110. }
  111. /**
  112. * @return the showTerminatedConnections
  113. */
  114. public boolean isShowTerminatedConnections() {
  115. return model.getConfigurator().getVisualizationConfiguration().isShowTerminatedConnections();
  116. }
  117. /**
  118. * @param showTerminatedConnections the showTerminatedConnections to set
  119. */
  120. public void setShowTerminatedConnections(boolean showTerminatedConnections) {
  121. model.getConfigurator().getVisualizationConfiguration().setShowTerminatedConnections(showTerminatedConnections);
  122. controller.notifyObservers();
  123. }
  124. /**
  125. * @return the deviceVisualizationRadius
  126. */
  127. public int getDeviceVisualizationRadius() {
  128. return model.getConfigurator().getVisualizationConfiguration().getDeviceVisualizationRadius();
  129. }
  130. /**
  131. * @param deviceVisualizationRadius the deviceVisualizationRadius to set
  132. */
  133. public void setDeviceVisualizationRadius(int deviceVisualizationRadius) {
  134. model.getConfigurator().getVisualizationConfiguration().setDeviceVisualizationRadius(deviceVisualizationRadius);
  135. controller.getNetworkController().validateDevicePosition();
  136. controller.notifyObservers();
  137. }
  138. /**
  139. * @return the linkRadius
  140. */
  141. public int getLinkRadius() {
  142. return model.getConfigurator().getVisualizationConfiguration().getLinkRadius();
  143. }
  144. /**
  145. * @param linkRadius the linkRadius to set
  146. */
  147. public void setLinkRadius(int linkRadius) {
  148. model.getConfigurator().getVisualizationConfiguration().setLinkRadius(linkRadius);
  149. controller.notifyObservers();
  150. }
  151. /**
  152. * @return the showLinkToolTips
  153. */
  154. public boolean isShowLinkToolTips() {
  155. return model.getConfigurator().getVisualizationConfiguration().isShowLinkToolTips();
  156. }
  157. /**
  158. * @param showLinkToolTips the showLinkToolTips to set
  159. */
  160. public void setShowLinkToolTips(boolean showLinkToolTips) {
  161. model.getConfigurator().getVisualizationConfiguration().setShowLinkToolTips(showLinkToolTips);
  162. controller.notifyObservers();
  163. }
  164. /**
  165. * @return the showLinkNameList
  166. */
  167. public boolean isShowLinkNameList() {
  168. return model.getConfigurator().getVisualizationConfiguration().isShowLinkNameList();
  169. }
  170. /**
  171. * @param showLinkNameList the showLinkNameList to set
  172. */
  173. public void setShowLinkNameList(boolean showLinkNameList) {
  174. model.getConfigurator().getVisualizationConfiguration().setShowLinkNameList(showLinkNameList);
  175. controller.notifyObservers();
  176. }
  177. /**
  178. * @return the debugModus
  179. */
  180. public boolean isDebugModus() {
  181. return model.getConfigurator().getVisualizationConfiguration().isDebugModus();
  182. }
  183. /**
  184. * @param debugModus the debugModus to set
  185. */
  186. public void setDebugModus(boolean debugModus) {
  187. model.getConfigurator().getVisualizationConfiguration().setDebugModus(debugModus);
  188. controller.notifyObservers();
  189. }
  190. /**
  191. * Changes the dimension of the model, without scaling the SmartDevice
  192. * positions
  193. *
  194. * @param width
  195. * the new width to set
  196. * @param height
  197. * the new height to set
  198. * @param depth
  199. * the new depth to set
  200. */
  201. public void setDimension(int width, int height, int depth) {
  202. setDimension(width, height, depth, false);
  203. }
  204. /**
  205. * Changes the dimension of the model and moves SmartDevices to new relative
  206. * positions if {@code stretchModel} is true
  207. *
  208. * @param width
  209. * the new width to set
  210. * @param height
  211. * the new height to set
  212. * @param depth
  213. * the new depth to set
  214. * @param stretchModel
  215. * true, if smartDevice positions should be stretched
  216. */
  217. public void setDimension(int width, int height, int depth, boolean stretchModel) {
  218. // Don't allow to small Models
  219. if (width < 400)
  220. width = 400;
  221. if (height < 400)
  222. height = 400;
  223. if (depth < 400)
  224. depth = 400;
  225. if (stretchModel) {
  226. /**
  227. * Factor that changes the x position. Example: 1 -> positions stay
  228. * the same 0.5 -> x position is halved 2 -> x position is doubled
  229. */
  230. double width_factor = ((double) width) / ((double) getWidth());
  231. /**
  232. * Factor that changes the y position. Example: 1 -> positions stay
  233. * the same 0.5 -> y position is halved 2 -> y position is doubled
  234. */
  235. double height_factor = ((double) height) / ((double) getHeight());
  236. /**
  237. * Factor that changes the z position. Example: 1 -> positions stay
  238. * the same 0.5 -> z position is halved 2 -> z position is doubled
  239. */
  240. double depth_factor = ((double) depth) / ((double) getDepth());
  241. // Update all device positions
  242. for (SmartDevice d : model.getDevices()) {
  243. if (width_factor != 1.0)
  244. d.setX(scalePos(d.getX(), width_factor, width));
  245. if (height_factor != 1.0)
  246. d.setY(scalePos(d.getY(), height_factor, height));
  247. if (depth_factor != 1.0)
  248. d.setZ(scalePos(d.getZ(), depth_factor, depth));
  249. }
  250. }
  251. setWidth(width);
  252. setHeight(height);
  253. setDepth(depth);
  254. }
  255. /**
  256. * Calculates the new scaled position, which should be in Bounds:
  257. * {@code radius < newPosition <
  258. * (upperBound - radius)}
  259. *
  260. * @param oldPosition
  261. * previous position that should be scaled
  262. * @param factor
  263. * how much it is stretched/shrunk
  264. * @param upperBound
  265. * upper bound of the frame, which should not be exceeded
  266. * @return new position that was calculated
  267. */
  268. int scalePos(int oldPosition, double factor, int upperBound) {
  269. /**
  270. * New Position that should be validated and returned
  271. */
  272. int newPosition = (int) Math.round(factor * oldPosition);
  273. // Update if it moves out of the frame
  274. if (newPosition < getDeviceVisualizationRadius())
  275. newPosition = getDeviceVisualizationRadius();
  276. if (newPosition >= upperBound - getDeviceVisualizationRadius())
  277. newPosition = upperBound - getDeviceVisualizationRadius();
  278. return newPosition;
  279. }
  280. /**
  281. * @return the width
  282. */
  283. public int getWidth() {
  284. return model.getWidth();
  285. }
  286. /**
  287. * @param width the width to set
  288. */
  289. public void setWidth(int width) {
  290. model.setWidth(width);
  291. }
  292. /**
  293. * @return the height
  294. */
  295. public int getHeight() {
  296. return model.getHeight();
  297. }
  298. /**
  299. * @param height the height to set
  300. */
  301. public void setHeight(int height) {
  302. model.setHeight(height);
  303. }
  304. /**
  305. * @return the depth
  306. */
  307. public int getDepth() {
  308. return model.getDepth();
  309. }
  310. /**
  311. * @param depth the depth to set
  312. */
  313. public void setDepth(int depth) {
  314. model.setDepth(depth);
  315. }
  316. }