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.

ParamModePanel.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.validating.ValidatingJTextField;
  20. import com.dmdirc.config.provider.AggregateConfigProvider;
  21. import com.dmdirc.addons.ui_swing.components.IconManager;
  22. import com.dmdirc.util.validators.RegexStringValidator;
  23. import java.awt.Component;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import javax.swing.JCheckBox;
  27. import javax.swing.JPanel;
  28. import net.miginfocom.swing.MigLayout;
  29. /**
  30. * A component to encapsulate one parameter-requiring channel mode, displaying the user a checkbox,
  31. * the mode's name, and a text field.
  32. */
  33. public final class ParamModePanel extends JPanel implements ActionListener {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = 1;
  36. /** The checkbox used in this mode panel. */
  37. private final JCheckBox checkBox;
  38. /** The textfield for the value of the mode. */
  39. private final ValidatingJTextField textField;
  40. /** the mode this component represents. */
  41. private final String mode;
  42. /** Original mode value. */
  43. private final String originalValue;
  44. /**
  45. * Creates a new instance of ParamModePanel.
  46. *
  47. * @param config The config to read mode aliases information from.
  48. * @param iconManager The manager to use to retrieve validation error icons.
  49. * @param thisMode The mode that this panel should deal with
  50. * @param state The current state of the mode
  51. * @param value The current value of the mode
  52. */
  53. public ParamModePanel(
  54. final AggregateConfigProvider config,
  55. final IconManager iconManager,
  56. final String thisMode,
  57. final boolean state,
  58. final String value) {
  59. this.mode = thisMode;
  60. this.originalValue = value;
  61. String text;
  62. final String tooltip;
  63. if (config.hasOptionString("server", "mode" + mode)) {
  64. tooltip = "Mode " + mode + ": " + config.getOption("server", "mode" + mode);
  65. } else {
  66. tooltip = "Mode " + mode + ": Unknown";
  67. }
  68. setLayout(new MigLayout("fill"));
  69. text = "Mode " + mode + ": ";
  70. if (config.hasOptionString("server", "mode" + mode)) {
  71. text = config.getOption("server", "mode" + mode)
  72. + " [+" + mode + "]: ";
  73. }
  74. checkBox = new JCheckBox(text, state);
  75. checkBox.setToolTipText(tooltip);
  76. checkBox.setOpaque(UIUtilities.getTabbedPaneOpaque());
  77. add(checkBox);
  78. textField = new ValidatingJTextField(iconManager,
  79. new RegexStringValidator("^[^ ]*$", "Cannot contain spaces"));
  80. textField.setText(value);
  81. add(textField, "growx, pushx");
  82. if (!state) {
  83. textField.setEnabled(false);
  84. }
  85. checkBox.addActionListener(this);
  86. }
  87. /**
  88. * Called when our checkbox is toggled.
  89. *
  90. * @param actionEvent associated action event
  91. */
  92. @Override
  93. public void actionPerformed(final ActionEvent actionEvent) {
  94. textField.setEnabled(checkBox.isSelected());
  95. if (checkBox.isSelected()) {
  96. textField.requestFocusInWindow();
  97. } else {
  98. textField.setText(originalValue);
  99. }
  100. }
  101. /**
  102. * returns the state of this component.
  103. *
  104. * @return boolean state of mode
  105. */
  106. public boolean getState() {
  107. return checkBox.isSelected() && textField.validateText();
  108. }
  109. /**
  110. * returns the parameter of this mode if enabled, else returns an empty string.
  111. *
  112. * @return String mode parameter or "" if unset
  113. */
  114. public String getValue() {
  115. return textField.getText();
  116. }
  117. /**
  118. * Returns the name of the mode this component represents.
  119. *
  120. * @return String name of the mode
  121. */
  122. public String getModeName() {
  123. return checkBox.getText();
  124. }
  125. /**
  126. * Returns the mode this component represents.
  127. *
  128. * @return String mode
  129. */
  130. public String getMode() {
  131. return mode;
  132. }
  133. /**
  134. * Returns the checkbox component.
  135. *
  136. * @return Checkbox component.
  137. */
  138. public Component getCheckboxComponent() {
  139. return checkBox;
  140. }
  141. /**
  142. * Returns the value component.
  143. *
  144. * @return Value component
  145. */
  146. public Component getValueComponent() {
  147. return textField;
  148. }
  149. }