Bladeren bron

Fixes issue 3596: support background image in desktoppane.

Change-Id: I86b856491379b78eeb8166bcc6a2f0b5e5ebc039
Reviewed-on: http://gerrit.dmdirc.com/578
Automatic-Compile: Gregory Holmes <greboid@dmdirc.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 jaren geleden
bovenliggende
commit
229c3c620a

src/com/dmdirc/addons/ui_swing/textpane/BackgroundOption.java → src/com/dmdirc/addons/ui_swing/BackgroundOption.java Bestand weergeven

@@ -21,7 +21,7 @@
21 21
  * SOFTWARE.
22 22
  */
23 23
 
24
-package com.dmdirc.addons.ui_swing.textpane;
24
+package com.dmdirc.addons.ui_swing;
25 25
 
26 26
 /**
27 27
  * Options available for the background image.

+ 1
- 1
src/com/dmdirc/addons/ui_swing/MainFrame.java Bestand weergeven

@@ -379,7 +379,7 @@ public final class MainFrame extends JFrame implements WindowListener,
379 379
     private void initComponents() {
380 380
         statusBar = new SwingStatusBar(controller, this);
381 381
         frameManagerPanel = new JPanel();
382
-        desktopPane = new DMDircDesktopPane(this);
382
+        desktopPane = new DMDircDesktopPane(this, controller.getDomain());
383 383
 
384 384
         initFrameManagers();
385 385
 

+ 87
- 0
src/com/dmdirc/addons/ui_swing/UIUtilities.java Bestand weergeven

@@ -31,6 +31,9 @@ import com.dmdirc.util.ReturnableThread;
31 31
 
32 32
 import java.awt.Dimension;
33 33
 import java.awt.FontMetrics;
34
+import java.awt.Graphics2D;
35
+import java.awt.Image;
36
+import java.awt.Rectangle;
34 37
 import java.awt.event.KeyEvent;
35 38
 import java.lang.reflect.InvocationTargetException;
36 39
 
@@ -348,4 +351,88 @@ public final class UIUtilities {
348 351
             }
349 352
         });
350 353
     }
354
+
355
+    /**
356
+     * Paints the background, either from the config setting or the background
357
+     * colour of the textpane.
358
+     *
359
+     * @param g Graphics object to draw onto
360
+     * @param bounds
361
+     * @param backgroundImage
362
+     * @param backgroundOption
363
+     */
364
+    public static void paintBackground(final Graphics2D g,
365
+            final Rectangle bounds,
366
+            final Image backgroundImage,
367
+            final BackgroundOption backgroundOption) {
368
+        if (backgroundImage != null) {
369
+            switch (backgroundOption) {
370
+                case TILED:
371
+                    paintTiledBackground(g, bounds, backgroundImage);
372
+                    break;
373
+                case SCALE:
374
+                    paintStretchedBackground(g, bounds, backgroundImage);
375
+                    break;
376
+                case SCALE_ASPECT_RATIO:
377
+                    paintStretchedAspectRatioBackground(g, bounds, backgroundImage);
378
+                    break;
379
+                case CENTER:
380
+                    paintCenterBackground(g, bounds, backgroundImage);
381
+                    break;
382
+                default:
383
+                    break;
384
+            }
385
+        } else {
386
+            paintNoBackground(g, bounds);
387
+        }
388
+    }
389
+
390
+    private static void paintNoBackground(final Graphics2D g, final Rectangle bounds) {
391
+        g.fill(bounds);
392
+    }
393
+
394
+    private static void paintStretchedBackground(final Graphics2D g,
395
+            final Rectangle bounds, final Image backgroundImage) {
396
+        g.drawImage(backgroundImage, 0, 0, bounds.width, bounds.height, null);
397
+    }
398
+
399
+    private static void paintCenterBackground(final Graphics2D g,
400
+            final Rectangle bounds, final Image backgroundImage) {
401
+        final int x = (bounds.width / 2) - (backgroundImage.getWidth(null) / 2);
402
+        final int y = (bounds.height / 2) - (backgroundImage.getHeight(null) / 2);
403
+        g.drawImage(backgroundImage, x, y, backgroundImage.getWidth(null),
404
+                backgroundImage.getWidth(null), null);
405
+    }
406
+
407
+    private static void paintStretchedAspectRatioBackground(final Graphics2D g,
408
+            final Rectangle bounds, final Image backgroundImage) {
409
+        final double widthratio = bounds.width
410
+                / (double) backgroundImage.getWidth(null);
411
+        final double heightratio = bounds.height
412
+                / (double) backgroundImage.getHeight(null);
413
+        final double ratio = Math.min(widthratio, heightratio);
414
+        final int width = (int) (backgroundImage.getWidth(null) * ratio);
415
+        final int height = (int) (backgroundImage.getWidth(null) * ratio);
416
+
417
+        final int x = (bounds.width / 2) - (width / 2);
418
+        final int y = (bounds.height / 2) - (height / 2);
419
+        g.drawImage(backgroundImage, x, y, width, height, null);
420
+    }
421
+
422
+    private static void paintTiledBackground(final Graphics2D g,
423
+            final Rectangle bounds, final Image backgroundImage) {
424
+        final int width = backgroundImage.getWidth(null);
425
+        final int height = backgroundImage.getWidth(null);
426
+
427
+        if (width <= 0 || height <= 0) {
428
+            // ARG!
429
+            return;
430
+        }
431
+
432
+        for (int x = 0; x < bounds.width; x += width) {
433
+            for (int y = 0; y < bounds.height; y += height) {
434
+                g.drawImage(backgroundImage, x, y, width, height, null);
435
+            }
436
+        }
437
+    }
351 438
 }

