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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.addons.ui_swing.components.frames;
  23. import com.dmdirc.ServerState;
  24. import com.dmdirc.addons.ui_swing.EDTInvocation;
  25. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  26. import com.dmdirc.addons.ui_swing.components.NickList;
  27. import com.dmdirc.addons.ui_swing.components.SplitPane;
  28. import com.dmdirc.addons.ui_swing.components.TopicBar;
  29. import com.dmdirc.addons.ui_swing.components.TopicBarFactory;
  30. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
  31. import com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelSettingsDialog;
  32. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  33. import com.dmdirc.commandparser.PopupType;
  34. import com.dmdirc.config.ConfigBinder;
  35. import com.dmdirc.config.ConfigBinding;
  36. import com.dmdirc.events.ClientClosingEvent;
  37. import com.dmdirc.events.FrameClosingEvent;
  38. import com.dmdirc.interfaces.EventBus;
  39. import com.dmdirc.interfaces.GroupChat;
  40. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  41. import com.dmdirc.interfaces.config.ConfigProvider;
  42. import com.dmdirc.interfaces.config.IdentityFactory;
  43. import com.dmdirc.ui.messages.ColourManagerFactory;
  44. import java.awt.Dimension;
  45. import javax.inject.Provider;
  46. import javax.swing.JMenuItem;
  47. import javax.swing.JPopupMenu;
  48. import net.miginfocom.swing.MigLayout;
  49. import net.engio.mbassy.listener.Handler;
  50. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  51. /**
  52. * The groupChat frame is the GUI component that represents a groupChat to the user.
  53. */
  54. public final class ChannelFrame extends InputTextFrame {
  55. /** A version number for this class. */
  56. private static final long serialVersionUID = 10;
  57. /** Identity. */
  58. private final ConfigProvider identity;
  59. /** split pane. */
  60. private SplitPane splitPane;
  61. /** popup menu item. */
  62. private JMenuItem settingsMI;
  63. /** Nicklist. */
  64. private NickList nicklist;
  65. /** Topic bar. */
  66. private TopicBar topicBar;
  67. /** Event bus to dispatch events on. */
  68. private final EventBus eventBus;
  69. /** Config to read settings from. */
  70. private final AggregateConfigProvider globalConfig;
  71. /** Channel settings dialog provider. */
  72. private final KeyedDialogProvider<GroupChat, ChannelSettingsDialog> dialogProvider;
  73. /** Group chat instance. */
  74. private final GroupChat groupChat;
  75. /** Config binder. */
  76. private final ConfigBinder binder;
  77. /**
  78. * Creates a new instance of ChannelFrame. Sets up callbacks and handlers, and default options
  79. * for the form.
  80. *
  81. * @param deps The dependencies required by text frames.
  82. * @param inputFieldProvider The provider to use to create a new input field.
  83. * @param identityFactory The factory to use to create a group chat identity.
  84. * @param topicBarFactory The factory to use to create topic bars.
  85. * @param owner The group chat object that owns this frame
  86. * @param domain The domain to read settings from
  87. * @param dialogProvider The dialog provider to get the group chat settings dialog from.
  88. */
  89. public ChannelFrame(
  90. final String domain,
  91. final TextFrameDependencies deps,
  92. final Provider<SwingInputField> inputFieldProvider,
  93. final IdentityFactory identityFactory,
  94. final KeyedDialogProvider<GroupChat, ChannelSettingsDialog> dialogProvider,
  95. final InputTextFramePasteActionFactory inputTextFramePasteActionFactory,
  96. final TopicBarFactory topicBarFactory,
  97. final GroupChat owner) {
  98. super(deps, inputFieldProvider, inputTextFramePasteActionFactory, owner.getWindowModel());
  99. this.eventBus = deps.eventBus;
  100. this.globalConfig = deps.globalConfig;
  101. this.dialogProvider = dialogProvider;
  102. this.groupChat = owner;
  103. initComponents(topicBarFactory, deps.colourManagerFactory);
  104. binder = getContainer().getConfigManager().getBinder().withDefaultDomain(domain);
  105. identity = identityFactory.createChannelConfig(owner.getConnection().get().getNetwork(),
  106. owner.getName());
  107. }
  108. /**
  109. * Initialises the instance, adding any required listeners.
  110. */
  111. @Override
  112. public void init() {
  113. binder.bind(this, ChannelFrame.class);
  114. eventBus.subscribe(this);
  115. super.init();
  116. }
  117. /**
  118. * Initialises the components in this frame.
  119. *
  120. * @param topicBarFactory The factory to use to produce topic bars.
  121. * @param colourManagerFactory The colour manager factory
  122. */
  123. private void initComponents(final TopicBarFactory topicBarFactory,
  124. final ColourManagerFactory colourManagerFactory) {
  125. topicBar = topicBarFactory.getTopicBar((GroupChat) getContainer(), this);
  126. nicklist = new NickList(this, getContainer().getConfigManager(), colourManagerFactory);
  127. settingsMI = new JMenuItem("Settings");
  128. settingsMI.addActionListener(l -> dialogProvider.displayOrRequestFocus(groupChat));
  129. splitPane = new SplitPane(globalConfig, SplitPane.Orientation.HORIZONTAL);
  130. setLayout(new MigLayout("fill, ins 0, hidemode 3, wrap 1"));
  131. add(topicBar, "growx");
  132. add(splitPane, "grow, push");
  133. add(getSearchBar(), "growx");
  134. add(inputPanel, "growx");
  135. splitPane.setLeftComponent(getTextPane());
  136. splitPane.setResizeWeight(1);
  137. splitPane.setDividerLocation(-1);
  138. }
  139. @ConfigBinding(domain = "ui", key = "channelSplitPanePosition",
  140. invocation = EDTInvocation.class)
  141. public void handleSplitPanePosition(final int value) {
  142. checkOnEDT();
  143. nicklist.setPreferredSize(new Dimension(value, 0));
  144. splitPane.setDividerLocation(splitPane.getWidth() - splitPane.getDividerSize() - value);
  145. }
  146. @ConfigBinding(key = "shownicklist", invocation = EDTInvocation.class)
  147. public void handleShowNickList(final boolean value) {
  148. checkOnEDT();
  149. if (value) {
  150. splitPane.setRightComponent(nicklist);
  151. } else {
  152. splitPane.setRightComponent(null);
  153. }
  154. }
  155. @Handler(invocation = EdtHandlerInvocation.class)
  156. public void handleClientClosing(final ClientClosingEvent event) {
  157. saveSplitPanePosition();
  158. }
  159. private void saveSplitPanePosition() {
  160. checkOnEDT();
  161. if (getContainer().getConfigManager().getOptionInt("ui",
  162. "channelSplitPanePosition") != nicklist.getWidth()) {
  163. identity.setOption("ui", "channelSplitPanePosition", nicklist.getWidth());
  164. }
  165. }
  166. @Override
  167. public PopupType getNicknamePopupType() {
  168. return PopupType.CHAN_NICK;
  169. }
  170. @Override
  171. public PopupType getChannelPopupType() {
  172. return PopupType.CHAN_NORMAL;
  173. }
  174. @Override
  175. public PopupType getHyperlinkPopupType() {
  176. return PopupType.CHAN_HYPERLINK;
  177. }
  178. @Override
  179. public PopupType getNormalPopupType() {
  180. return PopupType.CHAN_NORMAL;
  181. }
  182. @Override
  183. public void addCustomPopupItems(final JPopupMenu popupMenu) {
  184. if (groupChat.getConnection().get().getState() == ServerState.CONNECTED) {
  185. settingsMI.setEnabled(true);
  186. } else {
  187. settingsMI.setEnabled(false);
  188. }
  189. if (popupMenu.getComponentCount() > 0) {
  190. popupMenu.addSeparator();
  191. }
  192. popupMenu.add(settingsMI);
  193. }
  194. @Override
  195. @Handler(invocation = EdtHandlerInvocation.class)
  196. public void windowClosing(final FrameClosingEvent event) {
  197. if (event.getSource().equals(getContainer())) {
  198. saveSplitPanePosition();
  199. topicBar.close();
  200. dialogProvider.dispose(groupChat);
  201. super.windowClosing(event);
  202. }
  203. }
  204. @Override
  205. public void dispose() {
  206. eventBus.unsubscribe(this);
  207. binder.unbind(this);
  208. super.dispose();
  209. }
  210. }