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

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