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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /** 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. * Creates a new background painter.
  72. *
  73. * @param configManager Config manager to retrieve settings from
  74. * @param urlBuilder URL Builder
  75. * @param domain Domain to retrieve settings from
  76. * @param imageKey Key for background image
  77. * @param optionKey Key for background type
  78. */
  79. public BackgroundPainter(
  80. final AggregateConfigProvider configManager,
  81. final URLBuilder urlBuilder,
  82. @Nonnull final String domain, @Nonnull final String imageKey,
  83. @Nonnull final String optionKey) {
  84. this.configManager = configManager;
  85. this.urlBuilder = urlBuilder;
  86. this.domain = domain;
  87. this.imageKey = imageKey;
  88. this.optionKey = optionKey;
  89. configManager.getBinder().bind(this, BackgroundPainter.class);
  90. }
  91. @Nonnull
  92. protected String getDomain() {
  93. return domain;
  94. }
  95. @Nonnull
  96. protected String getImageKey() {
  97. return imageKey;
  98. }
  99. @Nonnull
  100. protected String getOptionKey() {
  101. return optionKey;
  102. }
  103. protected void setBackgroundImage(final Image backgroundImage) {
  104. this.backgroundImage = backgroundImage;
  105. }
  106. /**
  107. * Called to update the value of the image URL.
  108. *
  109. * @param value New image URL
  110. */
  111. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackground")
  112. public void updateImage(final String value) {
  113. if (value == null || value.isEmpty()) {
  114. backgroundImage = null;
  115. } else {
  116. new ImageLoader(urlBuilder.getUrl(value), this).execute();
  117. }
  118. }
  119. /**
  120. * Called to update the value of the background type
  121. *
  122. * @param value New background type
  123. */
  124. @ConfigBinding(domain = "plugin-ui_swing", key = "textpanebackgroundoption")
  125. public void updateOption(final String value) {
  126. try {
  127. backgroundOption = BackgroundOption.valueOf(value);
  128. } catch (IllegalArgumentException ex) {
  129. backgroundOption = BackgroundOption.CENTER;
  130. }
  131. }
  132. @Override
  133. public void paint(final Graphics graphics, final JComponent component) {
  134. final Graphics2D g2 = (Graphics2D) graphics;
  135. g2.setColor(component.getBackground());
  136. g2.fill(g2.getClipBounds());
  137. UIUtilities.paintBackground(g2, component.getBounds(),
  138. backgroundImage, backgroundOption);
  139. super.paint(graphics, component);
  140. }
  141. /**
  142. * Called to unbind this painter from its config binder.
  143. */
  144. public void unbind() {
  145. configManager.getBinder().unbind(this);
  146. }
  147. }