Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JIconTextField.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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;
  23. import java.awt.Graphics;
  24. import java.awt.Insets;
  25. import java.awt.event.MouseEvent;
  26. import javax.swing.Icon;
  27. import javax.swing.JTextField;
  28. import javax.swing.ToolTipManager;
  29. import javax.swing.UIManager;
  30. /**
  31. * JTextfield capable of displaying an icon and tooltip.
  32. */
  33. public class JIconTextField extends JTextField {
  34. /** A version number for this class, used in serialisation. */
  35. private static final long serialVersionUID = 1L;
  36. /** Insets used by a normal text field. */
  37. private final Insets dummyInsets;
  38. /** Icon to show, or null. */
  39. private Icon icon;
  40. /** Message to show, or null. */
  41. private String message;
  42. public JIconTextField() {
  43. super();
  44. this.icon = null;
  45. this.dummyInsets = UIManager.getBorder("TextField.border").getBorderInsets(new JTextField());
  46. ToolTipManager.sharedInstance().registerComponent(this);
  47. }
  48. public void setIcon(final Icon icon) {
  49. this.icon = icon;
  50. }
  51. public Icon getIcon() {
  52. return icon;
  53. }
  54. public void setMessage(final String message) {
  55. this.message = message;
  56. }
  57. public String getMessage() {
  58. return message;
  59. }
  60. @Override
  61. protected void paintComponent(final Graphics g) {
  62. super.paintComponent(g);
  63. if (this.icon != null) {
  64. final int x = getWidth() - dummyInsets.right - icon.getIconWidth();
  65. setMargin(new Insets(2, 2, 2, getWidth() - x));
  66. icon.paintIcon(this, g, x, ((getHeight() - icon.getIconHeight()) / 2));
  67. } else {
  68. setMargin(new Insets(2, 2, 2, 2));
  69. }
  70. }
  71. @Override
  72. public String getToolTipText(final MouseEvent event) {
  73. if (icon != null && (event.getX() >= getWidth() - dummyInsets.right - dummyInsets.right
  74. - icon.getIconWidth())) {
  75. return message;
  76. }
  77. return super.getToolTipText(event);
  78. }
  79. }