Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FrameContainer.java 8.4KB

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