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.

WicketUtils.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.gitblit.wicket;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.TimeZone;
  7. import org.apache.wicket.Component;
  8. import org.apache.wicket.PageParameters;
  9. import org.apache.wicket.behavior.SimpleAttributeModifier;
  10. import org.apache.wicket.markup.html.basic.Label;
  11. import org.eclipse.jgit.lib.Constants;
  12. import com.gitblit.Keys;
  13. import com.gitblit.StoredSettings;
  14. import com.gitblit.utils.Utils;
  15. public class WicketUtils {
  16. public static void setCssClass(Component container, String value) {
  17. container.add(new SimpleAttributeModifier("class", value));
  18. }
  19. public static void setCssStyle(Component container, String value) {
  20. container.add(new SimpleAttributeModifier("style", value));
  21. }
  22. public static void setHtmlTitle(Component container, String value) {
  23. container.add(new SimpleAttributeModifier("title", value));
  24. }
  25. public static String breakLines(String string) {
  26. return string.replace("\r", "<br/>").replace("\n", "<br/>");
  27. }
  28. public static void setTicketCssClass(Component container, String state) {
  29. String css = null;
  30. if (state.equals("open")) {
  31. css = "bug_open";
  32. } else if (state.equals("hold")) {
  33. css = "bug_hold";
  34. } else if (state.equals("resolved")) {
  35. css = "bug_resolved";
  36. } else if (state.equals("invalid")) {
  37. css = "bug_invalid";
  38. }
  39. if (css != null) {
  40. setCssClass(container, css);
  41. }
  42. }
  43. public static String flattenStrings(List<String> values) {
  44. StringBuilder sb = new StringBuilder();
  45. for (String value : values) {
  46. sb.append(value).append(" ");
  47. }
  48. return sb.toString().trim();
  49. }
  50. public static void setAlternatingBackground(Component c, int i) {
  51. String clazz = i % 2 == 0 ? "dark" : "light";
  52. setCssClass(c, clazz);
  53. }
  54. public static Label createAuthorLabel(String wicketId, String author) {
  55. Label label = new Label(wicketId, author);
  56. WicketUtils.setHtmlTitle(label, author);
  57. return label;
  58. }
  59. public static String trimShortLog(String string) {
  60. return trimString(string, 60);
  61. }
  62. public static String trimString(String value, int max) {
  63. if (value.length() <= max) {
  64. return value;
  65. }
  66. return value.substring(0, max - 3) + "...";
  67. }
  68. public static PageParameters newRepositoryParameter(String repositoryName) {
  69. return new PageParameters("r=" + repositoryName);
  70. }
  71. public static PageParameters newObjectParameter(String repositoryName, String objectId) {
  72. if (objectId == null || objectId.trim().length() == 0) {
  73. return newRepositoryParameter(repositoryName);
  74. }
  75. return new PageParameters("r=" + repositoryName + ",h=" + objectId);
  76. }
  77. public static PageParameters newPathParameter(String repositoryName, String objectId, String path) {
  78. if (path == null || path.trim().length() == 0) {
  79. return newObjectParameter(repositoryName, objectId);
  80. }
  81. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path);
  82. }
  83. public static PageParameters newLogPageParameter(String repositoryName, String objectId, int pageNumber) {
  84. if (pageNumber <= 1) {
  85. return newObjectParameter(repositoryName, objectId);
  86. }
  87. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",page=" + pageNumber);
  88. }
  89. public static String getRepositoryName(PageParameters params) {
  90. return params.getString("r", "");
  91. }
  92. public static String getObject(PageParameters params) {
  93. return params.getString("h", Constants.HEAD);
  94. }
  95. public static String getPath(PageParameters params) {
  96. return params.getString("f", null);
  97. }
  98. public static int getPage(PageParameters params) {
  99. return params.getInt("page", 1); // index from 1
  100. }
  101. public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone) {
  102. DateFormat df = new SimpleDateFormat(StoredSettings.getString(Keys.web_datestampShortFormat, "MM/dd/yy"));
  103. if (timeZone != null) {
  104. df.setTimeZone(timeZone);
  105. }
  106. String dateString = df.format(date);
  107. String title = Utils.timeAgo(date);
  108. if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000l) {
  109. String tmp = dateString;
  110. dateString = title;
  111. title = tmp;
  112. }
  113. Label label = new Label(wicketId, dateString);
  114. WicketUtils.setCssClass(label, Utils.timeAgoCss(date));
  115. WicketUtils.setHtmlTitle(label, title);
  116. return label;
  117. }
  118. public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone) {
  119. DateFormat df = new SimpleDateFormat(StoredSettings.getString(Keys.web_datetimestampLongFormat, "EEEE, MMMM d, yyyy h:mm a z"));
  120. if (timeZone != null) {
  121. df.setTimeZone(timeZone);
  122. }
  123. String dateString = df.format(date);
  124. String title = Utils.timeAgo(date);
  125. Label label = new Label(wicketId, dateString);
  126. WicketUtils.setHtmlTitle(label, title);
  127. return label;
  128. }
  129. }