Coverage Report - org.seasar.eclipse.common.util.StaticImageLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
StaticImageLoader
0%
0/35
0%
0/16
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.eclipse.common.util;
 17  
 
 18  
 import java.lang.reflect.Field;
 19  
 import java.lang.reflect.Modifier;
 20  
 import java.util.Locale;
 21  
 import java.util.Map;
 22  
 import java.util.MissingResourceException;
 23  
 import java.util.ResourceBundle;
 24  
 
 25  
 import org.eclipse.core.resources.ResourcesPlugin;
 26  
 import org.eclipse.jface.resource.ImageDescriptor;
 27  
 import org.eclipse.jface.resource.ImageRegistry;
 28  
 import org.eclipse.jface.resource.JFaceResources;
 29  
 import org.eclipse.swt.graphics.Image;
 30  
 import org.seasar.framework.beans.BeanDesc;
 31  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 32  
 import org.seasar.framework.util.FieldUtil;
 33  
 import org.seasar.framework.util.ResourceBundleUtil;
 34  
 
 35  
 /**
 36  
  * @author taichi
 37  
  * 
 38  
  */
 39  0
 public class StaticImageLoader {
 40  
 
 41  
     public static void loadResources(Class holder, String name) {
 42  0
         loadResources(JFaceResources.getImageRegistry(), holder, name);
 43  0
     }
 44  
 
 45  
     public static void loadResources(ImageRegistry registry, Class holder,
 46  
             String name) {
 47  0
         ResourceBundle bundle = getBundle(name, holder.getClassLoader());
 48  0
         if (bundle == null) {
 49  0
             return;
 50  
         }
 51  0
         BeanDesc holderBd = BeanDescFactory.getBeanDesc(holder);
 52  0
         Map pathMap = ResourceBundleUtil.convertMap(bundle);
 53  0
         for (int i = 0; i < holderBd.getFieldSize(); i++) {
 54  0
             Field field = holderBd.getField(i);
 55  0
             String key = field.getName();
 56  0
             if (validateMask(field)) {
 57  0
                 continue;
 58  
             }
 59  
 
 60  0
             if (pathMap.containsKey(key) == false) {
 61  0
                 log(key + " not found in " + name);
 62  0
                 continue;
 63  
             }
 64  
 
 65  0
             ImageDescriptor id = registry.getDescriptor(key);
 66  0
             if (id == null) {
 67  0
                 id = ImageDescriptor.createFromFile(holder, pathMap.get(key)
 68  
                         .toString());
 69  0
                 registry.put(key, id);
 70  
             } else {
 71  0
                 log(key + " is already registered [" + holder + "]");
 72  
             }
 73  
 
 74  0
             if (isAssignableFrom(ImageDescriptor.class, field)) {
 75  0
                 FieldUtil.set(field, null, id);
 76  0
             } else if (isAssignableFrom(Image.class, field)) {
 77  0
                 FieldUtil.set(field, null, registry.get(key));
 78  
             }
 79  
         }
 80  0
     }
 81  
 
 82  
     private static boolean validateMask(Field f) {
 83  0
         final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC;
 84  0
         final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL;
 85  0
         return (f.getModifiers() & MOD_MASK) != MOD_EXPECTED;
 86  
     }
 87  
 
 88  
     private static void log(String msg) {
 89  0
         LogUtil.log(ResourcesPlugin.getPlugin(), msg);
 90  0
     }
 91  
 
 92  
     private static ResourceBundle getBundle(String name, ClassLoader loader) {
 93  
         try {
 94  0
             return ResourceBundle.getBundle(name, Locale.getDefault(), loader);
 95  0
         } catch (MissingResourceException e) {
 96  0
             return null;
 97  
         }
 98  
     }
 99  
 
 100  
     private static boolean isAssignableFrom(final Class<?> clazz,
 101  
             final Field target) {
 102  0
         return clazz.isAssignableFrom(target.getType());
 103  
     }
 104  
 }