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.

ChannelSettingsDialog.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.dialogs.channelsetting;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
  25. import com.dmdirc.addons.ui_swing.SwingWindowFactory;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import com.dmdirc.addons.ui_swing.components.IconManager;
  28. import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel;
  29. import com.dmdirc.addons.ui_swing.components.modes.ChannelModesPane;
  30. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  31. import com.dmdirc.config.prefs.PreferencesManager;
  32. import com.dmdirc.interfaces.CommandController;
  33. import com.dmdirc.interfaces.GroupChat;
  34. import com.dmdirc.interfaces.config.ConfigProvider;
  35. import com.dmdirc.interfaces.config.IdentityFactory;
  36. import com.dmdirc.interfaces.ui.InputWindow;
  37. import com.dmdirc.plugins.ServiceManager;
  38. import com.dmdirc.ui.input.TabCompleterUtils;
  39. import com.dmdirc.ui.messages.ColourManagerFactory;
  40. import java.awt.Window;
  41. import java.awt.datatransfer.Clipboard;
  42. import java.awt.event.ActionEvent;
  43. import java.awt.event.ActionListener;
  44. import javax.swing.JButton;
  45. import javax.swing.JScrollPane;
  46. import javax.swing.JTabbedPane;
  47. import javax.swing.WindowConstants;
  48. import net.miginfocom.swing.MigLayout;
  49. import static com.google.common.base.Preconditions.checkNotNull;
  50. /**
  51. * Allows the user to modify channel settings (modes, topics, etc).
  52. */
  53. public class ChannelSettingsDialog extends StandardDialog implements ActionListener {
  54. /** Serial version UID. */
  55. private static final long serialVersionUID = 8;
  56. /** The channel object that this dialog belongs to. */
  57. private final GroupChat channel;
  58. /** Channel identity file. */
  59. private final ConfigProvider identity;
  60. /** Channel window. */
  61. private final InputWindow channelWindow;
  62. private final IconManager iconManager;
  63. /** Tabbed pane. */
  64. private JTabbedPane tabbedPane;
  65. /** Client settings panel. */
  66. private SettingsPanel channelSettingsPane;
  67. /** List modes panel. */
  68. private ChannelModesPane channelModesPane;
  69. /** List modes panel. */
  70. private TopicPane topicModesPane;
  71. /** List modes panel. */
  72. private ChannelListModesPane channelListModesPane;
  73. /** The config to write settings to. */
  74. private final ConfigProvider userConfig;
  75. /** Service manager. */
  76. private final ServiceManager serviceManager;
  77. /** Preferences Manager. */
  78. private final PreferencesManager preferencesManager;
  79. /** Preferences setting component factory. */
  80. private final PrefsComponentFactory compFactory;
  81. /** Clipboard to copy and paste from. */
  82. private final Clipboard clipboard;
  83. /** The controller to use to retrieve command information. */
  84. private final CommandController commandController;
  85. /** The event bus to post errors to. */
  86. private final DMDircMBassador eventBus;
  87. /** Colour manager factory. */
  88. private final ColourManagerFactory colourManagerFactory;
  89. /**
  90. * Creates a new instance of ChannelSettingsDialog.
  91. *
  92. * @param identityFactory Identity factory
  93. * @param windowFactory Swing window factory
  94. * @param userConfig The config to write global settings to.
  95. * @param serviceManager Service manager
  96. * @param preferencesManager Preferences Manager
  97. * @param compFactory Preferences setting component factory
  98. * @param groupChat The group chat object that we're editing settings for
  99. * @param parentWindow Parent window
  100. * @param clipboard Clipboard to copy and paste from
  101. * @param commandController The controller to use to retrieve command information.
  102. * @param eventBus The event bus to post errors to.
  103. */
  104. public ChannelSettingsDialog(
  105. final IdentityFactory identityFactory,
  106. final SwingWindowFactory windowFactory,
  107. final ConfigProvider userConfig,
  108. final ServiceManager serviceManager,
  109. final PreferencesManager preferencesManager,
  110. final PrefsComponentFactory compFactory,
  111. final GroupChat groupChat,
  112. final Window parentWindow,
  113. final Clipboard clipboard,
  114. final CommandController commandController,
  115. final DMDircMBassador eventBus,
  116. final ColourManagerFactory colourManagerFactory,
  117. final TabCompleterUtils tabCompleterUtils,
  118. final IconManager iconManager) {
  119. super(parentWindow, ModalityType.MODELESS);
  120. this.userConfig = checkNotNull(userConfig);
  121. this.serviceManager = checkNotNull(serviceManager);
  122. this.preferencesManager = checkNotNull(preferencesManager);
  123. this.compFactory = checkNotNull(compFactory);
  124. this.channel = checkNotNull(groupChat);
  125. this.clipboard = clipboard;
  126. this.commandController = checkNotNull(commandController);
  127. this.eventBus = eventBus;
  128. this.colourManagerFactory = colourManagerFactory;
  129. this.iconManager = iconManager;
  130. identity = identityFactory.createChannelConfig(groupChat.getConnection().get().getNetwork(),
  131. groupChat.getName());
  132. channelWindow = (InputWindow) windowFactory.getSwingWindow(groupChat.getWindowModel());
  133. initComponents(tabCompleterUtils);
  134. initListeners();
  135. }
  136. /** Initialises the main UI components. */
  137. private void initComponents(final TabCompleterUtils tabCompleterUtils) {
  138. tabbedPane = new JTabbedPane();
  139. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  140. setTitle("Channel settings for " + channel.getName());
  141. setResizable(false);
  142. orderButtons(new JButton(), new JButton());
  143. getContentPane().setLayout(new MigLayout(
  144. "fill, wrap 1, ins panel, hmax 80sp, wmin 460, wmax 460"));
  145. getContentPane().add(tabbedPane, "growy, pushy");
  146. getContentPane().add(getLeftButton(), "split 3, right");
  147. getContentPane().add(getRightButton(), "right");
  148. initTopicTab(tabCompleterUtils);
  149. initIrcTab();
  150. initListModesTab();
  151. initSettingsTab();
  152. tabbedPane.setSelectedIndex(channel.getWindowModel().getConfigManager().
  153. getOptionInt("dialogstate", "channelsettingsdialog"));
  154. }
  155. /** Initialises the Topic tab. */
  156. private void initTopicTab(final TabCompleterUtils tabCompleterUtils) {
  157. topicModesPane = new TopicPane(channel, iconManager,
  158. commandController, serviceManager,
  159. this, channelWindow, clipboard, eventBus, colourManagerFactory, tabCompleterUtils);
  160. tabbedPane.addTab("Topic", topicModesPane);
  161. }
  162. /** Initialises the IRC Settings tab. */
  163. private void initIrcTab() {
  164. channelModesPane = new ChannelModesPane(channel, iconManager);
  165. final JScrollPane channelModesSP = new JScrollPane(channelModesPane);
  166. channelModesSP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  167. channelModesSP.setOpaque(UIUtilities.getTabbedPaneOpaque());
  168. channelModesSP.getViewport().setOpaque(UIUtilities.getTabbedPaneOpaque());
  169. channelModesSP.setBorder(null);
  170. tabbedPane.addTab("Channel Modes", channelModesSP);
  171. }
  172. /** Initialises the IRC Settings tab. */
  173. private void initListModesTab() {
  174. channelListModesPane = new ChannelListModesPane(channel.getWindowModel().getConfigManager(),
  175. userConfig, iconManager, channel, this);
  176. tabbedPane.addTab("List Modes", channelListModesPane);
  177. }
  178. /** Initialises the channel Settings (identities) tab. */
  179. private void initSettingsTab() {
  180. initSettingsPanel();
  181. tabbedPane.addTab("Client Settings", channelSettingsPane);
  182. }
  183. /** Initialises the channel settings. */
  184. private void initSettingsPanel() {
  185. channelSettingsPane = new SettingsPanel(iconManager, compFactory,
  186. "These settings are specific to this channel on this network,"
  187. + " any settings specified here will overwrite global settings");
  188. channelSettingsPane.addOption(preferencesManager.getChannelSettings(
  189. channel.getWindowModel().getConfigManager(), identity));
  190. }
  191. /** Initialises listeners for this dialog. */
  192. private void initListeners() {
  193. getOkButton().addActionListener(this);
  194. getCancelButton().addActionListener(this);
  195. }
  196. /**
  197. * Called whenever the user clicks on one of the two buttons.
  198. *
  199. * @param actionEvent Event generated by this action
  200. */
  201. @Override
  202. public void actionPerformed(final ActionEvent actionEvent) {
  203. if (getOkButton().equals(actionEvent.getSource())) {
  204. save();
  205. } else if (getCancelButton().equals(actionEvent.getSource())) {
  206. dispose();
  207. }
  208. }
  209. /** Saves the settings. */
  210. public void save() {
  211. channelModesPane.save();
  212. topicModesPane.setChangedTopic();
  213. channelSettingsPane.save();
  214. channelListModesPane.save();
  215. identity.setOption("dialogstate", "channelsettingsdialog",
  216. String.valueOf(tabbedPane.getSelectedIndex()));
  217. dispose();
  218. }
  219. }