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.

ChannelFrame.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components.frames;
  18. import com.dmdirc.ServerState;
  19. import com.dmdirc.addons.ui_swing.EDTInvocation;
  20. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  21. import com.dmdirc.addons.ui_swing.components.NickList;
  22. import com.dmdirc.addons.ui_swing.components.SplitPane;
  23. import com.dmdirc.addons.ui_swing.components.TopicBar;
  24. import com.dmdirc.addons.ui_swing.components.TopicBarFactory;
  25. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
  26. import com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelSettingsDialog;
  27. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  28. import com.dmdirc.commandparser.PopupType;
  29. import com.dmdirc.config.ConfigBinder;
  30. import com.dmdirc.config.ConfigBinding;
  31. import com.dmdirc.events.ClientClosingEvent;
  32. import com.dmdirc.events.FrameClosingEvent;
  33. import com.dmdirc.events.eventbus.EventBus;
  34. import com.dmdirc.interfaces.GroupChat;
  35. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  36. import com.dmdirc.interfaces.config.ConfigProvider;
  37. import com.dmdirc.interfaces.config.IdentityFactory;
  38. import com.dmdirc.ui.messages.ColourManagerFactory;
  39. import java.awt.Dimension;
  40. import javax.inject.Provider;
  41. import javax.swing.JMenuItem;
  42. import javax.swing.JPopupMenu;
  43. import net.miginfocom.swing.MigLayout;
  44. import net.engio.mbassy.listener.Handler;
  45. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  46. /**
  47. * The groupChat frame is the GUI component that represents a groupChat to the user.
  48. */
  49. public final class ChannelFrame extends InputTextFrame {
  50. /** A version number for this class. */
  51. private static final long serialVersionUID = 10;
  52. /** Identity. */
  53. private final ConfigProvider identity;
  54. /** split pane. */
  55. private SplitPane splitPane;
  56. /** popup menu item. */
  57. private JMenuItem settingsMI;
  58. /** Nicklist. */
  59. private NickList nicklist;
  60. /** Topic bar. */
  61. private TopicBar topicBar;
  62. /** Event bus to dispatch events on. */
  63. private final EventBus eventBus;
  64. /** Config to read settings from. */
  65. private final AggregateConfigProvider globalConfig;
  66. /** Channel settings dialog provider. */
  67. private final KeyedDialogProvider<GroupChat, ChannelSettingsDialog> dialogProvider;
  68. /** Group chat instance. */
  69. private final GroupChat groupChat;
  70. /** Config binder. */
  71. private final ConfigBinder binder;
  72. /**
  73. * Creates a new instance of ChannelFrame. Sets up callbacks and handlers, and default options
  74. * for the form.
  75. *
  76. * @param deps The dependencies required by text frames.
  77. * @param inputFieldProvider The provider to use to create a new input field.
  78. * @param identityFactory The factory to use to create a group chat identity.
  79. * @param topicBarFactory The factory to use to create topic bars.
  80. * @param owner The group chat object that owns this frame
  81. * @param domain The domain to read settings from
  82. * @param dialogProvider The dialog provider to get the group chat settings dialog from.
  83. */
  84. public ChannelFrame(
  85. final String domain,
  86. final TextFrameDependencies deps,
  87. final Provider<SwingInputField> inputFieldProvider,
  88. final IdentityFactory identityFactory,
  89. final KeyedDialogProvider<GroupChat, ChannelSettingsDialog> dialogProvider,
  90. final InputTextFramePasteActionFactory inputTextFramePasteActionFactory,
  91. final TopicBarFactory topicBarFactory,
  92. final GroupChat owner) {
  93. super(deps, inputFieldProvider, inputTextFramePasteActionFactory, owner.getWindowModel());
  94. this.eventBus = deps.eventBus;
  95. this.globalConfig = deps.globalConfig;
  96. this.dialogProvider = dialogProvider;
  97. this.groupChat = owner;
  98. initComponents(topicBarFactory, deps.colourManagerFactory);
  99. binder = getContainer().getConfigManager().getBinder().withDefaultDomain(domain);
  100. identity = identityFactory.createChannelConfig(owner.getConnection().get().getNetwork(),
  101. owner.getName());
  102. }
  103. /**
  104. * Initialises the instance, adding any required listeners.
  105. */
  106. @Override
  107. public void init() {
  108. binder.bind(this, ChannelFrame.class);
  109. eventBus.subscribe(this);
  110. super.init();
  111. }
  112. /**
  113. * Initialises the components in this frame.
  114. *
  115. * @param topicBarFactory The factory to use to produce topic bars.
  116. * @param colourManagerFactory The colour manager factory
  117. */
  118. private void initComponents(final TopicBarFactory topicBarFactory,
  119. final ColourManagerFactory colourManagerFactory) {
  120. topicBar = topicBarFactory.getTopicBar((GroupChat) getContainer(), this);
  121. nicklist = new NickList(this, getContainer().getConfigManager(), colourManagerFactory);
  122. settingsMI = new JMenuItem("Settings");
  123. settingsMI.addActionListener(l -> dialogProvider.displayOrRequestFocus(groupChat));
  124. splitPane = new SplitPane(globalConfig, SplitPane.Orientation.HORIZONTAL);
  125. setLayout(new MigLayout("fill, ins 0, hidemode 3, wrap 1"));
  126. add(topicBar, "growx");
  127. add(splitPane, "grow, push");
  128. add(getSearchBar(), "growx");
  129. add(inputPanel, "growx");
  130. splitPane.setLeftComponent(getTextPane());
  131. splitPane.setResizeWeight(1);
  132. splitPane.setDividerLocation(-1);
  133. }
  134. @ConfigBinding(domain = "ui", key = "channelSplitPanePosition",
  135. invocation = EDTInvocation.class)
  136. public void handleSplitPanePosition(final int value) {
  137. checkOnEDT();
  138. nicklist.setPreferredSize(new Dimension(value, 0));
  139. splitPane.setDividerLocation(splitPane.getWidth() - splitPane.getDividerSize() - value);
  140. }
  141. @ConfigBinding(key = "shownicklist", invocation = EDTInvocation.class)
  142. public void handleShowNickList(final boolean value) {
  143. checkOnEDT();
  144. if (value) {
  145. splitPane.setRightComponent(nicklist);
  146. } else {
  147. splitPane.setRightComponent(null);
  148. }
  149. }
  150. @Handler(invocation = EdtHandlerInvocation.class)
  151. public void handleClientClosing(final ClientClosingEvent event) {
  152. saveSplitPanePosition();
  153. }
  154. private void saveSplitPanePosition() {
  155. checkOnEDT();
  156. if (getContainer().getConfigManager().getOptionInt("ui",
  157. "channelSplitPanePosition") != nicklist.getWidth()) {
  158. identity.setOption("ui", "channelSplitPanePosition", nicklist.getWidth());
  159. }
  160. }
  161. @Override
  162. public PopupType getNicknamePopupType() {
  163. return PopupType.CHAN_NICK;
  164. }
  165. @Override
  166. public PopupType getChannelPopupType() {
  167. return PopupType.CHAN_NORMAL;
  168. }
  169. @Override
  170. public PopupType getHyperlinkPopupType() {
  171. return PopupType.CHAN_HYPERLINK;
  172. }
  173. @Override
  174. public PopupType getNormalPopupType() {
  175. return PopupType.CHAN_NORMAL;
  176. }
  177. @Override
  178. public void addCustomPopupItems(final JPopupMenu popupMenu) {
  179. if (groupChat.getConnection().get().getState() == ServerState.CONNECTED) {
  180. settingsMI.setEnabled(true);
  181. } else {
  182. settingsMI.setEnabled(false);
  183. }
  184. if (popupMenu.getComponentCount() > 0) {
  185. popupMenu.addSeparator();
  186. }
  187. popupMenu.add(settingsMI);
  188. }
  189. @Override
  190. @Handler(invocation = EdtHandlerInvocation.class)
  191. public void windowClosing(final FrameClosingEvent event) {
  192. if (event.getSource().equals(getContainer())) {
  193. saveSplitPanePosition();
  194. topicBar.close();
  195. dialogProvider.dispose(groupChat);
  196. super.windowClosing(event);
  197. }
  198. }
  199. @Override
  200. public void dispose() {
  201. eventBus.unsubscribe(this);
  202. binder.unbind(this);
  203. super.dispose();
  204. }
  205. }