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

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

Fixing bugs for Rob

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