選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ChannelModesPane.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.channelsetting;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.addons.ui_swing.components.ParamModePanel;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import com.dmdirc.parser.interfaces.Parser;
  27. import java.awt.Insets;
  28. import java.util.Hashtable;
  29. import java.util.Map;
  30. import javax.swing.BorderFactory;
  31. import javax.swing.JCheckBox;
  32. import javax.swing.JPanel;
  33. import javax.swing.UIManager;
  34. import net.miginfocom.swing.MigLayout;
  35. /** Non list mode panel. */
  36. public final class ChannelModesPane extends JPanel {
  37. /**
  38. * A version number for this class. It should be changed whenever the class
  39. * structure is changed (or anything else that would prevent serialized
  40. * objects being unserialized with the new class).
  41. */
  42. private static final long serialVersionUID = 1;
  43. /** Parent channel. */
  44. private final Channel channel;
  45. /** The checkboxes used for boolean modes. */
  46. private Map<String, JCheckBox> modeCheckBoxes;
  47. /** The ParamModePanels used for parameter-requiring modes. */
  48. private Map<String, ParamModePanel> modeInputs;
  49. /**
  50. * Creates a new instance of ChannelModesPane.
  51. *
  52. * @param channel Parent channel
  53. */
  54. public ChannelModesPane(final Channel channel) {
  55. super();
  56. this.channel = channel;
  57. this.setOpaque(UIUtilities.getTabbedPaneOpaque());
  58. initModesPanel();
  59. layoutComponents();
  60. setVisible(true);
  61. }
  62. /** Updates the panel. */
  63. public void update() {
  64. setVisible(false);
  65. removeAll();
  66. initModesPanel();
  67. setVisible(true);
  68. }
  69. /** Initialises the modes panel. */
  70. private void initModesPanel() {
  71. final Parser parser = channel.getServer().getParser();
  72. final String booleanModes = parser.getBooleanChannelModes();
  73. final String ourBooleanModes = channel.getChannelInfo().getModes();
  74. final String paramModes =
  75. parser.getParameterChannelModes() + parser.
  76. getDoubleParameterChannelModes();
  77. modeCheckBoxes =
  78. new Hashtable<String, JCheckBox>();
  79. // Lay out all the boolean mode checkboxes
  80. for (int i = 0; i < booleanModes.length();
  81. i++) {
  82. final String mode = booleanModes.substring(i, i + 1);
  83. final char modeChar = mode.toCharArray()[0];
  84. final boolean state =
  85. ourBooleanModes.split(" ")[0].contains(
  86. mode.subSequence(0, 1));
  87. String text;
  88. String tooltip;
  89. final boolean opaque = UIUtilities.getTabbedPaneOpaque();
  90. if (channel.getConfigManager().hasOptionString("server",
  91. "mode" + mode)) {
  92. text = channel.getConfigManager().
  93. getOption("server", "mode" + mode) + " [+"+mode+"]";
  94. } else {
  95. text = "Mode " + mode;
  96. }
  97. if (channel.getConfigManager().hasOptionString("server", "mode" +
  98. mode)) {
  99. tooltip =
  100. "Mode " + mode + ": " +
  101. channel.getConfigManager().
  102. getOption("server", "mode" + mode);
  103. } else {
  104. tooltip = "Mode " + mode + ": Unknown";
  105. }
  106. final JCheckBox checkBox = new JCheckBox(text, state);
  107. checkBox.setMargin(new Insets(0, 0, 0, 0));
  108. checkBox.setToolTipText(tooltip);
  109. checkBox.setOpaque(opaque);
  110. modeCheckBoxes.put(mode, checkBox);
  111. if (!channel.getConfigManager().hasOptionString("server",
  112. "enablemode" + modeChar) || channel.getConfigManager().
  113. getOptionBool("server",
  114. "enablemode" + modeChar)) {
  115. checkBox.setEnabled(true);
  116. } else if (!channel.getServer().getParser().isUserSettable(
  117. modeChar)) {
  118. checkBox.setEnabled(false);
  119. }
  120. }
  121. // Lay out all the parameter-requiring modes
  122. modeInputs =
  123. new Hashtable<String, ParamModePanel>();
  124. for (int i = 0; i < paramModes.length();
  125. i++) {
  126. final String mode = paramModes.substring(i, i + 1);
  127. final String value =
  128. channel.getChannelInfo().getMode(mode.charAt(0));
  129. final boolean state =
  130. ourBooleanModes.split(" ")[0].contains(
  131. mode.subSequence(0, 1));
  132. final ParamModePanel panel =
  133. new ParamModePanel(mode, state, value,
  134. channel.getConfigManager());
  135. modeInputs.put(mode, panel);
  136. }
  137. }
  138. /** Lays out the components. */
  139. private void layoutComponents() {
  140. final JPanel booleanModes =
  141. new JPanel(new MigLayout("wrap 2, fillx"));
  142. for (JCheckBox checkBox : modeCheckBoxes.values()) {
  143. booleanModes.add(checkBox);
  144. }
  145. final JPanel paramModes =
  146. new JPanel(new MigLayout("wrap 2, fillx"));
  147. for (ParamModePanel modePanel : modeInputs.values()) {
  148. paramModes.add(modePanel.getCheckboxComponent());
  149. paramModes.add(modePanel.getValueComponent(), "growx, pushx");
  150. }
  151. booleanModes.setBorder(BorderFactory.createTitledBorder(UIManager.
  152. getBorder("TitledBorder.border"), "Boolean modes"));
  153. paramModes.setBorder(BorderFactory.createTitledBorder(UIManager.
  154. getBorder("TitledBorder.border"), "Parameter modes"));
  155. booleanModes.setOpaque(UIUtilities.getTabbedPaneOpaque());
  156. paramModes.setOpaque(UIUtilities.getTabbedPaneOpaque());
  157. setLayout(new MigLayout("flowy, fillx", "fill", ""));
  158. add(booleanModes);
  159. add(paramModes);
  160. }
  161. /**
  162. * Processes the channel settings dialog and constructs a mode string for
  163. * changed modes, then sends this to the server.
  164. */
  165. public void setChangedBooleanModes() {
  166. boolean changed = false;
  167. final Parser parser = channel.getServer().getParser();
  168. final String booleanModes = parser.getBooleanChannelModes();
  169. final String ourBooleanModes = channel.getChannelInfo().getModes();
  170. final String paramModes =
  171. parser.getParameterChannelModes() + parser.
  172. getDoubleParameterChannelModes();
  173. for (int i = 0; i < booleanModes.length();
  174. i++) {
  175. final String mode = booleanModes.substring(i, i + 1);
  176. final boolean state =
  177. ourBooleanModes.split(" ")[0].contains(
  178. mode.subSequence(0, 1));
  179. if (modeCheckBoxes.get(mode) != null &&
  180. state != modeCheckBoxes.get(mode).isSelected()) {
  181. changed = true;
  182. channel.getChannelInfo().
  183. alterMode(modeCheckBoxes.get(mode).isSelected(),
  184. mode.toCharArray()[0], "");
  185. }
  186. }
  187. for (int i = 0; i < paramModes.length();
  188. i++) {
  189. final String mode = paramModes.substring(i, i + 1);
  190. final String value =
  191. channel.getChannelInfo().getMode(mode.charAt(0));
  192. final boolean state =
  193. ourBooleanModes.split(" ")[0].contains(
  194. mode.subSequence(0, 1));
  195. final ParamModePanel paramModePanel = modeInputs.get(mode);
  196. if (state != paramModePanel.getState() ||
  197. !value.equals(paramModePanel.getValue())) {
  198. changed = true;
  199. channel.getChannelInfo().
  200. alterMode(paramModePanel.getState(),
  201. mode.toCharArray()[0], paramModePanel.getValue());
  202. }
  203. }
  204. if (changed) {
  205. channel.getChannelInfo().flushModes();
  206. }
  207. }
  208. }