You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathBreadcrumbsPanel.java 3.0KB

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