| author | akozak | 2011-11-23 03:46:11 (EST) |
|---|---|---|
| committer | Winston Prakash | 2011-12-01 20:47:07 (EST) |
| commit | c85fcabbfee5b9fd2501b9999fbc8f24f5d35097 (patch) (side-by-side diff) | |
| tree | 153f0fd4d36cd4b35a0c3aadb44dfb4617388d2e | |
| parent | 8cfaaa50e468fd5c045c071161117700d8acc3da (diff) | |
| download | org.eclipse.hudson.core-c85fcabbfee5b9fd2501b9999fbc8f24f5d35097.zip org.eclipse.hudson.core-c85fcabbfee5b9fd2501b9999fbc8f24f5d35097.tar.gz org.eclipse.hudson.core-c85fcabbfee5b9fd2501b9999fbc8f24f5d35097.tar.bz2 | |
Implement Utility methods for DescribableList
Signed-off-by: Winston Prakash <winston.prakash@gmail.com>
5 files changed, 150 insertions, 35 deletions
diff --git a/hudson-core/src/main/java/hudson/model/Project.java b/hudson-core/src/main/java/hudson/model/Project.java index 004c003..abc87a5 100644 --- a/hudson-core/src/main/java/hudson/model/Project.java +++ b/hudson-core/src/main/java/hudson/model/Project.java @@ -31,6 +31,7 @@ import hudson.tasks.Maven.ProjectWithMaven; import hudson.tasks.Maven.MavenInstallation; import hudson.triggers.Trigger; import hudson.util.DescribableList; +import hudson.util.DescribableListUtil; import net.sf.json.JSONObject; import org.eclipse.hudson.api.model.IProject; import org.kohsuke.stapler.StaplerRequest; @@ -53,6 +54,7 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> IProject { public static final String BUILDERS_PROPERTY_NAME = "builders"; + public static final String BUILD_WRAPPERS_PROPERTY_NAME = "buildWrappers"; /** * List of active {@link Builder}s configured for this project. @@ -71,6 +73,9 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> /** * List of active {@link BuildWrapper}s configured for this project. + * @deprecated as of 2.2.0 + * don't use this field directly, logic was moved to {@link org.eclipse.hudson.api.model.IProjectProperty}. + * Use getter/setter for accessing to this field. */ private DescribableList<BuildWrapper,Descriptor<BuildWrapper>> buildWrappers = new DescribableList<BuildWrapper,Descriptor<BuildWrapper>>(this); @@ -86,14 +91,9 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { super.onLoad(parent, name); - if (buildWrappers==null) { - // it didn't exist in < 1.64 - buildWrappers = new DescribableList<BuildWrapper, Descriptor<BuildWrapper>>(this); - OldDataMonitor.report(this, "1.64"); - } getBuildersList().setOwner(this); publishers.setOwner(this); - buildWrappers.setOwner(this); + getBuildWrappersList().setOwner(this); } @Override @@ -103,6 +103,10 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> setBuilders(builders); builders = null; } + if (null == getProperty(BUILD_WRAPPERS_PROPERTY_NAME)) { + setBuildWrappers(buildWrappers); + buildWrappers = null; + } } public AbstractProject<?, ?> asProject() { @@ -130,11 +134,15 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> } public Map<Descriptor<BuildWrapper>,BuildWrapper> getBuildWrappers() { - return buildWrappers.toMap(); + return getBuildWrappersList().toMap(); } public DescribableList<BuildWrapper, Descriptor<BuildWrapper>> getBuildWrappersList() { - return buildWrappers; + return getDescribableListProjectProperty(BUILD_WRAPPERS_PROPERTY_NAME).getValue(); + } + + public void setBuildWrappers(DescribableList<BuildWrapper, Descriptor<BuildWrapper>> buildWrappers) { + getDescribableListProjectProperty(BUILD_WRAPPERS_PROPERTY_NAME).setValue(buildWrappers); } @Override @@ -142,9 +150,9 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> final Set<ResourceActivity> activities = new HashSet<ResourceActivity>(); activities.addAll(super.getResourceActivities()); - activities.addAll(Util.filter(builders,ResourceActivity.class)); + activities.addAll(Util.filter(getBuildersList(), ResourceActivity.class)); activities.addAll(Util.filter(publishers,ResourceActivity.class)); - activities.addAll(Util.filter(buildWrappers,ResourceActivity.class)); + activities.addAll(Util.filter(getBuildWrappersList(), ResourceActivity.class)); return activities; } @@ -179,8 +187,8 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> protected void buildDependencyGraph(DependencyGraph graph) { publishers.buildDependencyGraph(this,graph); - builders.buildDependencyGraph(this,graph); - buildWrappers.buildDependencyGraph(this,graph); + getBuildersList().buildDependencyGraph(this, graph); + getBuildWrappersList().buildDependencyGraph(this, graph); } @Override @@ -202,14 +210,9 @@ public abstract class Project<P extends Project<P,B>,B extends Build<P,B>> @Override protected void submit( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, FormException { super.submit(req,rsp); - JSONObject json = req.getSubmittedForm(); - - buildWrappers.rebuild(req, json, BuildWrappers.getFor(this)); - DescribableList<Builder, Descriptor<Builder>> buildersCandidates - = new DescribableList<Builder, Descriptor<Builder>>(this); - buildersCandidates.replaceBy(Descriptor.newInstancesFromHeteroList(req, json, "builder", Builder.all())); - setBuilders(buildersCandidates); + setBuildWrappers(DescribableListUtil.buildFromJson(this, req, json, BuildWrappers.getFor(this))); + setBuilders(DescribableListUtil.buildFromHetero(this, req, json, "builder", Builder.all())); publishers.rebuild(req, json, BuildStepDescriptor.filter(Publisher.all(), this.getClass())); } diff --git a/hudson-core/src/main/java/hudson/util/DescribableList.java b/hudson-core/src/main/java/hudson/util/DescribableList.java index 209d980..910ba27 100644 --- a/hudson-core/src/main/java/hudson/util/DescribableList.java +++ b/hudson-core/src/main/java/hudson/util/DescribableList.java @@ -122,15 +122,17 @@ public class DescribableList<T extends Describable<T>, D extends Descriptor<T>> /** * Rebuilds the list by creating a fresh instances from the submitted form. - * - * <p> + * <p/> + * <p/> * This method is almost always used by the owner. * This method does not invoke the save method. * - * @param json - * Structured form data that includes the data for nested descriptor list. + * @param json Structured form data that includes the data for nested descriptor list. + * @deprecated as of 2.2.0, + * use {@link DescribableListUtil#buildFromJson(hudson.model.Saveable, org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject, java.util.List)} */ - public void rebuild(StaplerRequest req, JSONObject json, List<? extends Descriptor<T>> descriptors) throws FormException, IOException { + public void rebuild(StaplerRequest req, JSONObject json, List<? extends Descriptor<T>> descriptors) + throws FormException, IOException { List<T> newList = new ArrayList<T>(); for (Descriptor<T> d : descriptors) { @@ -154,14 +156,21 @@ public class DescribableList<T extends Describable<T>, D extends Descriptor<T>> /** * Rebuilds the list by creating a fresh instances from the submitted form. - * - * <p> + * <p/> * This version works with the the <f:hetero-list> UI tag, where the user * is allowed to create multiple instances of the same descriptor. Order is also * significant. + * + * @deprecated as of 2.2.0, + * use {@link DescribableListUtil#buildFromHetero(hudson.model.Saveable, org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject, String, java.util.Collection)} + * or {@link Descriptor#newInstancesFromHeteroList(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject, String, java.util.Collection)} */ - public void rebuildHetero(StaplerRequest req, JSONObject formData, Collection<? extends Descriptor<T>> descriptors, String key) throws FormException, IOException { - replaceBy(Descriptor.newInstancesFromHeteroList(req,formData,key,descriptors)); + public void rebuildHetero(StaplerRequest req, + JSONObject formData, + Collection<? extends Descriptor<T>> descriptors, + String key) + throws FormException, IOException { + replaceBy(Descriptor.newInstancesFromHeteroList(req, formData, key, descriptors)); } /** diff --git a/hudson-core/src/main/java/hudson/util/DescribableListUtil.java b/hudson-core/src/main/java/hudson/util/DescribableListUtil.java new file mode 100644 index 0000000..c8fe6ce --- a/dev/null +++ b/hudson-core/src/main/java/hudson/util/DescribableListUtil.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * + * Copyright (c) 2011 Oracle Corporation. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * + * Nikita Levyankov + * + *******************************************************************************/ + +package hudson.util; + +import hudson.model.Describable; +import hudson.model.Descriptor; +import hudson.model.Saveable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import net.sf.json.JSONObject; +import org.kohsuke.stapler.StaplerRequest; + +/** + * Utility class for DescribableList logic. + * <p/> + * Date: 10/6/11 + * + * @author Nikita Levyankov + */ +public final class DescribableListUtil { + + private DescribableListUtil() { + } + + /** + * Builds the list by creating a fresh instances from the submitted form. + * <p/> + * This method is almost always used by the owner. + * This method does not invoke the save method. + * + * @param owner represents owner of {@link DescribableList} + * @param req {@link StaplerRequest} + * @param json Structured form data that includes the data for nested descriptor list. + * @param descriptors list of descriptors to create instances from. + * @return list. + * @throws IOException if any. + * @throws Descriptor.FormException if any. + */ + public static <T extends Describable<T>, D extends Descriptor<T>> DescribableList<T, D> buildFromJson( + Saveable owner, + StaplerRequest req, + JSONObject json, + List<D> descriptors) + throws Descriptor.FormException, IOException { + List<T> newList = new ArrayList<T>(); + + for (Descriptor<T> d : descriptors) { + String name = d.getJsonSafeClassName(); + if (json.has(name)) { + newList.add(d.newInstance(req, json.getJSONObject(name))); + } + } + return new DescribableList<T, D>(owner, newList); + } + + /** + * Rebuilds the list by creating a fresh instances from the submitted form. + * <p/> + * This version works with the the <f:hetero-list> UI tag, where the user + * is allowed to create multiple instances of the same descriptor. Order is also + * significant. + * + * @param owner represents owner of {@link DescribableList} + * @param req {@link StaplerRequest} + * @param formData {@link JSONObject} populated based on form data, + * @param key the JSON property name for 'formData' that represents the data for the list of {@link Describable} + * @param descriptors list of descriptors to create instances from. + * @return list. + * @throws IOException if any. + * @throws Descriptor.FormException if any. + * @see Descriptor#newInstancesFromHeteroList(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject, String, java.util.Collection) + */ + public static <T extends Describable<T>, D extends Descriptor<T>> DescribableList<T, D> buildFromHetero( + Saveable owner, + StaplerRequest req, JSONObject formData, + String key, + Collection<D> descriptors) + throws Descriptor.FormException, IOException { + return new DescribableList<T, D>(owner, Descriptor.newInstancesFromHeteroList(req, formData, key, descriptors)); + } + +} diff --git a/hudson-core/src/main/resources/lib/form/hetero-list.jelly b/hudson-core/src/main/resources/lib/form/hetero-list.jelly index 42822fa..7608945 100644 --- a/hudson-core/src/main/resources/lib/form/hetero-list.jelly +++ b/hudson-core/src/main/resources/lib/form/hetero-list.jelly @@ -91,6 +91,13 @@ <j:set target="${attrs}" property="isPropertyOverridden" value="false"/> </j:if> <j:set var="targetType" value="${attrs.targetType?:it.class}"/> + <j:if test="${attrs.isPropertyOverridden and !empty(attrs.resetUrl)}"> + <div class="hetero-list-reset"> + <a href="#" class="reset-button" resetURL="${resetUrl}"> + <img src="${imagesURL}/16x16/undo.png" alt="Reset ${title}"/> + </a> + </div> + </j:if> <div class="hetero-list-container${hasHeader?' with-drag-drop':''}${attrs.isPropertyOverridden? ' modified' :''}"> <!-- display existing items --> <j:forEach var="i" items="${attrs.items}"> @@ -116,11 +123,4 @@ <input type="button" value="${attrs.addCaption?:'%Add'}" class="hetero-list-add" /> </div> </div> - <j:if test="${attrs.isPropertyOverridden and !empty(attrs.resetUrl)}"> - <div class="hetero-list-reset"> - <a href="#" class="reset-button" resetURL="${resetUrl}"> - <img src="${imagesURL}/16x16/undo.png" alt="Reset ${title}" /> - </a> - </div> - </j:if> </j:jelly> diff --git a/hudson-war/src/main/webapp/css/style.css b/hudson-war/src/main/webapp/css/style.css index a8e6c84..0203a9d 100644 --- a/hudson-war/src/main/webapp/css/style.css +++ b/hudson-war/src/main/webapp/css/style.css @@ -1012,6 +1012,12 @@ tr.modified > td.setting-leftspace { background-color: #FFE8C9; } +/* --- Reset button container for hetero-list --- */ +.hetero-list-reset { + float: right; + margin-right: -20px; + margin-top: 5px; +} /* ======================= Job Configuration table ========================== */ table.configure { |

