NetworkTreeSettingsController.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
  2. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  3. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.NetworkTreeNodeStatus;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.NetworkTreeSettings;
  9. /**
  10. * Controller which allows simple manipulation of the NetworkTreePanel, changing visibility of single or multiple links/connections and Devices
  11. *
  12. *
  13. * @author Andreas T. Meyer-Berg
  14. */
  15. public class NetworkTreeSettingsController {
  16. /**
  17. * Model to be manipulated
  18. */
  19. private Model model;
  20. /**
  21. * Controller which can be used
  22. */
  23. private Controller controller;
  24. /**
  25. * NetworkTreeSettings of the model
  26. */
  27. private NetworkTreeSettings networkTreeSettings;
  28. /**
  29. * Create a new NetworkTreeSettingsController, which controls the networkTree
  30. * @param model model of the application
  31. * @param controller man controller for manipulation
  32. */
  33. public NetworkTreeSettingsController(Model model, Controller controller) {
  34. this.model = model;
  35. this.controller = controller;
  36. networkTreeSettings = this.model.getConfigurator().getNetworkTreeSettings();
  37. }
  38. /**
  39. * Toggles visibility of the given object and all of its descendants
  40. * @param o Object to be shown/hidden
  41. */
  42. public void toggleVisibilityOf(Object o){
  43. /**
  44. * o visible ?
  45. */
  46. boolean visible = networkTreeSettings.getStatusOfObject(o).isVisible();
  47. setVisibilityOf(o, !visible);
  48. }
  49. /**
  50. * Sets the visibility of Object {@code o} to {@code visible}, will also change visibility of all descendants
  51. * @param o Object which should be set visible/hidden
  52. * @param visible true then object will be shown, false then object will not be shown
  53. */
  54. public void setVisibilityOf(Object o, boolean visible){
  55. if(o instanceof String){
  56. /**
  57. * String node, is the top most -> Hide/Show the hole network
  58. */
  59. getStatusOfObject(o).setVisible(visible);
  60. for(Link l:controller.getNetworkController().getLinks())
  61. setVisibilityOfLink(l, visible);
  62. }else if(o instanceof Link){
  63. setVisibilityOfLink((Link)o, visible);
  64. }else if(o instanceof Connection){
  65. setVisibilityOfConnection((Connection)o, visible);
  66. }else if(o instanceof SmartDevice){
  67. setVisibilityOfSmartDevice((SmartDevice)o, visible);
  68. }
  69. /**
  70. * Notify observer
  71. */
  72. controller.notifyObservers();
  73. }
  74. /**
  75. * Change visibility of Device to the new value
  76. * @param device device to hide/show
  77. * @param isVisible new Visibility
  78. */
  79. private void setVisibilityOfSmartDevice(SmartDevice device, boolean isVisible) {
  80. networkTreeSettings.getStatusOfObject(device).setVisible(isVisible);
  81. }
  82. /**
  83. * Change visibility of Connection and all its Devices
  84. * @param connection connection to hide/show
  85. * @param isVisible new Visibility
  86. */
  87. private void setVisibilityOfConnection(Connection connection, boolean isVisible) {
  88. networkTreeSettings.getStatusOfObject(connection).setVisible(isVisible);
  89. /**
  90. * Update all devices
  91. */
  92. for(Port port: connection.getParticipants()){
  93. if(port.getOwner()!=null){
  94. if(isVisible){
  95. networkTreeSettings.getStatusOfObject(port.getOwner()).setVisible(isVisible);
  96. }else{
  97. /**
  98. * Just hide Devices which are in no further connection
  99. */
  100. boolean found = false;
  101. for(Port c:port.getOwner().getPorts()){
  102. if(found)break;
  103. if(c.getConnection()!=connection&&c.getConnection()!=null && isVisible(c.getConnection())){
  104. found=true;
  105. break;
  106. }
  107. }
  108. networkTreeSettings.getStatusOfObject(port.getOwner()).setVisible(found);
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Set visibility of Link, and all its connections and devices
  115. * @param link link, which should be shown/hidden
  116. * @param visible whether it should be visible
  117. */
  118. public void setVisibilityOfLink(Link link, boolean visible){
  119. networkTreeSettings.getStatusOfObject(link).setVisible(visible);
  120. /**
  121. * Update all connections of this link
  122. */
  123. for(Connection c: link.getConnections()){
  124. setVisibilityOfConnection(c,visible);
  125. }
  126. }
  127. /**
  128. * Returns the Status for the given Object
  129. * @param o Object, whose status should be returned
  130. * @return Status of the object
  131. */
  132. public NetworkTreeNodeStatus getStatusOfObject(Object o){
  133. return networkTreeSettings.getStatusOfObject(o);
  134. }
  135. /**
  136. * Adds an status for the given Object
  137. * @param o Object to be added
  138. * @param status Status of the object
  139. */
  140. public void addStatusOfObject(Object o, NetworkTreeNodeStatus status){
  141. networkTreeSettings.addStatusOfObject(o, status);
  142. }
  143. /**
  144. * Toggle the expanded status, expanded nodes will be collapsed and collapsed ones will be expanded
  145. *
  146. * @param o Object to be expanded / collapsed
  147. */
  148. public void toggleExpanded(Object o){
  149. setExpanded(o, !networkTreeSettings.getStatusOfObject(o).isExpanded());
  150. controller.notifyObservers();
  151. }
  152. /**
  153. * Set the expanded status of the object o to the value of isExpanded
  154. * @param o Object to be expanded / collapsed
  155. * @param isExpanded whether it should be expanded or collapsed
  156. */
  157. public void setExpanded(Object o, boolean isExpanded){
  158. networkTreeSettings.getStatusOfObject(o).setExpanded(isExpanded);
  159. controller.notifyObservers();
  160. }
  161. /**
  162. * Toggle visibility of an object
  163. * @param o object to be shown/hidden
  164. */
  165. public void toggleVisibilityNonRecursive(Object o){
  166. setVisibilityNonRecursive(o, !getStatusOfObject(o).isVisible());
  167. controller.notifyObservers();
  168. }
  169. /**
  170. * Set Visibility of the object
  171. * @param o object to be shown/hidden
  172. * @param visible true if visible
  173. */
  174. public void setVisibilityNonRecursive(Object o, boolean visible) {
  175. getStatusOfObject(o).setVisible(visible);
  176. controller.notifyObservers();
  177. }
  178. /**
  179. * Returns true if the given object is visible
  180. * @param o object which should be checked
  181. * @return true if visible
  182. */
  183. public boolean isVisible(Object o){
  184. return getStatusOfObject(o).isVisible();
  185. }
  186. /**
  187. * Removes the status of the object o
  188. * @param o Object, which status should be removed
  189. */
  190. public void removeStatusOfObject(Object o){
  191. this.networkTreeSettings.removeStatusOfObject(o);
  192. }
  193. }