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.Item;
|
---|
14 | import org.expeditee.items.Justification;
|
---|
15 | import org.expeditee.items.Permission;
|
---|
16 | import org.expeditee.items.Text;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This class provides the methods to extract and set attributes of Items and
|
---|
20 | * Frames. These methods are called when a user merges a text item with
|
---|
21 | * <code>Attribute: Value</code> pairs.
|
---|
22 | *
|
---|
23 | * @author jdm18
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class AttributeUtils {
|
---|
27 |
|
---|
28 | private static final int GET_LENGTH = "get".length();
|
---|
29 |
|
---|
30 | private static int SET_LENGTH = "set".length();
|
---|
31 |
|
---|
32 | // List of method names to ignore when searching for a match
|
---|
33 | private static List<Method> _GetMethods = null;
|
---|
34 |
|
---|
35 | private static HashMap<String, Method> _SetMethods = null;
|
---|
36 |
|
---|
37 | // List of attributes which are ignored when copying.
|
---|
38 | private static List<String> _Ignore = null;
|
---|
39 |
|
---|
40 | /***************************************************************************
|
---|
41 | * List of method names to show in extraced lists even when they return null
|
---|
42 | * (Null is often used to indicate the default value is used)
|
---|
43 | **************************************************************************/
|
---|
44 | private static List<Method> _AllowNull = null;
|
---|
45 |
|
---|
46 | private static List<String> _ExtractIgnore = null;
|
---|
47 |
|
---|
48 | // private static HashMap<String, String> _Abbreviations = null;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Initialises the _Ignore and _AllowNull lists.
|
---|
52 | */
|
---|
53 | private static void initLists() {
|
---|
54 |
|
---|
55 | Class[] param = {};
|
---|
56 |
|
---|
57 | try {
|
---|
58 | // TODO load these in with reflection...
|
---|
59 | // Set the shortcuts with annotation tags on the methods
|
---|
60 | _Ignore = new LinkedList<String>();
|
---|
61 | _Ignore.add("date");
|
---|
62 | _Ignore.add("datecreated");
|
---|
63 | _Ignore.add("d");
|
---|
64 | _Ignore.add("link");
|
---|
65 | _Ignore.add("l");
|
---|
66 | _Ignore.add("action");
|
---|
67 | _Ignore.add("a");
|
---|
68 | _Ignore.add("position");
|
---|
69 | _Ignore.add("pos");
|
---|
70 | _Ignore.add("p");
|
---|
71 | _Ignore.add("x");
|
---|
72 | _Ignore.add("y");
|
---|
73 |
|
---|
74 | _ExtractIgnore = new LinkedList<String>();
|
---|
75 | _ExtractIgnore.add("x");
|
---|
76 | _ExtractIgnore.add("y");
|
---|
77 |
|
---|
78 | _AllowNull = new LinkedList<Method>();
|
---|
79 | _AllowNull.add(Item.class.getMethod("getColor", param));
|
---|
80 | _AllowNull.add(Item.class.getMethod("getBackgroundColor", param));
|
---|
81 |
|
---|
82 | _AllowNull.add(Frame.class.getMethod("getBackgroundColor", param));
|
---|
83 | _AllowNull.add(Frame.class.getMethod("getForegroundColor", param));
|
---|
84 |
|
---|
85 | _GetMethods = new LinkedList<Method>();
|
---|
86 | _GetMethods.add(Item.class.getMethod("getDateCreated", param));
|
---|
87 |
|
---|
88 | _GetMethods.add(Item.class.getMethod("getColor", param));
|
---|
89 | _GetMethods.add(Item.class.getMethod("getBorderColor", param));
|
---|
90 | _GetMethods.add(Item.class.getMethod("getBackgroundColor", param));
|
---|
91 | _GetMethods.add(Item.class.getMethod("getAction", param));
|
---|
92 | _GetMethods.add(Item.class.getMethod("getData", param));
|
---|
93 | _GetMethods.add(Item.class.getMethod("getLink", param));
|
---|
94 | _GetMethods.add(Item.class.getMethod("getFormula", param));
|
---|
95 | _GetMethods.add(Item.class.getMethod("getFillColor", param));
|
---|
96 | _GetMethods.add(Item.class.getMethod("getGradientColor", param));
|
---|
97 | _GetMethods.add(Item.class.getMethod("getFillPattern", param));
|
---|
98 | _GetMethods.add(Item.class.getMethod("getThickness", param));
|
---|
99 |
|
---|
100 | _GetMethods.add(Item.class.getMethod("getOwner", param));
|
---|
101 | _GetMethods.add(Item.class.getMethod("getLinkMark", param));
|
---|
102 | _GetMethods.add(Item.class.getMethod("getActionMark", param));
|
---|
103 |
|
---|
104 | _GetMethods
|
---|
105 | .add(Item.class.getMethod("getActionCursorEnter", param));
|
---|
106 | _GetMethods
|
---|
107 | .add(Item.class.getMethod("getActionCursorLeave", param));
|
---|
108 | _GetMethods.add(Item.class.getMethod("getActionEnterFrame", param));
|
---|
109 | _GetMethods.add(Item.class.getMethod("getActionLeaveFrame", param));
|
---|
110 |
|
---|
111 | _GetMethods.add(Item.class.getMethod("getTopShadowColor", param));
|
---|
112 | _GetMethods
|
---|
113 | .add(Item.class.getMethod("getBottomShadowColor", param));
|
---|
114 | _GetMethods.add(Item.class.getMethod("getArrow", param));
|
---|
115 |
|
---|
116 | _GetMethods.add(Item.class.getMethod("getLinePattern", param));
|
---|
117 | _GetMethods.add(Item.class.getMethod("getLinkFrameset", param));
|
---|
118 | _GetMethods.add(Item.class.getMethod("getLinkTemplate", param));
|
---|
119 | _GetMethods.add(Item.class.getMethod("getPosition", param));
|
---|
120 | _GetMethods.add(Item.class.getMethod("getX", param));
|
---|
121 | _GetMethods.add(Item.class.getMethod("getY", param));
|
---|
122 |
|
---|
123 | _GetMethods.add(Item.class.getMethod("getAnchorRight", param));
|
---|
124 | _GetMethods.add(Item.class.getMethod("getAnchorBottom", param));
|
---|
125 | _GetMethods.add(Item.class.getMethod("getSize", param));
|
---|
126 |
|
---|
127 | _GetMethods.add(Text.class.getMethod("getFamily", param));
|
---|
128 | _GetMethods.add(Text.class.getMethod("getFontStyle", param));
|
---|
129 | _GetMethods.add(Text.class.getMethod("getJustification", param));
|
---|
130 | _GetMethods.add(Text.class.getMethod("getWidth", param));
|
---|
131 |
|
---|
132 | _GetMethods.add(Frame.class.getMethod("getOwner", param));
|
---|
133 | _GetMethods.add(Frame.class.getMethod("getPermission", param));
|
---|
134 | _GetMethods.add(Frame.class.getMethod("getDateCreated", param));
|
---|
135 | _GetMethods.add(Frame.class.getMethod("getLastModifyUser", param));
|
---|
136 | _GetMethods.add(Frame.class.getMethod("getLastModifyDate", param));
|
---|
137 | _GetMethods.add(Frame.class.getMethod("getForegroundColor", param));
|
---|
138 | _GetMethods.add(Frame.class.getMethod("getBackgroundColor", param));
|
---|
139 |
|
---|
140 | Class[] pPoint = { Point.class };
|
---|
141 | Class[] pString = { String.class };
|
---|
142 | Class[] pInt = { int.class };
|
---|
143 | Class[] pIntO = { Integer.class };
|
---|
144 | Class[] pFloat = { float.class };
|
---|
145 | Class[] pFloatO = { Float.class };
|
---|
146 | Class[] pColor = { Color.class };
|
---|
147 | Class[] pBool = { boolean.class };
|
---|
148 | Class[] pArrow = { float.class, double.class };
|
---|
149 | Class[] pList = { List.class };
|
---|
150 | Class[] pIntArray = { int[].class };
|
---|
151 | Class[] pJustification = { Justification.class };
|
---|
152 | Class[] pPermission = { Permission.class };
|
---|
153 |
|
---|
154 | // TODO automatically load these by annotating the methods with the
|
---|
155 | // shortcut text
|
---|
156 | // Indicate to users of any symbols that clash
|
---|
157 |
|
---|
158 | _SetMethods = new HashMap<String, Method>();
|
---|
159 | _SetMethods.put("x", Item.class.getMethod("setX", pFloat));
|
---|
160 | _SetMethods.put("y", Item.class.getMethod("setY", pFloat));
|
---|
161 | _SetMethods.put("position", Item.class.getMethod("setPosition",
|
---|
162 | pPoint));
|
---|
163 |
|
---|
164 | _SetMethods.put("ab", Item.class.getMethod("setAnchorBottom",
|
---|
165 | pFloatO));
|
---|
166 | _SetMethods.put("ar", Item.class.getMethod("setAnchorRight",
|
---|
167 | pFloatO));
|
---|
168 | _SetMethods.put("anchorbottom", Item.class.getMethod(
|
---|
169 | "setAnchorBottom", pFloatO));
|
---|
170 | _SetMethods.put("anchorright", Item.class.getMethod(
|
---|
171 | "setAnchorRight", pFloatO));
|
---|
172 |
|
---|
173 | _SetMethods.put("p", Item.class.getMethod("setPosition", pPoint));
|
---|
174 | _SetMethods.put("xy", Item.class.getMethod("setPosition", pPoint));
|
---|
175 | _SetMethods.put("pos", Item.class.getMethod("setPosition", pPoint));
|
---|
176 | _SetMethods.put("thickness", Item.class.getMethod("setThickness",
|
---|
177 | pFloat));
|
---|
178 | _SetMethods.put("t", Item.class.getMethod("setThickness", pFloat));
|
---|
179 |
|
---|
180 | _SetMethods.put("color", Item.class.getMethod("setColor", pColor));
|
---|
181 | _SetMethods.put("c", Item.class.getMethod("setColor", pColor));
|
---|
182 |
|
---|
183 | _SetMethods.put("backgroundcolor", Item.class.getMethod(
|
---|
184 | "setBackgroundColor", pColor));
|
---|
185 | _SetMethods.put("bgc", Item.class.getMethod("setBackgroundColor",
|
---|
186 | pColor));
|
---|
187 | _SetMethods.put("bordercolor", Item.class.getMethod(
|
---|
188 | "setBorderColor", pColor));
|
---|
189 | _SetMethods.put("bc", Item.class
|
---|
190 | .getMethod("setBorderColor", pColor));
|
---|
191 |
|
---|
192 | _SetMethods
|
---|
193 | .put("action", Item.class.getMethod("setActions", pList));
|
---|
194 | _SetMethods.put("a", Item.class.getMethod("setActions", pList));
|
---|
195 | _SetMethods.put("d", Item.class.getMethod("setData", pList));
|
---|
196 | _SetMethods.put("data", Item.class.getMethod("setData", pList));
|
---|
197 |
|
---|
198 | _SetMethods.put("link", Item.class.getMethod("setLink", pString));
|
---|
199 | _SetMethods.put("l", Item.class.getMethod("setLink", pString));
|
---|
200 |
|
---|
201 | _SetMethods.put("formula", Item.class.getMethod("calculate",
|
---|
202 | pString));
|
---|
203 | _SetMethods.put("f", Item.class.getMethod("calculate", pString));
|
---|
204 |
|
---|
205 | _SetMethods.put("fillcolor", Item.class.getMethod("setFillColor",
|
---|
206 | pColor));
|
---|
207 | _SetMethods.put("fc", Item.class.getMethod("setFillColor", pColor));
|
---|
208 | _SetMethods.put("gc", Item.class.getMethod("setGradientColor",
|
---|
209 | pColor));
|
---|
210 | _SetMethods.put("gradientcolor", Item.class.getMethod(
|
---|
211 | "setGradientColor", pColor));
|
---|
212 |
|
---|
213 | _SetMethods.put("ga", Item.class
|
---|
214 | .getMethod("setGradientAngle", pInt));
|
---|
215 | _SetMethods.put("gradientangle", Item.class.getMethod(
|
---|
216 | "setGradientAngle", pInt));
|
---|
217 |
|
---|
218 | _SetMethods.put("fillpattern", Item.class.getMethod(
|
---|
219 | "setFillPattern", pString));
|
---|
220 | _SetMethods.put("fp", Item.class.getMethod("setFillPattern",
|
---|
221 | pString));
|
---|
222 |
|
---|
223 | _SetMethods.put("owner", Item.class.getMethod("setOwner", pString));
|
---|
224 | _SetMethods.put("linkmark", Item.class.getMethod("setLinkMark",
|
---|
225 | pBool));
|
---|
226 | _SetMethods.put("lm", Item.class.getMethod("setLinkMark", pBool));
|
---|
227 | _SetMethods.put("actionmark", Item.class.getMethod("setActionMark",
|
---|
228 | pBool));
|
---|
229 | _SetMethods.put("am", Item.class.getMethod("setActionMark", pBool));
|
---|
230 |
|
---|
231 | _SetMethods.put("actioncursorenter", Item.class.getMethod(
|
---|
232 | "setActionCursorEnter", pList));
|
---|
233 | _SetMethods.put("actioncursorleave", Item.class.getMethod(
|
---|
234 | "setActionCursorLeave", pList));
|
---|
235 | _SetMethods.put("actionenterframe", Item.class.getMethod(
|
---|
236 | "setActionEnterFrame", pList));
|
---|
237 | _SetMethods.put("actionleaveframe", Item.class.getMethod(
|
---|
238 | "setActionLeaveFrame", pList));
|
---|
239 |
|
---|
240 | _SetMethods.put("topshadow", Item.class.getMethod(
|
---|
241 | "setTopShadowColor", pColor));
|
---|
242 | _SetMethods.put("bottomshadow", Item.class.getMethod(
|
---|
243 | "setBottomShadowColor", pColor));
|
---|
244 | _SetMethods.put("arrow", Item.class.getMethod("setArrow", pArrow));
|
---|
245 |
|
---|
246 | _SetMethods.put("linepattern", Item.class.getMethod(
|
---|
247 | "setLinePattern", pIntArray));
|
---|
248 | _SetMethods.put("lp", Item.class.getMethod("setLinePattern",
|
---|
249 | pIntArray));
|
---|
250 |
|
---|
251 | _SetMethods.put("linkframeset", Item.class.getMethod(
|
---|
252 | "setLinkFrameset", pString));
|
---|
253 | _SetMethods.put("lf", Item.class.getMethod("setLinkFrameset",
|
---|
254 | pString));
|
---|
255 | _SetMethods.put("linktemplate", Item.class.getMethod(
|
---|
256 | "setLinkTemplate", pString));
|
---|
257 | _SetMethods.put("lt", Item.class.getMethod("setLinkTemplate",
|
---|
258 | pString));
|
---|
259 |
|
---|
260 | _SetMethods.put("family", Text.class
|
---|
261 | .getMethod("setFamily", pString));
|
---|
262 | _SetMethods.put("face", Text.class.getMethod("setFontStyle",
|
---|
263 | pString));
|
---|
264 | _SetMethods.put("fontstyle", Text.class.getMethod("setFontStyle",
|
---|
265 | pString));
|
---|
266 | _SetMethods.put("justification", Text.class.getMethod(
|
---|
267 | "setJustification", pJustification));
|
---|
268 | _SetMethods.put("j", Text.class.getMethod("setJustification",
|
---|
269 | pJustification));
|
---|
270 | _SetMethods.put("width", Text.class.getMethod("setWidth", pIntO));
|
---|
271 | _SetMethods.put("w", Text.class.getMethod("setWidth", pIntO));
|
---|
272 |
|
---|
273 | _SetMethods.put("size", Item.class.getMethod("setSize", pFloat));
|
---|
274 | _SetMethods.put("s", Item.class.getMethod("setSize", pFloat));
|
---|
275 |
|
---|
276 | // TODO have a separate map for item and frame methods.
|
---|
277 | // This will remove the need for p0 etc
|
---|
278 | _SetMethods.put("foregroundcolor", Frame.class.getMethod(
|
---|
279 | "setForegroundColor", pColor));
|
---|
280 | _SetMethods.put("fgc", Frame.class.getMethod("setForegroundColor",
|
---|
281 | pColor));
|
---|
282 | _SetMethods.put("backgroundcolor0", Frame.class.getMethod(
|
---|
283 | "setBackgroundColor", pColor));
|
---|
284 | _SetMethods.put("bgc0", Frame.class.getMethod("setBackgroundColor",
|
---|
285 | pColor));
|
---|
286 | _SetMethods.put("permission", Frame.class.getMethod(
|
---|
287 | "setPermission", pPermission));
|
---|
288 | _SetMethods.put("p0", Frame.class.getMethod("setPermission",
|
---|
289 | pPermission));
|
---|
290 |
|
---|
291 | } catch (SecurityException e) {
|
---|
292 | // TODO Auto-generated catch block
|
---|
293 | e.printStackTrace();
|
---|
294 | } catch (NoSuchMethodException e) {
|
---|
295 | // TODO Auto-generated catch block
|
---|
296 | e.printStackTrace();
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Extracts a list of attributes from the given Object. Any method that
|
---|
302 | * starts with <code>get</code>, takes no arguments and is not found in
|
---|
303 | * the Ignore list will be run, All the attributes are then put into a Text
|
---|
304 | * Item of the form <Name>:<Value> If the value returned by the get method
|
---|
305 | * is null, then the attribute will not be included, unless the name of the
|
---|
306 | * method is found in the AllowNull list.
|
---|
307 | *
|
---|
308 | * @param toExtract
|
---|
309 | * The Object from which to extract the attributes
|
---|
310 | * @return A Text Item containing the extracted Attributes.
|
---|
311 | */
|
---|
312 | public static Item extractAttributes(Object toExtract) {
|
---|
313 | if (toExtract == null)
|
---|
314 | return null;
|
---|
315 |
|
---|
316 | // ensure the lists are populated
|
---|
317 | if (_Ignore == null)
|
---|
318 | initLists();
|
---|
319 |
|
---|
320 | // StringBuffer to store all the extracted Attribute:Value pairs
|
---|
321 | StringBuffer attributes = new StringBuffer();
|
---|
322 |
|
---|
323 | // iterate through the list of methods
|
---|
324 | for (Method m : _GetMethods) {
|
---|
325 |
|
---|
326 | // Make sure the classes of the methods match the item
|
---|
327 | if (m.getDeclaringClass().isAssignableFrom(toExtract.getClass())) {
|
---|
328 | try {
|
---|
329 | String s = getValue(m, toExtract, true);
|
---|
330 |
|
---|
331 | if (s == null)
|
---|
332 | continue;
|
---|
333 | // Append the attributes
|
---|
334 | attributes.append(m.getName().substring(GET_LENGTH))
|
---|
335 | .append(AttributeValuePair.SEPARATOR_STRING)
|
---|
336 | .append(s).append('\n');
|
---|
337 | } catch (Exception e) {
|
---|
338 | // TODO Auto-generated catch block
|
---|
339 | e.printStackTrace();
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | // if no attributes were extracted
|
---|
345 | if (attributes.length() <= 0)
|
---|
346 | return null;
|
---|
347 |
|
---|
348 | while (attributes.charAt(attributes.length() - 1) == '\n')
|
---|
349 | attributes.delete(attributes.length() - 1, attributes.length());
|
---|
350 |
|
---|
351 | // create the text Item
|
---|
352 | Frame current = DisplayIO.getCurrentFrame();
|
---|
353 | Item attribs = current.getStatsTextItem(attributes.toString());
|
---|
354 | return attribs;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Gets a string form of the value for a given item get method.
|
---|
359 | * @param method
|
---|
360 | * @param item
|
---|
361 | * @param ignore true if the attributes in the IGNORE list should be ignored
|
---|
362 | * @return
|
---|
363 | */
|
---|
364 | private static String getValue(Method method, Object item, boolean ignore) {
|
---|
365 | assert(method.getName().startsWith("get"));
|
---|
366 |
|
---|
367 | Object o = null;
|
---|
368 | try {
|
---|
369 | o = method.invoke(item, (Object[]) null);
|
---|
370 | } catch (IllegalArgumentException e) {
|
---|
371 | e.printStackTrace();
|
---|
372 | return null;
|
---|
373 | } catch (IllegalAccessException e) {
|
---|
374 | e.printStackTrace();
|
---|
375 | return null;
|
---|
376 | } catch (InvocationTargetException e) {
|
---|
377 | e.printStackTrace();
|
---|
378 | return null;
|
---|
379 | }
|
---|
380 |
|
---|
381 | if (o == null) {
|
---|
382 | // methods that return null are only included if they
|
---|
383 | // are in the AllowNull list
|
---|
384 | if (_AllowNull.contains(method)) {
|
---|
385 | String name = method.getName().substring(GET_LENGTH)
|
---|
386 | .toLowerCase();
|
---|
387 | if (name.equals("color"))
|
---|
388 | o = "default";
|
---|
389 | else if (name.equals("backgroundcolor"))
|
---|
390 | o = "transparent";
|
---|
391 | else if (name.equals("foregroundcolor"))
|
---|
392 | o = "auto";
|
---|
393 | else
|
---|
394 | o = "";
|
---|
395 | } else {
|
---|
396 | return null;
|
---|
397 | }
|
---|
398 | }
|
---|
399 | // skip methods that are in the ignore lists
|
---|
400 | if (ignore
|
---|
401 | && _ExtractIgnore.contains(method.getName().substring(
|
---|
402 | GET_LENGTH).toLowerCase())) {
|
---|
403 | return null;
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (o instanceof Integer) {
|
---|
407 | Integer i = (Integer) o;
|
---|
408 | if (i == Item.DEFAULT_INTEGER)
|
---|
409 | return null;
|
---|
410 | if (method.getName().endsWith("Justification")
|
---|
411 | && ((Justification) o).toString() != null)
|
---|
412 | o = ((Justification) o).toString();
|
---|
413 | // -1 indicates default value
|
---|
414 | else
|
---|
415 | o = i;
|
---|
416 | } else if (o instanceof Float) {
|
---|
417 | if (((Float) o) < -0.0001)
|
---|
418 | return null;
|
---|
419 | // Null indicates default
|
---|
420 | o = Math.round((Float) o);
|
---|
421 | } else if (o instanceof Double) {
|
---|
422 | // -1 indicates default value
|
---|
423 | if (((Double) o) < 0.0001)
|
---|
424 | return null;
|
---|
425 | } else if (o instanceof Color) {
|
---|
426 | // converts the color to the Expeditee code
|
---|
427 | o = Conversion.getExpediteeColorCode((Color) o);
|
---|
428 | if (o == null)
|
---|
429 | return null;
|
---|
430 | } else if (o instanceof Point) {
|
---|
431 | Point p = (Point) o;
|
---|
432 | o = Math.round(p.getX()) + " " + Math.round(p.getY());
|
---|
433 | } else if (o instanceof Font) {
|
---|
434 | Font f = (Font) o;
|
---|
435 |
|
---|
436 | String s = f.getName() + "-";
|
---|
437 | if (f.isPlain())
|
---|
438 | s += "Plain";
|
---|
439 |
|
---|
440 | if (f.isBold())
|
---|
441 | s += "Bold";
|
---|
442 |
|
---|
443 | if (f.isItalic())
|
---|
444 | s += "Italic";
|
---|
445 |
|
---|
446 | s += "-" + f.getSize();
|
---|
447 | o = s;
|
---|
448 | } else if (o instanceof Text) {
|
---|
449 | o = ((Text) o).getFirstLine();
|
---|
450 | } else if (o instanceof List) {
|
---|
451 | List list = (List) o;
|
---|
452 | StringBuffer sb = new StringBuffer();
|
---|
453 | for (Object ob : list)
|
---|
454 | // TODO check that this works ok
|
---|
455 | if (sb.length() == 0) {
|
---|
456 | sb.append(ob);
|
---|
457 | } else {
|
---|
458 | sb.append('\n').append(
|
---|
459 | method.getName().substring(GET_LENGTH)).append(
|
---|
460 | AttributeValuePair.SEPARATOR_STRING).append(ob);
|
---|
461 | }
|
---|
462 | return sb.toString();
|
---|
463 | } else if (o instanceof int[]) {
|
---|
464 | StringBuffer sb = new StringBuffer();
|
---|
465 | int[] values = (int[]) o;
|
---|
466 | for (int i = 0; i < values.length; i++) {
|
---|
467 | sb.append(values[i]).append(' ');
|
---|
468 | }
|
---|
469 | sb.deleteCharAt(sb.length() - 1);
|
---|
470 | o = sb.toString();
|
---|
471 | } else if (o instanceof Boolean) {
|
---|
472 | // true is the default for boolean values
|
---|
473 | if (((Boolean) o).booleanValue())
|
---|
474 | return null;
|
---|
475 | }
|
---|
476 | return o.toString();
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Attempts to set the attribute in the given attribute: value pair. The
|
---|
481 | * value string should be formatted as follows:
|
---|
482 | * <code> Attribute: Value </code> Multiple values can be used if they are
|
---|
483 | * separated by spaces
|
---|
484 | *
|
---|
485 | * @param toSet
|
---|
486 | * The Item or Frame to set the attribute of
|
---|
487 | * @param attribs
|
---|
488 | * The Text item that contains the list of attributes to set
|
---|
489 | * @return True if the attribute(s) were sucessfully set, false otherwise
|
---|
490 | */
|
---|
491 | public static boolean setAttribute(Object toSet, Text attribs) {
|
---|
492 | return setAttribute(toSet, attribs, 1);
|
---|
493 | }
|
---|
494 |
|
---|
495 | public static boolean setAttribute(Object toSet, Text attribs,
|
---|
496 | int minAttributeLength) {
|
---|
497 | // error checking
|
---|
498 | if (toSet == null || attribs == null)
|
---|
499 | return false;
|
---|
500 |
|
---|
501 | if (_Ignore == null)
|
---|
502 | initLists();
|
---|
503 |
|
---|
504 | // if(attribs.isAnnotation())
|
---|
505 | // return false;
|
---|
506 |
|
---|
507 | // get the list of attribute: value pairs
|
---|
508 | List<String> values = attribs.getTextList();
|
---|
509 | // if no pairs exist, we are done
|
---|
510 | if (values == null || values.size() == 0) {
|
---|
511 | return false;
|
---|
512 | }
|
---|
513 |
|
---|
514 | // loop through all attribute: value pairs
|
---|
515 | for (int i = 0; i < values.size(); i++) {
|
---|
516 | AttributeValuePair avp = new AttributeValuePair(values.get(i),
|
---|
517 | false);
|
---|
518 |
|
---|
519 | // If the first is not an attribute value pair then dont do
|
---|
520 | // attrubute merging
|
---|
521 | if (!avp.hasAttribute()
|
---|
522 | || avp.getAttribute().length() < minAttributeLength)
|
---|
523 | return false;
|
---|
524 |
|
---|
525 | // check if the next string is another attribute to merge or a
|
---|
526 | // continuation
|
---|
527 | for (; i < values.size() - 1; i++) {
|
---|
528 | AttributeValuePair nextAvp = new AttributeValuePair(values
|
---|
529 | .get(i + 1), false);
|
---|
530 |
|
---|
531 | // if the next String has a colon, then it may be another
|
---|
532 | // attribute
|
---|
533 | if (nextAvp.hasAttribute()) {
|
---|
534 | // if the attribute is the same as v, then it is a
|
---|
535 | // continuation
|
---|
536 | if (nextAvp.getAttribute().equals(avp.getAttribute())) {
|
---|
537 | // strip the attribute from next
|
---|
538 | avp.appendValue(nextAvp.getValue());
|
---|
539 |
|
---|
540 | // if the attribute is not the same, then it may be a
|
---|
541 | // new method
|
---|
542 | } else {
|
---|
543 | break;
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | // v.append("\n").append(next);
|
---|
548 | }
|
---|
549 |
|
---|
550 | if (!setAttribute(toSet, avp, values.size() > 1)) {
|
---|
551 |
|
---|
552 | String stripped = avp.getAttribute();
|
---|
553 | if (!avp.hasPair()) {
|
---|
554 | // This happens when there is an attribute at the start
|
---|
555 | // Then a bunch of plain text
|
---|
556 | return false;
|
---|
557 | } else if (_Ignore.contains(stripped)) {
|
---|
558 | return false;
|
---|
559 | } else if (!(_SetMethods.containsKey(stripped))) {
|
---|
560 | return false;
|
---|
561 | } else {
|
---|
562 | String types = "";
|
---|
563 | for (Class c : _SetMethods.get(stripped)
|
---|
564 | .getParameterTypes())
|
---|
565 | types += c.getSimpleName() + " ";
|
---|
566 | MessageBay.warningMessage("Wrong arguments for: '"
|
---|
567 | + avp.getAttribute() + "' expecting "
|
---|
568 | + types.trim() + " found '" + avp.getValue() + "'");
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | return true;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Sets a single attrubute of a frame or item.
|
---|
578 | *
|
---|
579 | * @param toSet
|
---|
580 | * @param avp
|
---|
581 | * @param isAttributeList
|
---|
582 | * some properties are ignored when attribute list are injected
|
---|
583 | * into an item. These properties are ignored if this param is
|
---|
584 | * true
|
---|
585 | * @return
|
---|
586 | */
|
---|
587 | private static boolean setAttribute(Object toSet, AttributeValuePair avp,
|
---|
588 | boolean isAttributeList) {
|
---|
589 |
|
---|
590 | assert (avp.hasAttribute());
|
---|
591 |
|
---|
592 | // separate attribute and value from string
|
---|
593 | String attribute = avp.getAttribute().toLowerCase();
|
---|
594 |
|
---|
595 | String value = avp.getValue();
|
---|
596 | assert (value != null);
|
---|
597 |
|
---|
598 | // Some properties are ignored when multiple attributes are being set on
|
---|
599 | // an item at the same time
|
---|
600 | if (isAttributeList && _Ignore.contains(attribute)) {
|
---|
601 | // System.out.println("Attribute ignored: " + attribute);
|
---|
602 | return true;
|
---|
603 | }
|
---|
604 |
|
---|
605 | // Separate multiple values if required
|
---|
606 | Method toRun = _SetMethods.get(attribute);
|
---|
607 |
|
---|
608 | // if this is not the name of a method, it may be the name of an agent
|
---|
609 | if (toRun == null) {
|
---|
610 | // System.out.println("Attrib not found for: " + attribute);
|
---|
611 | return false;
|
---|
612 | }
|
---|
613 |
|
---|
614 | // if there are duplicate methods with the same name
|
---|
615 | List<Method> possibles = new LinkedList<Method>();
|
---|
616 | if (toRun.getDeclaringClass().isInstance(toSet))
|
---|
617 | possibles.add(toRun);
|
---|
618 | int i = 0;
|
---|
619 | while (_SetMethods.containsKey(attribute + i)) {
|
---|
620 | if (_SetMethods.get(attribute + i).getDeclaringClass()
|
---|
621 | .isAssignableFrom(toSet.getClass()))
|
---|
622 | possibles.add(_SetMethods.get(attribute + i));
|
---|
623 | i++;
|
---|
624 | }
|
---|
625 |
|
---|
626 | for (Method possible : possibles) {
|
---|
627 | Object current = null;
|
---|
628 | Object[] param = {};
|
---|
629 | // find the corresponding get method for this set method
|
---|
630 | // and get the current value of the attribute
|
---|
631 | for (Method m : _GetMethods) {
|
---|
632 | if (m.getDeclaringClass().isAssignableFrom(toSet.getClass())
|
---|
633 | && m.getName().substring(GET_LENGTH).equals(
|
---|
634 | possible.getName().substring(SET_LENGTH))) {
|
---|
635 | try {
|
---|
636 | current = m.invoke(toSet, param);
|
---|
637 | } catch (IllegalArgumentException e) {
|
---|
638 | // TODO Auto-generated catch block
|
---|
639 | e.printStackTrace();
|
---|
640 | } catch (IllegalAccessException e) {
|
---|
641 | // TODO Auto-generated catch block
|
---|
642 | e.printStackTrace();
|
---|
643 | } catch (InvocationTargetException e) {
|
---|
644 | // TODO Auto-generated catch block
|
---|
645 | e.printStackTrace();
|
---|
646 | }
|
---|
647 | break;
|
---|
648 | }
|
---|
649 |
|
---|
650 | }
|
---|
651 |
|
---|
652 | try {
|
---|
653 | Object[] params = Conversion.Convert(possible, value, current);
|
---|
654 |
|
---|
655 | try {
|
---|
656 | possible.invoke(toSet, params);
|
---|
657 | return true;
|
---|
658 | } catch (IllegalArgumentException e) {
|
---|
659 | // TODO Auto-generated catch block
|
---|
660 | e.printStackTrace();
|
---|
661 | } catch (IllegalAccessException e) {
|
---|
662 | // TODO Auto-generated catch block
|
---|
663 | e.printStackTrace();
|
---|
664 | } catch (InvocationTargetException e) {
|
---|
665 | MessageBay.displayMessage(toSet.getClass().getSimpleName()
|
---|
666 | + " type does not support that attribute.");
|
---|
667 | // e.printStackTrace();
|
---|
668 | }
|
---|
669 | } catch (NumberFormatException e) {
|
---|
670 |
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | return false;
|
---|
675 | }
|
---|
676 |
|
---|
677 | /**
|
---|
678 | * Replaces the current value for the text item with the new value.
|
---|
679 | *
|
---|
680 | * @param text
|
---|
681 | * the item whos value is to be changed
|
---|
682 | * @param newValue
|
---|
683 | * the new value for the item
|
---|
684 | */
|
---|
685 | public static void replaceValue(Text text, String newValue) {
|
---|
686 | assert (newValue != null);
|
---|
687 |
|
---|
688 | AttributeValuePair avp = new AttributeValuePair(text.getText());
|
---|
689 |
|
---|
690 | if (avp.getAttribute() == null) {
|
---|
691 | avp.setAttribute(avp.getValue());
|
---|
692 | }
|
---|
693 | avp.setValue(newValue);
|
---|
694 | text.setText(avp.toString());
|
---|
695 | }
|
---|
696 |
|
---|
697 | public static String getAttribute(Item item, String attribute) {
|
---|
698 |
|
---|
699 | // ensure the lists are populated
|
---|
700 | if (_Ignore == null)
|
---|
701 | initLists();
|
---|
702 |
|
---|
703 | // separate attribute and value from string
|
---|
704 | String lowerAttribute = attribute.trim().toLowerCase();
|
---|
705 |
|
---|
706 | String methodName = "get"
|
---|
707 | + Character.toUpperCase(lowerAttribute.charAt(0))
|
---|
708 | + lowerAttribute.substring(1);
|
---|
709 |
|
---|
710 | try {
|
---|
711 | return attribute + ": " + getValue(item.getClass().getMethod(methodName,
|
---|
712 | new Class[] {}), item, false);
|
---|
713 | } catch (SecurityException e) {
|
---|
714 | e.printStackTrace();
|
---|
715 | } catch (NoSuchMethodException e) {
|
---|
716 | MessageBay
|
---|
717 | .errorMessage("Could no extract unknown attribute value: "
|
---|
718 | + attribute);
|
---|
719 | }
|
---|
720 | return null;
|
---|
721 | }
|
---|
722 | }
|
---|