提交 355cd7d3 编写于 作者: M mindless

[ui-samples] add dropdownList sample


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@29988 71c3de6d-444a-0410-be80-ed276b4c234a
上级 0cd23f0e
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);
}
/**
......
<!--
The MIT License
Copyright (c) 2004-2010, Oracle Corporation
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -22,11 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:s="/lib/samples">
<h1>Source Files</h1>
<ul>
<j:forEach var="f" items="${it.sourceFiles}">
<li><a href="sourceFile/${f.name}">${f.name}</a></li>
</j:forEach>
</ul>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="Seeds">
<f:textbox clazz="number" name="seeds" value="${fruit.seeds}"/>
</f:entry>
</j:jelly>
<!--
The MIT License
Copyright (c) 2004-2010, Sun Microsystems, Inc., Alan Harder
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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="">
<f:checkbox name="yellow" checked="${fruit.yellow}"/> Yellow?
</f:entry>
</j:jelly>
<!--
The MIT License
Copyright (c) 2004-2010, Oracle Corporation
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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:s="/lib/samples">
<s:sample title="Dropdown List Sample">
<form action="submit" method="post">
<table>
<j:set var="currentFruit" value="${it.fruit}"/>
<!-- getJSONObject("fruit") in java code gets data for this block -->
<!-- A @DataBoundConstructor with parameter "Fruit fruit" can also be used -->
<f:dropdownList name="fruit" title="Fruits">
<!-- Loop through available fruit types -->
<j:forEach var="descriptor" items="${it.fruitDescriptors}" varStatus="loop">
<!-- Set "fruit" only if it matches this descriptor -->
<j:set var="fruit" value="${descriptor==currentFruit.descriptor?currentFruit:null}"/>
<f:dropdownListBlock title="${descriptor.displayName}" value="${loop.index}"
selected="${fruit!=null}" staplerClass="${descriptor.clazz.name}">
<!-- Include config.jelly for this fruit type -->
<st:include page="${descriptor.configPage}" from="${descriptor}"/>
</f:dropdownListBlock>
</j:forEach>
</f:dropdownList>
<f:entry title="" >
<f:submit value="Submit"/>
</f:entry>
</table>
</form>
</s:sample>
</j:jelly>
......@@ -22,33 +22,20 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:s="/lib/samples"
xmlns:this="this">
<l:layout title="Dynamic ComboBox Sample">
<l:main-panel>
<h1>Dynamic ComboBox Sample</h1>
<p>
<j:out value="${it.description}"/>
</p>
<this:_sources />
<s:sample>
<form>
<table>
<f:entry title="Country" field="country">
<f:textbox />
</f:entry>
<f:entry title="State" field="state">
<f:select />
</f:entry>
<f:entry title="City" field="city">
<f:select />
</f:entry>
</table>
</form>
</s:sample>
</l:main-panel>
</l:layout>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:s="/lib/samples">
<s:sample title="Dynamic ComboBox Sample">
<form>
<table>
<f:entry title="Country" field="country">
<f:textbox />
</f:entry>
<f:entry title="State" field="state">
<f:select />
</f:entry>
<f:entry title="City" field="city">
<f:select />
</f:entry>
</table>
</form>
</s:sample>
</j:jelly>
<!--
The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Alan Harder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -22,13 +22,29 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<st:documentation>
Set up variable bindings that emulate the form environment.
Page layout for UI samples and variable bindings that emulate the form environment.
<st:attribute name="title" use="required">
Page title
</st:attribute>
</st:documentation>
<j:set var="instance" value="${it}" />
<j:set var="descriptor" value="${it.descriptor}" />
<d:invokeBody />
<l:layout title="${title}">
<l:main-panel>
<h1>${title}</h1>
<p>
<j:out value="${it.description}"/>
</p>
<h3>Source Files</h3>
<ul>
<j:forEach var="f" items="${it.sourceFiles}">
<li><a href="sourceFile/${f.name}">${f.name}</a></li>
</j:forEach>
</ul>
<h3>Sample</h3>
<d:invokeBody/>
</l:main-panel>
</l:layout>
</j:jelly>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册