James Moger
2012-10-22 eba89539a29deba954035056437279088c3e047b
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;
a45caa 25 import org.apache.wicket.markup.html.basic.Label;
5fe7df 26 import org.apache.wicket.markup.html.panel.Panel;
JM 27 import org.apache.wicket.markup.repeater.Item;
28 import org.apache.wicket.markup.repeater.data.DataView;
29 import org.apache.wicket.markup.repeater.data.ListDataProvider;
30 import org.eclipse.jgit.lib.Constants;
31 import org.eclipse.jgit.lib.ObjectId;
32 import org.eclipse.jgit.revwalk.RevCommit;
33
4ab184 34 import com.gitblit.models.RefModel;
5fe7df 35 import com.gitblit.wicket.WicketUtils;
bc10f9 36 import com.gitblit.wicket.pages.CommitPage;
7d35e2 37 import com.gitblit.wicket.pages.LogPage;
5cc4f2 38 import com.gitblit.wicket.pages.RepositoryPage;
7d35e2 39 import com.gitblit.wicket.pages.TagPage;
5fe7df 40
JM 41 public class RefsPanel extends Panel {
42
43     private static final long serialVersionUID = 1L;
44
2a7306 45     public RefsPanel(String id, final String repositoryName, RevCommit c,
4ab184 46             Map<ObjectId, List<RefModel>> refs) {
JM 47         this(id, repositoryName, refs.get(c.getId()));
48     }
49
50     public RefsPanel(String id, final String repositoryName, List<RefModel> refs) {
5fe7df 51         super(id);
4ab184 52         if (refs == null) {
JM 53             refs = new ArrayList<RefModel>();
5fe7df 54         }
5cc4f2 55         Collections.sort(refs, new Comparator<RefModel>() {
JM 56             @Override
57             public int compare(RefModel o1, RefModel o2) {
a45caa 58                 // sort remote heads last, otherwise sort by name
JM 59                 // this is so we can insert a break on the refs panel
60                 // [head][branch][branch][tag][tag]
61                 // [remote][remote][remote]
62                 boolean remote1 = o1.displayName.startsWith(Constants.R_REMOTES);
63                 boolean remote2 = o2.displayName.startsWith(Constants.R_REMOTES);
64                 if (remote1 && remote2) {
65                     // both are remote heads, sort by name
66                     return o1.displayName.compareTo(o2.displayName);    
67                 }
68                 if (remote1) {
69                     // o1 is remote, o2 comes first
70                     return 1;
71                 }
72                 if (remote2) {
73                     // remote is o2, o1 comes first
74                     return -1;
75                 }
76                 // standard sort
5cc4f2 77                 return o1.displayName.compareTo(o2.displayName);
JM 78             }
79         });
a45caa 80         
JM 81         // count remote and determine if we should insert a break
82         int remoteCount = 0;
83         for (RefModel ref : refs) {
84             if (ref.displayName.startsWith(Constants.R_REMOTES)) {
85                 remoteCount++;
86             }
87         }
88         final boolean shouldBreak = remoteCount < refs.size();
89         
4ab184 90         ListDataProvider<RefModel> refsDp = new ListDataProvider<RefModel>(refs);
JM 91         DataView<RefModel> refsView = new DataView<RefModel>("ref", refsDp) {
5fe7df 92             private static final long serialVersionUID = 1L;
a45caa 93             private boolean alreadyInsertedBreak = !shouldBreak;
155bf7 94
4ab184 95             public void populateItem(final Item<RefModel> item) {
JM 96                 RefModel entry = item.getModelObject();
97                 String name = entry.displayName;
98                 String objectid = entry.getReferencedObjectId().getName();
a45caa 99                 boolean breakLine = false;
5cc4f2 100                 Class<? extends RepositoryPage> linkClass = CommitPage.class;
JM 101                 String cssClass = "";
4ab184 102                 if (name.startsWith(Constants.R_HEADS)) {
1fa5e8 103                     // local branch
5cc4f2 104                     linkClass = LogPage.class;
JM 105                     name = name.substring(Constants.R_HEADS.length());
1fa5e8 106                     cssClass = "localBranch";
4ab184 107                 } else if (name.equals(Constants.HEAD)) {
JM 108                     // local head
5cc4f2 109                     linkClass = LogPage.class;
JM 110                     cssClass = "headRef";
4ab184 111                 } else if (name.startsWith(Constants.R_REMOTES)) {
1fa5e8 112                     // remote branch
5cc4f2 113                     linkClass = LogPage.class;
JM 114                     name = name.substring(Constants.R_REMOTES.length());
1fa5e8 115                     cssClass = "remoteBranch";
a45caa 116                     if (!alreadyInsertedBreak) {
JM 117                         breakLine = true;
118                         alreadyInsertedBreak = true;
119                     }
4ab184 120                 } else if (name.startsWith(Constants.R_TAGS)) {
5fe7df 121                     // tag
5cc4f2 122                     if (entry.isAnnotatedTag()) {
JM 123                         linkClass = TagPage.class;
124                         objectid = entry.getObjectId().getName();
125                     } else {
126                         linkClass = CommitPage.class;
127                         objectid = entry.getReferencedObjectId().getName();
128                     }
129                     name = name.substring(Constants.R_TAGS.length());
130                     cssClass = "tagRef";
131                 } else if (name.startsWith(Constants.R_NOTES)) {
132                     linkClass = CommitPage.class;
133                     cssClass = "otherRef";
5fe7df 134                 }
5cc4f2 135
JM 136                 Component c = new LinkPanel("refName", null, name, linkClass,
137                         WicketUtils.newObjectParameter(repositoryName, objectid));
138                 WicketUtils.setCssClass(c, cssClass);
4ab184 139                 WicketUtils.setHtmlTooltip(c, name);
5fe7df 140                 item.add(c);
a45caa 141                 Label lb = new Label("lineBreak", "<br/>");
JM 142                 lb.setVisible(breakLine);
143                 lb.setRenderBodyOnly(true);
144                 item.add(lb.setEscapeModelStrings(false));
860290 145                 item.setRenderBodyOnly(true);
5fe7df 146             }
JM 147         };
148         add(refsView);
149     }
150 }