+ 69
- 4
src/com/dmdirc/addons/ui_swing/components/desktopPane/DMDircDesktopPane.java Bestand weergeven

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.addons.ui_swing.components.desktopPane;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26
+import com.dmdirc.addons.ui_swing.BackgroundOption;
26 27
 import com.dmdirc.addons.ui_swing.MainFrame;
27 28
 import com.dmdirc.addons.ui_swing.UIUtilities;
28 29
 import com.dmdirc.addons.ui_swing.components.TreeScroller;
@@ -30,6 +31,8 @@ import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
30 31
 import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
31 32
 import com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewModel;
32 33
 import com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewNode;
34
+import com.dmdirc.config.IdentityManager;
35
+import com.dmdirc.interfaces.ConfigChangeListener;
33 36
 import com.dmdirc.interfaces.SelectionListener;
34 37
 import com.dmdirc.logger.ErrorLevel;
35 38
 import com.dmdirc.logger.Logger;
@@ -37,15 +40,21 @@ import com.dmdirc.ui.WindowManager;
37 40
 import com.dmdirc.ui.interfaces.Window;
38 41
 import com.dmdirc.ui.interfaces.FrameListener;
39 42
 import com.dmdirc.util.ReturnableThread;
43
+import com.dmdirc.util.URLBuilder;
40 44
 
41 45
 import java.awt.Color;
46
+import java.awt.Graphics;
47
+import java.awt.Graphics2D;
48
+import java.awt.Image;
42 49
 import java.beans.PropertyChangeEvent;
43 50
 import java.beans.PropertyChangeListener;
51
+import java.io.IOException;
44 52
 import java.util.Arrays;
45 53
 import java.util.HashMap;
46 54
 import java.util.Map;
47 55
 import java.util.Stack;
48 56
 import java.util.concurrent.atomic.AtomicBoolean;
57
+import javax.imageio.ImageIO;
49 58
 
50 59
 import javax.swing.BorderFactory;
51 60
 import javax.swing.JComponent;
@@ -61,7 +70,7 @@ import javax.swing.tree.TreeSelectionModel;
61 70
  * DMDirc Extentions to JDesktopPane.
62 71
  */
63 72
 public class DMDircDesktopPane extends JDesktopPane implements FrameListener,
