Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/util/JarlInfoUtil.java


1   package com.arranger.jarl.util;
2   
3   import com.arranger.jarl.base.IJarlObject;
4   import com.arranger.jarl.base.IJarlObjectInfo;
5   import com.arranger.jarl.base.Time;
6   import com.arranger.jarl.base.BaseJarlObjectInfo;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  import java.awt.geom.Point2D;
11  import java.awt.*;
12  import java.lang.reflect.Field;
13  
14  import org.w3c.dom.Element;
15  
16  /**
17   * JarlInfoUtil
18   *
19   * @see IJarlObjectInfo.IJarlObjectDisplay
20   */
21  public class JarlInfoUtil {
22  
23      public static final IJarlObjectInfo.IJarlObjectDisplay PRIMITIVE_DISPLAY = new PrimitiveObjectDisplay();
24      public static final IJarlObjectInfo.IJarlObjectDisplay TIME_DISPLAY = new TimeObjectDisplay();
25      public static final IJarlObjectInfo.IJarlObjectDisplay COLOR_DISPLAY = new ColorObjectDisplay();
26      public static final IJarlObjectInfo.IJarlObjectDisplay PCT_DISPLAY = new PctObjectDisplay();
27      public static final IJarlObjectInfo.IJarlObjectDisplay COORDS_DISPLAY = new CoordsObjectDisplay();
28      public static final IJarlObjectInfo.IJarlObjectDisplay POINT2D_DISPLAY = new Point2DObjectDisplay();
29  
30      protected static Map m_jarlObjectInfoMap = new HashMap();
31  
32      /**
33       * Works for all primitives
34       */
35      public static class PrimitiveObjectDisplay extends BaseObjectDisplay {
36          protected String _getDisplayValue(Object object) {
37              if (object instanceof Double) {
38                  if (((Double) object).doubleValue() == Double.NEGATIVE_INFINITY) {
39                      return null;
40                  }
41              } else if (object instanceof Integer) {
42                  if (((Integer) object).intValue() == Integer.MAX_VALUE) {
43                      return null;
44                  }
45              } else if (object instanceof Float) {
46                  if (((Float) object).floatValue() == Float.NEGATIVE_INFINITY) {
47                      return null;
48                  }
49              }
50  
51              return object.toString();
52          }
53      }
54  
55      /**
56       * Works for time
57       */
58      public static class TimeObjectDisplay extends BaseObjectDisplay {
59          protected String _getDisplayValue(Object object) {
60              return String.valueOf(((Time) object).getFrame());
61          }
62      }
63  
64      /**
65       * Works for color/paint
66       */
67      public static class ColorObjectDisplay extends BaseObjectDisplay {
68          protected String _getDisplayValue(Object object) {
69              if (object instanceof Color) {
70                  return PaintUtil.getDisplayName((Color) object);
71              } else {
72                  return object.toString();
73              }
74          }
75      }
76  
77      /**
78       * Works for doubles to percentage
79       */
80      public static class PctObjectDisplay extends BaseObjectDisplay {
81          protected String _getDisplayValue(Object object) {
82              if (object instanceof Double) {
83                  return convertToPct(((Double) object).doubleValue());
84              } else if (object instanceof Float) {
85                  return convertToPct(((Float) object).floatValue());
86              } else {
87                  return "Not a float or double";
88              }
89          }
90      }
91  
92      /**
93       * Words for a pair of Points
94       */
95      public static class CoordsObjectDisplay extends BaseObjectDisplay {
96          protected String _getDisplayValue(Object object) {
97              Point2D[] points = (Point2D[]) object;
98              return "[" + convertToPct(points[0].getX()) + ", " + convertToPct(points[0].getY()) + "]" + " " +
99                      "[" + convertToPct(points[1].getX()) + ", " + convertToPct(points[1].getY()) + "]";
100         }
101     }
102 
103     /**
104      * Words for a Point
105      */
106     public static class Point2DObjectDisplay extends BaseObjectDisplay {
107         protected String _getDisplayValue(Object object) {
108             Point2D point = (Point2D) object;
109             return "[" + convertToPct(point.getX()) + ", " + convertToPct(point.getY()) + "]";
110         }
111     }
112 
113     public abstract static class BaseObjectDisplay implements IJarlObjectInfo.IJarlObjectDisplay {
114         protected abstract String _getDisplayValue(Object object);
115 
116         public String getDisplayValue(Object object) {
117             if (object != null) {
118                 return _getDisplayValue(object);
119             }
120             return null;
121         }
122     }
123 
124     /**
125      * Get the registered instance of {@link IJarlObjectInfo} for this object
126      * @param jarlObject
127      * @return the instance or null if not registered yet
128      */
129     public static IJarlObjectInfo getJarlObjectInfo(IJarlObject jarlObject) {
130         return (IJarlObjectInfo) m_jarlObjectInfoMap.get(jarlObject.getClass().getName());
131     }
132 
133     /**
134      * Add a new object info
135      * @param jarlObject
136      * @param jarlObjectInfo
137      */
138     public static void setJarlObjectInfo(IJarlObject jarlObject, IJarlObjectInfo jarlObjectInfo) {
139         m_jarlObjectInfoMap.put(jarlObject.getClass().getName(), jarlObjectInfo);
140     }
141 
142     /**
143      * for example:
144      * <code>
145      *   populateInfo(this, jarlObjectInfo, "zOrder", "Z-Order", JarlInfoUtil.PRIMITIVE_DISPLAY);
146      * </code>
147      *
148      * @param instance
149      * @param jarlObjectInfo
150      * @param fieldName the name of the field without the m_ prefix
151      * @param displayName
152      * @param jarlObjectDisplay
153      * @return the same jarlObjectInfo
154      */
155     public static IJarlObjectInfo populateInfo(Object instance,
156                                                IJarlObjectInfo jarlObjectInfo,
157                                                String fieldName,
158                                                String displayName,
159                                                IJarlObjectInfo.IJarlObjectDisplay jarlObjectDisplay) {
160         try {
161             Field field = ObjectUtil.getField(instance, instance.getClass(), "m_" + fieldName);
162             if (field == null) {
163                 throw new IllegalStateException("Unable to locate field: " + fieldName + " for class: " + instance.getClass().getName());
164             }
165             IJarlObjectInfo newJarlObjectInfo = new BaseJarlObjectInfo(field, displayName, jarlObjectDisplay);
166             if (jarlObjectInfo != null) {
167                 jarlObjectInfo.getChildren().add(newJarlObjectInfo);
168             } else {
169                 return newJarlObjectInfo;
170             }
171         } catch (Exception e) {
172             e.printStackTrace();
173         }
174         return jarlObjectInfo;
175     }
176 
177     protected static String convertToPct(double value) {
178         if (value == Double.NEGATIVE_INFINITY) {
179             return null;
180         }
181         return String.valueOf((float) value * 100f) + "%";
182     }
183 
184     public static String getName(IJarlObject jarlObject) {
185         Element configElem = jarlObject.getConfigElement();
186         String name = configElem.getAttribute("strokeDef");
187         if (!StringTools.isEmpty(name)) {
188             return name;
189         }
190 
191         name = configElem.getAttribute("traitDef");
192         if (!StringTools.isEmpty(name)) {
193             return name;
194         }
195 
196         name = configElem.getAttribute("widgetDef");
197         if (!StringTools.isEmpty(name)) {
198             return name;
199         }
200 
201         name = configElem.getAttribute("filterDef");
202         if (!StringTools.isEmpty(name)) {
203             return name;
204         }
205 
206         return StringTools.getLastSegment(jarlObject.getClass().getName(), ".", false);
207     }
208 }