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

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