64
-        SelectionListener, PropertyChangeListener {
73
+        SelectionListener, PropertyChangeListener, ConfigChangeListener {
65 74
 
66 75
     /** Logger to use. */
67 76
     private static final java.util.logging.Logger LOGGER =
@@ -96,16 +105,24 @@ public class DMDircDesktopPane extends JDesktopPane implements FrameListener,
96 105
     private AtomicBoolean changing = new AtomicBoolean(false);
97 106
     /** Main Frame. */
98 107
     private MainFrame mainFrame;
108
+    /** Background image. */
109
+    private Image backgroundImage;
110
+    /** Background image option. */
111
+    private BackgroundOption backgroundOption;
112
+    /** Config domain. */
113
+    private String domain;
99 114
 
100 115
     /**
101 116
      * Initialises the DMDirc desktop pane.
102 117
      * 
103 118
      * @param mainFrame Main frame
119
+     * @param domain Config domain
104 120
      */
105
-    public DMDircDesktopPane(final MainFrame mainFrame) {
121
+    public DMDircDesktopPane(final MainFrame mainFrame, final String domain) {
106 122
         super();
107 123
 
108 124
         this.mainFrame = mainFrame;
125
+        this.domain = domain;
109 126
         setBackground(new Color(238, 238, 238));
110 127
         setBorder(BorderFactory.createEtchedBorder());
111 128
         setUI(new ProxyDesktopPaneUI(getUI(), this));
@@ -125,6 +142,23 @@ public class DMDircDesktopPane extends JDesktopPane implements FrameListener,
125 142
         };
126 143
 
127 144
         WindowManager.addFrameListener(this);
145
+        IdentityManager.getGlobalConfig().addChangeListener(domain,
146
+                "desktopbackground", this);
147
+        IdentityManager.getGlobalConfig().addChangeListener(domain,
148
+                "desktopbackgroundoption", this);
149
+
150
+        updateCachedSettings();
151
+    }
152
+
153
+    /** {@inheritDoc} */
154
+    @Override
155
+    public void paintComponent(final Graphics g) {
156
+        if (backgroundImage == null) {
157
+            super.paintComponent(g);
158
+        } else {
159
+            UIUtilities.paintBackground((Graphics2D) g, getBounds(),
160
+                backgroundImage, backgroundOption);
161
+        }
128 162
     }
129 163
 
130 164
     /** {@inheritDoc} */
@@ -227,8 +261,8 @@ public class DMDircDesktopPane extends JDesktopPane implements FrameListener,
227 261
                 final TreeViewNode node = nodes.get(window);
228 262
                 if (node.getLevel() == 0) {
229 263
                     Logger.appError(ErrorLevel.MEDIUM,
230
-                            "delServer triggered for root node" +
231
-                            node.toString(),
264
+                            "delServer triggered for root node"
265
+                            + node.toString(),
232 266
                             new IllegalArgumentException());
233 267
                 } else {
234 268
                     model.removeNodeFromParent(nodes.get(window));
@@ -354,4 +388,35 @@ public class DMDircDesktopPane extends JDesktopPane implements FrameListener,
354 388
         }
355 389
         changing.set(false);
356 390
     }
391
+
392
+    private void updateCachedSettings() {
393
+        final String backgroundPath = IdentityManager.getGlobalConfig().
394
+                getOption(domain, "desktopbackground");
395
+        UIUtilities.invokeAndWait(new Runnable() {
396
+
397
+            /** {@inheritDoc} */
398
+            @Override
399
+            public void run() {
400
+                try {
401
+                    backgroundImage = ImageIO.read(URLBuilder.buildURL(
402
+                            backgroundPath));
403
+                } catch (IOException ex) {
404
+                    backgroundImage = null;
405
+                }
406
+            }
407
+        });
408
+        try {
409
+            backgroundOption = BackgroundOption.valueOf(IdentityManager.
410
+                    getGlobalConfig().getOption(domain,
411
+                    "desktopbackgroundoption"));
412
+        } catch (IllegalArgumentException ex) {
413
+            backgroundOption = BackgroundOption.CENTER;
414
+        }
415
+    }
416
+
417
+    /** {@inheritDoc} */
418
+    @Override
419
+    public void configChanged(final String domain, final String key) {
420
+        updateCachedSettings();
421
+    }
357 422
 }

+ 3
- 1
src/com/dmdirc/addons/ui_swing/plugin.config Bestand weergeven

@@ -35,4 +35,6 @@ defaults:
35 35
     showfulltopic=false
36 36
     hideEmptyTopicBar=false
37 37
     textpanebackground=
