提交 304ffabe 编写于 作者: K Kohsuke Kawaguchi

implemented ModelObjectWithChildren in various model object implementations

上级 c77baaae
......@@ -32,6 +32,9 @@ import jenkins.model.Jenkins;
import hudson.model.RSS;
import hudson.util.CopyOnWriteMap;
import jenkins.model.JenkinsLocationConfiguration;
import jenkins.model.ModelObjectWithChildren;
import jenkins.model.ModelObjectWithContextMenu;
import jenkins.model.ModelObjectWithContextMenu.ContextMenu;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
......@@ -49,6 +52,7 @@ import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
......@@ -58,9 +62,9 @@ import java.util.logging.Logger;
*
* @author Kohsuke Kawaguchi
*/
public class LogRecorderManager extends AbstractModelObject {
public class LogRecorderManager extends AbstractModelObject implements ModelObjectWithChildren {
/**
* {@link LogRecorder}s.
* {@link LogRecorder}s keyed by their {@linkplain LogRecorder#name name}.
*/
public transient final Map<String,LogRecorder> logRecorders = new CopyOnWriteMap.Tree<String,LogRecorder>();
......@@ -109,6 +113,15 @@ public class LogRecorderManager extends AbstractModelObject {
return new HttpRedirect(name+"/configure");
}
public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
ContextMenu menu = new ContextMenu();
menu.add("all","All Jenkins Logs");
for (Entry<String, LogRecorder> e : logRecorders.entrySet()) {
menu.add(e.getKey(),e.getKey());
}
return menu;
}
/**
* Configure the logging level.
*/
......
......@@ -63,6 +63,7 @@ import hudson.util.CopyOnWriteMap;
import hudson.util.DescribableList;
import hudson.util.FormValidation;
import hudson.util.FormValidation.Kind;
import jenkins.model.ModelObjectWithChildren;
import jenkins.scm.SCMCheckoutStrategyDescriptor;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.HttpResponse;
......@@ -95,7 +96,7 @@ import java.util.logging.Logger;
*
* @author Kohsuke Kawaguchi
*/
public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> implements TopLevelItem, SCMedItem, ItemGroup<MatrixConfiguration>, Saveable, FlyweightTask, BuildableItemWithBuildWrappers {
public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> implements TopLevelItem, SCMedItem, ItemGroup<MatrixConfiguration>, Saveable, FlyweightTask, BuildableItemWithBuildWrappers, ModelObjectWithChildren {
/**
* Configuration axes.
*/
......@@ -853,6 +854,13 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
return rsp;
}
public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
ContextMenu menu = new ContextMenu();
for (MatrixConfiguration c : getActiveConfigurations()) {
menu.add(c);
}
return menu;
}
public DescriptorImpl getDescriptor() {
return DESCRIPTOR;
......
......@@ -36,6 +36,8 @@ import hudson.util.DescribableList;
import hudson.util.FormApply;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithChildren;
import jenkins.model.ModelObjectWithContextMenu.ContextMenu;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
......@@ -65,7 +67,7 @@ import net.sf.json.JSONObject;
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public final class ComputerSet extends AbstractModelObject implements Describable<ComputerSet> {
public final class ComputerSet extends AbstractModelObject implements Describable<ComputerSet>, ModelObjectWithChildren {
/**
* This is the owner that persists {@link #monitors}.
*/
......@@ -97,6 +99,14 @@ public final class ComputerSet extends AbstractModelObject implements Describabl
return Jenkins.getInstance().getComputers();
}
public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
ContextMenu m = new ContextMenu();
for (Computer c : get_all()) {
m.add(c);
}
return m;
}
/**
* Exposing {@link NodeMonitor#all()} for Jelly binding.
*/
......
......@@ -44,6 +44,9 @@ import hudson.slaves.Cloud;
import hudson.util.QuotedStringTokenizer;
import hudson.util.VariableResolver;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithChildren;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
......@@ -70,7 +73,7 @@ import com.thoughtworks.xstream.io.HierarchicalStreamReader;
* @see Jenkins#getLabel(String)
*/
@ExportedBean
public abstract class Label extends Actionable implements Comparable<Label>, ModelObject {
public abstract class Label extends Actionable implements Comparable<Label>, ModelObjectWithChildren {
/**
* Display name of this label.
*/
......@@ -455,6 +458,14 @@ public abstract class Label extends Actionable implements Comparable<Label>, Mod
return name;
}
public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
ContextMenu menu = new ContextMenu();
for (Node node : getNodes()) {
menu.add(node);
}
return menu;
}
public static final class ConverterImpl implements Converter {
public ConverterImpl() {
}
......
......@@ -3948,7 +3948,7 @@ public class Jenkins extends AbstractCIBase implements ModifiableTopLevelItemGro
/**
* Prefix to static resources like images and javascripts in the war file.
* Either "" or strings like "/static/VERSION", which avoids Hudson to pick up
* Either "" or strings like "/static/VERSION", which avoids Jenkins to pick up
* stale cache when the user upgrades to a different version.
* <p>
* Value computed in {@link WebAppMain}.
......
......@@ -4,7 +4,12 @@ import hudson.Functions;
import hudson.Util;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.BallColor;
import hudson.model.Computer;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.ModelObject;
import hudson.model.Node;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.JellyTagException;
......@@ -118,6 +123,53 @@ public interface ModelObjectWithContextMenu extends ModelObject {
return this;
}
/**
* Adds a manually constructed {@link MenuItem}
*
* @since 1.513
*/
public ContextMenu add(MenuItem item) {
items.add(item);
return this;
}
/**
* Adds a node
*
* @since 1.513
*/
public ContextMenu add(Node n) {
Computer c = n.toComputer();
return add(new MenuItem()
.withDisplayName(n.getDisplayName())
.withStockIcon((c==null) ? "computer.png" : c.getIcon())
.withContextRelativeUrl(n.getSearchUrl()));
}
/**
* Adds a computer
*
* @since 1.513
*/
public ContextMenu add(Computer c) {
return add(new MenuItem()
.withDisplayName(c.getDisplayName())
.withStockIcon(c.getIcon())
.withContextRelativeUrl(c.getUrl()));
}
/**
* Adds a child item when rendering context menu of its parent.
*
* @since 1.513
*/
public ContextMenu add(Job job) {
return add(new MenuItem()
.withDisplayName(job.getDisplayName())
.withIcon(job.getIconColor())
.withUrl(job.getSearchUrl()));
}
/**
* Default implementation of the context menu generation.
*
......@@ -209,13 +261,57 @@ public interface ModelObjectWithContextMenu extends ModelObject {
public ContextMenu subMenu;
public MenuItem(String url, String icon, String displayName) {
withUrl(url).withIcon(icon).withDisplayName(displayName);
}
public MenuItem() {
}
public MenuItem withUrl(String url) {
try {
this.url = new URI(Stapler.getCurrentRequest().getRequestURI()).resolve(new URI(url)).toString();
} catch (URISyntaxException x) {
throw new IllegalArgumentException("Bad URI from " + Stapler.getCurrentRequest().getRequestURI() + " vs. " + url, x);
}
return this;
}
/**
* Sets the URL by passing in a URL relative to the context path of Jenkins
*/
public MenuItem withContextRelativeUrl(String url) {
if (!url.startsWith("/")) url = '/'+url;
this.url = Stapler.getCurrentRequest().getContextPath()+url;
return this;
}
public MenuItem withIcon(String icon) {
this.icon = icon;
return this;
}
public MenuItem withIcon(BallColor color) {
return withStockIcon(color.getImage());
}
/**
* Sets the icon from core's stock icon
*
* @param icon
* String like "gear.png" that resolves to 24x24 stock icon in the core
*/
public MenuItem withStockIcon(String icon) {
this.icon = Stapler.getCurrentRequest().getContextPath() + Jenkins.RESOURCE_PATH + "/images/24x24/"+icon;
return this;
}
public MenuItem withDisplayName(String displayName) {
this.displayName = Util.escape(displayName);
return this;
}
public MenuItem withDisplayName(ModelObject o) {
return withDisplayName(o.getDisplayName());
}
}
}
......@@ -33,6 +33,7 @@ import hudson.Functions;
import hudson.Indenter;
import hudson.Plugin;
import hudson.Util;
import hudson.matrix.MatrixConfiguration;
import hudson.maven.local_repo.DefaultLocalRepositoryLocator;
import hudson.maven.local_repo.LocalRepositoryLocator;
import hudson.maven.local_repo.PerJobLocalRepositoryLocator;
......@@ -89,6 +90,7 @@ import java.util.logging.Logger;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithChildren;
import jenkins.mvn.DefaultGlobalSettingsProvider;
import jenkins.mvn.DefaultSettingsProvider;
import jenkins.mvn.FilePathSettingsProvider;
......@@ -120,7 +122,7 @@ import org.kohsuke.stapler.export.Exported;
*
* @author Kohsuke Kawaguchi
*/
public class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,MavenModuleSetBuild> implements TopLevelItem, ItemGroup<MavenModule>, SCMedItem, Saveable, BuildableItemWithBuildWrappers {
public class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,MavenModuleSetBuild> implements TopLevelItem, ItemGroup<MavenModule>, SCMedItem, Saveable, BuildableItemWithBuildWrappers, ModelObjectWithChildren {
/**
* All {@link MavenModule}s, keyed by their {@link MavenModule#getModuleName()} module name}s.
......@@ -1170,6 +1172,14 @@ public class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,MavenMod
return FormValidation.ok();
}
public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse response) throws Exception {
ContextMenu menu = new ContextMenu();
for (MavenModule mm : getModules()) {
menu.add(mm);
}
return menu;
}
public DescriptorImpl getDescriptor() {
return DESCRIPTOR;
}
......
package jenkins.plugins.ui_samples;
import hudson.Extension;
import jenkins.model.Jenkins;
import jenkins.model.ModelObjectWithChildren;
import jenkins.model.ModelObjectWithContextMenu;
import org.kohsuke.stapler.StaplerRequest;
......@@ -31,7 +30,7 @@ public class NavigationContextMenu extends UISample implements ModelObjectWithCo
return new ContextMenu()
.add("http://jenkins-ci.org/","Jenkins project")
.add("http://www.cloudbees.com/","CloudBees")
.add(request.getContextPath(), Jenkins.RESOURCE_PATH+"/images/24x24/gear.png","top-page");
.add(new MenuItem().withContextRelativeUrl("/").withStockIcon("gear.png").withDisplayName("top page"));
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册