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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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.MessageFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Collection;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.TimeZone;
  24. import javax.servlet.http.HttpServletRequest;
  25. import org.apache.wicket.Component;
  26. import org.apache.wicket.PageParameters;
  27. import org.apache.wicket.Request;
  28. import org.apache.wicket.behavior.HeaderContributor;
  29. import org.apache.wicket.behavior.SimpleAttributeModifier;
  30. import org.apache.wicket.markup.html.IHeaderContributor;
  31. import org.apache.wicket.markup.html.IHeaderResponse;
  32. import org.apache.wicket.markup.html.basic.Label;
  33. import org.apache.wicket.markup.html.image.ContextImage;
  34. import org.apache.wicket.protocol.http.WebRequest;
  35. import org.apache.wicket.resource.ContextRelativeResource;
  36. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  37. import org.wicketstuff.googlecharts.AbstractChartData;
  38. import org.wicketstuff.googlecharts.IChartData;
  39. import com.gitblit.Constants;
  40. import com.gitblit.Constants.AccessPermission;
  41. import com.gitblit.Constants.FederationPullStatus;
  42. import com.gitblit.GitBlit;
  43. import com.gitblit.Keys;
  44. import com.gitblit.models.FederationModel;
  45. import com.gitblit.models.Metric;
  46. import com.gitblit.utils.HttpUtils;
  47. import com.gitblit.utils.StringUtils;
  48. import com.gitblit.utils.TimeUtils;
  49. public class WicketUtils {
  50. public static void setCssClass(Component container, String value) {
  51. container.add(new SimpleAttributeModifier("class", value));
  52. }
  53. public static void setCssStyle(Component container, String value) {
  54. container.add(new SimpleAttributeModifier("style", value));
  55. }
  56. public static void setCssBackground(Component container, String value) {
  57. String background = MessageFormat.format("background-color:{0};",
  58. StringUtils.getColor(value));
  59. container.add(new SimpleAttributeModifier("style", background));
  60. }
  61. public static void setHtmlTooltip(Component container, String value) {
  62. container.add(new SimpleAttributeModifier("title", value));
  63. }
  64. public static void setInputPlaceholder(Component container, String value) {
  65. container.add(new SimpleAttributeModifier("placeholder", value));
  66. }
  67. public static void setChangeTypeCssClass(Component container, ChangeType type) {
  68. switch (type) {
  69. case ADD:
  70. setCssClass(container, "addition");
  71. break;
  72. case COPY:
  73. case RENAME:
  74. setCssClass(container, "rename");
  75. break;
  76. case DELETE:
  77. setCssClass(container, "deletion");
  78. break;
  79. case MODIFY:
  80. setCssClass(container, "modification");
  81. break;
  82. }
  83. }
  84. public static void setTicketCssClass(Component container, String state) {
  85. String css = null;
  86. if (state.equals("open")) {
  87. css = "label label-important";
  88. } else if (state.equals("hold")) {
  89. css = "label label-warning";
  90. } else if (state.equals("resolved")) {
  91. css = "label label-success";
  92. } else if (state.equals("invalid")) {
  93. css = "label";
  94. }
  95. if (css != null) {
  96. setCssClass(container, css);
  97. }
  98. }
  99. public static void setPermissionClass(Component container, AccessPermission permission) {
  100. if (permission == null) {
  101. setCssClass(container, "badge");
  102. return;
  103. }
  104. switch (permission) {
  105. case REWIND:
  106. case DELETE:
  107. case CREATE:
  108. setCssClass(container, "badge badge-success");
  109. break;
  110. case PUSH:
  111. setCssClass(container, "badge badge-info");
  112. break;
  113. case CLONE:
  114. setCssClass(container, "badge badge-inverse");
  115. break;
  116. default:
  117. setCssClass(container, "badge");
  118. break;
  119. }
  120. }
  121. public static void setAlternatingBackground(Component c, int i) {
  122. String clazz = i % 2 == 0 ? "light" : "dark";
  123. setCssClass(c, clazz);
  124. }
  125. public static Label createAuthorLabel(String wicketId, String author) {
  126. Label label = new Label(wicketId, author);
  127. WicketUtils.setHtmlTooltip(label, author);
  128. return label;
  129. }
  130. public static ContextImage getPullStatusImage(String wicketId, FederationPullStatus status) {
  131. String filename = null;
  132. switch (status) {
  133. case MIRRORED:
  134. case PULLED:
  135. filename = "bullet_green.png";
  136. break;
  137. case SKIPPED:
  138. filename = "bullet_yellow.png";
  139. break;
  140. case FAILED:
  141. filename = "bullet_red.png";
  142. break;
  143. case EXCLUDED:
  144. filename = "bullet_white.png";
  145. break;
  146. case PENDING:
  147. case NOCHANGE:
  148. default:
  149. filename = "bullet_black.png";
  150. }
  151. return WicketUtils.newImage(wicketId, filename, status.name());
  152. }
  153. public static ContextImage getFileImage(String wicketId, String filename) {
  154. filename = filename.toLowerCase();
  155. if (filename.endsWith(".java")) {
  156. return newImage(wicketId, "file_java_16x16.png");
  157. } else if (filename.endsWith(".rb")) {
  158. return newImage(wicketId, "file_ruby_16x16.png");
  159. } else if (filename.endsWith(".php")) {
  160. return newImage(wicketId, "file_php_16x16.png");
  161. } else if (filename.endsWith(".cs")) {
  162. return newImage(wicketId, "file_cs_16x16.png");
  163. } else if (filename.endsWith(".cpp")) {
  164. return newImage(wicketId, "file_cpp_16x16.png");
  165. } else if (filename.endsWith(".c")) {
  166. return newImage(wicketId, "file_c_16x16.png");
  167. } else if (filename.endsWith(".h")) {
  168. return newImage(wicketId, "file_h_16x16.png");
  169. } else if (filename.endsWith(".sln")) {
  170. return newImage(wicketId, "file_vs_16x16.png");
  171. } else if (filename.endsWith(".csv") || filename.endsWith(".xls")
  172. || filename.endsWith(".xlsx")) {
  173. return newImage(wicketId, "file_excel_16x16.png");
  174. } else if (filename.endsWith(".doc") || filename.endsWith(".docx")) {
  175. return newImage(wicketId, "file_doc_16x16.png");
  176. } else if (filename.endsWith(".ppt")) {
  177. return newImage(wicketId, "file_ppt_16x16.png");
  178. } else if (filename.endsWith(".zip")) {
  179. return newImage(wicketId, "file_zip_16x16.png");
  180. } else if (filename.endsWith(".pdf")) {
  181. return newImage(wicketId, "file_acrobat_16x16.png");
  182. } else if (filename.endsWith(".htm") || filename.endsWith(".html")) {
  183. return newImage(wicketId, "file_world_16x16.png");
  184. } else if (filename.endsWith(".xml")) {
  185. return newImage(wicketId, "file_code_16x16.png");
  186. } else if (filename.endsWith(".properties")) {
  187. return newImage(wicketId, "file_settings_16x16.png");
  188. }
  189. List<String> mdExtensions = GitBlit.getStrings(Keys.web.markdownExtensions);
  190. for (String ext : mdExtensions) {
  191. if (filename.endsWith('.' + ext.toLowerCase())) {
  192. return newImage(wicketId, "file_world_16x16.png");
  193. }
  194. }
  195. return newImage(wicketId, "file_16x16.png");
  196. }
  197. public static ContextImage getRegistrationImage(String wicketId, FederationModel registration,
  198. Component c) {
  199. if (registration.isResultData()) {
  200. return WicketUtils.newImage(wicketId, "information_16x16.png",
  201. c.getString("gb.federationResults"));
  202. } else {
  203. return WicketUtils.newImage(wicketId, "arrow_left.png",
  204. c.getString("gb.federationRegistration"));
  205. }
  206. }
  207. public static ContextImage newClearPixel(String wicketId) {
  208. return newImage(wicketId, "pixel.png");
  209. }
  210. public static ContextImage newBlankImage(String wicketId) {
  211. return newImage(wicketId, "blank.png");
  212. }
  213. public static ContextImage newImage(String wicketId, String file) {
  214. return newImage(wicketId, file, null);
  215. }
  216. public static ContextImage newImage(String wicketId, String file, String tooltip) {
  217. ContextImage img = new ContextImage(wicketId, file);
  218. if (!StringUtils.isEmpty(tooltip)) {
  219. setHtmlTooltip(img, tooltip);
  220. }
  221. return img;
  222. }
  223. public static Label newIcon(String wicketId, String css) {
  224. Label lbl = new Label(wicketId);
  225. setCssClass(lbl, css);
  226. return lbl;
  227. }
  228. public static Label newBlankIcon(String wicketId) {
  229. Label lbl = new Label(wicketId);
  230. setCssClass(lbl, "");
  231. lbl.setRenderBodyOnly(true);
  232. return lbl;
  233. }
  234. public static ContextRelativeResource getResource(String file) {
  235. return new ContextRelativeResource(file);
  236. }
  237. public static String getGitblitURL(Request request) {
  238. HttpServletRequest req = ((WebRequest) request).getHttpServletRequest();
  239. return HttpUtils.getGitblitURL(req);
  240. }
  241. public static HeaderContributor syndicationDiscoveryLink(final String feedTitle,
  242. final String url) {
  243. return new HeaderContributor(new IHeaderContributor() {
  244. private static final long serialVersionUID = 1L;
  245. public void renderHead(IHeaderResponse response) {
  246. String contentType = "application/rss+xml";
  247. StringBuilder buffer = new StringBuilder();
  248. buffer.append("<link rel=\"alternate\" ");
  249. buffer.append("type=\"").append(contentType).append("\" ");
  250. buffer.append("title=\"").append(feedTitle).append("\" ");
  251. buffer.append("href=\"").append(url).append("\" />");
  252. response.renderString(buffer.toString());
  253. }
  254. });
  255. }
  256. public static PageParameters newTokenParameter(String token) {
  257. return new PageParameters("t=" + token);
  258. }
  259. public static PageParameters newRegistrationParameter(String url, String name) {
  260. return new PageParameters("u=" + url + ",n=" + name);
  261. }
  262. public static PageParameters newUsernameParameter(String username) {
  263. return new PageParameters("user=" + username);
  264. }
  265. public static PageParameters newTeamnameParameter(String teamname) {
  266. return new PageParameters("team=" + teamname);
  267. }
  268. public static PageParameters newProjectParameter(String projectName) {
  269. return new PageParameters("p=" + projectName);
  270. }
  271. public static PageParameters newRepositoryParameter(String repositoryName) {
  272. return new PageParameters("r=" + repositoryName);
  273. }
  274. public static PageParameters newObjectParameter(String objectId) {
  275. return new PageParameters("h=" + objectId);
  276. }
  277. public static PageParameters newObjectParameter(String repositoryName, String objectId) {
  278. if (StringUtils.isEmpty(objectId)) {
  279. return newRepositoryParameter(repositoryName);
  280. }
  281. return new PageParameters("r=" + repositoryName + ",h=" + objectId);
  282. }
  283. public static PageParameters newRangeParameter(String repositoryName, String startRange, String endRange) {
  284. return new PageParameters("r=" + repositoryName + ",h=" + startRange + ".." + endRange);
  285. }
  286. public static PageParameters newPathParameter(String repositoryName, String objectId,
  287. String path) {
  288. if (StringUtils.isEmpty(path)) {
  289. return newObjectParameter(repositoryName, objectId);
  290. }
  291. if (StringUtils.isEmpty(objectId)) {
  292. return new PageParameters("r=" + repositoryName + ",f=" + path);
  293. }
  294. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path);
  295. }
  296. public static PageParameters newLogPageParameter(String repositoryName, String objectId,
  297. int pageNumber) {
  298. if (pageNumber <= 1) {
  299. return newObjectParameter(repositoryName, objectId);
  300. }
  301. if (StringUtils.isEmpty(objectId)) {
  302. return new PageParameters("r=" + repositoryName + ",pg=" + pageNumber);
  303. }
  304. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",pg=" + pageNumber);
  305. }
  306. public static PageParameters newHistoryPageParameter(String repositoryName, String objectId,
  307. String path, int pageNumber) {
  308. if (pageNumber <= 1) {
  309. return newObjectParameter(repositoryName, objectId);
  310. }
  311. if (StringUtils.isEmpty(objectId)) {
  312. return new PageParameters("r=" + repositoryName + ",f=" + path + ",pg=" + pageNumber);
  313. }
  314. return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path + ",pg="
  315. + pageNumber);
  316. }
  317. public static PageParameters newBlobDiffParameter(String repositoryName, String baseCommitId,
  318. String commitId, String path) {
  319. if (StringUtils.isEmpty(commitId)) {
  320. return new PageParameters("r=" + repositoryName + ",f=" + path + ",hb=" + baseCommitId);
  321. }
  322. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",f=" + path + ",hb="
  323. + baseCommitId);
  324. }
  325. public static PageParameters newSearchParameter(String repositoryName, String commitId,
  326. String search, Constants.SearchType type) {
  327. if (StringUtils.isEmpty(commitId)) {
  328. return new PageParameters("r=" + repositoryName + ",s=" + search + ",st=" + type.name());
  329. }
  330. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search
  331. + ",st=" + type.name());
  332. }
  333. public static PageParameters newSearchParameter(String repositoryName, String commitId,
  334. String search, Constants.SearchType type, int pageNumber) {
  335. if (StringUtils.isEmpty(commitId)) {
  336. return new PageParameters("r=" + repositoryName + ",s=" + search + ",st=" + type.name()
  337. + ",pg=" + pageNumber);
  338. }
  339. return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search
  340. + ",st=" + type.name() + ",pg=" + pageNumber);
  341. }
  342. public static String getProjectName(PageParameters params) {
  343. return params.getString("p", "");
  344. }
  345. public static String getRepositoryName(PageParameters params) {
  346. return params.getString("r", "");
  347. }
  348. public static String getObject(PageParameters params) {
  349. return params.getString("h", null);
  350. }
  351. public static String getPath(PageParameters params) {
  352. return params.getString("f", null);
  353. }
  354. public static String getBaseObjectId(PageParameters params) {
  355. return params.getString("hb", null);
  356. }
  357. public static String getSearchString(PageParameters params) {
  358. return params.getString("s", null);
  359. }
  360. public static String getSearchType(PageParameters params) {
  361. return params.getString("st", null);
  362. }
  363. public static int getPage(PageParameters params) {
  364. // index from 1
  365. return params.getInt("pg", 1);
  366. }
  367. public static String getRegEx(PageParameters params) {
  368. return params.getString("x", "");
  369. }
  370. public static String getSet(PageParameters params) {
  371. return params.getString("set", "");
  372. }
  373. public static String getTeam(PageParameters params) {
  374. return params.getString("team", "");
  375. }
  376. public static int getDaysBack(PageParameters params) {
  377. return params.getInt("db", 0);
  378. }
  379. public static String getUsername(PageParameters params) {
  380. return params.getString("user", "");
  381. }
  382. public static String getTeamname(PageParameters params) {
  383. return params.getString("team", "");
  384. }
  385. public static String getToken(PageParameters params) {
  386. return params.getString("t", "");
  387. }
  388. public static String getUrlParameter(PageParameters params) {
  389. return params.getString("u", "");
  390. }
  391. public static String getNameParameter(PageParameters params) {
  392. return params.getString("n", "");
  393. }
  394. public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
  395. String format = GitBlit.getString(Keys.web.datestampShortFormat, "MM/dd/yy");
  396. DateFormat df = new SimpleDateFormat(format);
  397. if (timeZone == null) {
  398. timeZone = GitBlit.getTimezone();
  399. }
  400. df.setTimeZone(timeZone);
  401. String dateString;
  402. if (date.getTime() == 0) {
  403. dateString = "--";
  404. } else {
  405. dateString = df.format(date);
  406. }
  407. String title = null;
  408. if (date.getTime() <= System.currentTimeMillis()) {
  409. // past
  410. title = timeUtils.timeAgo(date);
  411. }
  412. if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
  413. String tmp = dateString;
  414. dateString = title;
  415. title = tmp;
  416. }
  417. Label label = new Label(wicketId, dateString);
  418. WicketUtils.setCssClass(label, timeUtils.timeAgoCss(date));
  419. if (!StringUtils.isEmpty(title)) {
  420. WicketUtils.setHtmlTooltip(label, title);
  421. }
  422. return label;
  423. }
  424. public static Label createTimeLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
  425. String format = GitBlit.getString(Keys.web.timeFormat, "HH:mm");
  426. DateFormat df = new SimpleDateFormat(format);
  427. if (timeZone == null) {
  428. timeZone = GitBlit.getTimezone();
  429. }
  430. df.setTimeZone(timeZone);
  431. String timeString;
  432. if (date.getTime() == 0) {
  433. timeString = "--";
  434. } else {
  435. timeString = df.format(date);
  436. }
  437. String title = timeUtils.timeAgo(date);
  438. Label label = new Label(wicketId, timeString);
  439. if (!StringUtils.isEmpty(title)) {
  440. WicketUtils.setHtmlTooltip(label, title);
  441. }
  442. return label;
  443. }
  444. public static Label createDatestampLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
  445. String format = GitBlit.getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
  446. DateFormat df = new SimpleDateFormat(format);
  447. if (timeZone == null) {
  448. timeZone = GitBlit.getTimezone();
  449. }
  450. df.setTimeZone(timeZone);
  451. String dateString;
  452. if (date.getTime() == 0) {
  453. dateString = "--";
  454. } else {
  455. dateString = df.format(date);
  456. }
  457. String title = null;
  458. if (TimeUtils.isToday(date, timeZone)) {
  459. title = timeUtils.today();
  460. } else if (TimeUtils.isYesterday(date, timeZone)) {
  461. title = timeUtils.yesterday();
  462. } else if (date.getTime() <= System.currentTimeMillis()) {
  463. // past
  464. title = timeUtils.timeAgo(date);
  465. }
  466. if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
  467. String tmp = dateString;
  468. dateString = title;
  469. title = tmp;
  470. }
  471. Label label = new Label(wicketId, dateString);
  472. if (!StringUtils.isEmpty(title)) {
  473. WicketUtils.setHtmlTooltip(label, title);
  474. }
  475. return label;
  476. }
  477. public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
  478. String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
  479. "EEEE, MMMM d, yyyy HH:mm Z");
  480. DateFormat df = new SimpleDateFormat(format);
  481. if (timeZone == null) {
  482. timeZone = GitBlit.getTimezone();
  483. }
  484. df.setTimeZone(timeZone);
  485. String dateString;
  486. if (date.getTime() == 0) {
  487. dateString = "--";
  488. } else {
  489. dateString = df.format(date);
  490. }
  491. String title = null;
  492. if (date.getTime() <= System.currentTimeMillis()) {
  493. // past
  494. title = timeUtils.timeAgo(date);
  495. }
  496. Label label = new Label(wicketId, dateString);
  497. if (!StringUtils.isEmpty(title)) {
  498. WicketUtils.setHtmlTooltip(label, title);
  499. }
  500. return label;
  501. }
  502. public static IChartData getChartData(Collection<Metric> metrics) {
  503. final double[] commits = new double[metrics.size()];
  504. final double[] tags = new double[metrics.size()];
  505. int i = 0;
  506. double max = 0;
  507. for (Metric m : metrics) {
  508. commits[i] = m.count;
  509. if (m.tag > 0) {
  510. tags[i] = m.count;
  511. } else {
  512. tags[i] = -1d;
  513. }
  514. max = Math.max(max, m.count);
  515. i++;
  516. }
  517. IChartData data = new AbstractChartData(max) {
  518. private static final long serialVersionUID = 1L;
  519. public double[][] getData() {
  520. return new double[][] { commits, tags };
  521. }
  522. };
  523. return data;
  524. }
  525. public static double maxValue(Collection<Metric> metrics) {
  526. double max = Double.MIN_VALUE;
  527. for (Metric m : metrics) {
  528. if (m.count > max) {
  529. max = m.count;
  530. }
  531. }
  532. return max;
  533. }
  534. public static IChartData getScatterData(Collection<Metric> metrics) {
  535. final double[] y = new double[metrics.size()];
  536. final double[] x = new double[metrics.size()];
  537. int i = 0;
  538. double max = 0;
  539. for (Metric m : metrics) {
  540. y[i] = m.count;
  541. if (m.duration > 0) {
  542. x[i] = m.duration;
  543. } else {
  544. x[i] = -1d;
  545. }
  546. max = Math.max(max, m.count);
  547. i++;
  548. }
  549. IChartData data = new AbstractChartData(max) {
  550. private static final long serialVersionUID = 1L;
  551. public double[][] getData() {
  552. return new double[][] { x, y };
  553. }
  554. };
  555. return data;
  556. }
  557. }