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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /**
  116. * Indicates whether this container has been assigned an ID yet.
  117. *
  118. * <p>The container should not post events until it has an ID.
  119. *
  120. * @return True if the container has an ID, false otherwise.
  121. */
  122. protected boolean hasId() {
  123. return id != null;
  124. }
  125. @Override
  126. public String getIcon() {
  127. return icon;
  128. }
  129. @Override
  130. public String getName() {
  131. return name;
  132. }
  133. @Override
  134. public String getTitle() {
  135. return title;
  136. }
  137. @Override
  138. public AggregateConfigProvider getConfigManager() {
  139. return configManager;
  140. }
  141. @Override
  142. public DMDircMBassador getEventBus() {
  143. return eventBus;
  144. }
  145. @Override
  146. public void setName(final String name) {
  147. this.name = name;
  148. if (hasId()) {
  149. eventBus.publishAsync(new FrameNameChangedEvent(this, name));
  150. }
  151. }
  152. @Override
  153. public void setTitle(final String title) {
  154. this.title = title;
  155. if (hasId()) {
  156. eventBus.publishAsync(new FrameTitleChangedEvent(this, title));
  157. }
  158. }
  159. @Override
  160. public Set<String> getComponents() {
  161. return Collections.unmodifiableSet(components);
  162. }
  163. @Override
  164. public void addComponent(final String component) {
  165. components.add(component);
  166. if (hasId()) {
  167. eventBus.publishAsync(new FrameComponentAddedEvent(this, component));
  168. }
  169. }
  170. @Override
  171. public void removeComponent(final String component) {
  172. components.remove(component);
  173. if (hasId()) {
  174. eventBus.publishAsync(new FrameComponentRemovedEvent(this, component));
  175. }
  176. }
  177. @Override
  178. public void close() {
  179. eventBus.unsubscribe(unreadStatusManager);
  180. configManager.getBinder().unbind(unreadStatusManager);
  181. eventBus.publish(new FrameClosingEvent(this));
  182. getBackBuffer().stopAddingEvents();
  183. }
  184. @Override
  185. public final void setIcon(final String icon) {
  186. this.icon = icon;
  187. iconUpdated();
  188. configManager.removeListener(changer);
  189. configManager.addChangeListener("icon", icon, changer);
  190. }
  191. /**
  192. * Called when this container's icon is updated.
  193. */
  194. private void iconUpdated() {
  195. if (hasId()) {
  196. eventBus.publish(new FrameIconChangedEvent(this, icon));
  197. }
  198. }
  199. @Override
  200. public BackBuffer getBackBuffer() {
  201. return backBuffer;
  202. }
  203. /**
  204. * Sets the composition state for the local user for this chat.
  205. *
  206. * @param state The new composition state
  207. */
  208. public void setCompositionState(final CompositionState state) {
  209. // Default implementation does nothing. Subclasses that support
  210. // composition should override this.
  211. }
  212. @Override
  213. public Optional<InputModel> getInputModel() {
  214. return inputModel;
  215. }
  216. /**
  217. * Sets an input model for this window. If a window does not have an input model, then it
  218. * will not accept user input.
  219. *
  220. * @param inputModel The new input model to use (null to disallow input).
  221. */
  222. public void setInputModel(@Nullable final InputModel inputModel) {
  223. this.inputModel = Optional.ofNullable(inputModel);
  224. }
  225. @Override
  226. public UnreadStatusManager getUnreadStatusManager() {
  227. return unreadStatusManager;
  228. }
  229. public void setConnection(@Nullable final Connection connection) {
  230. this.connection = Optional.ofNullable(connection);
  231. }
  232. @Override
  233. public Optional<Connection> getConnection() {
  234. return connection;
  235. }
  236. }