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.

Settings.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.serverlistdialog;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.addons.serverlists.ServerGroup;
  25. import com.dmdirc.addons.serverlists.ServerGroupItem;
  26. import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
  27. import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel;
  28. import com.dmdirc.config.prefs.PreferencesManager;
  29. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  30. import com.dmdirc.interfaces.config.ConfigProvider;
  31. import com.dmdirc.interfaces.config.IdentityFactory;
  32. import com.dmdirc.ui.IconManager;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. import java.util.Map.Entry;
  36. import javax.inject.Inject;
  37. import javax.swing.BorderFactory;
  38. import javax.swing.JPanel;
  39. import javax.swing.UIManager;
  40. import javax.swing.border.Border;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * Panel for listing and adding settings to the group item.
  44. */
  45. public class Settings extends JPanel implements ServerListListener {
  46. /** Serial version UID. */
  47. private static final long serialVersionUID = 2;
  48. /** Server list model. */
  49. private final ServerListModel model;
  50. /** Settings panel. */
  51. private final Map<ServerGroupItem, SettingsPanel> panels = new HashMap<>();
  52. /** Platform border. */
  53. private final Border border;
  54. /** Manager to use to retrieve icons. */
  55. private final IconManager iconManager;
  56. /** Factory to use to produce preference components. */
  57. private final PrefsComponentFactory componentFactory;
  58. /** Factory to use to produce identities. */
  59. private final IdentityFactory identityFactory;
  60. /**
  61. * Instantiates a new settings panel.
  62. *
  63. * @param model Backing model
  64. * @param iconManager Manager to use to retrieve icons.
  65. * @param componentFactory Factory to use to produce preference components.
  66. * @param identityFactory Factory to use to produce identities.
  67. */
  68. @Inject
  69. public Settings(
  70. final ServerListModel model,
  71. @GlobalConfig final IconManager iconManager,
  72. final PrefsComponentFactory componentFactory,
  73. final IdentityFactory identityFactory) {
  74. super();
  75. this.model = model;
  76. this.iconManager = iconManager;
  77. this.componentFactory = componentFactory;
  78. this.identityFactory = identityFactory;
  79. addListeners();
  80. border = UIManager.getBorder("TitledBorder.border");
  81. setBorder(BorderFactory.createTitledBorder(border, "Network Settings"));
  82. setLayout(new MigLayout("fill, ins 0"));
  83. add(getSettingsPanel(model.getSelectedItem()), "grow, push");
  84. }
  85. /**
  86. * Adds required listeners.
  87. */
  88. private void addListeners() {
  89. model.addServerListListener(this);
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. public void serverGroupChanged(final ServerGroupItem item) {
  94. setVisible(false);
  95. removeAll();
  96. if (item != null) {
  97. if (item.getGroup() == item) {
  98. setBorder(BorderFactory.createTitledBorder(border,
  99. "Network settings"));
  100. } else {
  101. setBorder(BorderFactory.createTitledBorder(border,
  102. "Server settings"));
  103. }
  104. }
  105. add(getSettingsPanel(item), "grow, push");
  106. setVisible(true);
  107. }
  108. /**
  109. * Gets a settings panel for the specified group item, creating it if required.
  110. *
  111. * @param item Group item panel
  112. *
  113. * @return Settings panel for group item
  114. */
  115. private SettingsPanel getSettingsPanel(final ServerGroupItem item) {
  116. if (!panels.containsKey(item)) {
  117. if (item instanceof ServerGroup) {
  118. panels.put(item, new SettingsPanel(iconManager, componentFactory, "", false));
  119. addSettings(panels.get(item),
  120. identityFactory.createAggregateConfig("irc", "", item.getGroup().
  121. getNetwork(), item.getName()),
  122. identityFactory.createServerConfig(item.getName()));
  123. } else {
  124. panels.put(item, new SettingsPanel(iconManager, componentFactory, "", false));
  125. }
  126. }
  127. return panels.get(item);
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public void dialogClosed(final boolean save) {
  132. if (save) {
  133. for (Entry<ServerGroupItem, SettingsPanel> entry : panels.entrySet()) {
  134. entry.getValue().save();
  135. }
  136. }
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public void serverGroupAdded(final ServerGroupItem parent,
  141. final ServerGroupItem group) {
  142. //Ignore
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public void serverGroupRemoved(final ServerGroupItem parent,
  147. final ServerGroupItem group) {
  148. //Ignore
  149. }
  150. /**
  151. * Adds the settings to the panel.
  152. *
  153. * @param settingsPanel Settings panel to add settings to
  154. * @param manager The config manager to read current settings from.
  155. * @param identity The provider to write settings to.
  156. */
  157. private void addSettings(final SettingsPanel settingsPanel,
  158. final AggregateConfigProvider manager, final ConfigProvider identity) {
  159. settingsPanel.addOption(PreferencesManager.getPreferencesManager()
  160. .getServerSettings(manager, identity));
  161. }
  162. }