Search in sources :

Example 1 with View

use of hudson.model.View in project hudson-2.x by hudson.

the class Functions method getRelativeLinkTo.

/**
     * Computes the relative path from the current page to the given item.
     */
public static String getRelativeLinkTo(Item p) {
    Map<Object, String> ancestors = new HashMap<Object, String>();
    View view = null;
    StaplerRequest request = Stapler.getCurrentRequest();
    for (Ancestor a : request.getAncestors()) {
        ancestors.put(a.getObject(), a.getRelativePath());
        if (a.getObject() instanceof View)
            view = (View) a.getObject();
    }
    String path = ancestors.get(p);
    if (path != null)
        return path;
    Item i = p;
    String url = "";
    while (true) {
        ItemGroup ig = i.getParent();
        url = i.getShortUrl() + url;
        if (ig == Hudson.getInstance()) {
            assert i instanceof TopLevelItem;
            if (view != null && view.contains((TopLevelItem) i)) {
                // if p and the current page belongs to the same view, then return a relative path
                return ancestors.get(view) + '/' + url;
            } else {
                // otherwise return a path from the root Hudson
                return request.getContextPath() + '/' + p.getUrl();
            }
        }
        path = ancestors.get(ig);
        if (path != null)
            return path + '/' + url;
        // if not, ig must have been the Hudson instance
        assert ig instanceof Item;
        i = (Item) ig;
    }
}
Also used : Item(hudson.model.Item) TopLevelItem(hudson.model.TopLevelItem) ItemGroup(hudson.model.ItemGroup) HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) TopLevelItem(hudson.model.TopLevelItem) SearchableModelObject(hudson.search.SearchableModelObject) ModelObject(hudson.model.ModelObject) View(hudson.model.View) Ancestor(org.kohsuke.stapler.Ancestor)

Aggregations

Item (hudson.model.Item)1 ItemGroup (hudson.model.ItemGroup)1 ModelObject (hudson.model.ModelObject)1 TopLevelItem (hudson.model.TopLevelItem)1 View (hudson.model.View)1 SearchableModelObject (hudson.search.SearchableModelObject)1 HashMap (java.util.HashMap)1 Ancestor (org.kohsuke.stapler.Ancestor)1 StaplerRequest (org.kohsuke.stapler.StaplerRequest)1