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

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

Added calculate action

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