01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
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 501094 2007-01-29 16:35:37Z deweese $
34: */
35: public class DefaultBrokenLinkProvider extends BrokenLinkProvider {
36:
37: static Filter brokenLinkImg = null;
38: static final String MESSAGE_RSRC = "resources.Messages";
39:
40: static final Color BROKEN_LINK_COLOR = new Color(255, 255, 255, 190);
41:
42: public static String formatMessage(Object base, String code,
43: Object[] params) {
44: // Should probably cache these...
45: ClassLoader cl = null;
46: try {
47: // Should work always
48: cl = DefaultBrokenLinkProvider.class.getClassLoader();
49: // may not work (depends on security and relationship
50: // of base's class loader to this class's class loader.
51: cl = base.getClass().getClassLoader();
52: } catch (SecurityException se) {
53: }
54: LocalizableSupport ls;
55: ls = new LocalizableSupport(MESSAGE_RSRC, base.getClass(), cl);
56: return ls.formatMessage(code, params);
57: }
58:
59: public Filter getBrokenLinkImage(Object base, String code,
60: Object[] params) {
61: synchronized (DefaultBrokenLinkProvider.class) {
62: if (brokenLinkImg != null)
63: return brokenLinkImg;
64:
65: BufferedImage bi;
66: bi = new BufferedImage(100, 100,
67: BufferedImage.TYPE_INT_ARGB);
68:
69: // Put the broken link property in the image so people know
70: // This isn't the "real" image.
71: Hashtable ht = new Hashtable();
72: ht.put(BROKEN_LINK_PROPERTY, formatMessage(base, code,
73: params));
74: bi = new BufferedImage(bi.getColorModel(), bi.getRaster(),
75: bi.isAlphaPremultiplied(), ht);
76: Graphics2D g2d = bi.createGraphics();
77:
78: g2d.setColor(BROKEN_LINK_COLOR);
79: g2d.fillRect(0, 0, 100, 100);
80: g2d.setColor(Color.black);
81: g2d.drawRect(2, 2, 96, 96);
82: g2d.drawString("Broken Image", 6, 50);
83: g2d.dispose();
84:
85: brokenLinkImg = new RedRable(GraphicsUtil.wrap(bi));
86: return brokenLinkImg;
87: }
88: }
89: }
|