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.

Profiles.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.addons.serverlists.ServerGroupItem;
  24. import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxModel;
  25. import com.dmdirc.interfaces.config.ConfigProvider;
  26. import com.dmdirc.interfaces.config.IdentityController;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.Map.Entry;
  30. import javax.swing.BorderFactory;
  31. import javax.swing.DefaultComboBoxModel;
  32. import javax.swing.JComboBox;
  33. import javax.swing.JLabel;
  34. import javax.swing.JPanel;
  35. import javax.swing.UIManager;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Profile selection field for an associated server group item.
  39. */
  40. public class Profiles extends JPanel implements ServerListListener {
  41. /** Serial version UID. */
  42. private static final long serialVersionUID = 2;
  43. /** Server list model. */
  44. private final ServerListModel model;
  45. /** Combo boxes. */
  46. private final Map<ServerGroupItem, JComboBox<ConfigProvider>> combos = new HashMap<>();
  47. /** Info label. */
  48. private final JLabel label;
  49. /** Controller to use to get profiles. */
  50. private final IdentityController identityController;
  51. /**
  52. * Creates a new profile panel backed by the specified model.
  53. *
  54. * @param model Backing server list model
  55. * @param identityController Controller to use to get profiles.
  56. */
  57. public Profiles(final ServerListModel model, final IdentityController identityController) {
  58. super();
  59. this.model = model;
  60. this.identityController = identityController;
  61. label = new JLabel("Use this profile on this network: ");
  62. setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  63. "TitledBorder.border"), "Default profile"));
  64. addListeners();
  65. setLayout(new MigLayout("fill"));
  66. add(label);
  67. add(getComboBox(model.getSelectedItem()), "grow, push");
  68. }
  69. /**
  70. * Adds required listeners.
  71. */
  72. private void addListeners() {
  73. model.addServerListListener(this);
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void serverGroupChanged(final ServerGroupItem item) {
  78. setVisible(false);
  79. removeAll();
  80. add(label);
  81. add(getComboBox(item), "grow, push");
  82. setVisible(true);
  83. }
  84. /**
  85. * Gets or creates a combo box for selecting performs for the specified server group item.
  86. *
  87. * @param item Server group item requiring the profile selection
  88. *
  89. * @return The server group item's associated profile selection box
  90. */
  91. private JComboBox<ConfigProvider> getComboBox(final ServerGroupItem item) {
  92. if (!combos.containsKey(item)) {
  93. final DefaultComboBoxModel<ConfigProvider> comboModel = new VetoableComboBoxModel<>();
  94. ConfigProvider selectedItem = null;
  95. comboModel.addElement(null);
  96. for (ConfigProvider profile : identityController.getProvidersByType("profile")) {
  97. comboModel.addElement(profile);
  98. if (item != null && profile.getName().equals(
  99. item.getProfile())) {
  100. selectedItem = profile;
  101. }
  102. }
  103. comboModel.setSelectedItem(selectedItem);
  104. combos.put(item, new JComboBox<>(comboModel));
  105. }
  106. return combos.get(item);
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public void dialogClosed(final boolean save) {
  111. if (save) {
  112. for (Entry<ServerGroupItem, JComboBox<ConfigProvider>> entry : combos.entrySet()) {
  113. if (entry.getKey() != null) {
  114. if (entry.getValue().getSelectedItem() == null) {
  115. entry.getKey().setProfile(null);
  116. } else {
  117. entry.getKey().setProfile(((ConfigProvider) entry.getValue().
  118. getSelectedItem()).getName());
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public void serverGroupAdded(final ServerGroupItem parent,
  127. final ServerGroupItem group) {
  128. //Ignore
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public void serverGroupRemoved(final ServerGroupItem parent,
  133. final ServerGroupItem group) {
  134. //Ignore
  135. }
  136. }