Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BackgroundPainter.java 5.9KB

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