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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.config.provider.AggregateConfigProvider;
  20. import com.dmdirc.interfaces.ui.InputField;
  21. import com.dmdirc.addons.ui_swing.components.IconManager;
  22. import com.dmdirc.ui.messages.ColourManager;
  23. import com.dmdirc.ui.messages.ColourManagerFactory;
  24. import java.awt.KeyboardFocusManager;
  25. import java.awt.event.ActionListener;
  26. import java.beans.PropertyChangeEvent;
  27. import java.beans.PropertyChangeListener;
  28. import javax.swing.JTextArea;
  29. import javax.swing.text.BadLocationException;
  30. /**
  31. * JTextArea implementing InputField
  32. */
  33. public class TextAreaInputField extends JTextArea implements InputField,
  34. PropertyChangeListener {
  35. /** Serial version UID. */
  36. private static final long serialVersionUID = 2;
  37. /** Colour picker. */
  38. protected ColourPickerDialog colourPicker;
  39. /** Icon manager. */
  40. private final IconManager iconManager;
  41. /** Colour manager. */
  42. private final ColourManager colourManager;
  43. /** Global config. */
  44. private final AggregateConfigProvider config;
  45. /**
  46. * Creates a new text area with the specified number of rows and columns.
  47. *
  48. * @param iconManager Icon manager
  49. * @param config Config to read settings from
  50. * @param rows The number of rows to use
  51. * @param columns The number of columns to use
  52. */
  53. public TextAreaInputField(final IconManager iconManager,
  54. final ColourManagerFactory colourManagerFactory, final AggregateConfigProvider config,
  55. final int rows, final int columns) {
  56. super(rows, columns);
  57. this.iconManager = iconManager;
  58. this.config = config;
  59. colourManager = colourManagerFactory.getColourManager(config);
  60. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  61. .addPropertyChangeListener(this);
  62. }
  63. /**
  64. * Creates a new text area containing the specified text.
  65. *
  66. * @param iconManager Icon manager
  67. * @param config Config to read settings from
  68. * @param text The text to contain initially
  69. */
  70. public TextAreaInputField(final IconManager iconManager,
  71. final ColourManagerFactory colourManagerFactory,
  72. final AggregateConfigProvider config, final String text) {
  73. super(text);
  74. this.iconManager = iconManager;
  75. this.config = config;
  76. colourManager = colourManagerFactory.getColourManager(config);
  77. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  78. .addPropertyChangeListener(this);
  79. }
  80. @Override
  81. public void addActionListener(final ActionListener listener) {
  82. // Ignore request - we don't handle returns for text areas
  83. }
  84. @Override
  85. public void removeActionListener(final ActionListener listener) {
  86. // Ignore request - we don't handle returns for text areas
  87. }
  88. @Override
  89. public void showColourPicker(final boolean irc, final boolean hex) {
  90. if (config.getOptionBool("general", "showcolourdialog")) {
  91. colourPicker = new ColourPickerDialog(TextAreaInputField.this,
  92. colourManager, iconManager, irc, hex);
  93. colourPicker.addActionListener(actionEvent -> {
  94. try {
  95. getDocument().insertString(getCaretPosition(),
  96. actionEvent.getActionCommand(), null);
  97. } catch (final BadLocationException ex) {
  98. //Ignore, wont happen
  99. }
  100. colourPicker.dispose();
  101. colourPicker = null;
  102. });
  103. colourPicker.setVisible(true);
  104. colourPicker.setLocation((int) getLocationOnScreen().getX(),
  105. (int) getLocationOnScreen().getY() - colourPicker.getHeight());
  106. }
  107. }
  108. @Override
  109. public void hideColourPicker() {
  110. if (colourPicker != null) {
  111. colourPicker.dispose();
  112. colourPicker = null;
  113. }
  114. }
  115. @Override
  116. public void propertyChange(final PropertyChangeEvent evt) {
  117. if (!isFocusOwner()) {
  118. hideColourPicker();
  119. }
  120. }
  121. }