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  */
5fe7df 16 package com.gitblit.wicket.panels;
JM 17
18 import java.util.ArrayList;
19 import java.util.Collections;
5cc4f2 20 import java.util.Comparator;
5fe7df 21 import java.util.List;
JM 22 import java.util.Map;
23
24 import org.apache.wicket.Component;
25 import org.apache.wicket.markup.html.panel.Panel;
26 import org.apache.wicket.markup.repeater.Item;
27 import org.apache.wicket.markup.repeater.data.DataView;
28 import org.apache.wicket.markup.repeater.data.ListDataProvider;
29 import org.eclipse.jgit.lib.Constants;
30 import org.eclipse.jgit.lib.ObjectId;
31 import org.eclipse.jgit.revwalk.RevCommit;
32
4ab184 33 import com.gitblit.models.RefModel;
5fe7df 34 import com.gitblit.wicket.WicketUtils;
bc10f9 35 import com.gitblit.wicket.pages.CommitPage;
7d35e2 36 import com.gitblit.wicket.pages.LogPage;
5cc4f2 37 import com.gitblit.wicket.pages.RepositoryPage;
7d35e2 38 import com.gitblit.wicket.pages.TagPage;
5fe7df 39
JM 40 public class RefsPanel extends Panel {
41
42     private static final long serialVersionUID = 1L;
43
2a7306 44     public RefsPanel(String id, final String repositoryName, RevCommit c,
4ab184 45             Map<ObjectId, List<RefModel>> refs) {
JM 46         this(id, repositoryName, refs.get(c.getId()));
47     }
48
49     public RefsPanel(String id, final String repositoryName, List<RefModel> refs) {
5fe7df 50         super(id);
4ab184 51         if (refs == null) {
JM 52             refs = new ArrayList<RefModel>();
5fe7df 53         }
5cc4f2 54         Collections.sort(refs, new Comparator<RefModel>() {
JM 55             @Override
56             public int compare(RefModel o1, RefModel o2) {
57                 return o1.displayName.compareTo(o2.displayName);
58             }
59         });
155bf7 60
4ab184 61         ListDataProvider<RefModel> refsDp = new ListDataProvider<RefModel>(refs);
JM 62         DataView<RefModel> refsView = new DataView<RefModel>("ref", refsDp) {
5fe7df 63             private static final long serialVersionUID = 1L;
155bf7 64
4ab184 65             public void populateItem(final Item<RefModel> item) {
JM 66                 RefModel entry = item.getModelObject();
67                 String name = entry.displayName;
68                 String objectid = entry.getReferencedObjectId().getName();
5cc4f2 69
JM 70                 Class<? extends RepositoryPage> linkClass = CommitPage.class;
71                 String cssClass = "";
4ab184 72                 if (name.startsWith(Constants.R_HEADS)) {
1fa5e8 73                     // local branch
5cc4f2 74                     linkClass = LogPage.class;
JM 75                     name = name.substring(Constants.R_HEADS.length());
1fa5e8 76                     cssClass = "localBranch";
4ab184 77                 } else if (name.equals(Constants.HEAD)) {
JM 78                     // local head
5cc4f2 79                     linkClass = LogPage.class;
JM 80                     cssClass = "headRef";
4ab184 81                 } else if (name.startsWith(Constants.R_REMOTES)) {
1fa5e8 82                     // remote branch
5cc4f2 83                     linkClass = LogPage.class;
JM 84                     name = name.substring(Constants.R_REMOTES.length());
1fa5e8 85                     cssClass = "remoteBranch";
4ab184 86                 } else if (name.startsWith(Constants.R_TAGS)) {
5fe7df 87                     // tag
5cc4f2 88                     if (entry.isAnnotatedTag()) {
JM 89                         linkClass = TagPage.class;
90                         objectid = entry.getObjectId().getName();
91                     } else {
92                         linkClass = CommitPage.class;
93                         objectid = entry.getReferencedObjectId().getName();
94                     }
95                     name = name.substring(Constants.R_TAGS.length());
96                     cssClass = "tagRef";
97                 } else if (name.startsWith(Constants.R_NOTES)) {
98                     linkClass = CommitPage.class;
99                     cssClass = "otherRef";
5fe7df 100                 }
5cc4f2 101
JM 102                 Component c = new LinkPanel("refName", null, name, linkClass,
103                         WicketUtils.newObjectParameter(repositoryName, objectid));
104                 WicketUtils.setCssClass(c, cssClass);
4ab184 105                 WicketUtils.setHtmlTooltip(c, name);
5fe7df 106                 item.add(c);
JM 107             }
108         };
109         add(refsView);
110     }
111 }