You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptionList.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package cap;
  2. import java.lang.reflect.*;
  3. import java.util.*;
  4. import java.net.*;
  5. import java.text.*;
  6. /**
  7. * This class builds a list of <option> HTML elements given a data object,
  8. * typically a GAPI output object, and a list of accessor method names.
  9. * <p>
  10. * <b>Usage:</b><pre>
  11. * // Create the bank account list select
  12. * RBBankAcctList2Input acctIn = new RBBankAcctList2Input();
  13. * initApiHeader(acctIn.getHeader(),sessInfo);
  14. * ArrayList accts = new ArrayList();
  15. * getBankAccounts(acctIn,accts);
  16. *
  17. * String ol = OptionList.createListHtmlFromApi(accts.toArray(),
  18. * new String[]{"getBankAcctNbr","getBankRtgNbr","getBankAcctTyp"},
  19. * new String[]{"getBankAcctNbr"},
  20. * new MessageFormat("{0}"),
  21. * Integer.parseInt(acctIndex));
  22. *
  23. * </pre>
  24. * @author Rich Price
  25. */
  26. class OptionList
  27. {
  28. private static final String OPTION_PATTERN = "<option value=\"{0}\" {1}>{2}</option>";
  29. private static final Object[] GETTER_ARGS = new Object[0];
  30. private static final Class[] GETTER_ARG_TYPES = new Class[0];
  31. private static final String DELIM = "&";
  32. /**
  33. * Parses the value string and returns a HashMap of name/value pairs
  34. * @return A HashMap of name/value pairs.
  35. * @see createListHtmlFromApi
  36. */
  37. public static HashMap getSelectedValues(String optionListValueString)
  38. {
  39. HashMap map = new HashMap();
  40. if ( optionListValueString != null )
  41. {
  42. StringTokenizer lex = new StringTokenizer(optionListValueString,DELIM + "=",false);
  43. while ( lex.hasMoreTokens() )
  44. map.put(lex.nextToken(),lex.nextToken());
  45. }
  46. return map;
  47. }
  48. /**
  49. * This method creates a String of HTML &lt;option&gt; elements in the following
  50. * format:<p>
  51. * <pre>
  52. * &lt;option value="valueName1=value1^valueName2=value2"&gt; optionValues &lt;option&gt;
  53. * </pre>
  54. * @param api An array of Objects, typically a GAPI output object, from which data
  55. * will be retrieved by name(s).
  56. * @param valueNames An array of method names declared in <code>api</code>. Only
  57. * public methods taking zero arguments can be used. Each non-null value
  58. * is used to create a value string for the particular HTML option element in the form:
  59. * valueName1=value1^valueName2=value2...where valueName[n] is the method name.
  60. * For convenience, the getValues() method will return a HashMap of these name/value
  61. * pairs.
  62. * @param optionNames An array of method names declared in <code>api</code>. Only
  63. * public methods taking zero arguments can be listed. Each non-null value
  64. * is used to create a parameter list to pass to a supplied MessageFormat object.
  65. * Each value retrieved from the api object will be substituted using the MessageFormat
  66. * object, the resulting String is used to create the optionValues string that is
  67. * displayed to the user.
  68. * @param selectedIndex The index of the option that should be selected. If -1, nothing
  69. * will be selected.
  70. *
  71. */
  72. public static String createListHtmlFromApi(Object[] api,
  73. String[] valueNames,
  74. String[] optionNames,
  75. MessageFormat optionFormat,
  76. int selectedIndex )
  77. {
  78. StringBuffer html = new StringBuffer();
  79. for ( int apiIndex = 0; apiIndex < api.length; apiIndex++ )
  80. {
  81. final String[] messageArgs = new String[3];
  82. // for each valueName, use reflection to look up data from the api
  83. StringBuffer buf = new StringBuffer();
  84. for ( int i = 0; i < valueNames.length; i++ )
  85. {
  86. try
  87. {
  88. Method m = api[apiIndex].getClass().getMethod(valueNames[i], GETTER_ARG_TYPES);
  89. String value = m.invoke(api[apiIndex],GETTER_ARGS).toString();
  90. if ( value != null && value.length() > 0 )
  91. {
  92. if ( buf.length() > 0 )
  93. buf.append(DELIM);
  94. buf.append(valueNames[i]).append("=").append(value);
  95. }
  96. }
  97. catch (Exception e) {}
  98. }
  99. // set the first and second value arguments for the pattern
  100. messageArgs[0] = buf.toString();
  101. if ( apiIndex == selectedIndex )
  102. messageArgs[1] = "selected";
  103. else
  104. messageArgs[1] = "";
  105. // now, handle the option part
  106. buf.setLength(0);
  107. String[] optionFormatArgs = new String[optionNames.length];
  108. for ( int i = 0; i < optionNames.length; i++ )
  109. {
  110. try
  111. {
  112. optionFormatArgs[i] = "";
  113. Method m = api[apiIndex].getClass().getMethod(optionNames[i],GETTER_ARG_TYPES);
  114. String value = m.invoke(api[apiIndex],GETTER_ARGS).toString();
  115. if ( value != null )
  116. optionFormatArgs[i] = value;
  117. }
  118. catch(Exception e) {}
  119. }
  120. messageArgs[2] = optionFormat.format(optionFormatArgs,buf,new FieldPosition(0)).toString();
  121. html.append(MessageFormat.format(OPTION_PATTERN,messageArgs));
  122. }
  123. return html.toString();
  124. }
  125. public static void main(String[] args) throws Exception
  126. {
  127. OptionList.createListHtmlFromApi(new Object[]{new String()},new String[]{"getFoo"},new String[]{"getFoo"},new MessageFormat("{0}"),-1);
  128. }
  129. }