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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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")
  108. || filename.endsWith(".xlsx")) {
  109. return newImage(wicketId, "file_excel_16x16.png");
  110. } else if (filename.endsWith(".doc") || filename.endsWith(".docx")) {
  111. return newImage(wicketId, "file_word_16x16.png");
  112. } else if (filename.endsWith(".ppt")) {
  113. return newImage(wicketId, "file_ppt_16x16.png");
  114. } else if (filename.endsWith(".zip")) {
  115. return newImage(wicketId, "file_zip_16x16.png");
  116. } else if (filename.endsWith(".pdf")) {
  117. return newImage(wicketId, "file_acrobat_16x16.png");
  118. } else if (filename.endsWith(".htm") || filename.endsWith(".html")) {
  119. return newImage(wicketId, "file_world_16x16.png");
  120. } else if (filename.endsWith(".xml")) {
  121. return newImage(wicketId, "file_code_16x16.png");
  122. } else if (filename.endsWith(".properties")) {
  123. return newImage(wicketId, "file_settings_16x16.png");
  124. }
  125. List<String> mdExtensions = GitBlit.getStrings(Keys.web.markdownExtensions);
  126. for (String ext : mdExtensions) {
  127. if (filename.endsWith('.' + ext.toLowerCase())) {
  128. return newImage(wicketId, "file_world_16x16.png");
  129. }
  130. }
  131. return newImage(wicketId, "file_16x16.png");
  132. }
  133. public static ContextImage newClearPixel(String wicketId) {
  134. return newImage(wicketId, "pixel.png");
  135. }
  136. public static ContextImage newBlankImage(String wicketId) {
  137. return newImage(wicketId, "blank.png");
  138. }
  139. public static ContextImage newImage(String wicketId, String file) {
  140. return newImage(wicketId, file, null);
  141. }
  142. public static ContextImage newImage(String wicketId, String file, String tooltip) {
  143. ContextImage img = new ContextImage(wicketId, "/com/gitblit/wicket/resources/" + file);
  144. if (!StringUtils.isEmpty(tooltip)) {
  145. setHtmlTooltip(img, tooltip);
  146. }
  147. return img;
  148. }
  149. public static ContextRelativeResource getResource(String file) {
  150. return new ContextRelativeResource("/com/gitblit/wicket/resources/" + file);
  151. }
  152. public static PageParameters newUsernameParameter(String username) {
  153. return new PageParameters("user=" + username);
  154. }
  155. public static PageParameters newRepositoryParameter(String repositoryName) {
  156. return new PageParameters("r=" + repositoryName);
  157. }
  158. public static PageParameters newObjectParameter(String repositoryName, String objectId) {
  159. if (StringUtils.isEmpty(objectId)) {
  160. return newRepositoryParameter(repositoryName);
  161. }
  162. return new PageParameters("r=" + repositoryName + ",h=" + objectId);
  163. }
  164. public static PageParameters newPathParameter(String repositoryName, String objectId,
  165. String path) {
  166. if (StringUtils.isEmpty(path)) {
  167. return newObjectParameter(repositoryName, objectId);
  168. }
  169. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path);
  170. }
  171. public static PageParameters newLogPageParameter(String repositoryName, String objectId,
  172. int pageNumber) {
  173. if (pageNumber <= 1) {
  174. return newObjectParameter(repositoryName, objectId);
  175. }
  176. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",page=" + pageNumber);
  177. }
  178. public static PageParameters newHistoryPageParameter(String repositoryName, String objectId,
  179. String path, int pageNumber) {
  180. if (pageNumber <= 1) {
  181. return newObjectParameter(repositoryName, objectId);
  182. }
  183. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path
  184. + ",page=" + pageNumber);
  185. }
  186. public static PageParameters newBlobDiffParameter(String repositoryName, String baseCommitId,
  187. String commitId, String path) {
  188. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",f=" + path + ",hb="
  189. + baseCommitId);
  190. }
  191. public static PageParameters newSearchParameter(String repositoryName, String commitId,
  192. String search, SearchType type) {
  193. if (StringUtils.isEmpty(commitId)) {
  194. return new PageParameters("r=" + repositoryName + ",s=" + search + ",st=" + type.name());
  195. }
  196. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search
  197. + ",st=" + type.name());
  198. }
  199. public static PageParameters newSearchParameter(String repositoryName, String commitId,
  200. String search, SearchType type, int pageNumber) {
  201. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search
  202. + ",st=" + type.name() + ",page=" + pageNumber);
  203. }
  204. public static String getRepositoryName(PageParameters params) {
  205. return params.getString("r", "");
  206. }
  207. public static String getObject(PageParameters params) {
  208. return params.getString("h", Constants.HEAD);
  209. }
  210. public static String getPath(PageParameters params) {
  211. return params.getString("f", null);
  212. }
  213. public static String getBaseObjectId(PageParameters params) {
  214. return params.getString("hb", null);
  215. }
  216. public static String getSearchString(PageParameters params) {
  217. return params.getString("s", null);
  218. }
  219. public static String getSearchType(PageParameters params) {
  220. return params.getString("st", null);
  221. }
  222. public static int getPage(PageParameters params) {
  223. // index from 1
  224. return params.getInt("page", 1);
  225. }
  226. public static String getUsername(PageParameters params) {
  227. return params.getString("user", "");
  228. }
  229. public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone) {
  230. String format = GitBlit.getString(Keys.web.datestampShortFormat, "MM/dd/yy");
  231. DateFormat df = new SimpleDateFormat(format);
  232. if (timeZone != null) {
  233. df.setTimeZone(timeZone);
  234. }
  235. String dateString = df.format(date);
  236. String title = TimeUtils.timeAgo(date);
  237. if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
  238. String tmp = dateString;
  239. dateString = title;
  240. title = tmp;
  241. }
  242. Label label = new Label(wicketId, dateString);
  243. WicketUtils.setCssClass(label, TimeUtils.timeAgoCss(date));
  244. WicketUtils.setHtmlTooltip(label, title);
  245. return label;
  246. }
  247. public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone) {
  248. String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
  249. "EEEE, MMMM d, yyyy h:mm a z");
  250. DateFormat df = new SimpleDateFormat(format);
  251. if (timeZone != null) {
  252. df.setTimeZone(timeZone);
  253. }
  254. String dateString = df.format(date);
  255. String title = TimeUtils.timeAgo(date);
  256. Label label = new Label(wicketId, dateString);
  257. WicketUtils.setHtmlTooltip(label, title);
  258. return label;
  259. }
  260. }