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.

ModesPane.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.components.modes;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  20. import com.dmdirc.config.provider.AggregateConfigProvider;
  21. import com.dmdirc.addons.ui_swing.components.IconManager;
  22. import java.awt.Insets;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.TreeSet;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.JCheckBox;
  29. import javax.swing.JPanel;
  30. import javax.swing.UIManager;
  31. import net.miginfocom.swing.MigLayout;
  32. /**
  33. * Abstract panel to retrieve, show and set user and channel modes.
  34. */
  35. public abstract class ModesPane extends JPanel {
  36. /** Serial version UID. */
  37. private static final long serialVersionUID = 1;
  38. /** The checkboxes used for boolean modes. */
  39. private final Map<String, JCheckBox> modeCheckBoxes;
  40. /** The ParamModePanels used for parameter-requiring modes. */
  41. private final Map<String, ParamModePanel> modeInputs;
  42. /** Boolean modes panel. */
  43. private final JPanel booleanModesPanel;
  44. /** Param modes panel. */
  45. private final JPanel paramModesPanel;
  46. /** Modes set, used for layout. */
  47. private final Set<String> modes;
  48. /** The config to read mode aliases information from. */
  49. private final AggregateConfigProvider config;
  50. /** The manager to use to retrieve validation error icons. */
  51. private final IconManager iconManager;
  52. /**
  53. * @param config The config to read mode aliases information from.
  54. * @param iconManager The manager to use to retrieve validation error icons.
  55. */
  56. public ModesPane(final AggregateConfigProvider config, final IconManager iconManager) {
  57. setLayout(new MigLayout("fill, wmax 100%, wrap 1"));
  58. booleanModesPanel = new JPanel(new MigLayout("wrap 2"));
  59. paramModesPanel = new JPanel(new MigLayout("wrap 2"));
  60. modes = new TreeSet<>(new ModesComparator());
  61. modeCheckBoxes = new HashMap<>();
  62. modeInputs = new HashMap<>();
  63. this.setOpaque(UIUtilities.getTabbedPaneOpaque());
  64. this.config = config;
  65. this.iconManager = iconManager;
  66. }
  67. public Map<String, JCheckBox> getBooleanModes() {
  68. return modeCheckBoxes;
  69. }
  70. public Map<String, ParamModePanel> getParamModes() {
  71. return modeInputs;
  72. }
  73. /** Initialises the modes panel. */
  74. protected void initModesPanel() {
  75. getBooleanModes().clear();
  76. final String booleanModes = getAvailableBooleanModes();
  77. final String ourBooleanModes = getOurBooleanModes();
  78. // Lay out all the boolean mode checkboxes
  79. for (int i = 0; i < booleanModes.length(); i++) {
  80. final String mode = booleanModes.substring(i, i + 1);
  81. final boolean state = ourBooleanModes.split(" ")[0].contains(
  82. mode.subSequence(0, 1));
  83. final String text = getModeText(mode);
  84. final String tooltip = getModeTooltip(mode);
  85. final JCheckBox checkBox = new JCheckBox(text, state);
  86. checkBox.setMargin(new Insets(0, 0, 0, 0));
  87. checkBox.setToolTipText(tooltip);
  88. checkBox.setOpaque(false);
  89. getBooleanModes().put(mode, checkBox);
  90. if (isModeEnabled(mode)) {
  91. checkBox.setEnabled(true);
  92. } else if (!isModeSettable(mode)) {
  93. checkBox.setEnabled(false);
  94. }
  95. }
  96. getParamModes().clear();
  97. final String paramModes = getAllParamModes();
  98. // Lay out all the parameter-requiring modes
  99. for (int i = 0; i < paramModes.length(); i++) {
  100. final String mode = paramModes.substring(i, i + 1);
  101. final String value = getParamModeValue(mode);
  102. final boolean state = ourBooleanModes.split(" ")[0].contains(
  103. mode.subSequence(0, 1));
  104. final ParamModePanel panel = new ParamModePanel(config, iconManager, mode,
  105. state, value);
  106. getParamModes().put(mode, panel);
  107. }
  108. layoutComponents();
  109. }
  110. /** Lays out the components. */
  111. protected void layoutComponents() {
  112. booleanModesPanel.removeAll();
  113. paramModesPanel.removeAll();
  114. modes.clear();
  115. modes.addAll(getBooleanModes().keySet());
  116. if (modes.isEmpty()) {
  117. booleanModesPanel.add(new TextLabel("No boolean modes."));
  118. }
  119. for (String mode : modes) {
  120. booleanModesPanel.add(getBooleanModes().get(mode), "growx, wmax 49%-rel*4");
  121. }
  122. modes.clear();
  123. modes.addAll(getParamModes().keySet());
  124. if (modes.isEmpty()) {
  125. paramModesPanel.add(new TextLabel("No parameter modes."));
  126. }
  127. for (String mode : modes) {
  128. final ParamModePanel modePanel = getParamModes().get(mode);
  129. paramModesPanel.add(modePanel.getCheckboxComponent());
  130. paramModesPanel.add(modePanel.getValueComponent(), "growx, pushx");
  131. }
  132. booleanModesPanel.setBorder(BorderFactory.createTitledBorder(
  133. UIManager.getBorder("TitledBorder.border"), "Boolean modes"));
  134. paramModesPanel.setBorder(BorderFactory.createTitledBorder(
  135. UIManager.getBorder("TitledBorder.border"), "Parameter modes"));
  136. booleanModesPanel.setOpaque(UIUtilities.getTabbedPaneOpaque());
  137. paramModesPanel.setOpaque(UIUtilities.getTabbedPaneOpaque());
  138. add(booleanModesPanel, "grow, wmax 100%");
  139. add(paramModesPanel, "grow, wmax 100%");
  140. }
  141. /** Updates the panel. */
  142. public void update() {
  143. setVisible(false);
  144. removeAll();
  145. initModesPanel();
  146. setVisible(true);
  147. }
  148. /**
  149. * Gets the description for a given mode
  150. *
  151. * @param mode Mode to be described
  152. *
  153. * @return Description of mode
  154. */
  155. private String getModeText(final String mode) {
  156. if (hasModeValue(mode)) {
  157. return getModeValue(mode) + " [+" + mode + ']';
  158. } else {
  159. return "Mode " + mode;
  160. }
  161. }
  162. /**
  163. * Gets a tooltip text for a mode, shows the description and mode value.
  164. *
  165. * @param mode Mode to be described
  166. *
  167. * @return Tooltip text for a mode
  168. */
  169. private String getModeTooltip(final String mode) {
  170. if (hasModeValue(mode)) {
  171. return "Mode " + mode + ": " + getModeValue(mode);
  172. } else {
  173. return "Mode " + mode + ": Unknown";
  174. }
  175. }
  176. /**
  177. * Processes the channel settings dialog and constructs a mode string for changed modes, then
  178. * sends this to the server.
  179. */
  180. public void save() {
  181. boolean changed = false;
  182. final String booleanModes = getAvailableBooleanModes();
  183. final String ourBooleanModes = getOurBooleanModes();
  184. final String paramModes = getAllParamModes();
  185. for (int i = 0; i < booleanModes.length(); i++) {
  186. final String mode = booleanModes.substring(i, i + 1);
  187. final boolean state = ourBooleanModes.split(" ")[0].contains(
  188. mode.subSequence(0, 1));
  189. if (getBooleanModes().get(mode) != null
  190. && state != getBooleanModes().get(mode).isSelected()) {
  191. changed = true;
  192. alterMode(getBooleanModes().get(mode).isSelected(), mode, "");
  193. }
  194. }
  195. for (int i = 0; i < paramModes.length(); i++) {
  196. final String mode = paramModes.substring(i, i + 1);
  197. final String value = getParamModeValue(mode);
  198. final boolean state = ourBooleanModes.split(" ")[0].contains(
  199. mode.subSequence(0, 1));
  200. final ParamModePanel paramModePanel = getParamModes().get(mode);
  201. if (state != paramModePanel.getState()
  202. || !value.equals(paramModePanel.getValue())) {
  203. changed = true;
  204. alterMode(paramModePanel.getState(), mode, paramModePanel.getValue());
  205. }
  206. }
  207. if (changed) {
  208. flushModes();
  209. }
  210. }
  211. /**
  212. * Checks whether there is a plain text description for this mode.
  213. *
  214. * @param mode Mode to check
  215. *
  216. * @return true iif there is a plain text description
  217. */
  218. public abstract boolean hasModeValue(final String mode);
  219. /**
  220. * Returns the plain text description for a mode.
  221. *
  222. * @param mode Mode to check
  223. *
  224. * @return Valid plain text description for a mode, or an empty string
  225. */
  226. public abstract String getModeValue(final String mode);
  227. /**
  228. * Is this mode enabled, are we allowed to set it?
  229. *
  230. * @param mode Mode to check
  231. *
  232. * @return true iif the mode is enabled
  233. */
  234. public abstract boolean isModeEnabled(final String mode);
  235. /**
  236. * Is this mode settable by us?
  237. *
  238. * @param mode Mode to check
  239. *
  240. * @return true iif the mode is settable
  241. */
  242. public abstract boolean isModeSettable(final String mode);
  243. /**
  244. * Returns all the available boolean modes.
  245. *
  246. * @return string containing all available boolean modes
  247. */
  248. public abstract String getAvailableBooleanModes();
  249. /**
  250. * Returns which boolean modes are set.
  251. *
  252. * @return string containing all set boolean modes
  253. */
  254. public abstract String getOurBooleanModes();
  255. /**
  256. * Returns all available param modes.
  257. *
  258. * @return string containing all available param modes
  259. */
  260. public abstract String getAllParamModes();
  261. /**
  262. * Returns the value of a given parameter mode.
  263. *
  264. * @param mode Mode to check
  265. *
  266. * @return value of a mode or an empty string
  267. */
  268. public abstract String getParamModeValue(final String mode);
  269. /**
  270. * Queues the specified mode change to be flushed with flushmodes.
  271. *
  272. * @param add Whether to add or remove the specified mode
  273. * @param mode The mode to be changed
  274. * @param parameter Optional parameter needed to make change
  275. */
  276. public abstract void alterMode(final boolean add, final String mode,
  277. final String parameter);
  278. /**
  279. * Sends the queued mode changes to the server.
  280. */
  281. public abstract void flushModes();
  282. }