Ignore:
Timestamp:
05/30/08 10:14:43 (16 years ago)
Author:
ra33
Message:

Added some more unit tests
Did a bunch of refactoring
AND added a few new features... @b @v were the most significant

File:
1 edited

Legend:

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

    r78 r80  
    7777                        _AllowNull = new LinkedList<Method>();
    7878                        _AllowNull.add(Item.class.getMethod("getColor", param));
    79                         _AllowNull.add(Item.class.getMethod("getBackgroundColor", param));
    8079
    8180                        _AllowNull.add(Frame.class.getMethod("getBackgroundColor", param));
     
    160159                                        pColor));
    161160
    162                         _SetMethods.put("action", Item.class.getMethod("setActions", pList));
     161                        _SetMethods
     162                                        .put("action", Item.class.getMethod("setActions", pList));
    163163                        _SetMethods.put("a", Item.class.getMethod("setActions", pList));
    164164                        _SetMethods.put("d", Item.class.getMethod("setData", pList));
     
    180180                        _SetMethods.put("linkmark", Item.class.getMethod("setLinkMark",
    181181                                        pBool));
    182                         _SetMethods.put("lm", Item.class.getMethod("setLinkMark",
    183                                         pBool));
     182                        _SetMethods.put("lm", Item.class.getMethod("setLinkMark", pBool));
    184183                        _SetMethods.put("actionmark", Item.class.getMethod("setActionMark",
    185184                                        pBool));
    186                         _SetMethods.put("am", Item.class.getMethod("setActionMark",
    187                                         pBool));
     185                        _SetMethods.put("am", Item.class.getMethod("setActionMark", pBool));
    188186
    189187                        _SetMethods.put("actioncursorenter", Item.class.getMethod(
     
    206204                        _SetMethods.put("lp", Item.class.getMethod("setLinePattern",
    207205                                        pIntArray));
    208                        
     206
    209207                        _SetMethods.put("linkframeset", Item.class.getMethod(
    210208                                        "setLinkFrameset", pString));
    211                         _SetMethods.put("lf", Item.class.getMethod(
    212                                         "setLinkFrameset", pString));
     209                        _SetMethods.put("lf", Item.class.getMethod("setLinkFrameset",
     210                                        pString));
    213211                        _SetMethods.put("linktemplate", Item.class.getMethod(
    214212                                        "setLinkTemplate", pString));
    215                         _SetMethods.put("lt", Item.class.getMethod(
    216                                         "setLinkTemplate", pString));
     213                        _SetMethods.put("lt", Item.class.getMethod("setLinkTemplate",
     214                                        pString));
    217215
    218216                        _SetMethods.put("family", Text.class
     
    236234                        _SetMethods.put("bgc0", Frame.class.getMethod("setBackgroundColor",
    237235                                        pColor));
    238                         _SetMethods.put("protection", Frame.class.getMethod("setProtection",
    239                                         pString));
     236                        _SetMethods.put("protection", Frame.class.getMethod(
     237                                        "setProtection", pString));
    240238
    241239                } catch (SecurityException e) {
     
    280278                                try {
    281279                                        Object o = m.invoke(toExtract, (Object[]) null);
    282                                         if (o != null) {
    283                                                 // skip methods that are in the ignore lists
    284                                                 if (_ExtractIgnore.contains(m.getName().substring(
    285                                                                 GET_LENGTH).toLowerCase()))
     280
     281                                        if (o == null) {
     282                                                // methods that return null are only included if they
     283                                                // are in the AllowNull list
     284                                                if (_AllowNull.contains(m)) {
     285                                                        String name = m.getName().substring(
     286                                                                        GET_LENGTH).toLowerCase();
     287                                                        if (name.equals("color"))
     288                                                                o = "default";
     289                                                        else if (name.equals("backgroundcolor"))
     290                                                                o = "transparent";
     291                                                        else if (name.equals("foregroundcolor"))
     292                                                                o = "auto";
     293                                                        else
     294                                                                o = "";
     295                                                } else {
    286296                                                        continue;
    287 
    288                                                 if (o instanceof Integer) {
    289                                                         if (m.getName().endsWith("Justification")
    290                                                                         && convertJustificationToString((Integer) o) != null)
    291                                                                 attributes
    292                                                                                 .append(
    293                                                                                                 m.getName().substring(
    294                                                                                                                 GET_LENGTH))
    295                                                                                 .append(SEPARATOR_STRING)
    296                                                                                 .append(
    297                                                                                                 convertJustificationToString((Integer) o))
    298                                                                                 .append("\n");
    299                                                         // -1 indicates default value
    300                                                         else if (((Integer) o) > -1)
    301                                                                 attributes.append(
    302                                                                                 m.getName().substring(GET_LENGTH))
    303                                                                                 .append(SEPARATOR_STRING).append(o)
    304                                                                                 .append("\n");
    305                                                 } else if (o instanceof Float) {
    306                                                         // -1 indicates default value
    307                                                         if (((Float) o) > 0.0001)
    308                                                                 attributes.append(
    309                                                                                 m.getName().substring(GET_LENGTH))
    310                                                                                 .append(SEPARATOR_STRING).append(o)
    311                                                                                 .append("\n");
    312                                                 } else if (o instanceof Double) {
    313                                                         // -1 indicates default value
    314                                                         if (((Double) o) > 0.0001)
    315                                                                 attributes.append(
    316                                                                                 m.getName().substring(GET_LENGTH))
    317                                                                                 .append(SEPARATOR_STRING).append(o)
    318                                                                                 .append("\n");
    319                                                 } else if (o instanceof Color) {
    320                                                         // converts the color to the KMS code
    321                                                         String s = Conversion.getKMSColorCode((Color) o);
    322                                                         if (s != null)
    323                                                                 attributes.append(
    324                                                                                 m.getName().substring(GET_LENGTH))
    325                                                                                 .append(SEPARATOR_STRING).append(s)
    326                                                                                 .append("\n");
    327                                                 } else if (o instanceof Point) {
    328                                                         Point p = (Point) o;
    329                                                         String s = (int) p.getX() + " " + (int) p.getY();
     297                                                }
     298                                        }
     299                                        // skip methods that are in the ignore lists
     300                                        if (_ExtractIgnore.contains(m.getName().substring(
     301                                                        GET_LENGTH).toLowerCase())) {
     302                                                continue;
     303                                        }
     304
     305                                        if (o instanceof Integer) {
     306                                                Integer i =  (Integer)o;
     307                                                if (i == Item.DEFAULT_INTEGER)
     308                                                        continue;
     309                                                if (m.getName().endsWith("Justification")
     310                                                                && convertJustificationToString((Integer) o) != null)
     311                                                        o = convertJustificationToString((Integer) o);
     312                                                // -1 indicates default value
     313                                                else
     314                                                        o = i;
     315                                        } else if (o instanceof Float) {
     316                                                // -1 indicates default value
     317                                                if (((Float) o) < 0.0001)
     318                                                        continue;
     319                                        } else if (o instanceof Double) {
     320                                                // -1 indicates default value
     321                                                if (((Double) o) < 0.0001)
     322                                                        continue;
     323                                        } else if (o instanceof Color) {
     324                                                // converts the color to the Expeditee code
     325                                                o = Conversion.getExpediteeColorCode((Color) o);
     326                                                if (o == null)
     327                                                        continue;
     328                                        } else if (o instanceof Point) {
     329                                                Point p = (Point) o;
     330                                                o = (int) p.getX() + " " + (int) p.getY();
     331                                        } else if (o instanceof Font) {
     332                                                Font f = (Font) o;
     333
     334                                                String s = f.getName() + "-";
     335                                                if (f.isPlain())
     336                                                        s += "Plain";
     337
     338                                                if (f.isBold())
     339                                                        s += "Bold";
     340
     341                                                if (f.isItalic())
     342                                                        s += "Italic";
     343
     344                                                s += "-" + f.getSize();
     345                                                o = s;
     346                                        } else if (o instanceof Text) {
     347                                                o = ((Text) o).getFirstLine();
     348                                        } else if (o instanceof List) {
     349                                                List list = (List) o;
     350                                                for (Object ob : list)
    330351                                                        attributes
    331352                                                                        .append(m.getName().substring(GET_LENGTH))
    332                                                                         .append(SEPARATOR_STRING).append(s).append(
    333                                                                                         "\n");
    334                                                 } else if (o instanceof Font) {
    335                                                         Font f = (Font) o;
    336 
    337                                                         String s = f.getName() + "-";
    338                                                         if (f.isPlain())
    339                                                                 s += "Plain";
    340 
    341                                                         if (f.isBold())
    342                                                                 s += "Bold";
    343 
    344                                                         if (f.isItalic())
    345                                                                 s += "Italic";
    346 
    347                                                         s += "-" + f.getSize();
    348 
    349                                                         attributes
    350                                                                         .append(m.getName().substring(GET_LENGTH))
    351                                                                         .append(SEPARATOR_STRING).append(s).append(
    352                                                                                         "\n");
    353                                                 } else if (o instanceof Text) {
    354                                                         attributes
    355                                                                         .append(m.getName().substring(GET_LENGTH))
    356                                                                         .append(SEPARATOR_STRING).append(
    357                                                                                         ((Text) o).getFirstLine()).append(
    358                                                                                         "\n");
    359                                                 } else if (o instanceof List) {
    360                                                         List list = (List) o;
    361                                                         for (Object ob : list)
    362                                                                 attributes.append(
    363                                                                                 m.getName().substring(GET_LENGTH))
    364                                                                                 .append(SEPARATOR_STRING).append(ob)
    365                                                                                 .append("\n");
    366                                                 } else if (o instanceof int[]) {
    367                                                         attributes
    368                                                                         .append(m.getName().substring(GET_LENGTH))
    369                                                                         .append(SEPARATOR_STRING);
    370                                                         int[] values = (int[]) o;
    371                                                         for (int i = 0; i < values.length; i++) {
    372                                                                 attributes.append(values[i]).append(' ');
    373                                                         }
    374                                                         attributes.deleteCharAt(attributes.length() - 1);
    375                                                         attributes.append("\n");
    376                                                 } else if (o instanceof Boolean) {
    377                                                         // true is the default for boolean values
    378                                                         if (!((Boolean) o).booleanValue())
    379                                                                 attributes.append(
    380                                                                                 m.getName().substring(GET_LENGTH))
    381                                                                                 .append(SEPARATOR_STRING).append(o)
    382                                                                                 .append("\n");
    383                                                 } else
    384                                                         attributes
    385                                                                         .append(m.getName().substring(GET_LENGTH))
    386                                                                         .append(SEPARATOR_STRING).append(o).append(
    387                                                                                         "\n");
    388 
    389                                                 /*
    390                                                  * } catch (SecurityException e) { e.printStackTrace(); }
    391                                                  * /*catch (NoSuchMethodException e) {
    392                                                  * e.printStackTrace(); }
    393                                                  */
    394 
    395                                                 // methods that return null are only included if they
    396                                                 // are in the AllowNull list
    397                                         } else if (_AllowNull.contains(m))
    398                                                 attributes.append(m.getName().substring(GET_LENGTH))
    399                                                                 .append(SEPARATOR_CHAR + "\n");
    400                                 } catch (IllegalArgumentException e) {
    401                                         // TODO Auto-generated catch block
    402                                         e.printStackTrace();
    403                                 } catch (IllegalAccessException e) {
    404                                         // TODO Auto-generated catch block
    405                                         e.printStackTrace();
    406                                 } catch (InvocationTargetException e) {
     353                                                                        .append(SEPARATOR_STRING).append(ob)
     354                                                                        .append("\n");
     355                                                continue;
     356                                        } else if (o instanceof int[]) {
     357                                                StringBuffer sb = new StringBuffer();
     358                                                int[] values = (int[]) o;
     359                                                for (int i = 0; i < values.length; i++) {
     360                                                        sb.append(values[i]).append(' ');
     361                                                }
     362                                                sb.deleteCharAt(attributes.length() - 1);
     363                                                o = sb.toString();
     364                                        } else if (o instanceof Boolean) {
     365                                                // true is the default for boolean values
     366                                                if (((Boolean) o).booleanValue())
     367                                                        continue;
     368                                        }
     369                                        //Append the attributes
     370                                        attributes.append(m.getName().substring(GET_LENGTH))
     371                                                        .append(SEPARATOR_STRING).append(o).append("\n");
     372                                } catch (Exception e) {
    407373                                        // TODO Auto-generated catch block
    408374                                        e.printStackTrace();
     
    471437
    472438                // get the list of attribute: value pairs
    473                 List<String> values = attribs.getText();
     439                List<String> values = attribs.getTextList();
    474440                // if no paris exist, we are done
    475441                if (values.size() == 0
     
    495461                                        // if the attribute is the same as v, then it is a
    496462                                        // continuation
    497                                         if (v.indexOf(stripAttribute(next.toString())) == 0) {
     463                                        if (v.indexOf(getAttribute(next.toString())) == 0) {
    498464                                                // strip the attribute from next
    499                                                 next = new StringBuffer(stripValue(next.toString()));
     465                                                next = new StringBuffer(getValue(next.toString()));
    500466                                                // if the attribute is not the same, then it may be a
    501467                                                // new method
     
    519485                                // otherwise, this is a list of attributes, so continue
    520486                                else {
    521                                         String stripped = stripAttribute(v.toString());
     487                                        String stripped = getAttribute(v.toString());
    522488                                        if (stripped == null) {
    523489                                                // This happens when there is an attribute at the start
     
    530496                                                // attributes to ignore when copying
    531497                                                FrameGraphics.WarningMessage("Attribute: '"
    532                                                                 + stripAttribute(v.toString())
     498                                                                + getAttribute(v.toString())
    533499                                                                + "' does not exist.");
    534500                                        } else {
     
    538504                                                        types += c.getSimpleName() + " ";
    539505                                                FrameGraphics.WarningMessage("Wrong arguments for: '"
    540                                                                 + stripAttribute(v.toString()) + "' expecting "
     506                                                                + getAttribute(v.toString()) + "' expecting "
    541507                                                                + types.trim() + " found '"
    542                                                                 + stripValue(v.toString()) + "'");
     508                                                                + getValue(v.toString()) + "'");
    543509                                        }
    544510                                }
     
    553519                        boolean isAttributeList) {
    554520                // separate attribute and value from string
    555                 String attribute = stripAttribute(value);
    556                 value = stripValue(value);
     521                String attribute = getAttribute(value);
     522                value = getValue(value);
    557523
    558524                // if there was no colon
     
    601567                        Class toSetSuperClass = toSetClass.getSuperclass();
    602568                        // find the corresponding get method for this set method
     569                        // and get the current value of the attribute
    603570                        for (Method m : _GetMethods) {
    604571                                if ((m.getDeclaringClass() == toSetClass || m
     
    651618
    652619        /**
    653          * Returns the part of the given string that is after a colon, or null if
    654          * the given String has no colon
     620         * Returns the part of the given string that is after the attribute value
     621         * pair separator. If that character is not there it returns the empty
     622         * string if it is an annotation item or the entire string if it is not.
    655623         *
    656          * @param toStrip
     624         * @param attributeValuePair
     625         *            the string to get the value from.
     626         * @return the value from the attribute value pair.
     627         */
     628        public static String getValue(String toStrip) {
     629                assert (toStrip != null);
     630                if (toStrip.length() == 0)
     631                        return "";
     632
     633                int ind = toStrip.lastIndexOf(SEPARATOR_CHAR);
     634
     635                if (ind < 0) {
     636                        // If it is an annotation item return the empty string
     637                        // Annotation items can not be values only
     638                        if (toStrip.charAt(0) == '@') {
     639                                return "";
     640                        }
     641                        // It is a value with no attribute.
     642                        return toStrip;
     643                }
     644
     645                return toStrip.substring(ind + 1).trim();
     646        }
     647
     648        /**
     649         * Returns the part of the given string that is before the attribute value
     650         * pair separator, or null if the given String does not include the
     651         * separator.
     652         *
     653         * @param attributeValuePair
    657654         *            The String to strip
    658          * @return The part of the String that is after a colon if there is one, or
    659          *         the entire string if there is no colon
     655         * @return the attribute if there is one or null if there is not
    660656         */
    661         public static String stripValue(String toStrip) {
    662                 int ind = toStrip.lastIndexOf(SEPARATOR_CHAR);
    663                 if (ind < 0)
    664                         return toStrip;
    665 
    666                 toStrip = toStrip.substring(ind + 1);
    667                 // This line must be separate from above
    668                 // It removes spaces from the string returned by the above statement
    669                 toStrip = toStrip.trim();
    670 
    671                 return toStrip;
    672         }
    673 
    674         /**
    675          * Returns the part of the given string that is before a colon, or null if
    676          * the given String has no colon
    677          *
    678          * @param toStrip
    679          *            The String to strip
    680          * @return The part of the String that is before a colon if there is one, or
    681          *         null if there is no colon
    682          */
    683         public static String stripAttribute(String toStrip) {
    684                 int ind = toStrip.indexOf(SEPARATOR_CHAR);
     657        public static String getAttribute(String attributeValuePair) {
     658                int ind = attributeValuePair.indexOf(SEPARATOR_CHAR);
    685659                if (ind < 0)
    686660                        return null;
    687661
    688                 toStrip = toStrip.substring(0, ind);
    689                 toStrip = toStrip.trim();
    690 
    691                 if (toStrip.length() == 0)
     662                attributeValuePair = attributeValuePair.substring(0, ind);
     663                attributeValuePair = attributeValuePair.trim();
     664
     665                if (attributeValuePair.length() == 0)
    692666                        return null;
    693667
    694                 return toStrip;
     668                return attributeValuePair;
    695669        }
    696670
     
    698672                assert (value != null);
    699673
    700                 String oldText = text.getTextNoList();
    701                 String attribute = stripAttribute(oldText);
     674                String oldText = text.getText();
     675                String attribute = getAttribute(oldText);
    702676
    703677                if (attribute == null)
    704                         attribute = text.getTextNoList().trim();
     678                        attribute = text.getText().trim();
    705679
    706680                text.setText(attribute + SEPARATOR_STRING + value);
     
    708682
    709683        public static Double getDoubleValue(String attributeValuePair) {
    710                 String value = stripValue(attributeValuePair);
     684                String value = getValue(attributeValuePair);
    711685
    712686                if (value == null)
Note: See TracChangeset for help on using the changeset viewer.