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.

TextPaneInputField.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 java.awt.KeyboardFocusManager;
  24. import java.awt.Window;
  25. import java.awt.event.ActionListener;
  26. import java.beans.PropertyChangeEvent;
  27. import java.beans.PropertyChangeListener;
  28. import javax.swing.BorderFactory;
  29. import javax.swing.JEditorPane;
  30. import javax.swing.plaf.ComponentUI;
  31. import javax.swing.plaf.basic.BasicEditorPaneUI;
  32. import javax.swing.text.BadLocationException;
  33. /**
  34. * JTextPane implementing InputField.
  35. */
  36. public class TextPaneInputField extends JEditorPane implements InputField,
  37. PropertyChangeListener {
  38. /** Serial version UID. */
  39. private static final long serialVersionUID = 1;
  40. /** Parent window. */
  41. private final Window parentWindow;
  42. /** Colour picker. */
  43. protected ColourPickerDialog colourPicker;
  44. /** The config to read settings from. */
  45. private final AggregateConfigProvider globalConfig;
  46. /** The colour manager to use when picking colours. */
  47. private final ColourManager colourManager;
  48. /** The manager to use to retrieve icons. */
  49. private final IconManager iconManager;
  50. /**
  51. * Creates a new text pane input field.
  52. *
  53. * @param parentWindow Parent window.
  54. * @param globalConfig The config to read settings from.
  55. * @param colourManager The colour manager to use when picking colours.
  56. * @param iconManager The manager to use to retrieve icons.
  57. */
  58. public TextPaneInputField(
  59. final Window parentWindow,
  60. final AggregateConfigProvider globalConfig,
  61. final ColourManager colourManager,
  62. final IconManager iconManager) {
  63. KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(this);
  64. this.parentWindow = parentWindow;
  65. this.globalConfig = globalConfig;
  66. this.colourManager = colourManager;
  67. this.iconManager = iconManager;
  68. }
  69. @Override
  70. public void showColourPicker(final boolean irc, final boolean hex) {
  71. if (globalConfig.getOptionBool("general", "showcolourdialog")) {
  72. colourPicker = new ColourPickerDialog(this, colourManager,
  73. iconManager, irc, hex, parentWindow);
  74. colourPicker.addActionListener(actionEvent -> {
  75. try {
  76. getDocument().insertString(getCaretPosition(),
  77. actionEvent.getActionCommand(), null);
  78. } catch (final BadLocationException ex) {
  79. //Ignore, wont happen
  80. }
  81. colourPicker.dispose();
  82. colourPicker = null;
  83. });
  84. colourPicker.setVisible(true);
  85. colourPicker.setLocation((int) getLocationOnScreen().getX(),
  86. (int) getLocationOnScreen().getY()
  87. - colourPicker.getHeight());
  88. }
  89. }
  90. @Override
  91. public void hideColourPicker() {
  92. if (colourPicker != null) {
  93. colourPicker.dispose();
  94. colourPicker = null;
  95. }
  96. }
  97. @Override
  98. public void addActionListener(final ActionListener listener) {
  99. //Ignore
  100. }
  101. @Override
  102. public void removeActionListener(final ActionListener listener) {
  103. //Ignore
  104. }
  105. @Override
  106. protected void setUI(final ComponentUI newUI) {
  107. setUI(new BasicEditorPaneUI());
  108. setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
  109. }
  110. @Override
  111. public void updateUI() {
  112. setUI(new BasicEditorPaneUI());
  113. setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
  114. }
  115. @Override
  116. public void propertyChange(final PropertyChangeEvent evt) {
  117. if (!isFocusOwner()) {
  118. hideColourPicker();
  119. }
  120. }
  121. }