TextLayoutHelper.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package lib;
  2. public class TextLayoutHelper {
  3. /**
  4. * Modifies the given string such that its lenght matches the given length.
  5. * If the string is too small, whitespace is appended on both sides equally.
  6. * Then, if the string is too large, it is cut off on the right.
  7. * @param s the string
  8. * @param lenght the target length
  9. * @return the modified string
  10. */
  11. public static String strToLen( String s, int lenght )
  12. {
  13. while( s.length() < lenght )
  14. {
  15. s = " " + s + " ";
  16. }
  17. if( s.length() > lenght )
  18. return s.substring( 0, lenght );
  19. return s;
  20. }
  21. private static String[] keywords = { "for", "do", "to", "then", "else", "if", "foreach", "while", "or", "and" };
  22. private static String[] delimiter = { "\\+", "\\-", "\\[", "\\]", "\\|", " ", "^", "$", "\\=", "\\,", "\\(", "\\;" };
  23. private static String getDelimiterRegex()
  24. {
  25. String reg = "(";
  26. for( String d : delimiter )
  27. {
  28. reg += d + "|";
  29. }
  30. reg = reg.substring( 0, reg.length() - 1 ) + ")";
  31. return reg;
  32. }
  33. public static String setupPseudoCodeStage( String s )
  34. {
  35. return "<html><font color=#FDD017>"+s+"</font></html>";
  36. }
  37. public static String setupPseudoCode( String s, String[] vars )
  38. {
  39. String delimiter = getDelimiterRegex();
  40. String ret = "<html>";
  41. String current = s.replaceAll( "&", "&amp" ).replaceAll( "<", "&lt").replaceAll( ">", "&gt");
  42. for( String k : keywords )
  43. {
  44. current = current.replaceAll( delimiter + "(" + k + ")" + delimiter, "$1<font color=orange>$2</font>$3" );
  45. }
  46. for( String v : vars )
  47. {
  48. current = current.replaceAll( delimiter + "(" + v + ")" + delimiter, "$1<font color=#3BB9FF>$2</font>$3" );
  49. }
  50. ret += current + "</html>";
  51. return ret;
  52. }
  53. }