You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FrameContainer.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.events.FrameClosingEvent;
  19. import com.dmdirc.events.FrameComponentAddedEvent;
  20. import com.dmdirc.events.FrameComponentRemovedEvent;
  21. import com.dmdirc.events.FrameIconChangedEvent;
  22. import com.dmdirc.events.FrameNameChangedEvent;
  23. import com.dmdirc.events.FrameTitleChangedEvent;
  24. import com.dmdirc.interfaces.Connection;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.interfaces.InputModel;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import com.dmdirc.config.provider.AggregateConfigProvider;
  29. import com.dmdirc.config.provider.ConfigChangeListener;
  30. import com.dmdirc.parser.common.CompositionState;
  31. import com.dmdirc.ui.messages.BackBuffer;
  32. import com.dmdirc.ui.messages.BackBufferFactory;
  33. import com.dmdirc.ui.messages.BackBufferImpl;
  34. import com.dmdirc.ui.messages.UnreadStatusManager;
  35. import com.dmdirc.ui.messages.UnreadStatusManagerImpl;
  36. import java.util.Collection;
  37. import java.util.Collections;
  38. import java.util.HashSet;
  39. import java.util.Optional;
  40. import java.util.Set;
  41. import javax.annotation.Nullable;
  42. /**
  43. * The frame container implements basic methods that should be present in all objects that handle a
  44. * frame.
  45. */
  46. public class FrameContainer implements WindowModel {
  47. /** The name of the icon being used for this container's frame. */
  48. private String icon;
  49. /** The name of this container. */
  50. private String name;
  51. /** The title of this container. */
  52. private String title;
  53. /** The config manager for this container. */
  54. private final AggregateConfigProvider configManager;
  55. /** The IconChanger for this container. */
  56. private final ConfigChangeListener changer = (d, k) -> iconUpdated();
  57. /** The UI components that this frame requires. */
  58. private final Set<String> components;
  59. /** Event bus to dispatch events to. */
  60. private final EventBus eventBus;
  61. /** The manager handling this frame's unread status. */
  62. private final UnreadStatusManager unreadStatusManager;
  63. /** The back buffer factory. */
  64. private final BackBufferFactory backBufferFactory;
  65. /** The back buffer for this container. */
  66. private BackBufferImpl backBuffer;
  67. /** The input model for this container. */
  68. private Optional<InputModel> inputModel = Optional.empty();
  69. /** The connection associated with this model. */
  70. private Optional<Connection> connection = Optional.empty();
  71. /** The ID for this window. */
  72. @Nullable private String id;
  73. /**
  74. * Instantiate new frame container.
  75. */
  76. public FrameContainer(
  77. final String icon,
  78. final String name,
  79. final String title,
  80. final AggregateConfigProvider config,
  81. final BackBufferFactory backBufferFactory,
  82. final EventBus eventBus,
  83. final Collection<String> components) {
  84. this.configManager = config;
  85. this.name = name;
  86. this.title = title;
  87. this.components = new HashSet<>(components);
  88. this.backBufferFactory = backBufferFactory;
  89. this.eventBus = eventBus;
  90. this.unreadStatusManager = new UnreadStatusManagerImpl(this);
  91. this.eventBus.subscribe(unreadStatusManager);
  92. configManager.getBinder().bind(unreadStatusManager, UnreadStatusManagerImpl.class);
  93. setIcon(icon);
  94. }
  95. protected void initBackBuffer() {
  96. backBuffer = backBufferFactory.getBackBuffer(this);
  97. backBuffer.startAddingEvents();
  98. }
  99. @Override
  100. public String getId() {
  101. if (id == null) {
  102. throw new IllegalStateException("ID has not been set");
  103. }
  104. return id;
  105. }
  106. @Override
  107. public void setId(@Nullable final String id) {
  108. if (this.id != null) {
  109. throw new IllegalStateException("ID already set");
  110. }
  111. this.id = id;
  112. }
  113. /**
  114. * Indicates whether this container has been assigned an ID yet.
  115. *
  116. * <p>The container should not post events until it has an ID.
  117. *
  118. * @return True if the container has an ID, false otherwise.
  119. */
  120. protected boolean hasId() {
  121. return id != null;
  122. }
  123. @Override
  124. public String getIcon() {
  125. return icon;
  126. }
  127. @Override
  128. public String getName() {
  129. return name;
  130. }
  131. @Override
  132. public String getTitle() {
  133. return title;
  134. }
  135. @Override
  136. public AggregateConfigProvider getConfigManager() {
  137. return configManager;
  138. }
  139. @Override
  140. public EventBus getEventBus() {
  141. return eventBus;
  142. }
  143. @Override
  144. public void setName(final String name) {
  145. this.name = name;
  146. if (hasId()) {
  147. eventBus.publishAsync(new FrameNameChangedEvent(this, name));
  148. }
  149. }
  150. @Override
  151. public void setTitle(final String title) {
  152. this.title = title;
  153. if (hasId()) {
  154. eventBus.publishAsync(new FrameTitleChangedEvent(this, title));
  155. }
  156. }
  157. @Override
  158. public Set<String> getComponents() {
  159. return Collections.unmodifiableSet(components);
  160. }
  161. @Override
  162. public void addComponent(final String component) {
  163. components.add(component);
  164. if (hasId()) {
  165. eventBus.publishAsync(new FrameComponentAddedEvent(this, component));
  166. }
  167. }
  168. @Override
  169. public void removeComponent(final String component) {
  170. components.remove(component);
  171. if (hasId()) {
  172. eventBus.publishAsync(new FrameComponentRemovedEvent(this, component));
  173. }
  174. }
  175. @Override
  176. public void close() {
  177. eventBus.unsubscribe(unreadStatusManager);
  178. configManager.getBinder().unbind(unreadStatusManager);
  179. eventBus.publish(new FrameClosingEvent(this));
  180. backBuffer.stopAddingEvents();
  181. }
  182. @Override
  183. public final void setIcon(final String icon) {
  184. this.icon = icon;
  185. iconUpdated();
  186. configManager.removeListener(changer);
  187. configManager.addChangeListener("icon", icon, changer);
  188. }
  189. /**
  190. * Called when this container's icon is updated.
  191. */
  192. private void iconUpdated() {
  193. if (hasId()) {
  194. eventBus.publish(new FrameIconChangedEvent(this, icon));
  195. }
  196. }
  197. @Override
  198. public BackBuffer getBackBuffer() {
  199. return backBuffer;
  200. }
  201. /**
  202. * Sets the composition state for the local user for this chat.
  203. *
  204. * @param state The new composition state
  205. */
  206. public void setCompositionState(final CompositionState state) {
  207. // Default implementation does nothing. Subclasses that support
  208. // composition should override this.
  209. }
  210. @Override
  211. public Optional<InputModel> getInputModel() {
  212. return inputModel;
  213. }
  214. /**
  215. * Sets an input model for this window. If a window does not have an input model, then it
  216. * will not accept user input.
  217. *
  218. * @param inputModel The new input model to use (null to disallow input).
  219. */
  220. public void setInputModel(@Nullable final InputModel inputModel) {
  221. this.inputModel = Optional.ofNullable(inputModel);
  222. }
  223. @Override
  224. public UnreadStatusManager getUnreadStatusManager() {
  225. return unreadStatusManager;
  226. }
  227. public void setConnection(@Nullable final Connection connection) {
  228. this.connection = Optional.ofNullable(connection);
  229. }
  230. @Override
  231. public Optional<Connection> getConnection() {
  232. return connection;
  233. }
  234. }