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.

ValidatingTextFieldInputField.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.inputfields;
  18. import com.dmdirc.addons.ui_swing.components.colours.ColourPickerDialog;
  19. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  20. import com.dmdirc.config.provider.AggregateConfigProvider;
  21. import com.dmdirc.interfaces.ui.InputField;
  22. import com.dmdirc.addons.ui_swing.components.IconManager;
  23. import com.dmdirc.ui.messages.ColourManager;
  24. import com.dmdirc.util.validators.Validator;
  25. import java.awt.event.ActionListener;
  26. import javax.swing.text.BadLocationException;
  27. /**
  28. * Extended ValidatingTextField that adds Inputfield support.
  29. */
  30. public class ValidatingTextFieldInputField extends ValidatingJTextField
  31. implements InputField {
  32. /** Serial version UID. */
  33. private static final long serialVersionUID = 2;
  34. /** Colour picker. */
  35. private ColourPickerDialog colourPicker;
  36. /** The icon manager to use for validation and dialog icons. */
  37. private final IconManager iconManager;
  38. /** The manager to use for colour input. */
  39. private final ColourManager colourManager;
  40. /** The config to read settings from. */
  41. private final AggregateConfigProvider globalConfig;
  42. /**
  43. * Creates a new text field with the specified validator.
  44. *
  45. * @param iconManager The icon manager to use for validation and dialog icons.
  46. * @param colourManager The manager to use for colour input.
  47. * @param globalConfig The config to read settings from.
  48. * @param validator Validator for this textfield
  49. */
  50. public ValidatingTextFieldInputField(
  51. final IconManager iconManager,
  52. final ColourManager colourManager,
  53. final AggregateConfigProvider globalConfig,
  54. final Validator<String> validator) {
  55. this(iconManager, colourManager, globalConfig, "", validator);
  56. }
  57. /**
  58. * Creates a new text field with the specified validator.
  59. *
  60. * @param iconManager The icon manager to use for validation and dialog icons.
  61. * @param colourManager The manager to use for colour input.
  62. * @param globalConfig The config to read settings from.
  63. * @param validator Validator for this textfield
  64. * @param text Text to use
  65. */
  66. public ValidatingTextFieldInputField(
  67. final IconManager iconManager,
  68. final ColourManager colourManager,
  69. final AggregateConfigProvider globalConfig,
  70. final String text,
  71. final Validator<String> validator) {
  72. super(iconManager, text, validator);
  73. this.iconManager = iconManager;
  74. this.colourManager = colourManager;
  75. this.globalConfig = globalConfig;
  76. }
  77. @Override
  78. public void addActionListener(final ActionListener listener) {
  79. // Ignore request - we don't handle returns for text areas
  80. }
  81. @Override
  82. public void removeActionListener(final ActionListener listener) {
  83. // Ignore request - we don't handle returns for text areas
  84. }
  85. @Override
  86. public void showColourPicker(final boolean irc, final boolean hex) {
  87. if (globalConfig.getOptionBool("general", "showcolourdialog")) {
  88. colourPicker = new ColourPickerDialog(this, colourManager, iconManager, irc, hex);
  89. colourPicker.addActionListener(actionEvent -> {
  90. try {
  91. getDocument().insertString(getCaretPosition(),
  92. actionEvent.getActionCommand(), null);
  93. } catch (final BadLocationException ex) {
  94. //Ignore, wont happen
  95. }
  96. colourPicker.dispose();
  97. colourPicker = null;
  98. });
  99. colourPicker.setVisible(true);
  100. colourPicker.setLocation((int) getLocationOnScreen().getX(),
  101. (int) getLocationOnScreen().getY() - colourPicker.getHeight());
  102. }
  103. }
  104. @Override
  105. public void hideColourPicker() {
  106. if (colourPicker != null) {
  107. colourPicker.dispose();
  108. colourPicker = null;
  109. }
  110. }
  111. }