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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.InputModel;
  31. import com.dmdirc.interfaces.WindowModel;
  32. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  33. import com.dmdirc.interfaces.config.ConfigChangeListener;
  34. import com.dmdirc.parser.common.CompositionState;
  35. import com.dmdirc.ui.messages.BackBuffer;
  36. import com.dmdirc.ui.messages.BackBufferFactory;
  37. import com.dmdirc.ui.messages.UnreadStatusManager;
  38. import java.util.Collection;
  39. import java.util.Collections;
  40. import java.util.HashSet;
  41. import java.util.Optional;
  42. import java.util.Set;
  43. import javax.annotation.Nullable;
  44. /**
  45. * The frame container implements basic methods that should be present in all objects that handle a
  46. * frame.
  47. */
  48. public class FrameContainer implements WindowModel {
  49. /** The name of the icon being used for this container's frame. */
  50. private String icon;
  51. /** The name of this container. */
  52. private String name;
  53. /** The title of this container. */
  54. private String title;
  55. /** The config manager for this container. */
  56. private final AggregateConfigProvider configManager;
  57. /** The IconChanger for this container. */
  58. private final ConfigChangeListener changer = (d, k) -> iconUpdated();
  59. /** The UI components that this frame requires. */
  60. private final Set<String> components;
  61. /** Event bus to dispatch events to. */
  62. private final DMDircMBassador eventBus;
  63. /** The manager handling this frame's unread status. */
  64. private final UnreadStatusManager unreadStatusManager;
  65. /** The back buffer factory. */
  66. private final BackBufferFactory backBufferFactory;
  67. /** The back buffer for this container. */
  68. private BackBuffer backBuffer;
  69. /** The input model for this container. */
  70. private Optional<InputModel> inputModel = Optional.empty();
  71. /** The connection associated with this model. */
  72. private Optional<Connection> connection = Optional.empty();
  73. /** The ID for this window. */
  74. @Nullable private String id;
  75. /**
  76. * Instantiate new frame container.
  77. */
  78. public FrameContainer(
  79. final String icon,
  80. final String name,
  81. final String title,
  82. final AggregateConfigProvider config,
  83. final BackBufferFactory backBufferFactory,
  84. final DMDircMBassador eventBus,
  85. final Collection<String> components) {
  86. this.configManager = config;
  87. this.name = name;
  88. this.title = title;
  89. this.components = new HashSet<>(components);
  90. this.backBufferFactory = backBufferFactory;
  91. this.eventBus = eventBus;
  92. this.unreadStatusManager = new UnreadStatusManager(this);
  93. this.eventBus.subscribe(unreadStatusManager);
  94. configManager.getBinder().bind(unreadStatusManager, UnreadStatusManager.class);
  95. setIcon(icon);
  96. }
  97. protected void initBackBuffer() {
  98. backBuffer = backBufferFactory.getBackBuffer(this);
  99. backBuffer.startAddingEvents();
  100. }
  101. @Override
  102. public String getId() {
  103. if (id == null) {
  104. throw new IllegalStateException("ID has not been set");
  105. }
  106. return id;
  107. }
  108. @Override
  109. public void setId(@Nullable final String id) {
  110. if (this.id != null) {
  111. throw new IllegalStateException("ID already set");
  112. }
  113. this.id = id;
  114. }
  115. @Override
  116. public String getIcon() {
  117. return icon;
  118. }
  119. @Override
  120. public String getName() {
  121. return name;
  122. }
  123. @Override
  124. public String getTitle() {
  125. return title;
  126. }
  127. @Override
  128. public AggregateConfigProvider getConfigManager() {
  129. return configManager;
  130. }
  131. @Override
  132. public DMDircMBassador getEventBus() {
  133. return eventBus;
  134. }
  135. @Override
  136. public void setName(final String name) {
  137. this.name = name;
  138. eventBus.publishAsync(new FrameNameChangedEvent(this, name));
  139. }
  140. @Override
  141. public void setTitle(final String title) {
  142. this.title = title;
  143. eventBus.publishAsync(new FrameTitleChangedEvent(this, title));
  144. }
  145. @Override
  146. public Set<String> getComponents() {
  147. return Collections.unmodifiableSet(components);
  148. }
  149. @Override
  150. public void addComponent(final String component) {
  151. components.add(component);
  152. eventBus.publishAsync(new FrameComponentAddedEvent(this, component));
  153. }
  154. @Override
  155. public void removeComponent(final String component) {
  156. components.remove(component);
  157. eventBus.publishAsync(new FrameComponentRemovedEvent(this, component));
  158. }
  159. @Override
  160. public void close() {
  161. eventBus.unsubscribe(unreadStatusManager);
  162. configManager.getBinder().unbind(unreadStatusManager);
  163. eventBus.publish(new FrameClosingEvent(this));
  164. getBackBuffer().stopAddingEvents();
  165. }
  166. @Override
  167. public final void setIcon(final String icon) {
  168. this.icon = icon;
  169. iconUpdated();
  170. configManager.removeListener(changer);
  171. configManager.addChangeListener("icon", icon, changer);
  172. }
  173. /**
  174. * Called when this container's icon is updated.
  175. */
  176. private void iconUpdated() {
  177. eventBus.publish(new FrameIconChangedEvent(this, icon));
  178. }
  179. @Override
  180. public BackBuffer getBackBuffer() {
  181. return backBuffer;
  182. }
  183. /**
  184. * Sets the composition state for the local user for this chat.
  185. *
  186. * @param state The new composition state
  187. */
  188. public void setCompositionState(final CompositionState state) {
  189. // Default implementation does nothing. Subclasses that support
  190. // composition should override this.
  191. }
  192. @Override
  193. public Optional<InputModel> getInputModel() {
  194. return inputModel;
  195. }
  196. /**
  197. * Sets an input model for this window. If a window does not have an input model, then it
  198. * will not accept user input.
  199. *
  200. * @param inputModel The new input model to use (null to disallow input).
  201. */
  202. public void setInputModel(@Nullable final InputModel inputModel) {
  203. this.inputModel = Optional.ofNullable(inputModel);
  204. }
  205. @Override
  206. public UnreadStatusManager getUnreadStatusManager() {
  207. return unreadStatusManager;
  208. }
  209. public void setConnection(@Nullable final Connection connection) {
  210. this.connection = Optional.ofNullable(connection);
  211. }
  212. @Override
  213. public Optional<Connection> getConnection() {
  214. return connection;
  215. }
  216. }