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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.gitblit.wicket;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.TimeZone;
  6. import org.apache.wicket.Component;
  7. import org.apache.wicket.PageParameters;
  8. import org.apache.wicket.behavior.SimpleAttributeModifier;
  9. import org.apache.wicket.markup.html.basic.Label;
  10. import org.eclipse.jgit.lib.Constants;
  11. import com.gitblit.GitBlit;
  12. import com.gitblit.Keys;
  13. import com.gitblit.utils.StringUtils;
  14. import com.gitblit.utils.TimeUtils;
  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 void setTicketCssClass(Component container, String state) {
  26. String css = null;
  27. if (state.equals("open")) {
  28. css = "bug_open";
  29. } else if (state.equals("hold")) {
  30. css = "bug_hold";
  31. } else if (state.equals("resolved")) {
  32. css = "bug_resolved";
  33. } else if (state.equals("invalid")) {
  34. css = "bug_invalid";
  35. }
  36. if (css != null) {
  37. setCssClass(container, css);
  38. }
  39. }
  40. public static void setAlternatingBackground(Component c, int i) {
  41. String clazz = i % 2 == 0 ? "dark" : "light";
  42. setCssClass(c, clazz);
  43. }
  44. public static Label createAuthorLabel(String wicketId, String author) {
  45. Label label = new Label(wicketId, author);
  46. WicketUtils.setHtmlTitle(label, author);
  47. return label;
  48. }
  49. public static PageParameters newRepositoryParameter(String repositoryName) {
  50. return new PageParameters("r=" + repositoryName);
  51. }
  52. public static PageParameters newObjectParameter(String repositoryName, String objectId) {
  53. if (StringUtils.isEmpty(objectId)) {
  54. return newRepositoryParameter(repositoryName);
  55. }
  56. return new PageParameters("r=" + repositoryName + ",h=" + objectId);
  57. }
  58. public static PageParameters newPathParameter(String repositoryName, String objectId, String path) {
  59. if (StringUtils.isEmpty(path)) {
  60. return newObjectParameter(repositoryName, objectId);
  61. }
  62. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path);
  63. }
  64. public static PageParameters newLogPageParameter(String repositoryName, String objectId, int pageNumber) {
  65. if (pageNumber <= 1) {
  66. return newObjectParameter(repositoryName, objectId);
  67. }
  68. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",page=" + pageNumber);
  69. }
  70. public static PageParameters newHistoryPageParameter(String repositoryName, String objectId, String path, int pageNumber) {
  71. if (pageNumber <= 1) {
  72. return newObjectParameter(repositoryName, objectId);
  73. }
  74. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path + ",page=" + pageNumber);
  75. }
  76. public static PageParameters newBlobDiffParameter(String repositoryName, String baseCommitId, String commitId, String path) {
  77. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",f=" + path + ",hb=" + baseCommitId);
  78. }
  79. public static String getRepositoryName(PageParameters params) {
  80. return params.getString("r", "");
  81. }
  82. public static String getObject(PageParameters params) {
  83. return params.getString("h", Constants.HEAD);
  84. }
  85. public static String getPath(PageParameters params) {
  86. return params.getString("f", null);
  87. }
  88. public static String getBaseObjectId(PageParameters params) {
  89. return params.getString("hb", null);
  90. }
  91. public static int getPage(PageParameters params) {
  92. return params.getInt("page", 1); // index from 1
  93. }
  94. public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone) {
  95. DateFormat df = new SimpleDateFormat(GitBlit.self().settings().getString(Keys.web.datestampShortFormat, "MM/dd/yy"));
  96. if (timeZone != null) {
  97. df.setTimeZone(timeZone);
  98. }
  99. String dateString = df.format(date);
  100. String title = TimeUtils.timeAgo(date);
  101. if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000l) {
  102. String tmp = dateString;
  103. dateString = title;
  104. title = tmp;
  105. }
  106. Label label = new Label(wicketId, dateString);
  107. WicketUtils.setCssClass(label, TimeUtils.timeAgoCss(date));
  108. WicketUtils.setHtmlTitle(label, title);
  109. return label;
  110. }
  111. public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone) {
  112. DateFormat df = new SimpleDateFormat(GitBlit.self().settings().getString(Keys.web.datetimestampLongFormat, "EEEE, MMMM d, yyyy h:mm a z"));
  113. if (timeZone != null) {
  114. df.setTimeZone(timeZone);
  115. }
  116. String dateString = df.format(date);
  117. String title = TimeUtils.timeAgo(date);
  118. Label label = new Label(wicketId, dateString);
  119. WicketUtils.setHtmlTitle(label, title);
  120. return label;
  121. }
  122. }