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 javax.swing.*;
21  import javax.swing.plaf.metal.MetalLookAndFeel;
22  import java.awt.*;
23  import java.net.URL;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   */
29  public class LevelIconFactory {
30      private static final LevelIconFactory instance = new LevelIconFactory();
31      private final Map<String, javax.swing.Icon> iconMap = new HashMap<>();
32  
33      private LevelIconFactory() {
34          //reuse ERROR icon for FATAL level
35          String[] iconFileNames =
36              new String[]{"Warn.gif", "Inform.gif", "Error.gif", "Error.gif"};
37          String[] iconLabels = new String[]{"WARN", "INFO", "ERROR", "FATAL"};
38  
39          for (int i = 0; i < iconLabels.length; i++) {
40              URL resourceURL = UIManager.getLookAndFeel().getClass().getResource(
41                  "icons/" + iconFileNames[i]);
42              if (resourceURL == null) {
43                  resourceURL = MetalLookAndFeel.class.getResource(
44                      "icons/" + iconFileNames[i]);
45              }
46              if (resourceURL == null) {
47                  iconMap.put(iconLabels[i], ChainsawIcons.ICON_DEBUG);
48              } else {
49  
50                  final ImageIcon icon =
51                      new ImageIcon(resourceURL);
52                  double scalex = .5;
53                  double scaley = .5;
54                  final int newWidth = (int) (scalex * icon.getIconWidth());
55                  final int newHeight = (int) (scaley * icon.getIconHeight());
56                  Image iconImage =
57                      icon.getImage().getScaledInstance(
58                          newWidth, newHeight, Image.SCALE_SMOOTH);
59                  iconMap.put(iconLabels[i], new ImageIcon(iconImage));
60              }
61          }
62          //reuse DEBUG icon for TRACE level
63          iconMap.put("TRACE", ChainsawIcons.ICON_DEBUG);
64          iconMap.put("DEBUG", ChainsawIcons.ICON_DEBUG);
65      }
66  
67      public static final LevelIconFactory getInstance() {
68          return instance;
69      }
70  
71      public Map<String, javax.swing.Icon> getLevelToIconMap() {
72          return iconMap;
73      }
74  }