James Moger
2011-11-18 309c55b5a9670e7c327ad8d4e5d94b8af840d00f
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  */
a645ba 16 package com.gitblit.wicket.panels;
JM 17
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
a125cf 20 import java.util.HashMap;
a645ba 21 import java.util.List;
JM 22 import java.util.Map;
23 import java.util.concurrent.atomic.AtomicInteger;
24
25 import org.apache.wicket.markup.html.basic.Label;
26 import org.apache.wicket.markup.html.panel.Panel;
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.diff.DiffEntry.ChangeType;
31
1f9dae 32 import com.gitblit.models.PathModel.PathChangeModel;
a645ba 33 import com.gitblit.wicket.WicketUtils;
JM 34
35 public class CommitLegendPanel extends Panel {
36
37     private static final long serialVersionUID = 1L;
38
39     public CommitLegendPanel(String id, List<PathChangeModel> paths) {
40         super(id);
a125cf 41         final Map<ChangeType, AtomicInteger> stats = getChangedPathsStats(paths);
2a7306 42         ListDataProvider<ChangeType> legendDp = new ListDataProvider<ChangeType>(
JM 43                 new ArrayList<ChangeType>(stats.keySet()));
a645ba 44         DataView<ChangeType> legendsView = new DataView<ChangeType>("legend", legendDp) {
JM 45             private static final long serialVersionUID = 1L;
46
47             public void populateItem(final Item<ChangeType> item) {
48                 ChangeType entry = item.getModelObject();
49
50                 Label changeType = new Label("changeType", "");
51                 WicketUtils.setChangeTypeCssClass(changeType, entry);
52                 item.add(changeType);
53                 int count = stats.get(entry).intValue();
2a7306 54                 String description = "";
JM 55                 switch (entry) {
a645ba 56                 case ADD:
JM 57                     description = MessageFormat.format(getString("gb.filesAdded"), count);
58                     break;
59                 case MODIFY:
60                     description = MessageFormat.format(getString("gb.filesModified"), count);
61                     break;
62                 case DELETE:
63                     description = MessageFormat.format(getString("gb.filesDeleted"), count);
64                     break;
65                 case COPY:
66                     description = MessageFormat.format(getString("gb.filesCopied"), count);
67                     break;
68                 case RENAME:
69                     description = MessageFormat.format(getString("gb.filesRenamed"), count);
70                     break;
2a7306 71                 }
a645ba 72                 item.add(new Label("description", description));
JM 73             }
74         };
75         add(legendsView);
76     }
008322 77
a125cf 78     protected Map<ChangeType, AtomicInteger> getChangedPathsStats(List<PathChangeModel> paths) {
JM 79         Map<ChangeType, AtomicInteger> stats = new HashMap<ChangeType, AtomicInteger>();
80         for (PathChangeModel path : paths) {
81             if (!stats.containsKey(path.changeType)) {
82                 stats.put(path.changeType, new AtomicInteger(0));
83             }
84             stats.get(path.changeType).incrementAndGet();
85         }
86         return stats;
87     }
a645ba 88 }