James Moger
2012-09-20 33622b7acfa037d0218dd7a9d62b4831015768f3
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  */
698678 16 package com.gitblit.wicket.panels;
JM 17
eb9979 18 import java.text.MessageFormat;
698678 19 import java.util.ArrayList;
JM 20 import java.util.Collections;
21 import java.util.List;
22
23 import org.apache.wicket.markup.html.basic.Label;
24 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
c22722 25 import org.apache.wicket.markup.html.link.ExternalLink;
eb9979 26 import org.apache.wicket.markup.html.link.Link;
1fa5e8 27 import org.apache.wicket.markup.html.panel.Fragment;
698678 28 import org.apache.wicket.markup.repeater.Item;
JM 29 import org.apache.wicket.markup.repeater.data.DataView;
30 import org.apache.wicket.markup.repeater.data.ListDataProvider;
31 import org.apache.wicket.model.StringResourceModel;
32 import org.eclipse.jgit.lib.Repository;
33
33d8d8 34 import com.gitblit.Constants;
33622b 35 import com.gitblit.GitBlit;
c22722 36 import com.gitblit.SyndicationServlet;
1f9dae 37 import com.gitblit.models.RefModel;
JM 38 import com.gitblit.models.RepositoryModel;
698678 39 import com.gitblit.utils.JGitUtils;
87cc1e 40 import com.gitblit.utils.StringUtils;
698678 41 import com.gitblit.wicket.WicketUtils;
JM 42 import com.gitblit.wicket.pages.BranchesPage;
85c2e6 43 import com.gitblit.wicket.pages.CommitPage;
eb9979 44 import com.gitblit.wicket.pages.GitSearchPage;
698678 45 import com.gitblit.wicket.pages.LogPage;
1fa5e8 46 import com.gitblit.wicket.pages.MetricsPage;
698678 47 import com.gitblit.wicket.pages.TreePage;
JM 48
bc10f9 49 public class BranchesPanel extends BasePanel {
698678 50
JM 51     private static final long serialVersionUID = 1L;
1fa5e8 52
JM 53     private final boolean hasBranches;
698678 54
33622b 55     public BranchesPanel(String wicketId, final RepositoryModel model, Repository r,
eb9979 56             final int maxCount, final boolean showAdmin) {
698678 57         super(wicketId);
JM 58
59         // branches
60         List<RefModel> branches = new ArrayList<RefModel>();
5cc4f2 61         branches.addAll(JGitUtils.getLocalBranches(r, false, maxCount));
cf9550 62         if (model.showRemoteBranches) {
5cc4f2 63             branches.addAll(JGitUtils.getRemoteBranches(r, false, maxCount));
cf9550 64         }
698678 65         Collections.sort(branches);
JM 66         Collections.reverse(branches);
67         if (maxCount > 0 && branches.size() > maxCount) {
68             branches = new ArrayList<RefModel>(branches.subList(0, maxCount));
69         }
70
71         if (maxCount > 0) {
72             // summary page
73             // show branches page link
2a7306 74             add(new LinkPanel("branches", "title", new StringResourceModel("gb.branches", this,
JM 75                     null), BranchesPage.class, WicketUtils.newRepositoryParameter(model.name)));
698678 76         } else {
JM 77             // branches page
a45caa 78             add(new Label("branches", new StringResourceModel("gb.branches", this, null)));
698678 79         }
33622b 80         
JM 81         // only allow delete if we have multiple branches
82         final boolean showDelete = showAdmin && branches.size() > 1;
83         
698678 84         ListDataProvider<RefModel> branchesDp = new ListDataProvider<RefModel>(branches);
JM 85         DataView<RefModel> branchesView = new DataView<RefModel>("branch", branchesDp) {
86             private static final long serialVersionUID = 1L;
2a7306 87             int counter;
698678 88
JM 89             public void populateItem(final Item<RefModel> item) {
90                 final RefModel entry = item.getModelObject();
91
9adf62 92                 item.add(WicketUtils.createDateLabel("branchDate", entry.getDate(), getTimeZone(), getTimeUtils()));
698678 93
2a7306 94                 item.add(new LinkPanel("branchName", "list name", StringUtils.trimString(
JM 95                         entry.displayName, 28), LogPage.class, WicketUtils.newObjectParameter(
96                         model.name, entry.getName())));
155bf7 97
85c2e6 98                 String author = entry.getAuthorIdent().getName();
JM 99                 LinkPanel authorLink = new LinkPanel("branchAuthor", "list", author,
4e1930 100                         GitSearchPage.class, WicketUtils.newSearchParameter(model.name,
33d8d8 101                                 entry.getName(), author, Constants.SearchType.AUTHOR));
JM 102                 setPersonSearchTooltip(authorLink, author, Constants.SearchType.AUTHOR);
85c2e6 103                 item.add(authorLink);
88598b 104
85c2e6 105                 // short message
JM 106                 String shortMessage = entry.getShortMessage();
a45caa 107                 String trimmedMessage = StringUtils.trimString(shortMessage, Constants.LEN_SHORTLOG);
88598b 108                 LinkPanel shortlog = new LinkPanel("branchLog", "list subject", trimmedMessage,
JM 109                         CommitPage.class, WicketUtils.newObjectParameter(model.name,
110                                 entry.getName()));
85c2e6 111                 if (!shortMessage.equals(trimmedMessage)) {
JM 112                     WicketUtils.setHtmlTooltip(shortlog, shortMessage);
113                 }
114                 item.add(shortlog);
eb9979 115                 
1fa5e8 116                 if (maxCount <= 0) {
33622b 117                     Fragment fragment = new Fragment("branchLinks", showDelete? "branchPageAdminLinks" : "branchPageLinks", this);
1fa5e8 118                     fragment.add(new BookmarkablePageLink<Void>("log", LogPage.class, WicketUtils
JM 119                             .newObjectParameter(model.name, entry.getName())));
120                     fragment.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
121                             .newObjectParameter(model.name, entry.getName())));
122                     fragment.add(new BookmarkablePageLink<Void>("metrics", MetricsPage.class,
123                             WicketUtils.newObjectParameter(model.name, entry.getName())));
85c2e6 124                     fragment.add(new ExternalLink("syndication", SyndicationServlet.asLink(
JM 125                             getRequest().getRelativePathPrefixToContextRoot(), model.name,
126                             entry.getName(), 0)));
33622b 127                     if (showDelete) {
JM 128                         fragment.add(createDeleteBranchLink(model, entry));
129                     }
1fa5e8 130                     item.add(fragment);
JM 131                 } else {
132                     Fragment fragment = new Fragment("branchLinks", "branchPanelLinks", this);
133                     fragment.add(new BookmarkablePageLink<Void>("log", LogPage.class, WicketUtils
134                             .newObjectParameter(model.name, entry.getName())));
135                     fragment.add(new BookmarkablePageLink<Void>("tree", TreePage.class, WicketUtils
136                             .newObjectParameter(model.name, entry.getName())));
137                     item.add(fragment);
138                 }
698678 139                 WicketUtils.setAlternatingBackground(item, counter);
JM 140                 counter++;
141             }
142         };
143         add(branchesView);
144         if (branches.size() < maxCount || maxCount <= 0) {
145             add(new Label("allBranches", "").setVisible(false));
146         } else {
2a7306 147             add(new LinkPanel("allBranches", "link", new StringResourceModel("gb.allBranches",
JM 148                     this, null), BranchesPage.class, WicketUtils.newRepositoryParameter(model.name)));
698678 149         }
1fa5e8 150         // We always have 1 branch
a2709d 151         hasBranches = (branches.size() > 1)
JM 152                 || ((branches.size() == 1) && !branches.get(0).displayName
caad17 153                         .equalsIgnoreCase("master"));
1fa5e8 154     }
JM 155
156     public BranchesPanel hideIfEmpty() {
157         setVisible(hasBranches);
158         return this;
698678 159     }
eb9979 160
33622b 161     private Link<Void> createDeleteBranchLink(final RepositoryModel repositoryModel, final RefModel entry)
eb9979 162     {
A 163         Link<Void> deleteLink = new Link<Void>("deleteBranch") {
164             private static final long serialVersionUID = 1L;
165
166             @Override
167             public void onClick() {
33622b 168                 Repository r = GitBlit.self().getRepository(repositoryModel.name);
JM 169                 boolean success = JGitUtils.deleteBranchRef(r, entry.getName());
170                 r.close();
171                 if (success) {
eb9979 172                     info(MessageFormat.format("Branch \"{0}\" deleted", entry.displayName));
33622b 173                     // redirect to the owning page
JM 174                     setResponsePage(getPage().getClass(), WicketUtils.newRepositoryParameter(repositoryModel.name));
eb9979 175                 }
A 176                 else {
177                     error(MessageFormat.format("Failed to delete branch \"{0}\"", entry.displayName));
178                 }
179             }
180         };
181         
182         deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(
183                 "Delete branch \"{0}\"?", entry.displayName )));
184         return deleteLink;
185     }
698678 186 }