Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ChannelFrame.java 9.3KB

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