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

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