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

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