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.

TextAreaInputField.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.JTextArea;
  32. import javax.swing.text.BadLocationException;
  33. /**
  34. * JTextArea implementing InputField
  35. */
  36. public class TextAreaInputField extends JTextArea implements InputField,
  37. PropertyChangeListener {
  38. /** Serial version UID. */
  39. private static final long serialVersionUID = 2;
  40. /** Colour picker. */
  41. protected ColourPickerDialog colourPicker;
  42. /** Swing controller. */
  43. private final SwingController controller;
  44. /**
  45. * Creates a new text area with the specified number of rows and columns.
  46. *
  47. * @param controller Swing controller
  48. * @param rows The number of rows to use
  49. * @param columns The number of columns to use
  50. */
  51. public TextAreaInputField(final SwingController controller, final int rows,
  52. final int columns) {
  53. super(rows, columns);
  54. this.controller = controller;
  55. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  56. .addPropertyChangeListener(this);
  57. }
  58. /**
  59. * Creates a new text area containing the specified text.
  60. *
  61. * @param controller Swing controller
  62. * @param text The text to contain initially
  63. */
  64. public TextAreaInputField(final SwingController controller,
  65. final String text) {
  66. super(text);
  67. this.controller = controller;
  68. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  69. .addPropertyChangeListener(this);
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public void addActionListener(final ActionListener listener) {
  74. // Ignore request - we don't handle returns for text areas
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public void removeActionListener(final ActionListener listener) {
  79. // Ignore request - we don't handle returns for text areas
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public void showColourPicker(final boolean irc, final boolean hex) {
  84. if (controller.getGlobalConfig().getOptionBool("general",
  85. "showcolourdialog")) {
  86. colourPicker = new ColourPickerDialog(controller.getIconManager(),
  87. irc, hex);
  88. colourPicker.addActionListener(new ActionListener() {
  89. @Override
  90. public void actionPerformed(final ActionEvent actionEvent) {
  91. try {
  92. getDocument().insertString(getCaretPosition(),
  93. actionEvent.getActionCommand(), null);
  94. } catch (final BadLocationException ex) {
  95. //Ignore, wont happen
  96. }
  97. colourPicker.dispose();
  98. colourPicker = null;
  99. }
  100. });
  101. colourPicker.display();
  102. colourPicker.setLocation((int) getLocationOnScreen().getX(),
  103. (int) getLocationOnScreen().getY() -
  104. colourPicker.getHeight());
  105. }
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public void hideColourPicker() {
  110. if (colourPicker != null) {
  111. colourPicker.dispose();
  112. colourPicker = null;
  113. }
  114. }
  115. /** {@inheritDoc} */
  116. @Override
  117. public void propertyChange(final PropertyChangeEvent evt) {
  118. if (!isFocusOwner()) {
  119. hideColourPicker();
  120. }
  121. }
  122. }