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.

SearchPanel.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.panels;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.apache.wicket.markup.html.basic.Label;
  21. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  22. import org.apache.wicket.markup.repeater.Item;
  23. import org.apache.wicket.markup.repeater.data.DataView;
  24. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  25. import org.eclipse.jgit.lib.ObjectId;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.revwalk.RevCommit;
  28. import com.gitblit.Constants;
  29. import com.gitblit.GitBlit;
  30. import com.gitblit.Keys;
  31. import com.gitblit.models.RefModel;
  32. import com.gitblit.utils.JGitUtils;
  33. import com.gitblit.utils.StringUtils;
  34. import com.gitblit.wicket.WicketUtils;
  35. import com.gitblit.wicket.pages.CommitDiffPage;
  36. import com.gitblit.wicket.pages.CommitPage;
  37. import com.gitblit.wicket.pages.GitSearchPage;
  38. import com.gitblit.wicket.pages.TreePage;
  39. public class SearchPanel extends BasePanel {
  40. private static final long serialVersionUID = 1L;
  41. private boolean hasMore;
  42. public SearchPanel(String wicketId, final String repositoryName, final String objectId,
  43. final String value, Constants.SearchType searchType, Repository r, int limit, int pageOffset,
  44. boolean showRemoteRefs) {
  45. super(wicketId);
  46. boolean pageResults = limit <= 0;
  47. int itemsPerPage = GitBlit.getInteger(Keys.web.itemsPerPage, 50);
  48. if (itemsPerPage <= 1) {
  49. itemsPerPage = 50;
  50. }
  51. RevCommit commit = JGitUtils.getCommit(r, objectId);
  52. final Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(r, showRemoteRefs);
  53. List<RevCommit> commits;
  54. if (pageResults) {
  55. // Paging result set
  56. commits = JGitUtils.searchRevlogs(r, objectId, value, searchType, pageOffset
  57. * itemsPerPage, itemsPerPage);
  58. } else {
  59. // Fixed size result set
  60. commits = JGitUtils.searchRevlogs(r, objectId, value, searchType, 0, limit);
  61. }
  62. // inaccurate way to determine if there are more commits.
  63. // works unless commits.size() represents the exact end.
  64. hasMore = commits.size() >= itemsPerPage;
  65. // header
  66. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  67. add(new Label("searchString", value));
  68. add(new Label("searchType", searchType.toString()));
  69. ListDataProvider<RevCommit> dp = new ListDataProvider<RevCommit>(commits);
  70. DataView<RevCommit> searchView = new DataView<RevCommit>("commit", dp) {
  71. private static final long serialVersionUID = 1L;
  72. int counter;
  73. public void populateItem(final Item<RevCommit> item) {
  74. final RevCommit entry = item.getModelObject();
  75. final Date date = JGitUtils.getCommitDate(entry);
  76. item.add(WicketUtils.createDateLabel("commitDate", date, getTimeZone(), getTimeUtils()));
  77. // author search link
  78. String author = entry.getAuthorIdent().getName();
  79. LinkPanel authorLink = new LinkPanel("commitAuthor", "list", author,
  80. GitSearchPage.class, WicketUtils.newSearchParameter(repositoryName, objectId,
  81. author, Constants.SearchType.AUTHOR));
  82. setPersonSearchTooltip(authorLink, author, Constants.SearchType.AUTHOR);
  83. item.add(authorLink);
  84. // merge icon
  85. if (entry.getParentCount() > 1) {
  86. item.add(WicketUtils.newImage("commitIcon", "commit_merge_16x16.png"));
  87. } else {
  88. item.add(WicketUtils.newBlankImage("commitIcon"));
  89. }
  90. String shortMessage = entry.getShortMessage();
  91. String trimmedMessage = shortMessage;
  92. if (allRefs.containsKey(entry.getId())) {
  93. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG_REFS);
  94. } else {
  95. trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
  96. }
  97. LinkPanel shortlog = new LinkPanel("commitShortMessage", "list subject",
  98. trimmedMessage, CommitPage.class, WicketUtils.newObjectParameter(
  99. repositoryName, entry.getName()));
  100. if (!shortMessage.equals(trimmedMessage)) {
  101. WicketUtils.setHtmlTooltip(shortlog, shortMessage);
  102. }
  103. item.add(shortlog);
  104. item.add(new RefsPanel("commitRefs", repositoryName, entry, allRefs));
  105. item.add(new BookmarkablePageLink<Void>("commit", CommitPage.class, WicketUtils
  106. .newObjectParameter(repositoryName, entry.getName())));
  107. item.add(new BookmarkablePageLink<Void>("commitdiff", CommitDiffPage.class,
  108. WicketUtils.newObjectParameter(repositoryName, entry.getName())));
  109. item.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
  110. .newObjectParameter(repositoryName, entry.getName())));
  111. WicketUtils.setAlternatingBackground(item, counter);
  112. counter++;
  113. }
  114. };
  115. add(searchView);
  116. }
  117. public boolean hasMore() {
  118. return hasMore;
  119. }
  120. }