38
-    textpanebackgroundoption=stretch
38
+    textpanebackgroundoption=STRETCH
39
+    desktopbackground=
40
+    desktopbackgroundoption=STRETCH

+ 3
- 76
src/com/dmdirc/addons/ui_swing/textpane/TextPaneCanvas.java Bestand weergeven

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.textpane;
24 24
 
25
+import com.dmdirc.addons.ui_swing.BackgroundOption;
25 26
 import com.dmdirc.addons.ui_swing.UIUtilities;
26 27
 import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
27 28
 import com.dmdirc.config.ConfigManager;
@@ -147,7 +148,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
147 148
         }
148 149
         g.setColor(textPane.getBackground());
149 150
         g.fill(g.getClipBounds());
150
-        paintBackground(g);
151
+        UIUtilities.paintBackground(g, getBounds(), backgroundImage,
152
+                backgroundOption);
151 153
         paintOntoGraphics(g);
152 154
         //g.drawImage(buffer, 0, 0, null);
153 155
     }
@@ -162,81 +164,6 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
162 164
         }
163 165
     }
164 166
 
165
-    /**
166
-     * Paints the background, either from the config setting or the background
167
-     * colour of the textpane.
168
-     *
169
-     * @param g Graphics object to draw onto
170
-     */
171
-    private void paintBackground(final Graphics2D g) {
172
-        if (backgroundImage != null) {
173
-            switch (backgroundOption) {
174
-                case TILED:
175
-                    paintTiledBackground(g);
176
-                    break;
177
-                case SCALE:
178
-                    paintStretchedBackground(g);
179
-                    break;
180
-                case SCALE_ASPECT_RATIO:
181
-                    paintStretchedAspectRatioBackground(g);
182
-                    break;
183
-                case CENTER:
184
-                    paintCenterBackground(g);
185
-                    break;
186
-                default:
187
-                    break;
188
-            }
189
-        } else {
190
-            paintNoBackground(g);
191
-        }
192
-    }
193
-
194
-    private void paintNoBackground(final Graphics2D g) {
195
-        g.fill(getBounds());
196
-    }
197
-
198
-    private void paintStretchedBackground(final Graphics2D g) {
199
-        g.drawImage(backgroundImage, 0, 0, getBounds().width,
200
-                getBounds().height, null);
201
-    }
202
-
203
-    private void paintCenterBackground(final Graphics2D g) {
204
-        final int x = (getBounds().width / 2) - (backgroundImage.getWidth(null) / 2);
205
-        final int y = (getBounds().height / 2) - (backgroundImage.getHeight(null) / 2);
206
-        g.drawImage(backgroundImage, x, y, backgroundImage.getWidth(null),
207
-                backgroundImage.getWidth(null), null);
208
-    }
209
-
210
-    private void paintStretchedAspectRatioBackground(final Graphics2D g) {
211
-        final double widthratio = getBounds().width
212
-                / (double) backgroundImage.getWidth(null);
213
-        final double heightratio = getBounds().height
214
-                / (double) backgroundImage.getHeight(null);
215
-        final double ratio = Math.min(widthratio, heightratio);
216
-        final int width = (int) (backgroundImage.getWidth(null) * ratio);
217
-        final int height = (int) (backgroundImage.getWidth(null) * ratio);
218
-
219
-        final int x = (getBounds().width / 2) - (width / 2);
220
-        final int y = (getBounds().height / 2) - (height / 2);
221
-        g.drawImage(backgroundImage, x, y, width, height, null);
222
-    }
223
-
224
-    private void paintTiledBackground(final Graphics2D g) {
225
-        final int width = backgroundImage.getWidth(null);
226
-        final int height = backgroundImage.getWidth(null);
227
-
228
-        if (width <= 0 || height <= 0) {
229
-            // ARG!
230
-            return;
231
-        }
232
-
233
-        for (int x = 0; x < getBounds().width; x += width) {
234
-            for (int y = 0; y < getBounds().height; y += height) {
235
-                g.drawImage(backgroundImage, x, y, width, height, null);
236
-            }
237
-        }
238
-    }
239
-
240 167
     private void updateCachedSettings() {
241 168
         final String backgroundPath = manager.getOption(domain,
242 169
                 "textpanebackground");

Laden…
Annuleren
Opslaan