Ignore:
Timestamp:
06/10/08 10:03:03 (16 years ago)
Author:
ra33
Message:

Lots of changes!!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gui/AttributeUtils.java

    r95 r97  
    1212import org.expeditee.io.Conversion;
    1313import org.expeditee.items.Item;
     14import org.expeditee.items.Justification;
    1415import org.expeditee.items.Text;
    1516
     
    5758
    5859                try {
     60                        // TODO load these in with reflection...
     61                        // Set the shortcuts with annotation tags on the methods
    5962                        _Ignore = new LinkedList<String>();
    6063                        _Ignore.add("date");
     
    140143                        Class[] pList = { List.class };
    141144                        Class[] pIntArray = { int[].class };
     145                        Class[] pJustification = { Justification.class };
    142146
    143147                        _SetMethods = new HashMap<String, Method>();
    144                         _SetMethods.put("x", Item.class.getMethod("setX", pInt));
    145                         _SetMethods.put("y", Item.class.getMethod("setY", pInt));
     148                        _SetMethods.put("x", Item.class.getMethod("setX", pFloat));
     149                        _SetMethods.put("y", Item.class.getMethod("setY", pFloat));
    146150                        _SetMethods.put("position", Item.class.getMethod("setPosition",
    147151                                        pPoint));
     
    222226                                        pString));
    223227                        _SetMethods.put("justification", Text.class.getMethod(
    224                                         "setJustification", pInt));
     228                                        "setJustification", pJustification));
    225229                        _SetMethods.put("width", Text.class.getMethod("setWidth", pInt));
    226230                        _SetMethods.put("size", Item.class.getMethod("setSize", pInt));
     
    259263         * @return A Text Item containing the extracted Attributes.
    260264         */
    261         public static Item ExtractAttributes(Object toExtract) {
     265        public static Item extractAttributes(Object toExtract) {
    262266                if (toExtract == null)
    263267                        return null;
     
    282286                                                // are in the AllowNull list
    283287                                                if (_AllowNull.contains(m)) {
    284                                                         String name = m.getName().substring(
    285                                                                         GET_LENGTH).toLowerCase();
     288                                                        String name = m.getName().substring(GET_LENGTH)
     289                                                                        .toLowerCase();
    286290                                                        if (name.equals("color"))
    287291                                                                o = "default";
     
    303307
    304308                                        if (o instanceof Integer) {
    305                                                 Integer i =  (Integer)o;
     309                                                Integer i = (Integer) o;
    306310                                                if (i == Item.DEFAULT_INTEGER)
    307311                                                        continue;
    308312                                                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();
    311315                                                // -1 indicates default value
    312                                                 else 
     316                                                else
    313317                                                        o = i;
    314318                                        } else if (o instanceof Float) {
     
    366370                                                        continue;
    367371                                        }
    368                                         //Append the attributes
     372                                        // Append the attributes
    369373                                        attributes.append(m.getName().substring(GET_LENGTH))
    370374                                                        .append(SEPARATOR_STRING).append(o).append("\n");
     
    388392                return attribs;
    389393        }
    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       
    417395        /**
    418396         * Attempts to set the attribute in the given attribute: value pair. The
     
    422400         *
    423401         * @param toSet
    424          *            The Item to set the attribute of
     402         *            The Item or Frame to set the attribute of
    425403         * @param attribs
    426404         *            The Text item that contains the list of attributes to set
    427405         * @return True if the attribute(s) were sucessfully set, false otherwise
    428406         */
    429         public static boolean SetAttribute(Object toSet, Text attribs) {
     407        public static boolean setAttribute(Object toSet, Text attribs) {
    430408                // error checking
    431409                if (toSet == null || attribs == null)
     
    437415                // get the list of attribute: value pairs
    438416                List<String> values = attribs.getTextList();
    439                 // if no paris exist, we are done
     417                // if no pairs exist, we are done
    440418                if (values.size() == 0
    441419                                || (values.size() == 1 && values.get(0).length() == 0)) {
     
    479457
    480458                        if (v.length() > 0
    481                                         && !SetAttribute(toSet, v.toString(), values.size() > 1)) {
     459                                        && !setAttribute(toSet, v.toString(), values.size() > 1)) {
    482460                                // if no other attributes have been set
    483461                                if (i == 0)
     
    516494        }
    517495
    518         private static boolean SetAttribute(Object toSet, String value,
     496        private static boolean setAttribute(Object toSet, String value,
    519497                        boolean isAttributeList) {
    520498                // 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               
    522504                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);
    532506
    533507                // Some properties are ignored when multiple attributes are being set on
     
    538512                }
    539513
    540                 // separate multiple values if required
    541                 Method toRun = _SetMethods.get(attribute);
     514                // Separate multiple values if required
     515                Method toRun = _SetMethods.get( attribute);
    542516
    543517                // if this is not the name of a method, it may be the name of an agent
     
    553527                int i = 0;
    554528                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()))
    556531                                possibles.add(_SetMethods.get(attribute + i));
    557532                        i++;
     
    608583
    609584                return false;
    610 
    611585        }
    612586
    613587        /**
    614588         * 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.
     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.
    617591         *
    618592         * @param attributeValuePair
     
    622596        public static String getValue(String toStrip) {
    623597                assert (toStrip != null);
     598
     599                toStrip = toStrip.trim();
    624600                if (toStrip.length() == 0)
    625601                        return "";
    626602
    627                 int lineSeparator = toStrip.indexOf(Character.LINE_SEPARATOR);
    628                 if(lineSeparator > 0){
    629                         toStrip = toStrip.substring(0,lineSeparator);
    630                 }
    631                
    632603                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();
    645615        }
    646616
     
    655625         */
    656626        public static String getAttribute(String attributeValuePair) {
     627                attributeValuePair = attributeValuePair.trim();
     628
    657629                int ind = attributeValuePair.indexOf(SEPARATOR_CHAR);
    658                 if (ind < 0)
     630                if (ind < 1)
    659631                        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();
    666644
    667645                return attributeValuePair;
     
    669647
    670648        /**
     649         * Replaces the current value for the text item with the new value.
    671650         *
    672651         * @param text
    673          * @param value
     652         *            the item whos value is to be changed
     653         * @param newValue
     654         *            the new value for the item
    674655         */
    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());
    679661                String attribute = getAttribute(oldText);
    680662
     
    682664                        attribute = text.getText().trim();
    683665
    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         */
    687676        public static Double getDoubleValue(String attributeValuePair) {
    688677                String value = getValue(attributeValuePair);
    689678
    690                 if (value == null)
    691                         return null;
     679                assert (value != null);
    692680
    693681                try {
Note: See TracChangeset for help on using the changeset viewer.