1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.lang.reflect.InvocationTargetException;
|
---|
7 | import java.lang.reflect.Method;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.expeditee.io.Conversion;
|
---|
13 | import org.expeditee.items.DotType;
|
---|
14 | import org.expeditee.items.Item;
|
---|
15 | import org.expeditee.items.Justification;
|
---|
16 | import org.expeditee.items.PermissionPair;
|
---|
17 | import org.expeditee.items.Picture;
|
---|
18 | import org.expeditee.items.Text;
|
---|
19 | import org.expeditee.simple.IncorrectTypeException;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This class provides the methods to extract and set attributes of Items and
|
---|
23 | * Frames. These methods are called when a user merges a text item with
|
---|
24 | * <code>Attribute: Value</code> pairs.
|
---|
25 | *
|
---|
26 | * @author jdm18
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class AttributeUtils {
|
---|
30 |
|
---|
31 | // private static final int GET_LENGTH = "get".length();
|
---|
32 |
|
---|
33 | //private static int SET_LENGTH = "set".length();
|
---|
34 |
|
---|
35 | public static final class Attribute {
|
---|
36 | public final String displayName;
|
---|
37 | public final Method getter;
|
---|
38 | public final Method setter;
|
---|
39 |
|
---|
40 | public Attribute(String displayName, Method getter, Method setter) {
|
---|
41 | this.displayName = displayName;
|
---|
42 | this.getter = getter;
|
---|
43 | this.setter = setter;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public static final class AttributeSet {
|
---|
48 |
|
---|
49 | // the internal hashmap
|
---|
50 | private final HashMap<String, Attribute> map = new HashMap<String, Attribute>(256);
|
---|
51 | // a list of keys in the order they were added (used to make attribute extraction consistent)
|
---|
52 | public final List<String> keys = new LinkedList<String>();
|
---|
53 |
|
---|
54 | public void put(String attributeName, Method getter, Method setter) {
|
---|
55 | if(map.containsKey(attributeName.toLowerCase())) {
|
---|
56 | System.err.println(this + " already contains key '" + attributeName + "', overwriting value!");
|
---|
57 | } else {
|
---|
58 | // we keep an ordered list of attributes for extraction
|
---|
59 | keys.add(attributeName.toLowerCase());
|
---|
60 | }
|
---|
61 | map.put(attributeName.toLowerCase(), new Attribute(attributeName, getter, setter));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Create a second reference the the same Attribute, using a different name
|
---|
66 | * Does not modify the list of keys
|
---|
67 | */
|
---|
68 | public void alias(String alias, String name) {
|
---|
69 | if(map.containsKey(name.toLowerCase())) {
|
---|
70 | map.put(alias.toLowerCase(), map.get(name.toLowerCase()));
|
---|
71 | } else {
|
---|
72 | System.err.println("Cannot add alias '" + alias + "', because key '" + name + "' does not exist!");
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void putAll(AttributeSet toAdd) {
|
---|
77 | keys.addAll(toAdd.keys);
|
---|
78 | map.putAll(toAdd.map);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Attribute remove(String name) {
|
---|
82 | keys.remove(name);
|
---|
83 | return map.remove(name);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public boolean containsKey(String key) {
|
---|
87 | return map.containsKey(key);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public Attribute get(String key) {
|
---|
91 | return map.get(key);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | // Frames
|
---|
97 | // Common list of attributes
|
---|
98 | public static final AttributeSet _FrameAttrib = new AttributeSet();
|
---|
99 | // List of attributes that may be set
|
---|
100 | public static final AttributeSet _FrameAttribSet = new AttributeSet();
|
---|
101 | // List of attributes that should be saved
|
---|
102 | public static final AttributeSet _FrameAttribSave = new AttributeSet();
|
---|
103 | // Items
|
---|
104 | // Common list of attributes
|
---|
105 | public static final AttributeSet _Attrib = new AttributeSet();
|
---|
106 | // List of attributes that may be set
|
---|
107 | public static final AttributeSet _AttribSet = new AttributeSet();
|
---|
108 | // List of attributes that should be saved
|
---|
109 | public static final AttributeSet _AttribSave = new AttributeSet();
|
---|
110 |
|
---|
111 | // List of method names to ignore when searching for a match
|
---|
112 | // private static List<Method> _GetMethods = null;
|
---|
113 |
|
---|
114 | // private static HashMap<String, Method> _SetMethods = null;
|
---|
115 |
|
---|
116 | // List of attributes which are ignored when extracting attributes
|
---|
117 | private static List<String> _IgnoreGet = null;
|
---|
118 | // List of attributes which are ignored when setting attributes,
|
---|
119 | // if multiple attributes are being set at once
|
---|
120 | private static List<String> _IgnoreSet = null;
|
---|
121 |
|
---|
122 | /***************************************************************************
|
---|
123 | * List of method names to show in extraced lists even when they return null
|
---|
124 | * (Null is often used to indicate the default value is used)
|
---|
125 | **************************************************************************/
|
---|
126 | private static List<Method> _AllowNull = null;
|
---|
127 |
|
---|
128 | // private static HashMap<String, String> _Abbreviations = null;
|
---|
129 |
|
---|
130 | public static void ensureReady() {
|
---|
131 | if(_IgnoreSet == null) {
|
---|
132 | initLists();
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Initialises the _Ignore and _AllowNull lists.
|
---|
138 | */
|
---|
139 | private static void initLists() {
|
---|
140 |
|
---|
141 | try {
|
---|
142 |
|
---|
143 | Class<?>[] pPoint = { Point.class };
|
---|
144 | Class<?>[] pString = { String.class };
|
---|
145 | Class<?>[] pInt = { int.class };
|
---|
146 | Class<?>[] pIntO = { Integer.class };
|
---|
147 | Class<?>[] pFloat = { float.class };
|
---|
148 | Class<?>[] pFloatO = { Float.class };
|
---|
149 | Class<?>[] pColor = { Color.class };
|
---|
150 | Class<?>[] pBool = { boolean.class };
|
---|
151 | //Class[] pDouble = { double.class };
|
---|
152 | //Class[] pDoubleO = { Double.class };
|
---|
153 | Class<?>[] pArrow = { float.class, double.class, double.class };
|
---|
154 | Class<?>[] pList = { List.class };
|
---|
155 | Class<?>[] pIntArray = { int[].class };
|
---|
156 | Class<?>[] pJustification = { Justification.class };
|
---|
157 | Class<?>[] pPermission = { PermissionPair.class };
|
---|
158 | Class<?>[] pDotType = { DotType.class };
|
---|
159 |
|
---|
160 | _IgnoreSet = new LinkedList<String>();
|
---|
161 | _IgnoreGet = new LinkedList<String>();
|
---|
162 | _AllowNull = new LinkedList<Method>();
|
---|
163 |
|
---|
164 | // TODO load these in with reflection...
|
---|
165 | // Set the shortcuts with annotation tags on the methods
|
---|
166 | _IgnoreSet.add("date");
|
---|
167 | _IgnoreSet.add("datecreated");
|
---|
168 | _IgnoreSet.add("d");
|
---|
169 | _IgnoreSet.add("link");
|
---|
170 | _IgnoreSet.add("l");
|
---|
171 | _IgnoreSet.add("action");
|
---|
172 | _IgnoreSet.add("a");
|
---|
173 | _IgnoreSet.add("position");
|
---|
174 | _IgnoreSet.add("pos");
|
---|
175 | _IgnoreSet.add("p");
|
---|
176 | _IgnoreSet.add("x");
|
---|
177 | _IgnoreSet.add("y");
|
---|
178 |
|
---|
179 | _IgnoreGet.add("x");
|
---|
180 | _IgnoreGet.add("y");
|
---|
181 | _IgnoreGet.add("text");
|
---|
182 | _IgnoreGet.add("gradientangle");
|
---|
183 |
|
---|
184 | _AllowNull.add(Item.class.getMethod("getColor"));
|
---|
185 | _AllowNull.add(Item.class.getMethod("getBackgroundColor"));
|
---|
186 |
|
---|
187 | _AllowNull.add(Frame.class.getMethod("getBackgroundColor"));
|
---|
188 | _AllowNull.add(Frame.class.getMethod("getForegroundColor"));
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Populate the backing lists of attributes
|
---|
192 | */
|
---|
193 |
|
---|
194 | // Frames
|
---|
195 | _FrameAttrib.put("Permission", Frame.class.getMethod("getPermission"),
|
---|
196 | Frame.class.getMethod("setPermission", pPermission));
|
---|
197 | _FrameAttrib.put("Owner", Frame.class.getMethod("getOwner"),
|
---|
198 | Frame.class.getMethod("setOwner", pString));
|
---|
199 | _FrameAttrib.put("DateCreated", Frame.class.getMethod("getDateCreated"),
|
---|
200 | Frame.class.getMethod("setDateCreated", pString));
|
---|
201 | _FrameAttrib.put("LastModifyUser", Frame.class.getMethod("getLastModifyUser"),
|
---|
202 | Frame.class.getMethod("setLastModifyUser", pString));
|
---|
203 | _FrameAttrib.put("LastModifyDate", Frame.class.getMethod("getLastModifyDate"),
|
---|
204 | Frame.class.getMethod("setLastModifyDate", pString));
|
---|
205 | _FrameAttrib.put("ForegroundColor", Frame.class.getMethod("getForegroundColor"),
|
---|
206 | Frame.class.getMethod("setForegroundColor", pColor));
|
---|
207 | _FrameAttrib.put("BackgroundColor", Frame.class.getMethod("getBackgroundColor"),
|
---|
208 | Frame.class.getMethod("setBackgroundColor", pColor));
|
---|
209 |
|
---|
210 | // FrameAttribSet has some stuff removed (so users can't set the creation date), and some aliases
|
---|
211 | _FrameAttribSet.putAll(_FrameAttrib);
|
---|
212 | _FrameAttribSet.remove("DateCreated");
|
---|
213 | _FrameAttribSet.remove("LastModifyUser");
|
---|
214 | _FrameAttribSet.remove("LastModifyDate");
|
---|
215 | _FrameAttribSet.alias("fgc", "foregroundcolor");
|
---|
216 | _FrameAttribSet.alias("bgc", "backgroundcolor");
|
---|
217 | _FrameAttribSet.alias("p", "permission");
|
---|
218 |
|
---|
219 | // FrameAttribSave has some extra stuff
|
---|
220 | _FrameAttribSave.putAll(_FrameAttrib);
|
---|
221 | _FrameAttribSave.put("Version", Frame.class.getMethod("getVersion"),
|
---|
222 | Frame.class.getMethod("setVersion", pInt));
|
---|
223 | _FrameAttribSave.put("FrozenDate", Frame.class.getMethod("getFrozenDate"),
|
---|
224 | Frame.class.getMethod("setFrozenDate", pString));
|
---|
225 |
|
---|
226 | // Generic Items
|
---|
227 | _Attrib.put("DateCreated", Item.class.getMethod("getDateCreated"),
|
---|
228 | Item.class.getMethod("setDateCreated", pString));
|
---|
229 | _Attrib.put("Color", Item.class.getMethod("getColor"),
|
---|
230 | Item.class.getMethod("setColor", pColor));
|
---|
231 | _Attrib.put("BackgroundColor", Item.class.getMethod("getBackgroundColor"),
|
---|
232 | Item.class.getMethod("setBackgroundColor", pColor));
|
---|
233 | _Attrib.put("BorderColor", Item.class.getMethod("getBorderColor"),
|
---|
234 | Item.class.getMethod("setBorderColor", pColor));
|
---|
235 | _Attrib.put("AnchorRight", Item.class.getMethod("getAnchorRight"),
|
---|
236 | Item.class.getMethod("setAnchorRight", pFloatO));
|
---|
237 | _Attrib.put("AnchorBottom", Item.class.getMethod("getAnchorBottom"),
|
---|
238 | Item.class.getMethod("setAnchorBottom", pFloatO));
|
---|
239 | _Attrib.put("Position", Item.class.getMethod("getPosition"),
|
---|
240 | Item.class.getMethod("setPosition", pPoint));
|
---|
241 | _Attrib.put("Text", Item.class.getMethod("getText"),
|
---|
242 | Item.class.getMethod("setText", pString));
|
---|
243 | _Attrib.put("Link", Item.class.getMethod("getLink"),
|
---|
244 | Item.class.getMethod("setLink", pString));
|
---|
245 | _Attrib.put("Action", Item.class.getMethod("getAction"),
|
---|
246 | Item.class.getMethod("setActions", pList));
|
---|
247 | _Attrib.put("ActionMark", Item.class.getMethod("getActionMark"),
|
---|
248 | Item.class.getMethod("setActionMark", pBool));
|
---|
249 | _Attrib.put("ActionCursorEnter", Item.class.getMethod("getActionCursorEnter"),
|
---|
250 | Item.class.getMethod("setActionCursorEnter", pList));
|
---|
251 | _Attrib.put("ActionCursorLeave", Item.class.getMethod("getActionCursorLeave"),
|
---|
252 | Item.class.getMethod("setActionCursorLeave", pList));
|
---|
253 | _Attrib.put("ActionEnterFrame", Item.class.getMethod("getActionEnterFrame"),
|
---|
254 | Item.class.getMethod("setActionEnterFrame", pList));
|
---|
255 | _Attrib.put("ActionLeaveFrame", Item.class.getMethod("getActionLeaveFrame"),
|
---|
256 | Item.class.getMethod("setActionLeaveFrame", pList));
|
---|
257 | _Attrib.put("Data", Item.class.getMethod("getData"),
|
---|
258 | Item.class.getMethod("setData", pList));
|
---|
259 | _Attrib.put("Highlight", Item.class.getMethod("getHighlight"),
|
---|
260 | Item.class.getMethod("setHighlight", pBool));
|
---|
261 | _Attrib.put("FillColor", Item.class.getMethod("getFillColor"),
|
---|
262 | Item.class.getMethod("setFillColor", pColor));
|
---|
263 | _Attrib.put("GradientColor", Item.class.getMethod("getGradientColor"),
|
---|
264 | Item.class.getMethod("setGradientColor", pColor));
|
---|
265 | _Attrib.put("GradientAngle", Item.class.getMethod("getGradientAngle"),
|
---|
266 | Item.class.getMethod("setGradientAngle", pInt));
|
---|
267 | _Attrib.put("FillPattern", Item.class.getMethod("getFillPattern"),
|
---|
268 | Item.class.getMethod("setFillPattern", pString));
|
---|
269 | _Attrib.put("Owner", Item.class.getMethod("getOwner"),
|
---|
270 | Item.class.getMethod("setOwner", pString));
|
---|
271 | _Attrib.put("LinkMark", Item.class.getMethod("getLinkMark"),
|
---|
272 | Item.class.getMethod("setLinkMark", pBool));
|
---|
273 | _Attrib.put("LinkFrameset", Item.class.getMethod("getLinkFrameset"),
|
---|
274 | Item.class.getMethod("setLinkFrameset", pString));
|
---|
275 | _Attrib.put("LinkTemplate", Item.class.getMethod("getLinkTemplate"),
|
---|
276 | Item.class.getMethod("setLinkTemplate", pString));
|
---|
277 | _Attrib.put("LinePattern", Item.class.getMethod("getLinePattern"),
|
---|
278 | Item.class.getMethod("setLinePattern", pIntArray));
|
---|
279 | _Attrib.put("Arrow", Item.class.getMethod("getArrow"),
|
---|
280 | Item.class.getMethod("setArrow", pArrow));
|
---|
281 | _Attrib.put("DotType", Item.class.getMethod("getDotType"),
|
---|
282 | Item.class.getMethod("setDotType", pDotType));
|
---|
283 | _Attrib.put("Filled", Item.class.getMethod("getFilled"),
|
---|
284 | Item.class.getMethod("setFilled", pBool));
|
---|
285 | _Attrib.put("Formula", Item.class.getMethod("getFormula"),
|
---|
286 | Item.class.getMethod("setFormula", pString));
|
---|
287 | _Attrib.put("Thickness", Item.class.getMethod("getThickness"),
|
---|
288 | Item.class.getMethod("setThickness", pFloat));
|
---|
289 | _Attrib.put("LineIDs", Item.class.getMethod("getLineIDs"),
|
---|
290 | Item.class.getMethod("setLineIDs", pString));
|
---|
291 | _Attrib.put("ConstraintIDs", Item.class.getMethod("getConstraintIDs"),
|
---|
292 | Item.class.getMethod("setConstraintIDs", pString));
|
---|
293 | _Attrib.put("Size", Item.class.getMethod("getSize"),
|
---|
294 | Item.class.getMethod("setSize", pFloat));
|
---|
295 | _Attrib.put("Save", Item.class.getMethod("getSave"),
|
---|
296 | Item.class.getMethod("setSave", pBool));
|
---|
297 | _Attrib.put("AutoStamp", Item.class.getMethod("getAutoStamp"),
|
---|
298 | Item.class.getMethod("setAutoStamp", pFloatO));
|
---|
299 | _Attrib.put("Width", Item.class.getMethod("getWidthToSave"),
|
---|
300 | Item.class.getMethod("setWidth", pIntO));
|
---|
301 |
|
---|
302 | // Text Items
|
---|
303 | _Attrib.put("Family", Text.class.getMethod("getFamily"),
|
---|
304 | Text.class.getMethod("setFamily", pString));
|
---|
305 | _Attrib.put("FontStyle", Text.class.getMethod("getFontStyle"),
|
---|
306 | Text.class.getMethod("setFontStyle", pString));
|
---|
307 | _Attrib.put("Justification", Text.class.getMethod("getJustification"),
|
---|
308 | Text.class.getMethod("setJustification", pJustification));
|
---|
309 |
|
---|
310 | // Picture Items
|
---|
311 | _Attrib.put("Scale", Picture.class.getMethod("getScaleToSave"),
|
---|
312 | Picture.class.getMethod("setScale", pFloat));
|
---|
313 | _Attrib.put("CropStart", Picture.class.getMethod("getCropStart"),
|
---|
314 | Picture.class.getMethod("setCropStart", pPoint));
|
---|
315 | _Attrib.put("CropEnd", Picture.class.getMethod("getCropEnd"),
|
---|
316 | Picture.class.getMethod("setCropEnd", pPoint));
|
---|
317 | _Attrib.put("Image", Picture.class.getMethod("getImageSource"),
|
---|
318 | Picture.class.getMethod("setImageSource", pString));
|
---|
319 |
|
---|
320 | // AttribSet has some extra stuff and some aliases
|
---|
321 | _AttribSet.putAll(_Attrib);
|
---|
322 | _AttribSet.put("X", null,
|
---|
323 | Item.class.getMethod("setX", pFloat));
|
---|
324 | _AttribSet.put("Y", null,
|
---|
325 | Item.class.getMethod("setY", pFloat));
|
---|
326 | _AttribSet.alias("pos", "position");
|
---|
327 | _AttribSet.alias("p", "position");
|
---|
328 | _AttribSet.alias("xy", "position");
|
---|
329 | _AttribSet.alias("a", "action");
|
---|
330 | _AttribSet.alias("d", "data");
|
---|
331 | _AttribSet.alias("f", "formula");
|
---|
332 | _AttribSet.alias("s", "size");
|
---|
333 | _AttribSet.alias("l", "link");
|
---|
334 | _AttribSet.alias("ab", "anchorbottom");
|
---|
335 | _AttribSet.alias("ar", "anchorright");
|
---|
336 | _AttribSet.alias("t", "thickness");
|
---|
337 | // _SetAttrib.alias("c", "color"); // breaks circle creation
|
---|
338 | _AttribSet.alias("bgc", "backgroundcolor");
|
---|
339 | _AttribSet.alias("bc", "bordercolor");
|
---|
340 | _AttribSet.alias("fc", "fillcolor");
|
---|
341 | _AttribSet.alias("gc", "gradientcolor");
|
---|
342 | _AttribSet.alias("ga", "gradientangle");
|
---|
343 | _AttribSet.alias("fp", "fillpattern");
|
---|
344 | _AttribSet.alias("lm", "linkmark");
|
---|
345 | _AttribSet.alias("am", "actionmark");
|
---|
346 | _AttribSet.alias("dt", "dottype");
|
---|
347 | _AttribSet.alias("fill", "filled");
|
---|
348 | _AttribSet.alias("lp", "linepattern");
|
---|
349 | _AttribSet.alias("lf", "linkframeset");
|
---|
350 | _AttribSet.alias("lt", "linktemplate");
|
---|
351 | _AttribSet.alias("face", "fontstyle");
|
---|
352 | _AttribSet.alias("j", "justification");
|
---|
353 | _AttribSet.alias("w", "width");
|
---|
354 | _AttribSet.alias("as", "autostamp");
|
---|
355 |
|
---|
356 |
|
---|
357 | // AttribSave has some extra stuff and some stuff removed
|
---|
358 | _AttribSave.putAll(_Attrib);
|
---|
359 | _AttribSave.remove("save");
|
---|
360 | _AttribSave.remove("autostamp");
|
---|
361 | _AttribSave.put("Spacing", Text.class.getMethod("getSpacing"),
|
---|
362 | Text.class.getMethod("setSpacing", pInt));
|
---|
363 | _AttribSave.put("WordSpacing", Text.class.getMethod("getWordSpacing"),
|
---|
364 | Text.class.getMethod("setWordSpacing", pInt));
|
---|
365 | _AttribSave.put("LetterSpacing", Text.class.getMethod("getLetterSpacing"),
|
---|
366 | Text.class.getMethod("setLetterSpacing", pInt));
|
---|
367 | _AttribSave.put("InitialSpacing", Text.class.getMethod("getInitialSpacing"),
|
---|
368 | Text.class.getMethod("setInitialSpacing", pInt));
|
---|
369 |
|
---|
370 |
|
---|
371 | } catch (SecurityException e) {
|
---|
372 | // TODO Auto-generated catch block
|
---|
373 | e.printStackTrace();
|
---|
374 | } catch (NoSuchMethodException e) {
|
---|
375 | // TODO Auto-generated catch block
|
---|
376 | e.printStackTrace();
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Extracts a list of attributes from the given Item. Any method that
|
---|
382 | * starts with <code>get</code>, takes no arguments and is not found in
|
---|
383 | * the Ignore list will be run, All the attributes are then put into a Text
|
---|
384 | * Item of the form <Name>:<Value> If the value returned by the get method
|
---|
385 | * is null, then the attribute will not be included, unless the name of the
|
---|
386 | * method is found in the AllowNull list.
|
---|
387 | *
|
---|
388 | * @param toExtract
|
---|
389 | * The Object from which to extract the attributes
|
---|
390 | * @return A Text Item containing the extracted Attributes.
|
---|
391 | */
|
---|
392 | public static Item extractAttributes(Object toExtract) {
|
---|
393 |
|
---|
394 | // System.out.println(toExtract);
|
---|
395 |
|
---|
396 | if (toExtract == null) {
|
---|
397 | return null;
|
---|
398 | }
|
---|
399 |
|
---|
400 | // Ensure the lists are populated
|
---|
401 | ensureReady();
|
---|
402 |
|
---|
403 | AttributeSet as = null;
|
---|
404 | if(toExtract instanceof Frame) {
|
---|
405 | as = _FrameAttrib;
|
---|
406 | } else if(toExtract instanceof Item) {
|
---|
407 | as = _Attrib;
|
---|
408 | } else {
|
---|
409 | throw new IncorrectTypeException("toExtract", "Item | Frame");
|
---|
410 | }
|
---|
411 |
|
---|
412 | // StringBuffer to store all the extracted Attribute:Value pairs
|
---|
413 | StringBuffer attributes = new StringBuffer();
|
---|
414 |
|
---|
415 | // iterate through the list of methods
|
---|
416 | for (String prop : as.keys) {
|
---|
417 |
|
---|
418 | Attribute a = as.get(prop);
|
---|
419 | // Make sure the classes of the methods match the item
|
---|
420 | if (a != null && a.getter.getDeclaringClass().isAssignableFrom(toExtract.getClass())) {
|
---|
421 |
|
---|
422 | try {
|
---|
423 | String s = getValue(prop, a, toExtract, true);
|
---|
424 |
|
---|
425 | if (s == null)
|
---|
426 | continue;
|
---|
427 | // Append the attributes
|
---|
428 | attributes.append(a.displayName)
|
---|
429 | .append(AttributeValuePair.SEPARATOR_STRING)
|
---|
430 | .append(s).append('\n');
|
---|
431 | } catch (Exception e) {
|
---|
432 | // TODO Auto-generated catch block
|
---|
433 | e.printStackTrace();
|
---|
434 | }
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | // if no attributes were extracted
|
---|
439 | if (attributes.length() <= 0)
|
---|
440 | return null;
|
---|
441 |
|
---|
442 | while (attributes.charAt(attributes.length() - 1) == '\n')
|
---|
443 | attributes.delete(attributes.length() - 1, attributes.length());
|
---|
444 |
|
---|
445 | // create the text Item
|
---|
446 | Frame current = DisplayIO.getCurrentFrame();
|
---|
447 | Item attribs = current.getStatsTextItem(attributes.toString());
|
---|
448 | return attribs;
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Gets a string form of the value for a given item get method.
|
---|
453 | * @param method
|
---|
454 | * @param item
|
---|
455 | * @param ignore true if the attributes in the IGNORE list should be ignored
|
---|
456 | * @return
|
---|
457 | */
|
---|
458 | private static String getValue(String name, Attribute a, Object item, boolean ignore) {
|
---|
459 | // assert(method.getName().startsWith("get"));
|
---|
460 |
|
---|
461 | Object o = null;
|
---|
462 | try {
|
---|
463 | o = a.getter.invoke(item, (Object[]) null);
|
---|
464 | } catch (IllegalArgumentException e) {
|
---|
465 | e.printStackTrace();
|
---|
466 | return null;
|
---|
467 | } catch (IllegalAccessException e) {
|
---|
468 | e.printStackTrace();
|
---|
469 | return null;
|
---|
470 | } catch (InvocationTargetException e) {
|
---|
471 | e.printStackTrace();
|
---|
472 | return null;
|
---|
473 | }
|
---|
474 |
|
---|
475 | if (o == null) {
|
---|
476 | // methods that return null are only included if they
|
---|
477 | // are in the AllowNull list
|
---|
478 | if (_AllowNull.contains(a.getter)) {
|
---|
479 | if (name.equals("color"))
|
---|
480 | o = "default";
|
---|
481 | else if (name.equals("backgroundcolor"))
|
---|
482 | o = "transparent";
|
---|
483 | else if (name.equals("foregroundcolor"))
|
---|
484 | o = "auto";
|
---|
485 | else
|
---|
486 | o = "";
|
---|
487 | } else {
|
---|
488 | return null;
|
---|
489 | }
|
---|
490 | }
|
---|
491 | // skip methods that are in the ignore lists
|
---|
492 | if (ignore && _IgnoreGet.contains(name)) {
|
---|
493 | return null;
|
---|
494 | }
|
---|
495 |
|
---|
496 | if (o instanceof Integer) {
|
---|
497 | Integer i = (Integer) o;
|
---|
498 | if (i == Item.DEFAULT_INTEGER)
|
---|
499 | return null;
|
---|
500 | if (a.getter.getName().endsWith("Justification")
|
---|
501 | && ((Justification) o).toString() != null)
|
---|
502 | o = ((Justification) o).toString();
|
---|
503 | // -1 indicates default value
|
---|
504 | else
|
---|
505 | o = i;
|
---|
506 | } else if (o instanceof Float) {
|
---|
507 | if (((Float) o) < -0.0001)
|
---|
508 | return null;
|
---|
509 | // Null indicates default
|
---|
510 | // o = Math.round((Float) o);
|
---|
511 | } else if (o instanceof Double) {
|
---|
512 | // -1 indicates default value
|
---|
513 | if (((Double) o) < 0.0001)
|
---|
514 | return null;
|
---|
515 | } else if (o instanceof Color) {
|
---|
516 | // converts the color to the Expeditee code
|
---|
517 | o = Conversion.getExpediteeColorCode((Color) o);
|
---|
518 | if (o == null)
|
---|
519 | return null;
|
---|
520 | } else if (o instanceof Point) {
|
---|
521 | Point p = (Point) o;
|
---|
522 | o = Math.round(p.getX()) + " " + Math.round(p.getY());
|
---|
523 | } else if (o instanceof Font) {
|
---|
524 | Font f = (Font) o;
|
---|
525 |
|
---|
526 | String s = f.getName() + "-";
|
---|
527 | if (f.isPlain())
|
---|
528 | s += "Plain";
|
---|
529 |
|
---|
530 | if (f.isBold())
|
---|
531 | s += "Bold";
|
---|
532 |
|
---|
533 | if (f.isItalic())
|
---|
534 | s += "Italic";
|
---|
535 |
|
---|
536 | s += "-" + f.getSize();
|
---|
537 | o = s;
|
---|
538 | } else if (o instanceof Text) {
|
---|
539 | o = ((Text) o).getFirstLine();
|
---|
540 | } else if (o instanceof List) {
|
---|
541 | List list = (List) o;
|
---|
542 | StringBuffer sb = new StringBuffer();
|
---|
543 | for (Object ob : list)
|
---|
544 | // TODO check that this works ok
|
---|
545 | if (sb.length() == 0) {
|
---|
546 | sb.append(ob);
|
---|
547 | } else {
|
---|
548 | sb.append('\n').append(a.displayName).append(AttributeValuePair.SEPARATOR_STRING).append(ob);
|
---|
549 | }
|
---|
550 | return sb.toString();
|
---|
551 | } else if (o instanceof int[]) {
|
---|
552 | StringBuffer sb = new StringBuffer();
|
---|
553 | int[] values = (int[]) o;
|
---|
554 | for (int i = 0; i < values.length; i++) {
|
---|
555 | sb.append(values[i]).append(' ');
|
---|
556 | }
|
---|
557 | sb.deleteCharAt(sb.length() - 1);
|
---|
558 | o = sb.toString();
|
---|
559 | } else if (o instanceof Boolean) {
|
---|
560 | // true is the default for boolean values
|
---|
561 | if (((Boolean) o).booleanValue())
|
---|
562 | return null;
|
---|
563 | }
|
---|
564 | return o.toString();
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Attempts to set the attribute in the given attribute: value pair. The
|
---|
569 | * value string should be formatted as follows:
|
---|
570 | * <code> Attribute: Value </code> Multiple values can be used if they are
|
---|
571 | * separated by spaces
|
---|
572 | *
|
---|
573 | * @param toSet
|
---|
574 | * The Item or Frame to set the attribute of
|
---|
575 | * @param attribs
|
---|
576 | * The Text item that contains the list of attributes to set
|
---|
577 | * @return True if the attribute(s) were sucessfully set, false otherwise
|
---|
578 | */
|
---|
579 | public static boolean setAttribute(Object toSet, Text attribs) {
|
---|
580 | return setAttribute(toSet, attribs, 1);
|
---|
581 | }
|
---|
582 |
|
---|
583 | public static boolean setAttribute(Object toSet, Text attribs,
|
---|
584 | int minAttributeLength) {
|
---|
585 | // error checking
|
---|
586 | if (toSet == null || attribs == null)
|
---|
587 | return false;
|
---|
588 |
|
---|
589 | ensureReady();
|
---|
590 |
|
---|
591 | // if(attribs.isAnnotation())
|
---|
592 | // return false;
|
---|
593 |
|
---|
594 | // get the list of attribute: value pairs
|
---|
595 | List<String> values = attribs.getTextList();
|
---|
596 | // if no pairs exist, we are done
|
---|
597 | if (values == null || values.size() == 0) {
|
---|
598 | return false;
|
---|
599 | }
|
---|
600 |
|
---|
601 | // loop through all attribute: value pairs
|
---|
602 | for (int i = 0; i < values.size(); i++) {
|
---|
603 | AttributeValuePair avp = new AttributeValuePair(values.get(i),
|
---|
604 | false);
|
---|
605 |
|
---|
606 | // If the first is not an attribute value pair then don't do
|
---|
607 | // attribute merging
|
---|
608 | if (!avp.hasAttribute()
|
---|
609 | || avp.getAttribute().length() < minAttributeLength)
|
---|
610 | return false;
|
---|
611 |
|
---|
612 | // check if the next string is another attribute to merge or a
|
---|
613 | // continuation
|
---|
614 | for (; i < values.size() - 1; i++) {
|
---|
615 | AttributeValuePair nextAvp = new AttributeValuePair(values
|
---|
616 | .get(i + 1), false);
|
---|
617 |
|
---|
618 | // if the next String has a colon, then it may be another
|
---|
619 | // attribute
|
---|
620 | if (nextAvp.hasAttribute()) {
|
---|
621 | // if the attribute is the same as v, then it is a
|
---|
622 | // continuation
|
---|
623 | if (nextAvp.getAttribute().equals(avp.getAttribute())) {
|
---|
624 | // strip the attribute from next
|
---|
625 | avp.appendValue(nextAvp.getValue());
|
---|
626 |
|
---|
627 | // if the attribute is not the same, then it may be a
|
---|
628 | // new method
|
---|
629 | } else {
|
---|
630 | break;
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | // v.append("\n").append(next);
|
---|
635 | }
|
---|
636 |
|
---|
637 | try {
|
---|
638 | if (!setAttribute(toSet, avp, values.size() > 1)) {
|
---|
639 |
|
---|
640 | String stripped = avp.getAttribute();
|
---|
641 | if (!avp.hasPair()) {
|
---|
642 | // This happens when there is an attribute at the start
|
---|
643 | // Then a bunch of plain text
|
---|
644 | return false;
|
---|
645 | } else if (_IgnoreSet.contains(stripped)) {
|
---|
646 | return false;
|
---|
647 | } else if (!(_AttribSet.containsKey(stripped))) {
|
---|
648 | return false;
|
---|
649 | } else {
|
---|
650 | String types = "";
|
---|
651 | for (Class c : _AttribSet.get(stripped).setter.getParameterTypes()) {
|
---|
652 | types += c.getSimpleName() + " ";
|
---|
653 | }
|
---|
654 | MessageBay.warningMessage("Wrong arguments for: '"
|
---|
655 | + avp.getAttribute() + "' expecting "
|
---|
656 | + types.trim() + " found '" + avp.getValue() + "'");
|
---|
657 | }
|
---|
658 | }
|
---|
659 | } catch (AttributeException e) {
|
---|
660 | MessageBay.errorMessage(e.getMessage());
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | return true;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * Sets a single attrubute of a frame or item.
|
---|
669 | *
|
---|
670 | * @param toSet
|
---|
671 | * @param avp
|
---|
672 | * @param isAttributeList
|
---|
673 | * some properties are ignored when attribute list are injected
|
---|
674 | * into an item. These properties are ignored if this param is
|
---|
675 | * true
|
---|
676 | * @return
|
---|
677 | * @throws NoSuchAttributeException
|
---|
678 | */
|
---|
679 | private static boolean setAttribute(Object toSet, AttributeValuePair avp,
|
---|
680 | boolean isAttributeList) throws AttributeException {
|
---|
681 |
|
---|
682 | assert (avp.hasAttribute());
|
---|
683 |
|
---|
684 | // separate attribute and value from string
|
---|
685 | String attribute = avp.getAttribute().toLowerCase();
|
---|
686 |
|
---|
687 | String value = avp.getValue();
|
---|
688 | assert (value != null);
|
---|
689 |
|
---|
690 | // Some properties are ignored when multiple attributes are being set on
|
---|
691 | // an item at the same time
|
---|
692 | if (isAttributeList && _IgnoreSet.contains(attribute)) {
|
---|
693 | // System.out.println("Attribute ignored: " + attribute);
|
---|
694 | return true;
|
---|
695 | }
|
---|
696 |
|
---|
697 | // Separate multiple values if required
|
---|
698 |
|
---|
699 | Attribute a = _AttribSet.get(attribute);
|
---|
700 | // if this is not the name of a method, it may be the name of an agent
|
---|
701 | if (a == null) {
|
---|
702 | // System.out.println("Attrib not found for: " + attribute);
|
---|
703 | return false;
|
---|
704 | }
|
---|
705 |
|
---|
706 | // if there are duplicate methods with the same name
|
---|
707 | List<Method> possibles = new LinkedList<Method>();
|
---|
708 | if (a.setter.getDeclaringClass().isInstance(toSet))
|
---|
709 | possibles.add(a.setter);
|
---|
710 | int i = 0;
|
---|
711 | while (_AttribSet.containsKey(attribute + i)) {
|
---|
712 | Method m = _AttribSet.get(attribute + i).setter;
|
---|
713 | if (m.getDeclaringClass().isAssignableFrom(toSet.getClass())) {
|
---|
714 | possibles.add(m);
|
---|
715 | }
|
---|
716 | i++;
|
---|
717 | }
|
---|
718 |
|
---|
719 | for (Method possible : possibles) {
|
---|
720 | Object current = invokeAttributeGetMethod(avp.getAttribute(), toSet);
|
---|
721 | // find the corresponding get method for this set method
|
---|
722 | // and get the current value of the attribute
|
---|
723 |
|
---|
724 | try {
|
---|
725 | Object[] params = Conversion.Convert(possible, value, current);
|
---|
726 |
|
---|
727 | try {
|
---|
728 | possible.invoke(toSet, params);
|
---|
729 | return true;
|
---|
730 | } catch (IllegalArgumentException e) {
|
---|
731 | // TODO Auto-generated catch block
|
---|
732 | e.printStackTrace();
|
---|
733 | } catch (IllegalAccessException e) {
|
---|
734 | // TODO Auto-generated catch block
|
---|
735 | e.printStackTrace();
|
---|
736 | } catch (InvocationTargetException e) {
|
---|
737 | MessageBay.displayMessage(toSet.getClass().getSimpleName()
|
---|
738 | + " type does not support that attribute.");
|
---|
739 | // e.printStackTrace();
|
---|
740 | }
|
---|
741 | } catch (NumberFormatException e) {
|
---|
742 |
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | if(possibles.size() == 0){
|
---|
747 | if(invokeAttributeGetMethod(avp.getAttribute(), toSet) == null)
|
---|
748 | throw new NoSuchAttributeException(avp.getAttribute(), toSet.getClass().getSimpleName());
|
---|
749 | throw new ReadOnlyAttributeException(avp.getAttribute(), toSet.getClass().getSimpleName());
|
---|
750 | }
|
---|
751 |
|
---|
752 | return false;
|
---|
753 | }
|
---|
754 |
|
---|
755 | private static Object invokeAttributeGetMethod(String name, Object toSet) {
|
---|
756 |
|
---|
757 | Attribute a = _Attrib.get(name.toLowerCase());
|
---|
758 | if(a == null) {
|
---|
759 | return null;
|
---|
760 | }
|
---|
761 | try {
|
---|
762 | return a.getter.invoke(toSet);
|
---|
763 | } catch (Exception e) {
|
---|
764 | e.printStackTrace();
|
---|
765 | }
|
---|
766 | return null;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Replaces the current value for the text item with the new value.
|
---|
771 | *
|
---|
772 | * @param text
|
---|
773 | * the item whos value is to be changed
|
---|
774 | * @param newValue
|
---|
775 | * the new value for the item
|
---|
776 | */
|
---|
777 | public static void replaceValue(Text text, String newValue) {
|
---|
778 | assert (newValue != null);
|
---|
779 |
|
---|
780 | AttributeValuePair avp = new AttributeValuePair(text.getText());
|
---|
781 |
|
---|
782 | if (avp.getAttribute() == null) {
|
---|
783 | avp.setAttribute(avp.getValue());
|
---|
784 | }
|
---|
785 | avp.setValue(newValue);
|
---|
786 | text.setText(avp.toString());
|
---|
787 | }
|
---|
788 |
|
---|
789 | public static String getAttribute(Item item, String attribute) {
|
---|
790 |
|
---|
791 | // ensure the lists are populated
|
---|
792 | ensureReady();
|
---|
793 |
|
---|
794 | // separate attribute and value from string
|
---|
795 | String lowerAttribute = attribute.trim().toLowerCase();
|
---|
796 |
|
---|
797 | Attribute a = _Attrib.get(lowerAttribute);
|
---|
798 | if(a == null) {
|
---|
799 | MessageBay.errorMessage("Could no extract unknown attribute value: " + attribute);
|
---|
800 | return null;
|
---|
801 | }
|
---|
802 | return a.displayName + AttributeValuePair.SEPARATOR_STRING + getValue(lowerAttribute, a, item, false);
|
---|
803 | }
|
---|
804 | }
|
---|