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

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