View Javadoc

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  public class StaticImageLoader {
40  
41      public static void loadResources(Class holder, String name) {
42          loadResources(JFaceResources.getImageRegistry(), holder, name);
43      }
44  
45      public static void loadResources(ImageRegistry registry, Class holder,
46              String name) {
47          ResourceBundle bundle = getBundle(name, holder.getClassLoader());
48          if (bundle == null) {
49              return;
50          }
51          BeanDesc holderBd = BeanDescFactory.getBeanDesc(holder);
52          Map pathMap = ResourceBundleUtil.convertMap(bundle);
53          for (int i = 0; i < holderBd.getFieldSize(); i++) {
54              Field field = holderBd.getField(i);
55              String key = field.getName();
56              if (validateMask(field)) {
57                  continue;
58              }
59  
60              if (pathMap.containsKey(key) == false) {
61                  log(key + " not found in " + name);
62                  continue;
63              }
64  
65              ImageDescriptor id = registry.getDescriptor(key);
66              if (id == null) {
67                  id = ImageDescriptor.createFromFile(holder, pathMap.get(key)
68                          .toString());
69                  registry.put(key, id);
70              } else {
71                  log(key + " is already registered [" + holder + "]");
72              }
73  
74              if (isAssignableFrom(ImageDescriptor.class, field)) {
75                  FieldUtil.set(field, null, id);
76              } else if (isAssignableFrom(Image.class, field)) {
77                  FieldUtil.set(field, null, registry.get(key));
78              }
79          }
80      }
81  
82      private static boolean validateMask(Field f) {
83          final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC;
84          final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL;
85          return (f.getModifiers() & MOD_MASK) != MOD_EXPECTED;
86      }
87  
88      private static void log(String msg) {
89          LogUtil.log(ResourcesPlugin.getPlugin(), msg);
90      }
91  
92      private static ResourceBundle getBundle(String name, ClassLoader loader) {
93          try {
94              return ResourceBundle.getBundle(name, Locale.getDefault(), loader);
95          } catch (MissingResourceException e) {
96              return null;
97          }
98      }
99  
100     private static boolean isAssignableFrom(final Class<?> clazz,
101             final Field target) {
102         return clazz.isAssignableFrom(target.getType());
103     }
104 }