source: trunk/src/org/expeditee/gui/AttributeUtils.java@ 86

Last change on this file since 86 was 86, checked in by ra33, 16 years ago

Added DebugFrame action
Added GetCometStats
Lots of bug fixes

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