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