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.

ImageButton.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.Dimension;
  24. import java.awt.Insets;
  25. import javax.swing.BorderFactory;
  26. import javax.swing.Icon;
  27. import javax.swing.JButton;
  28. /**
  29. * Image button, also has the ability to hold an object.
  30. *
  31. * @param <T> Type of object the button holds.
  32. */
  33. public class ImageButton<T> extends JButton {
  34. /**
  35. * A version number for this class. It should be changed whenever the class structure is changed
  36. * (or anything else that would prevent serialized objects being unserialized with the new
  37. * class).
  38. */
  39. private static final long serialVersionUID = 2;
  40. /** Button object. */
  41. private T object;
  42. /**
  43. * Creates a new instance of ImageButton.
  44. *
  45. * @param actionCommand Action command for the button
  46. * @param icon Normal icon for the button
  47. */
  48. public ImageButton(final String actionCommand, final Icon icon) {
  49. this(actionCommand, icon, icon);
  50. }
  51. /**
  52. * Creates a new instance of ImageButton.
  53. *
  54. * @param actionCommand Action command for the button
  55. * @param icon Normal icon for the button
  56. * @param rolloverIcon Rollover icon for the button
  57. */
  58. public ImageButton(final String actionCommand, final Icon icon,
  59. final Icon rolloverIcon) {
  60. this(actionCommand, icon, rolloverIcon, rolloverIcon);
  61. }
  62. /**
  63. * Creates a new instance of ImageButton.
  64. *
  65. * @param actionCommand Action command for the button
  66. * @param icon Normal icon for the button
  67. * @param rolloverIcon Rollover icon for the button
  68. * @param pressedIcon Pressed icon for the button
  69. */
  70. public ImageButton(final String actionCommand, final Icon icon,
  71. final Icon rolloverIcon, final Icon pressedIcon) {
  72. super();
  73. setIcon(icon);
  74. setRolloverIcon(rolloverIcon);
  75. setPressedIcon(pressedIcon);
  76. setContentAreaFilled(false);
  77. setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  78. setMargin(new Insets(0, 0, 0, 0));
  79. setPreferredSize(new Dimension(16, 16));
  80. setActionCommand(actionCommand);
  81. }
  82. /**
  83. * Sets all the image buttons icons.
  84. *
  85. * @param icon New icon
  86. */
  87. public void setIcons(final Icon icon) {
  88. setIcon(icon);
  89. setRolloverIcon(icon);
  90. setPressedIcon(icon);
  91. }
  92. /**
  93. * Sets the object for this button.
  94. *
  95. * @param object Object to hold
  96. */
  97. public void setObject(final T object) {
  98. this.object = object;
  99. }
  100. /**
  101. * Get's this buttons object.
  102. *
  103. * @return This button's object
  104. */
  105. public T getObject() {
  106. return object;
  107. }
  108. }