Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TextFieldInputField.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.interfaces.ui.InputField;
  26. import java.awt.KeyboardFocusManager;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.beans.PropertyChangeEvent;
  30. import java.beans.PropertyChangeListener;
  31. import javax.swing.JTextField;
  32. import javax.swing.text.BadLocationException;
  33. /**
  34. * JTextField implementing InputField.
  35. */
  36. public class TextFieldInputField extends JTextField implements InputField,
  37. PropertyChangeListener {
  38. /** Serial version UID. */
  39. private static final long serialVersionUID = 1;
  40. /** Colour picker. */
  41. protected ColourPickerDialog colourPicker;
  42. /** Swing controller. */
  43. private final SwingController controller;
  44. /**
  45. * Creates a new text field input field.
  46. *
  47. * @param controller Swing controller
  48. */
  49. public TextFieldInputField(final SwingController controller) {
  50. super();
  51. this.controller = controller;
  52. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  53. .addPropertyChangeListener(this);
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public void showColourPicker(final boolean irc, final boolean hex) {
  58. if (controller.getGlobalConfig().getOptionBool("general",
  59. "showcolourdialog")) {
  60. colourPicker = new ColourPickerDialog(controller.getIconManager(),
  61. irc, hex);
  62. colourPicker.addActionListener(new ActionListener() {
  63. @Override
  64. public void actionPerformed(final ActionEvent actionEvent) {
  65. try {
  66. getDocument().insertString(getCaretPosition(),
  67. actionEvent.getActionCommand(), null);
  68. } catch (final BadLocationException ex) {
  69. //Ignore, wont happen
  70. }
  71. colourPicker.dispose();
  72. colourPicker = null;
  73. }
  74. });
  75. colourPicker.display();
  76. colourPicker.setLocation((int) getLocationOnScreen().getX(),
  77. (int) getLocationOnScreen().getY()
  78. - colourPicker.getHeight());
  79. }
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public void hideColourPicker() {
  84. if (colourPicker != null) {
  85. colourPicker.dispose();
  86. colourPicker = null;
  87. }
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public void propertyChange(final PropertyChangeEvent evt) {
  92. if (!isFocusOwner()) {
  93. hideColourPicker();
  94. }
  95. }
  96. }