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.3KB

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