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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2006-2013 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
  20. * THE SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.textpane;
  23. import com.dmdirc.addons.ui_swing.BackgroundOption;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  26. import com.dmdirc.config.ConfigBinding;
  27. import com.dmdirc.config.ConfigManager;
  28. import com.dmdirc.util.URLBuilder;
  29. import java.awt.Graphics;
  30. import java.awt.Graphics2D;
  31. import java.awt.Image;
  32. import java.io.IOException;
  33. import java.net.URL;
  34. import java.util.concurrent.ExecutionException;
  35. import javax.imageio.ImageIO;
  36. import javax.swing.JComponent;
  37. import lombok.AccessLevel;
  38. import lombok.AllArgsConstructor;
  39. import lombok.Getter;
  40. import lombok.NonNull;
  41. import lombok.extern.slf4j.Slf4j;
  42. import org.jdesktop.jxlayer.plaf.LayerUI;
  43. /**
  44. * Background painting layer UI. Paints a opaque background then paints the
  45. * specified image onto the background layer.
  46. */
  47. @Slf4j
  48. @SuppressWarnings("unused")
  49. public class BackgroundPainter extends LayerUI<JComponent> {
  50. /**
  51. * Domain to retrieve settings from.
  52. */
  53. @Getter(value = AccessLevel.PROTECTED)
  54. @NonNull
  55. private final String domain;
  56. /**
  57. * Key in domain to get image URL from.
  58. */
  59. @NonNull
  60. @Getter(value = AccessLevel.PROTECTED)
  61. private final String imageKey;
  62. /**
  63. * Key in domain to get image background type from.
  64. */
  65. @NonNull
  66. @Getter(value = AccessLevel.PROTECTED)
  67. private final String optionKey;
  68. /**
  69. * Config manager to bind to and retrieve settings from.
  70. */
  71. private final ConfigManager configManager;
  72. /**
  73. * Background image.
  74. */
  75. private Image backgroundImage;
  76. /**
  77. * Background option type.
  78. */
  79. private BackgroundOption backgroundOption;
  80. /**
  81. * Creates a new background painter.
  82. *
  83. * @param configManager Config manager to retrieve settings from
  84. * @param domain Domain to retrieve settings from
  85. * @param imageKey Key for background image
  86. * @param optionKey Key for background type
  87. */
  88. public BackgroundPainter(final ConfigManager configManager,
  89. final String domain, final String imageKey,
  90. final String optionKey) {
  91. this.configManager = configManager;
  92. this.domain = domain;
  93. this.imageKey = imageKey;
  94. this.optionKey = optionKey;
  95. configManager.getBinder().bind(this, BackgroundPainter.class);
  96. }
  97. /**
  98. * Called to update the value of the image URL.
  99. *
  100. * @param value New image URL
  101. */
  102. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackground")
  103. public void updateImage(final String value) {
  104. if (value == null || value.isEmpty()) {
  105. backgroundImage = null;
  106. } else {
  107. new ImageLoader(URLBuilder.buildURL(value)).executeInExecutor();
  108. }
  109. }
  110. /**
  111. * Called to update the value of the background type
  112. *
  113. * @param value New background type
  114. */
  115. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackgroundoption")
  116. public void updateOption(final String value) {
  117. try {
  118. backgroundOption = BackgroundOption.valueOf(value);
  119. } catch (IllegalArgumentException ex) {
  120. backgroundOption = BackgroundOption.CENTER;
  121. }
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. @Override
  127. public void paint(final Graphics graphics, final JComponent component) {
  128. final Graphics2D g2 = (Graphics2D) graphics;
  129. g2.setColor(component.getBackground());
  130. g2.fill(g2.getClipBounds());
  131. UIUtilities.paintBackground(g2, component.getBounds(),
  132. backgroundImage, backgroundOption);
  133. super.paint(graphics, component);
  134. }
  135. /**
  136. * Called to unbind this painter from its config binder.
  137. */
  138. public void unbind() {
  139. configManager.getBinder().unbind(this);
  140. }
  141. /**
  142. * Loads image URLs in the background.
  143. */
  144. @AllArgsConstructor
  145. private class ImageLoader extends LoggingSwingWorker<Image, Void> {
  146. /**
  147. * URL of image file to load.
  148. */
  149. private final URL imageURL;
  150. /**
  151. * {@inheritDoc}
  152. */
  153. @Override
  154. protected Image doInBackground() {
  155. try {
  156. log.trace("Loading image: {}", imageURL);
  157. if (imageURL != null) {
  158. return ImageIO.read(imageURL);
  159. }
  160. return null;
  161. } catch (IOException ex) {
  162. log.trace("Background loading IOException: {}", ex.getMessage());
  163. return null;
  164. }
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. @Override
  170. protected void done() {
  171. try {
  172. if (isCancelled()) {
  173. log.trace("Background loading cancelled.");
  174. return;
  175. }
  176. log.trace("Background loading complete: {}", get());
  177. backgroundImage = get();
  178. } catch (InterruptedException ex) {
  179. log.debug("Interrupted whilst loading image: {}", imageURL);
  180. } catch (ExecutionException ex) {
  181. log.debug("Exception whilst loading image: {}. "
  182. + "Exception message:", imageURL, ex.getMessage());
  183. }
  184. }
  185. }
  186. }