View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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.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      //reuse ERROR icon for FATAL level
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      //reuse DEBUG icon for TRACE level
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  }