KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > spi > DefaultBrokenLinkProvider


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

18 package org.apache.batik.ext.awt.image.spi;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.image.BufferedImage JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import org.apache.batik.ext.awt.image.GraphicsUtil;
26 import org.apache.batik.ext.awt.image.renderable.Filter;
27 import org.apache.batik.ext.awt.image.renderable.RedRable;
28 import org.apache.batik.i18n.LocalizableSupport;
29
30 public class DefaultBrokenLinkProvider
31     implements BrokenLinkProvider {
32
33     static Filter brokenLinkImg = null;
34
35     public static String JavaDoc formatMessage(Object JavaDoc base,
36                                        String JavaDoc code,
37                                        Object JavaDoc [] params) {
38         String JavaDoc res = (base.getClass().getPackage().getName() +
39                       ".resources.Messages");
40         // Should probably cache these...
41
ClassLoader JavaDoc cl = null;
42         try {
43             // Should work always
44
cl = DefaultBrokenLinkProvider.class.getClassLoader();
45             // may not work (depends on security and relationship
46
// of base's class loader to this class's class loader.
47
cl = base.getClass().getClassLoader();
48         } catch (SecurityException JavaDoc se) {
49         }
50         LocalizableSupport ls = new LocalizableSupport(res, cl);
51         return ls.formatMessage(code, params);
52     }
53
54     public Filter getBrokenLinkImage(Object JavaDoc base,
55                                      String JavaDoc code, Object JavaDoc [] params) {
56         synchronized (DefaultBrokenLinkProvider.class) {
57             if (brokenLinkImg != null)
58                 return brokenLinkImg;
59
60             BufferedImage JavaDoc bi;
61             bi = new BufferedImage JavaDoc(100, 100, BufferedImage.TYPE_INT_ARGB);
62
63             // Put the broken link property in the image so people know
64
// This isn't the "real" image.
65
Hashtable JavaDoc ht = new Hashtable JavaDoc();
66             ht.put(BROKEN_LINK_PROPERTY,
67                    formatMessage(base, code, params));
68             bi = new BufferedImage JavaDoc(bi.getColorModel(), bi.getRaster(),
69                                    bi.isAlphaPremultiplied(),
70                                    ht);
71             Graphics2D JavaDoc g2d = bi.createGraphics();
72     
73             g2d.setColor(new Color JavaDoc(255,255,255,190));
74             g2d.fillRect(0, 0, 100, 100);
75             g2d.setColor(Color.black);
76             g2d.drawRect(2, 2, 96, 96);
77             g2d.drawString("Broken Image", 6, 50);
78             g2d.dispose();
79
80             brokenLinkImg = new RedRable(GraphicsUtil.wrap(bi));
81             return brokenLinkImg;
82         }
83     }
84 }
85
Popular Tags