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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket;
  17. import java.text.DateFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.TimeZone;
  22. import org.apache.wicket.Component;
  23. import org.apache.wicket.PageParameters;
  24. import org.apache.wicket.behavior.SimpleAttributeModifier;
  25. import org.apache.wicket.markup.html.basic.Label;
  26. import org.apache.wicket.markup.html.image.ContextImage;
  27. import org.apache.wicket.resource.ContextRelativeResource;
  28. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  29. import org.eclipse.jgit.lib.Constants;
  30. import com.gitblit.GitBlit;
  31. import com.gitblit.Keys;
  32. import com.gitblit.utils.JGitUtils.SearchType;
  33. import com.gitblit.utils.StringUtils;
  34. import com.gitblit.utils.TimeUtils;
  35. public class WicketUtils {
  36. public static void setCssClass(Component container, String value) {
  37. container.add(new SimpleAttributeModifier("class", value));
  38. }
  39. public static void setCssStyle(Component container, String value) {
  40. container.add(new SimpleAttributeModifier("style", value));
  41. }
  42. public static void setHtmlTooltip(Component container, String value) {
  43. container.add(new SimpleAttributeModifier("title", value));
  44. }
  45. public static void setInputPlaceholder(Component container, String value) {
  46. container.add(new SimpleAttributeModifier("placeholder", value));
  47. }
  48. public static void setChangeTypeCssClass(Component container, ChangeType type) {
  49. switch (type) {
  50. case ADD:
  51. setCssClass(container, "addition");
  52. break;
  53. case COPY:
  54. case RENAME:
  55. setCssClass(container, "rename");
  56. break;
  57. case DELETE:
  58. setCssClass(container, "deletion");
  59. break;
  60. case MODIFY:
  61. setCssClass(container, "modification");
  62. break;
  63. }
  64. }
  65. public static void setTicketCssClass(Component container, String state) {
  66. String css = null;
  67. if (state.equals("open")) {
  68. css = "bug_open";
  69. } else if (state.equals("hold")) {
  70. css = "bug_hold";
  71. } else if (state.equals("resolved")) {
  72. css = "bug_resolved";
  73. } else if (state.equals("invalid")) {
  74. css = "bug_invalid";
  75. }
  76. if (css != null) {
  77. setCssClass(container, css);
  78. }
  79. }
  80. public static void setAlternatingBackground(Component c, int i) {
  81. String clazz = i % 2 == 0 ? "dark" : "light";
  82. setCssClass(c, clazz);
  83. }
  84. public static Label createAuthorLabel(String wicketId, String author) {
  85. Label label = new Label(wicketId, author);
  86. WicketUtils.setHtmlTooltip(label, author);
  87. return label;
  88. }
  89. public static ContextImage getFileImage(String wicketId, String filename) {
  90. filename = filename.toLowerCase();
  91. if (filename.endsWith(".java")) {
  92. return newImage(wicketId, "file_java_16x16.png");
  93. } else if (filename.endsWith(".rb")) {
  94. return newImage(wicketId, "file_ruby_16x16.png");
  95. } else if (filename.endsWith(".php")) {
  96. return newImage(wicketId, "file_php_16x16.png");
  97. } else if (filename.endsWith(".cs")) {
  98. return newImage(wicketId, "file_cs_16x16.png");
  99. } else if (filename.endsWith(".cpp")) {
  100. return newImage(wicketId, "file_cpp_16x16.png");
  101. } else if (filename.endsWith(".c")) {
  102. return newImage(wicketId, "file_c_16x16.png");
  103. } else if (filename.endsWith(".h")) {
  104. return newImage(wicketId, "file_h_16x16.png");
  105. } else if (filename.endsWith(".sln")) {
  106. return newImage(wicketId, "file_vs_16x16.png");
  107. } else if (filename.endsWith(".csv") || filename.endsWith(".xls") || filename.endsWith(".xlsx")) {
  108. return newImage(wicketId, "file_excel_16x16.png");
  109. } else if (filename.endsWith(".doc") || filename.endsWith(".docx")) {
  110. return newImage(wicketId, "file_word_16x16.png");
  111. } else if (filename.endsWith(".ppt")) {
  112. return newImage(wicketId, "file_ppt_16x16.png");
  113. } else if (filename.endsWith(".zip")) {
  114. return newImage(wicketId, "file_zip_16x16.png");
  115. } else if (filename.endsWith(".pdf")) {
  116. return newImage(wicketId, "file_acrobat_16x16.png");
  117. } else if (filename.endsWith(".htm") || filename.endsWith(".html")) {
  118. return newImage(wicketId, "file_world_16x16.png");
  119. } else if (filename.endsWith(".xml")) {
  120. return newImage(wicketId, "file_code_16x16.png");
  121. } else if (filename.endsWith(".properties")) {
  122. return newImage(wicketId, "file_settings_16x16.png");
  123. }
  124. List<String> mdExtensions = GitBlit.self().settings().getStrings(Keys.web.markdownExtensions);
  125. for (String ext : mdExtensions) {
  126. if (filename.endsWith('.' + ext.toLowerCase())) {
  127. return newImage(wicketId, "file_world_16x16.png");
  128. }
  129. }
  130. return newImage(wicketId, "file_16x16.png");
  131. }
  132. public static ContextImage newClearPixel(String wicketId) {
  133. return newImage(wicketId, "pixel.png");
  134. }
  135. public static ContextImage newBlankImage(String wicketId) {
  136. return newImage(wicketId, "blank.png");
  137. }
  138. public static ContextImage newImage(String wicketId, String file) {
  139. return newImage(wicketId, file, null);
  140. }
  141. public static ContextImage newImage(String wicketId, String file, String tooltip) {
  142. ContextImage img = new ContextImage(wicketId, "/com/gitblit/wicket/resources/" + file);
  143. if (!StringUtils.isEmpty(tooltip)) {
  144. setHtmlTooltip(img, tooltip);
  145. }
  146. return img;
  147. }
  148. public static ContextRelativeResource getResource(String file) {
  149. return new ContextRelativeResource("/com/gitblit/wicket/resources/" + file);
  150. }
  151. public static PageParameters newUsernameParameter(String username) {
  152. return new PageParameters("user=" + username);
  153. }
  154. public static PageParameters newRepositoryParameter(String repositoryName) {
  155. return new PageParameters("r=" + repositoryName);
  156. }
  157. public static PageParameters newObjectParameter(String repositoryName, String objectId) {
  158. if (StringUtils.isEmpty(objectId)) {
  159. return newRepositoryParameter(repositoryName);
  160. }
  161. return new PageParameters("r=" + repositoryName + ",h=" + objectId);
  162. }
  163. public static PageParameters newPathParameter(String repositoryName, String objectId, String path) {
  164. if (StringUtils.isEmpty(path)) {
  165. return newObjectParameter(repositoryName, objectId);
  166. }
  167. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path);
  168. }
  169. public static PageParameters newLogPageParameter(String repositoryName, String objectId, int pageNumber) {
  170. if (pageNumber <= 1) {
  171. return newObjectParameter(repositoryName, objectId);
  172. }
  173. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",page=" + pageNumber);
  174. }
  175. public static PageParameters newHistoryPageParameter(String repositoryName, String objectId, String path, int pageNumber) {
  176. if (pageNumber <= 1) {
  177. return newObjectParameter(repositoryName, objectId);
  178. }
  179. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path + ",page=" + pageNumber);
  180. }
  181. public static PageParameters newBlobDiffParameter(String repositoryName, String baseCommitId, String commitId, String path) {
  182. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",f=" + path + ",hb=" + baseCommitId);
  183. }
  184. public static PageParameters newSearchParameter(String repositoryName, String commitId, String search, SearchType type) {
  185. if (StringUtils.isEmpty(commitId)) {
  186. return new PageParameters("r=" + repositoryName + ",s=" + search + ",st=" + type.name());
  187. }
  188. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search + ",st=" + type.name());
  189. }
  190. public static PageParameters newSearchParameter(String repositoryName, String commitId, String search, SearchType type, int pageNumber) {
  191. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search + ",st=" + type.name() + ",page=" + pageNumber);
  192. }
  193. public static String getRepositoryName(PageParameters params) {
  194. return params.getString("r", "");
  195. }
  196. public static String getObject(PageParameters params) {
  197. return params.getString("h", Constants.HEAD);
  198. }
  199. public static String getPath(PageParameters params) {
  200. return params.getString("f", null);
  201. }
  202. public static String getBaseObjectId(PageParameters params) {
  203. return params.getString("hb", null);
  204. }
  205. public static String getSearchString(PageParameters params) {
  206. return params.getString("s", null);
  207. }
  208. public static String getSearchType(PageParameters params) {
  209. return params.getString("st", null);
  210. }
  211. public static int getPage(PageParameters params) {
  212. return params.getInt("page", 1); // index from 1
  213. }
  214. public static String getUsername(PageParameters params) {
  215. return params.getString("user", "");
  216. }
  217. public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone) {
  218. DateFormat df = new SimpleDateFormat(GitBlit.self().settings().getString(Keys.web.datestampShortFormat, "MM/dd/yy"));
  219. if (timeZone != null) {
  220. df.setTimeZone(timeZone);
  221. }
  222. String dateString = df.format(date);
  223. String title = TimeUtils.timeAgo(date);
  224. if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000l) {
  225. String tmp = dateString;
  226. dateString = title;
  227. title = tmp;
  228. }
  229. Label label = new Label(wicketId, dateString);
  230. WicketUtils.setCssClass(label, TimeUtils.timeAgoCss(date));
  231. WicketUtils.setHtmlTooltip(label, title);
  232. return label;
  233. }
  234. public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone) {
  235. DateFormat df = new SimpleDateFormat(GitBlit.self().settings().getString(Keys.web.datetimestampLongFormat, "EEEE, MMMM d, yyyy h:mm a z"));
  236. if (timeZone != null) {
  237. df.setTimeZone(timeZone);
  238. }
  239. String dateString = df.format(date);
  240. String title = TimeUtils.timeAgo(date);
  241. Label label = new Label(wicketId, dateString);
  242. WicketUtils.setHtmlTooltip(label, title);
  243. return label;
  244. }
  245. }