James Moger
2013-05-11 42472246660161a79b3976296190baf73a5a1e02
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.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.wicket.markup.html.basic.Label;
23 import org.apache.wicket.markup.html.panel.Panel;
24 import org.apache.wicket.markup.repeater.Item;
25 import org.apache.wicket.markup.repeater.data.DataView;
26 import org.apache.wicket.markup.repeater.data.ListDataProvider;
27
698678 28 import com.gitblit.wicket.WicketUtils;
5fe7df 29 import com.gitblit.wicket.pages.TreePage;
JM 30
31 public class PathBreadcrumbsPanel extends Panel {
32
33     private static final long serialVersionUID = 1L;
34
2a7306 35     private static final String ROOT = "--ROOT--";
5fe7df 36
2a7306 37     public PathBreadcrumbsPanel(String id, final String repositoryName, String pathName,
JM 38             final String objectId) {
5fe7df 39         super(id);
JM 40         List<BreadCrumb> crumbs = new ArrayList<BreadCrumb>();
41         crumbs.add(new BreadCrumb("[" + repositoryName + "]", ROOT, false));
42
fc8426 43         if (pathName != null && pathName.length() > 0) {
JM 44             String[] paths = pathName.split("/");
45             StringBuilder sb = new StringBuilder();
46
47             for (int i = 0; i < paths.length; i++) {
48                 String path = paths[i];
49                 sb.append(path);
2a7306 50                 crumbs.add(new BreadCrumb(path, sb.toString(), i == (paths.length - 1)));
JM 51                 sb.append('/');
fc8426 52             }
5fe7df 53         }
JM 54
55         ListDataProvider<BreadCrumb> crumbsDp = new ListDataProvider<BreadCrumb>(crumbs);
56         DataView<BreadCrumb> pathsView = new DataView<BreadCrumb>("path", crumbsDp) {
57             private static final long serialVersionUID = 1L;
58
59             public void populateItem(final Item<BreadCrumb> item) {
60                 final BreadCrumb entry = item.getModelObject();
1f9dae 61                 String path = entry.path;
JM 62                 if (path.equals(ROOT)) {
63                     path = null;
64                 }
1a3fc5 65                 if (entry.isLeaf) {
JM 66                     item.add(new Label("pathLink", entry.name));
67                     item.add(new Label("pathSeparator", "").setVisible(false));
68                 } else {
2a7306 69                     item.add(new LinkPanel("pathLink", null, entry.name, TreePage.class,
JM 70                             WicketUtils.newPathParameter(repositoryName, objectId, path)));
1a3fc5 71                     item.add(new Label("pathSeparator", "/"));
5fe7df 72                 }
JM 73             }
74         };
75         add(pathsView);
76     }
77
2a7306 78     private static class BreadCrumb implements Serializable {
fc8426 79
5fe7df 80         private static final long serialVersionUID = 1L;
fc8426 81
5fe7df 82         final String name;
JM 83         final String path;
84         final boolean isLeaf;
fc8426 85
5fe7df 86         BreadCrumb(String name, String path, boolean isLeaf) {
JM 87             this.name = name;
88             this.path = path;
89             this.isLeaf = isLeaf;
db653a 90         }
5fe7df 91     }
JM 92 }