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.

ServerSettingsDialog.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.dialogs.serversetting;
  23. import com.dmdirc.ServerState;
  24. import com.dmdirc.actions.wrappers.PerformWrapper;
  25. import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel;
  28. import com.dmdirc.addons.ui_swing.components.modes.UserModesPane;
  29. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  30. import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
  31. import com.dmdirc.config.prefs.PreferencesManager;
  32. import com.dmdirc.interfaces.Connection;
  33. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  34. import com.dmdirc.interfaces.config.ConfigProvider;
  35. import com.dmdirc.ui.messages.ColourManagerFactory;
  36. import java.awt.Window;
  37. import java.awt.event.ActionEvent;
  38. import java.awt.event.ActionListener;
  39. import javax.swing.JButton;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JTabbedPane;
  42. import javax.swing.ScrollPaneConstants;
  43. import net.miginfocom.swing.MigLayout;
  44. /**
  45. * Allows the user to modify server settings and the ignore list.
  46. */
  47. public class ServerSettingsDialog extends StandardDialog implements ActionListener {
  48. /** Serial version UID. */
  49. private static final long serialVersionUID = 2;
  50. /** Parent connection. */
  51. private final Connection connection;
  52. /** Perform wrapper for the perform panel. */
  53. private final PerformWrapper performWrapper;
  54. /** Preferences manager to retrieve settings from. */
  55. private final PreferencesManager preferencesManager;
  56. /** User modes panel. */
  57. private UserModesPane modesPanel;
  58. /** Ignore list panel. */
  59. private IgnoreListPanel ignoreList;
  60. /** Perform panel. */
  61. private PerformTab performPanel;
  62. /** Settings panel. */
  63. private SettingsPanel settingsPanel;
  64. /** The tabbed pane. */
  65. private JTabbedPane tabbedPane;
  66. /**
  67. * Creates a new instance of ServerSettingsDialog.
  68. *
  69. * @param preferencesManager Preferences manager to retrieve settings from
  70. * @param compFactory Preferences setting component factory
  71. * @param performWrapper Wrapper for the perform tab.
  72. * @param connection The server object that we're editing settings for
  73. * @param parentWindow Parent window
  74. */
  75. public ServerSettingsDialog(
  76. final PreferencesManager preferencesManager,
  77. final PrefsComponentFactory compFactory,
  78. final PerformWrapper performWrapper,
  79. final Connection connection,
  80. final Window parentWindow,
  81. final ColourManagerFactory colourManagerFactory) {
  82. super(parentWindow, ModalityType.MODELESS);
  83. this.connection = connection;
  84. this.performWrapper = performWrapper;
  85. this.preferencesManager = preferencesManager;
  86. setTitle("Server settings");
  87. setResizable(false);
  88. initComponents(parentWindow, connection.getWindowModel().getConfigManager(), compFactory,
  89. colourManagerFactory);
  90. initListeners();
  91. }
  92. /**
  93. * Initialises the main UI components.
  94. *
  95. * @param parentWindow The window that owns this dialog
  96. * @param config Config to read from
  97. * @param compFactory Preferences setting component factory
  98. */
  99. private void initComponents(
  100. final Window parentWindow,
  101. final AggregateConfigProvider config,
  102. final PrefsComponentFactory compFactory,
  103. final ColourManagerFactory colourManagerFactory) {
  104. orderButtons(new JButton(), new JButton());
  105. tabbedPane = new JTabbedPane();
  106. modesPanel = new UserModesPane(connection);
  107. ignoreList = new IgnoreListPanel(connection.getWindowModel().getIconManager(),
  108. connection, parentWindow);
  109. performPanel = new PerformTab(connection.getWindowModel().getIconManager(),
  110. colourManagerFactory, config, performWrapper, connection);
  111. settingsPanel = new SettingsPanel(connection.getWindowModel().getIconManager(), compFactory,
  112. "These settings are specific to this network, any settings specified here will "
  113. + "overwrite global settings");
  114. addSettings();
  115. final JScrollPane userModesSP = new JScrollPane(modesPanel);
  116. userModesSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  117. userModesSP.setOpaque(UIUtilities.getTabbedPaneOpaque());
  118. userModesSP.getViewport().setOpaque(UIUtilities.getTabbedPaneOpaque());
  119. userModesSP.setBorder(null);
  120. tabbedPane.add("User modes", userModesSP);
  121. tabbedPane.add("Ignore list", ignoreList);
  122. tabbedPane.add("Perform", performPanel);
  123. if (settingsPanel != null) {
  124. tabbedPane.add("Settings", settingsPanel);
  125. }
  126. setLayout(new MigLayout("fill, wrap 1, hmax 80sp"));
  127. add(tabbedPane, "grow");
  128. add(getLeftButton(), "split 2, right");
  129. add(getRightButton(), "right");
  130. tabbedPane.setSelectedIndex(connection.getWindowModel().getConfigManager().
  131. getOptionInt("dialogstate", "serversettingsdialog"));
  132. }
  133. /** Adds the settings to the panel. */
  134. private void addSettings() {
  135. settingsPanel.addOption(preferencesManager.getServerSettings(
  136. connection.getWindowModel().getConfigManager(),
  137. connection.getServerIdentity()));
  138. }
  139. /** Initialises listeners for this dialog. */
  140. private void initListeners() {
  141. getOkButton().addActionListener(this);
  142. getCancelButton().addActionListener(this);
  143. }
  144. /** Saves the settings from this dialog. */
  145. public void saveSettings() {
  146. if (connection.getState() == ServerState.CONNECTED) {
  147. closeAndSave();
  148. } else {
  149. new StandardQuestionDialog(getOwner(), ModalityType.MODELESS,
  150. "Server has been disconnected.", "Any changes you have " +
  151. "made will be lost, are you sure you want to close this " + "dialog?",
  152. () -> { dispose(); }).display();
  153. }
  154. }
  155. /** Closes this dialog and saves the settings. */
  156. private void closeAndSave() {
  157. modesPanel.save();
  158. settingsPanel.save();
  159. performPanel.savePerforms();
  160. ignoreList.saveList();
  161. final ConfigProvider identity = connection.getNetworkIdentity();
  162. identity.setOption("dialogstate", "serversettingsdialog",
  163. String.valueOf(tabbedPane.getSelectedIndex()));
  164. dispose();
  165. }
  166. @Override
  167. public void actionPerformed(final ActionEvent e) {
  168. if (e.getSource() == getOkButton()) {
  169. saveSettings();
  170. } else if (e.getSource() == getCancelButton()) {
  171. dispose();
  172. }
  173. }
  174. }