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

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