Changeset 97 for trunk/src/org/expeditee/gui/AttributeUtils.java
- Timestamp:
- 06/10/08 10:03:03 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/gui/AttributeUtils.java
r95 r97 12 12 import org.expeditee.io.Conversion; 13 13 import org.expeditee.items.Item; 14 import org.expeditee.items.Justification; 14 15 import org.expeditee.items.Text; 15 16 … … 57 58 58 59 try { 60 // TODO load these in with reflection... 61 // Set the shortcuts with annotation tags on the methods 59 62 _Ignore = new LinkedList<String>(); 60 63 _Ignore.add("date"); … … 140 143 Class[] pList = { List.class }; 141 144 Class[] pIntArray = { int[].class }; 145 Class[] pJustification = { Justification.class }; 142 146 143 147 _SetMethods = new HashMap<String, Method>(); 144 _SetMethods.put("x", Item.class.getMethod("setX", p Int));145 _SetMethods.put("y", Item.class.getMethod("setY", p Int));148 _SetMethods.put("x", Item.class.getMethod("setX", pFloat)); 149 _SetMethods.put("y", Item.class.getMethod("setY", pFloat)); 146 150 _SetMethods.put("position", Item.class.getMethod("setPosition", 147 151 pPoint)); … … 222 226 pString)); 223 227 _SetMethods.put("justification", Text.class.getMethod( 224 "setJustification", p Int));228 "setJustification", pJustification)); 225 229 _SetMethods.put("width", Text.class.getMethod("setWidth", pInt)); 226 230 _SetMethods.put("size", Item.class.getMethod("setSize", pInt)); … … 259 263 * @return A Text Item containing the extracted Attributes. 260 264 */ 261 public static Item ExtractAttributes(Object toExtract) {265 public static Item extractAttributes(Object toExtract) { 262 266 if (toExtract == null) 263 267 return null; … … 282 286 // are in the AllowNull list 283 287 if (_AllowNull.contains(m)) { 284 String name = m.getName().substring( 285 GET_LENGTH).toLowerCase();288 String name = m.getName().substring(GET_LENGTH) 289 .toLowerCase(); 286 290 if (name.equals("color")) 287 291 o = "default"; … … 303 307 304 308 if (o instanceof Integer) { 305 Integer i = (Integer)o;309 Integer i = (Integer) o; 306 310 if (i == Item.DEFAULT_INTEGER) 307 311 continue; 308 312 if (m.getName().endsWith("Justification") 309 && convertJustificationToString((Integer) o) != null)310 o = convertJustificationToString((Integer) o);313 && ((Justification) o).toString() != null) 314 o = ((Justification) o).toString(); 311 315 // -1 indicates default value 312 else 316 else 313 317 o = i; 314 318 } else if (o instanceof Float) { … … 366 370 continue; 367 371 } 368 // Append the attributes372 // Append the attributes 369 373 attributes.append(m.getName().substring(GET_LENGTH)) 370 374 .append(SEPARATOR_STRING).append(o).append("\n"); … … 388 392 return attribs; 389 393 } 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 394 417 395 /** 418 396 * Attempts to set the attribute in the given attribute: value pair. The … … 422 400 * 423 401 * @param toSet 424 * The Item to set the attribute of402 * The Item or Frame to set the attribute of 425 403 * @param attribs 426 404 * The Text item that contains the list of attributes to set 427 405 * @return True if the attribute(s) were sucessfully set, false otherwise 428 406 */ 429 public static boolean SetAttribute(Object toSet, Text attribs) {407 public static boolean setAttribute(Object toSet, Text attribs) { 430 408 // error checking 431 409 if (toSet == null || attribs == null) … … 437 415 // get the list of attribute: value pairs 438 416 List<String> values = attribs.getTextList(); 439 // if no pa ris exist, we are done417 // if no pairs exist, we are done 440 418 if (values.size() == 0 441 419 || (values.size() == 1 && values.get(0).length() == 0)) { … … 479 457 480 458 if (v.length() > 0 481 && ! SetAttribute(toSet, v.toString(), values.size() > 1)) {459 && !setAttribute(toSet, v.toString(), values.size() > 1)) { 482 460 // if no other attributes have been set 483 461 if (i == 0) … … 516 494 } 517 495 518 private static boolean SetAttribute(Object toSet, String value,496 private static boolean setAttribute(Object toSet, String value, 519 497 boolean isAttributeList) { 520 498 // separate attribute and value from string 521 String attribute = getAttribute(value); 499 String attribute = getAttribute(value).toLowerCase(); 500 //Check that an attribute was found 501 if(attribute == null) 502 return false; 503 522 504 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(); 505 assert(value != null); 532 506 533 507 // Some properties are ignored when multiple attributes are being set on … … 538 512 } 539 513 540 // separate multiple values if required541 Method toRun = _SetMethods.get( attribute);514 // Separate multiple values if required 515 Method toRun = _SetMethods.get( attribute); 542 516 543 517 // if this is not the name of a method, it may be the name of an agent … … 553 527 int i = 0; 554 528 while (_SetMethods.containsKey(attribute + i)) { 555 if (_SetMethods.get(attribute + i).getDeclaringClass().isAssignableFrom(toSet.getClass())) 529 if (_SetMethods.get(attribute + i).getDeclaringClass() 530 .isAssignableFrom(toSet.getClass())) 556 531 possibles.add(_SetMethods.get(attribute + i)); 557 532 i++; … … 608 583 609 584 return false; 610 611 585 } 612 586 613 587 /** 614 588 * Returns the part of the given string that is after the attribute value 615 * pair separator. If that character is not there it returns theempty616 * stringif it is an annotation item or the entire string if it is not.589 * pair separator. If that character is not there it returns empty 590 * if it is an annotation item or the entire string if it is not. 617 591 * 618 592 * @param attributeValuePair … … 622 596 public static String getValue(String toStrip) { 623 597 assert (toStrip != null); 598 599 toStrip = toStrip.trim(); 624 600 if (toStrip.length() == 0) 625 601 return ""; 626 602 627 int lineSeparator = toStrip.indexOf(Character.LINE_SEPARATOR);628 if(lineSeparator > 0){629 toStrip = toStrip.substring(0,lineSeparator);630 }631 632 603 int ind = toStrip.lastIndexOf(SEPARATOR_CHAR); 633 634 if (ind < 0) { 635 // If it is an annotation item return the empty string 636 // Annotation items can not be values only 637 if (toStrip.charAt(0) == '@') { 638 return ""; 639 } 640 // It is a value with no attribute. 641 return toStrip.trim(); 642 } 643 644 return toStrip.substring(ind + 1).trim(); 604 int lineSeparator = toStrip.indexOf(Character.LINE_SEPARATOR, ind); 605 // If it is an annotation item return the empty string 606 // Annotation items can not be values only 607 if (ind < 0 && toStrip.charAt(0) == '@') { 608 return ""; 609 } 610 //If its one line then our value goes to the end of the string 611 if(lineSeparator < 0) 612 lineSeparator = toStrip.length(); 613 614 return toStrip.substring(ind + 1, lineSeparator).trim(); 645 615 } 646 616 … … 655 625 */ 656 626 public static String getAttribute(String attributeValuePair) { 627 attributeValuePair = attributeValuePair.trim(); 628 657 629 int ind = attributeValuePair.indexOf(SEPARATOR_CHAR); 658 if (ind < 0)630 if (ind < 1) 659 631 return null; 660 661 attributeValuePair = attributeValuePair.substring(0, ind); 662 attributeValuePair = attributeValuePair.trim(); 663 664 if (attributeValuePair.length() == 0) 665 return null; 632 // If its an annotation there must be no space between the @ and colon 633 // and the first character after the annotation must be a letter 634 if (attributeValuePair.charAt(0) == '@') { 635 if (!Character.isLetter(attributeValuePair.charAt(1))) 636 return null; 637 for (int i = 2; i < ind; i++) { 638 if (!Character.isLetterOrDigit(attributeValuePair.charAt(i))) 639 return null; 640 } 641 } 642 643 attributeValuePair = attributeValuePair.substring(0, ind).trim(); 666 644 667 645 return attributeValuePair; … … 669 647 670 648 /** 649 * Replaces the current value for the text item with the new value. 671 650 * 672 651 * @param text 673 * @param value 652 * the item whos value is to be changed 653 * @param newValue 654 * the new value for the item 674 655 */ 675 public static void setSingleValue(Text text, String value) { 676 assert (value != null); 677 678 String oldText = text.getText(); 656 public static void replaceValue(Text text, String newValue) { 657 assert (newValue != null); 658 659 String oldText = text.getFirstLine(); 660 String restOfText = text.getText().substring(oldText.length()); 679 661 String attribute = getAttribute(oldText); 680 662 … … 682 664 attribute = text.getText().trim(); 683 665 684 text.setText(attribute + SEPARATOR_STRING + value); 685 } 686 666 text.setText(attribute + SEPARATOR_STRING + newValue + restOfText); 667 } 668 669 /** 670 * Gets the value from an attribute value pair as a double. 671 * 672 * @param attributeValuePair 673 * the text to get the value from 674 * @return the double value or null if there is none 675 */ 687 676 public static Double getDoubleValue(String attributeValuePair) { 688 677 String value = getValue(attributeValuePair); 689 678 690 if (value == null) 691 return null; 679 assert (value != null); 692 680 693 681 try {
Note:
See TracChangeset
for help on using the changeset viewer.