ViewsTabBar.java 4.3 KB
Newer Older
W
wjprakash 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2010, Winston.Prakash@oracle.com, CloudBees, Inc.
W
wjprakash 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.views;

import hudson.DescriptorExtensionList;
27
import hudson.Extension;
W
wjprakash 已提交
28 29 30
import hudson.ExtensionPoint;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
31
import hudson.model.Descriptor.FormException;
32 33 34 35 36 37
import hudson.model.View;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Nonnull;
38
import jenkins.model.GlobalConfiguration;
39
import jenkins.model.Jenkins;
W
wjprakash 已提交
40
import hudson.model.ListView;
41
import net.sf.json.JSONObject;
K
Kohsuke Kawaguchi 已提交
42
import org.jenkinsci.Symbol;
43 44
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
45
import org.kohsuke.stapler.StaplerRequest;
W
wjprakash 已提交
46 47 48 49 50

/**
 * Extension point for adding a ViewsTabBar header to Projects {@link ListView}.
 *
 * <p>
51
 * This object must have the {@code viewTabs.jelly}. This view
W
wjprakash 已提交
52 53 54 55 56 57 58 59
 * is called once when the project views main panel is built.
 * The "views" attribute is set to the "Collection of views".
 *
 * <p>
 * There also must be a default constructor, which is invoked to create a Views TabBar in
 * the default configuration.
 *
 * @author Winston Prakash
60
 * @since 1.381
W
wjprakash 已提交
61 62 63 64
 * @see ViewsTabBarDescriptor
 */
public abstract class ViewsTabBar extends AbstractDescribableImpl<ViewsTabBar> implements ExtensionPoint {
    /**
65
     * Returns all the registered {@link ViewsTabBar} descriptors.
W
wjprakash 已提交
66 67
     */
    public static DescriptorExtensionList<ViewsTabBar, Descriptor<ViewsTabBar>> all() {
68
        return Jenkins.getInstance().<ViewsTabBar, Descriptor<ViewsTabBar>>getDescriptorList(ViewsTabBar.class);
W
wjprakash 已提交
69 70
    }

71
    @Override
W
wjprakash 已提交
72 73 74
    public ViewsTabBarDescriptor getDescriptor() {
        return (ViewsTabBarDescriptor)super.getDescriptor();
    }
75

76 77 78 79 80
    /**
     * Sorts the views by {@link View#getDisplayName()}.
     *
     * @param views the views.
     * @return the sorted views
S
Stephen Connolly 已提交
81
     * @since 2.37
82 83 84 85 86
     */
    @Nonnull
    @Restricted(NoExternalUse.class)
    @SuppressWarnings("unused") // invoked from stapler view
    public List<View> sort(@Nonnull List<? extends View> views) {
J
Jesse Glick 已提交
87
        List<View> result = new ArrayList<View>(views);
88 89 90 91 92 93 94 95 96
        Collections.sort(result, new Comparator<View>() {
            @Override
            public int compare(View o1, View o2) {
                return o1.getDisplayName().compareTo(o2.getDisplayName());
            }
        });
        return result;
    }

97 98 99 100 101
    /**
     * Configures {@link ViewsTabBar} in the system configuration.
     *
     * @author Kohsuke Kawaguchi
     */
K
Kohsuke Kawaguchi 已提交
102
    @Extension(ordinal=310) @Symbol("viewsTabBar")
103 104
    public static class GlobalConfigurationImpl extends GlobalConfiguration {
        public ViewsTabBar getViewsTabBar() {
105
            return Jenkins.get().getViewsTabBar();
106 107 108 109 110
        }

        @Override
        public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
            // for compatibility reasons, the actual value is stored in Jenkins
111
            Jenkins j = Jenkins.get();
112 113 114 115 116 117 118 119 120 121

            if (json.has("viewsTabBar")) {
                j.setViewsTabBar(req.bindJSON(ViewsTabBar.class,json.getJSONObject("viewsTabBar")));
            } else {
                j.setViewsTabBar(new DefaultViewsTabBar());
            }

            return true;
        }
    }
W
wjprakash 已提交
122
}