1 /*
2
3    Licensed to the Apache Software Foundation (ASF) under one or more
4    contributor license agreements.  See the NOTICE file distributed with
5    this work for additional information regarding copyright ownership.
6    The ASF licenses this file to You under the Apache License, Version 2.0
7    (the "License"); you may not use this file except in compliance with
8    the License.  You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17
18  */

19 package org.apache.batik.ext.awt.image.spi;
20
21 import java.awt.Color;
22 import java.awt.Graphics2D;
23 import java.awt.image.BufferedImage;
24 import java.util.Hashtable;
25
26 import org.apache.batik.ext.awt.image.GraphicsUtil;
27 import org.apache.batik.ext.awt.image.renderable.Filter;
28 import org.apache.batik.ext.awt.image.renderable.RedRable;
29 import org.apache.batik.i18n.LocalizableSupport;
30
31 /**
32  *
33  * @version $Id: DefaultBrokenLinkProvider.java 1733416 2016-03-03 07:07:13Z gadams $
34  */

35 public class DefaultBrokenLinkProvider
36     extends BrokenLinkProvider {
37
38     static Filter brokenLinkImg = null;
39     static final String MESSAGE_RSRC = "resources.Messages";
40
41     static final Color BROKEN_LINK_COLOR = new Color( 255,255,255,190 );
42
43     public static String formatMessage(Object base,
44                                        String code,
45                                        Object [] params) {
46         // Should probably cache these...
47         ClassLoader cl = null;
48         try {
49             // Should work always
50             cl = DefaultBrokenLinkProvider.class.getClassLoader();
51             // may not work (depends on security and relationship
52             // of base's class loader to this class's class loader.
53             cl = base.getClass().getClassLoader();
54         } catch (SecurityException se) {
55         }
56         LocalizableSupport ls;
57         ls = new LocalizableSupport(MESSAGE_RSRC, base.getClass(), cl);
58         return ls.formatMessage(code, params);
59     }
60
61     public Filter getBrokenLinkImage(Object base,
62                                      String code, Object [] params) {
63         synchronized (DefaultBrokenLinkProvider.class) {
64             if (brokenLinkImg != null)
65                 return brokenLinkImg;
66
67             BufferedImage bi;
68             bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
69
70             // Put the broken link property in the image so people know
71             // This isn't the "real" image.
72             Hashtable ht = new Hashtable();
73             ht.put(BROKEN_LINK_PROPERTY,
74                    formatMessage(base, code, params));
75             bi = new BufferedImage(bi.getColorModel(), bi.getRaster(),
76                                    bi.isAlphaPremultiplied(),
77                                    ht);
78             Graphics2D g2d = bi.createGraphics();
79
80             g2d.setColor( BROKEN_LINK_COLOR );
81             g2d.fillRect(0, 0, 100, 100);
82             g2d.setColor(Color.black);
83             g2d.drawRect(2, 2, 96, 96);
84             g2d.drawString("Broken Image", 6, 50);
85             g2d.dispose();
86
87             brokenLinkImg = new RedRable(GraphicsUtil.wrap(bi));
88             return brokenLinkImg;
89         }
90     }
91 }
92