mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-13 12:02:20 +00:00
[ui-samples] add dropdownList sample
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@29988 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
0cd23f0ea6
commit
355cd7d343
8 files changed
+221
-49
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package hudson.plugins.ui_samples;
|
||||
|
||||
import hudson.DescriptorExtensionList;
|
||||
import hudson.Extension;
|
||||
import hudson.ExtensionPoint;
|
||||
import hudson.model.Describable;
|
||||
import hudson.model.Descriptor;
|
||||
import hudson.model.Hudson;
|
||||
import hudson.util.XStream2;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import javax.servlet.ServletException;
|
||||
import org.kohsuke.stapler.DataBoundConstructor;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
|
||||
/**
|
||||
* @author Alan.Harder@oracle.com
|
||||
*/
|
||||
@Extension
|
||||
public class DropdownList extends UISample {
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Show different form elements based on choice in a <select> control";
|
||||
}
|
||||
|
||||
public Fruit getFruit() {
|
||||
// Could return currently configured/saved item here to initialized form with this data
|
||||
return null;
|
||||
}
|
||||
|
||||
public DescriptorExtensionList<Fruit,Descriptor<Fruit>> getFruitDescriptors() {
|
||||
return Hudson.getInstance().getDescriptorList(Fruit.class);
|
||||
}
|
||||
|
||||
// Process form data and show it as serialized XML
|
||||
public void doSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
|
||||
// stapler-class in form data tells Stapler which Fruit subclass to use
|
||||
Fruit fruit = req.bindJSON(Fruit.class, req.getSubmittedForm().getJSONObject("fruit"));
|
||||
rsp.setContentType("text/plain");
|
||||
new XStream2().toXML(fruit, rsp.getWriter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SourceFile> getSourceFiles() {
|
||||
List<SourceFile> list = new java.util.ArrayList<SourceFile>(super.getSourceFiles());
|
||||
list.add(new SourceFile("Apple/config.jelly"));
|
||||
list.add(new SourceFile("Banana/config.jelly"));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Extension
|
||||
public static final class DescriptorImpl extends UISampleDescriptor {
|
||||
}
|
||||
|
||||
public static class Fruit implements ExtensionPoint, Describable<Fruit> {
|
||||
protected String name;
|
||||
private Fruit(String name) { this.name = name; }
|
||||
|
||||
public Descriptor<Fruit> getDescriptor() {
|
||||
return Hudson.getInstance().getDescriptor(getClass());
|
||||
}
|
||||
}
|
||||
|
||||
public static class FruitDescriptor extends Descriptor<Fruit> {
|
||||
public FruitDescriptor(Class<? extends Fruit> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
public String getDisplayName() {
|
||||
return clazz.getSimpleName();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Apple extends Fruit {
|
||||
private int seeds;
|
||||
@DataBoundConstructor public Apple(int seeds) {
|
||||
super("Apple");
|
||||
this.seeds = seeds;
|
||||
}
|
||||
@Extension public static final FruitDescriptor D = new FruitDescriptor(Apple.class);
|
||||
}
|
||||
|
||||
public static class Banana extends Fruit {
|
||||
private boolean yellow;
|
||||
@DataBoundConstructor public Banana(boolean yellow) {
|
||||
super("Banana");
|
||||
this.yellow = yellow;
|
||||
}
|
||||
@Extension public static final FruitDescriptor D = new FruitDescriptor(Banana.class);
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,6 @@ import hudson.Extension;
|
||||
import hudson.util.ListBoxModel;
|
||||
import org.kohsuke.stapler.QueryParameter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ import hudson.ExtensionPoint;
|
||||
import hudson.model.Action;
|
||||
import hudson.model.Describable;
|
||||
import hudson.model.Hudson;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -44,11 +45,14 @@ public abstract class UISample implements ExtensionPoint, Action, Describable<UI
|
||||
/**
|
||||
* Binds {@link SourceFile}s into URL.
|
||||
*/
|
||||
public SourceFile getSourceFile(String name) {
|
||||
public void doSourceFile(StaplerRequest req, StaplerResponse rsp) throws IOException {
|
||||
String name = req.getRestOfPath().substring(1); // Remove leading /
|
||||
for (SourceFile sf : getSourceFiles())
|
||||
if (sf.name.equals(name))
|
||||
return sf;
|
||||
return null;
|
||||
if (sf.name.equals(name)) {
|
||||
sf.doIndex(rsp);
|
||||
return;
|
||||
}
|
||||
rsp.sendError(rsp.SC_NOT_FOUND);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user