Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ValidatingTextFieldInputField.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.inputfields;
  23. import com.dmdirc.addons.ui_swing.SwingController;
  24. import com.dmdirc.addons.ui_swing.components.colours.ColourPickerDialog;
  25. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  26. import com.dmdirc.interfaces.ui.InputField;
  27. import com.dmdirc.util.validators.Validator;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import javax.swing.JTextField;
  31. import javax.swing.text.BadLocationException;
  32. /**
  33. * Extended ValidatingTextField that adds Inputfield support.
  34. */
  35. public class ValidatingTextFieldInputField extends ValidatingJTextField
  36. implements InputField {
  37. /** Serial version UID. */
  38. private static final long serialVersionUID = 2;
  39. /** Colour picker. */
  40. private ColourPickerDialog colourPicker;
  41. /** Swing controller. */
  42. private final SwingController controller;
  43. /**
  44. * Creates a new text field with the specified validator.
  45. *
  46. * @param controller Swing controller
  47. * @param validator Validator for this textfield
  48. */
  49. public ValidatingTextFieldInputField(final SwingController controller,
  50. final Validator<String> validator) {
  51. this(controller, new JTextField(), validator);
  52. }
  53. /**
  54. * Creates a new text field with the specified validator.
  55. *
  56. * @param controller Swing controller
  57. * @param validator Validator for this textfield
  58. * @param textField Textfield to use as a base
  59. */
  60. public ValidatingTextFieldInputField(final SwingController controller,
  61. final JTextField textField, final Validator<String> validator) {
  62. super(controller.getIconManager(), textField, validator);
  63. this.controller = controller;
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void addActionListener(final ActionListener listener) {
  68. // Ignore request - we don't handle returns for text areas
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public void removeActionListener(final ActionListener listener) {
  73. // Ignore request - we don't handle returns for text areas
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void showColourPicker(final boolean irc, final boolean hex) {
  78. if (controller.getGlobalConfig().getOptionBool("general",
  79. "showcolourdialog")) {
  80. colourPicker = new ColourPickerDialog(controller.getIconManager(),
  81. irc, hex);
  82. colourPicker.addActionListener(new ActionListener() {
  83. @Override
  84. public void actionPerformed(final ActionEvent actionEvent) {
  85. try {
  86. getDocument().insertString(getCaretPosition(),
  87. actionEvent.getActionCommand(), null);
  88. } catch (final BadLocationException ex) {
  89. //Ignore, wont happen
  90. }
  91. colourPicker.dispose();
  92. colourPicker = null;
  93. }
  94. });
  95. colourPicker.display();
  96. colourPicker.setLocation((int) getLocationOnScreen().getX(),
  97. (int) getLocationOnScreen().getY() -
  98. colourPicker.getHeight());
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public void hideColourPicker() {
  104. if (colourPicker != null) {
  105. colourPicker.dispose();
  106. colourPicker = null;
  107. }
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public int getCaretPosition() {
  112. return getTextField().getCaretPosition();
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. public void setCaretPosition(final int position) {
  117. getTextField().setCaretPosition(position);
  118. }
  119. }