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