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.

BackgroundPainter.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.textpane;
  18. import com.dmdirc.addons.ui_swing.BackgroundOption;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.config.binding.ConfigBinding;
  21. import com.dmdirc.config.provider.AggregateConfigProvider;
  22. import com.dmdirc.util.URLBuilder;
  23. import java.awt.Graphics;
  24. import java.awt.Graphics2D;
  25. import java.awt.Image;
  26. import javax.annotation.Nonnull;
  27. import javax.swing.JComponent;
  28. import javax.swing.plaf.LayerUI;
  29. /**
  30. * Background painting layer UI. Paints a opaque background then paints the specified image onto the
  31. * background layer.
  32. */
  33. public class BackgroundPainter extends LayerUI<JComponent> {
  34. /** A version for this class, sued for serialisation. */
  35. private static final long serialVersionUID = 1L;
  36. /**
  37. * Domain to retrieve settings from.
  38. */
  39. @Nonnull
  40. private final String domain;
  41. /**
  42. * Key in domain to get image URL from.
  43. */
  44. @Nonnull
  45. private final String imageKey;
  46. /**
  47. * Key in domain to get image background type from.
  48. */
  49. @Nonnull
  50. private final String optionKey;
  51. /**
  52. * Key in the domain to get image opacity level.
  53. */
  54. @Nonnull
  55. private final String opacityKey;
  56. /** The URL builder to use to find icons. */
  57. private final URLBuilder urlBuilder;
  58. /**
  59. * Config manager to bind to and retrieve settings from.
  60. */
  61. private final AggregateConfigProvider configManager;
  62. /**
  63. * Background image.
  64. */
  65. private Image backgroundImage;
  66. /**
  67. * Background option type.
  68. */
  69. private BackgroundOption backgroundOption;
  70. /**
  71. * Background opacity.
  72. */
  73. private float opacity;
  74. /**
  75. * Creates a new background painter.
  76. *
  77. * @param configManager Config manager to retrieve settings from
  78. * @param urlBuilder URL Builder
  79. * @param domain Domain to retrieve settings from
  80. * @param imageKey Key for background image
  81. * @param optionKey Key for background type
  82. * @param opacityKey Key for the opacity
  83. */
  84. public BackgroundPainter(
  85. final AggregateConfigProvider configManager,
  86. final URLBuilder urlBuilder,
  87. @Nonnull final String domain, @Nonnull final String imageKey,
  88. @Nonnull final String optionKey, @Nonnull final String opacityKey) {
  89. this.configManager = configManager;
  90. this.urlBuilder = urlBuilder;
  91. this.domain = domain;
  92. this.imageKey = imageKey;
  93. this.optionKey = optionKey;
  94. this.opacityKey = opacityKey;
  95. configManager.getBinder().bind(this, BackgroundPainter.class);
  96. }
  97. @Nonnull
  98. protected String getDomain() {
  99. return domain;
  100. }
  101. @Nonnull
  102. protected String getImageKey() {
  103. return imageKey;
  104. }
  105. @Nonnull
  106. protected String getOptionKey() {
  107. return optionKey;
  108. }
  109. @Nonnull
  110. protected String getOpacityKey() {
  111. return opacityKey;
  112. }
  113. protected void setBackgroundImage(final Image backgroundImage) {
  114. this.backgroundImage = backgroundImage;
  115. }
  116. /**
  117. * Called to update the value of the image URL.
  118. *
  119. * @param value New image URL
  120. */
  121. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackground")
  122. public void updateImage(final String value) {
  123. if (value == null || value.isEmpty()) {
  124. backgroundImage = null;
  125. } else {
  126. new ImageLoader(urlBuilder.getUrl(value), this).execute();
  127. }
  128. }
  129. /**
  130. * Called to update the value of the background type
  131. *
  132. * @param value New background type
  133. */
  134. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackgroundoption")
  135. public void updateOption(final String value) {
  136. try {
  137. backgroundOption = BackgroundOption.valueOf(value);
  138. } catch (IllegalArgumentException ex) {
  139. backgroundOption = BackgroundOption.CENTER;
  140. }
  141. }
  142. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackgroundopacity")
  143. public void updateOpacity(final String opacity) {
  144. try {
  145. this.opacity = Float.valueOf(opacity);
  146. if (this.opacity < 0 || this.opacity > 1) {
  147. this.opacity = 1;
  148. }
  149. } catch (IllegalArgumentException ex) {
  150. this.opacity = 1;
  151. }
  152. }
  153. @Override
  154. public void paint(final Graphics graphics, final JComponent component) {
  155. final Graphics2D g2 = (Graphics2D) graphics;
  156. g2.setColor(component.getBackground());
  157. g2.fill(g2.getClipBounds());
  158. UIUtilities.paintBackground(g2, component.getBounds(),
  159. backgroundImage, backgroundOption, opacity);
  160. super.paint(graphics, component);
  161. }
  162. /**
  163. * Called to unbind this painter from its config binder.
  164. */
  165. public void unbind() {
  166. configManager.getBinder().unbind(this);
  167. }
  168. }