1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw.icons;
19
20 import java.awt.Image;
21 import java.net.URL;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.swing.ImageIcon;
26 import javax.swing.UIManager;
27 import javax.swing.plaf.metal.MetalLookAndFeel;
28
29
30
31 public class LevelIconFactory {
32 private static final LevelIconFactory instance = new LevelIconFactory();
33 private final Map iconMap = new HashMap();
34
35 private LevelIconFactory() {
36
37 String[] iconFileNames =
38 new String[] { "Warn.gif", "Inform.gif", "Error.gif" , "Error.gif"};
39 String[] iconLabels = new String[] { "WARN", "INFO", "ERROR", "FATAL" };
40
41 for (int i = 0; i < iconLabels.length; i++) {
42 URL resourceURL = UIManager.getLookAndFeel().getClass().getResource(
43 "icons/" + iconFileNames[i]);
44 if(resourceURL == null) {
45 resourceURL = MetalLookAndFeel.class.getResource(
46 "icons/" + iconFileNames[i]);
47 }
48 if(resourceURL == null) {
49 throw new IllegalStateException("Was unable to locate an L&F icon using either the current L&F or the cross platform L&F.");
50 }
51
52 final ImageIcon icon =
53 new ImageIcon(resourceURL);
54 double scalex = .5;
55 double scaley = .5;
56 final int newWidth = (int) (scalex * icon.getIconWidth());
57 final int newHeight = (int) (scaley * icon.getIconHeight());
58 Image iconImage =
59 icon.getImage().getScaledInstance(
60 newWidth, newHeight, Image.SCALE_SMOOTH);
61 iconMap.put(iconLabels[i], new ImageIcon(iconImage));
62 }
63
64 iconMap.put("TRACE", ChainsawIcons.ICON_DEBUG);
65 iconMap.put("DEBUG", ChainsawIcons.ICON_DEBUG);
66 }
67
68 public static final LevelIconFactory getInstance() {
69 return instance;
70 }
71
72 public Map getLevelToIconMap() {
73 return iconMap;
74 }
75 }