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.

UserModesPane.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Server;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import java.awt.Insets;
  27. import java.util.Hashtable;
  28. import java.util.Map;
  29. import javax.swing.BorderFactory;
  30. import javax.swing.JCheckBox;
  31. import javax.swing.JPanel;
  32. import javax.swing.UIManager;
  33. import net.miginfocom.swing.MigLayout;
  34. /** User mode panel. */
  35. public final class UserModesPane extends JPanel {
  36. /**
  37. * A version number for this class. It should be changed whenever the class
  38. * structure is changed (or anything else that would prevent serialized
  39. * objects being unserialized with the new class).
  40. */
  41. private static final long serialVersionUID = 1;
  42. /** Parent server. */
  43. private final Server server;
  44. /** The checkboxes used for user modes. */
  45. private Map<String, JCheckBox> modeCheckBoxes;
  46. /**
  47. * Creates a new instance of UserModesPane.
  48. *
  49. * @param server Parent server
  50. */
  51. public UserModesPane(final Server server) {
  52. super();
  53. this.server = server;
  54. this.setOpaque(UIUtilities.getTabbedPaneOpaque());
  55. initModesPanel();
  56. layoutComponents();
  57. setVisible(true);
  58. }
  59. /** Updates the panel. */
  60. public void update() {
  61. setVisible(false);
  62. removeAll();
  63. initModesPanel();
  64. setVisible(true);
  65. }
  66. /** Initialises the modes panel. */
  67. private void initModesPanel() {
  68. final Parser parser = server.getParser();
  69. final String userModes = parser.getUserModes();
  70. final String ourUserModes = parser.getLocalClient().getModes();
  71. modeCheckBoxes =
  72. new Hashtable<String, JCheckBox>();
  73. final boolean opaque = UIUtilities.getTabbedPaneOpaque();
  74. // Lay out all the boolean mode checkboxes
  75. for (int i = 0; i < userModes.length();
  76. i++) {
  77. final String mode = userModes.substring(i, i + 1);
  78. final boolean state =
  79. ourUserModes.split(" ")[0].contains(mode.subSequence(0, 1));
  80. String text;
  81. String tooltip;
  82. if (server.getConfigManager().hasOptionString("server",
  83. "umode" + mode)) {
  84. text = server.getConfigManager().
  85. getOption("server", "umode" + mode) + " [+"+mode+"]";
  86. } else {
  87. text = "Mode " + mode;
  88. }
  89. if (server.getConfigManager().hasOptionString("server", "umode" +
  90. mode)) {
  91. tooltip = "Mode " + mode + ": " + server.getConfigManager().
  92. getOption("server", "umode" + mode);
  93. } else {
  94. tooltip = "Mode " + mode + ": Unknown";
  95. }
  96. final JCheckBox checkBox = new JCheckBox(text, state);
  97. checkBox.setMargin(new Insets(0, 0, 0, 0));
  98. checkBox.setToolTipText(tooltip);
  99. checkBox.setOpaque(opaque);
  100. modeCheckBoxes.put(mode, checkBox);
  101. }
  102. }
  103. /** Lays out the components. */
  104. private void layoutComponents() {
  105. final JPanel userModes =
  106. new JPanel(new MigLayout("wrap 2, fillx"));
  107. for (JCheckBox checkBox : modeCheckBoxes.values()) {
  108. userModes.add(checkBox);
  109. }
  110. userModes.setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  111. "TitledBorder.border"), "User modes"));
  112. userModes.setOpaque(UIUtilities.getTabbedPaneOpaque());
  113. setLayout(new MigLayout("flowy, fillx", "fill", ""));
  114. add(userModes);
  115. }
  116. /**
  117. * Sends changed modes to the server.
  118. */
  119. public void save() {
  120. if (server == null || server.getParser() == null) {
  121. return;
  122. }
  123. boolean changed = false;
  124. final Parser parser = server.getParser();
  125. final String userModes = parser.getUserModes();
  126. final String ourUserModes = parser.getLocalClient().getModes();
  127. for (int i = 0; i < userModes.length();
  128. i++) {
  129. final String mode = userModes.substring(i, i + 1);
  130. final boolean state =
  131. ourUserModes.split(" ")[0].contains(mode.subSequence(0, 1));
  132. if (modeCheckBoxes.get(mode) != null &&
  133. state != modeCheckBoxes.get(mode).isSelected()) {
  134. changed = true;
  135. server.getParser().getLocalClient().
  136. alterMode(modeCheckBoxes.get(mode).isSelected(),
  137. mode.toCharArray()[0]);
  138. }
  139. }
  140. if (changed) {
  141. server.getParser().getLocalClient().flushModes();
  142. }
  143. }
  144. }