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