private static Attribute getAttribute(List<Attribute> attrs, String attempName) {
   for (Iterator<Attribute> iterator = attrs.iterator(); iterator.hasNext(); ) {
     Attribute attribute = iterator.next();
     if (attribute.getSystemName().equalsIgnoreCase(attempName)) {
       return attribute;
     }
   }
   return null;
 }
  /**
   * Translates the given JFormDesigner generated Java GridBagLayout source code to ActionScript.
   *
   * @param entity the target entity
   * @param containerVarName If not null, it is the container var name in ActionScript; otherwise
   *     the container var name is used from Java.
   * @param sourceJava the editor generated Java source code
   * @return the translated ActionScript source code
   */
  public static String translateGridBagLayoutSourceToFlex(
      Entity entity, String containerVarName, String sourceJava) {
    StringBuilder sb = new StringBuilder();
    Pattern pattern = Pattern.compile(REGEX_COMPONENT_HEADER);
    Matcher matcher = pattern.matcher(sourceJava);

    List<LayoutComponent> lcs = new ArrayList<LayoutComponent>();
    int lastEndPos = -1;
    String lastVarName = null;
    while (matcher.find()) {
      if (lastEndPos != -1) {
        lcs.addAll(parseComponent(lastVarName, sourceJava.substring(lastEndPos, matcher.start())));
      }
      lastEndPos = matcher.end();
      lastVarName = matcher.group(1);
    }

    if (lastEndPos != -1) {
      lcs.addAll(parseComponent(lastVarName, sourceJava.substring(lastEndPos)));
    }

    List<Attribute> attrs = entity == null ? null : entity.getAttributes();
    List<Relationship> rels = entity == null ? null : entity.getRelationships();

    for (int i = 0; i < lcs.size(); i++) {
      LayoutComponent lc = lcs.get(i);
      LayoutComponent lcPair = null;

      for (int k = i + 1; entity != null && k < lcs.size(); k++) {
        if (sameAttributeOrRel(lc, lcs.get(k))) {
          lcPair = lcs.get(k);
          lcs.remove(k); // remove it.
          break;
        }
      }

      Attribute attr = entity == null ? null : getAttribute(attrs, lc);
      Relationship rel = entity == null ? null : getRelationship(rels, lc);

      if (rel != null && !rel.isToOne()) { // not to-one; unable to handle.
        rel = null;
      }

      if (lcPair != null) { // same one.
        if (attr == null && rel == null) {
          sb.append("\t\t")
              .append(getVarNameWithDot(containerVarName, lcPair.containerVarName))
              .append("addComponent(")
              .append(lc.varName)
              .append("/*")
              .append(lc.varName);
          if (lc.text != null && lc.text.trim().length() > 0) {
            sb.append("-'").append(lc.text).append("'");
          }
          sb.append("*/, ").append(lc.gridBagLayoutConstrains).append("); \n");
          // pair.
          sb.append("\t\t")
              .append(getVarNameWithDot(containerVarName, lcPair.containerVarName))
              .append("addComponent(")
              .append(lcPair.varName)
              .append("/*")
              .append(lcPair.varName);
          if (lcPair.text != null && lcPair.text.trim().length() > 0) {
            sb.append("-'").append(lcPair.text).append("'");
          }
          sb.append("*/, ").append(lcPair.gridBagLayoutConstrains).append("); \n");
        } else {
          if (attr == null) { // use rel
            sb.append("\t\t_editorGroup.createAndAddRelToOneLookupToContainer(")
                .append(entity.getSystemName())
                .append("_EO.REL_")
                .append(rel == null ? "!" + lc.varName : rel.getSystemName());
          } else { // use attr
            sb.append("\t\t_editorGroup.createAndAddAttrEditorToContainer(")
                .append(entity.getSystemName())
                .append("_EO.ATTR_")
                .append(attr == null ? "!" + lc.varName : attr.getSystemName());
          }
          sb.append(", ")
              .append(
                  containerVarName == null || containerVarName.trim().length() == 0
                      ? lc.containerVarName
                      : containerVarName)
              .append(", true, ")
              .append(lc.gridBagLayoutConstrains)
              .append(", \n\t\t\t")
              .append(lcPair.gridBagLayoutConstrains)
              .append("); \n");
        }
      } else { // lonely, assuming no label.
        if (attr == null && rel == null) {
          sb.append("\t\t")
              .append(getVarNameWithDot(containerVarName, lc.containerVarName))
              .append("addComponent(")
              .append(lc.varName)
              .append("/*")
              .append(lc.varName);
          if (lc.text != null && lc.text.trim().length() > 0) {
            sb.append("-'").append(lc.text).append("'");
          }
          sb.append("*/, ").append(lc.gridBagLayoutConstrains).append("); \n");
        } else {
          if (attr == null) { // use rel
            sb.append("\t\t_editorGroup.createAndAddRelToOneLookupToContainer(")
                .append(entity.getSystemName())
                .append("_EO.REL_")
                .append(rel == null ? "!" + lc.varName : rel.getSystemName());
          } else { // use attr
            sb.append("\t\t_editorGroup.createAndAddAttrEditorToContainer(")
                .append(entity.getSystemName())
                .append("_EO.ATTR_")
                .append(attr == null ? "!" + lc.varName : attr.getSystemName());
          }
          sb.append(", ")
              .append(
                  containerVarName == null || containerVarName.trim().length() == 0
                      ? lc.containerVarName
                      : containerVarName)
              .append(", false, ")
              .append("null")
              .append(", ")
              .append(lc.gridBagLayoutConstrains)
              .append(");\n");
        }
      }
    }

    return sb.toString();
  }