Skip to content
Permalink
Browse files

[JENKINS-38889] Add an option to disable the default maximum number o…

…f visible elements. Also add another option to specify the number of visible elements on the UI. Unit tests included.
  • Loading branch information
kinow committed Oct 28, 2016
1 parent 2c966b6 commit 83f53bd14f939711e56213ca91ad679fffedd7e6
@@ -77,13 +77,30 @@ protected AbstractCascadableParameter(String name, String description, Script sc
* @param randomName parameter random generated name (uuid)
* @param script script used to generate the list of parameter values
* @param referencedParameters comma separated list of referenced parameters
* @deprecated see JENKINS-38889
*/
protected AbstractCascadableParameter(String name, String description, String randomName,
Script script, String referencedParameters) {
super(name, description, randomName, script);
this.referencedParameters = referencedParameters;
}

/**
* Create a new abstract cascadable parameter.
* @param name name
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script used to generate the list of parameter values
* @param referencedParameters comma separated list of referenced parameters
* @param useDefaultMaxVisibleItemCount flag to enable the default maximum of visible item count
* @param visibleItemCount number of visible items on the UI
*/
protected AbstractCascadableParameter(String name, String description, String randomName,
Script script, String referencedParameters, Boolean useDefaultMaxVisibleItemCount, Integer visibleItemCount) {
super(name, description, randomName, script, useDefaultMaxVisibleItemCount, visibleItemCount);
this.referencedParameters = referencedParameters;
}

/*
* (non-Javadoc)
* @see org.biouno.unochoice.CascadableParameter#getReferencedParameters()
@@ -52,7 +52,7 @@
* @since 0.20
*/
public abstract class AbstractScriptableParameter extends AbstractUnoChoiceParameter
implements ScriptableParameter<Map<Object, Object>> {
implements ScriptableParameter<Map<Object, Object>> {

/*
* Serial UID.
@@ -73,7 +73,15 @@
/**
* Number of visible items on the screen.
*/
private volatile int visibleItemCount = 1;
private final int visibleItemCount;
/**
* Computer number of visible items.
*/
private volatile int computedVisibleItemCount;
/**
* Use the default maximum visible item count.
*/
private final Boolean useDefaultMaxVisibleItemCount;
/**
* Script used to render the parameter.
*/
@@ -96,6 +104,8 @@
protected AbstractScriptableParameter(String name, String description, Script script) {
super(name, description);
this.script = script;
this.visibleItemCount = 1;
this.useDefaultMaxVisibleItemCount = Boolean.TRUE;
this.projectName = null;
}

@@ -108,10 +118,49 @@ protected AbstractScriptableParameter(String name, String description, Script sc
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script used to generate the list of parameter values
* @deprecated see JENKINS-38889
*/
protected AbstractScriptableParameter(String name, String description, String randomName, Script script) {
super(name, description, randomName);
this.script = script;
this.visibleItemCount = 1;
this.useDefaultMaxVisibleItemCount = Boolean.TRUE;
// Try to get the project name from the current request. In case of being called in some other non-web way,
// the name will be fetched later via Jenkins.getInstance() and iterating through all items. This is for a
// performance wise approach first.
final StaplerRequest currentRequest = Stapler.getCurrentRequest();
String projectName = null;
if (currentRequest != null) {
final Ancestor ancestor = currentRequest.findAncestor(AbstractItem.class);
if (ancestor != null) {
final Object o = ancestor.getObject();
if (o instanceof AbstractItem) {
final AbstractItem parentItem = (AbstractItem) o;
projectName = parentItem.getName();
}
}
}
this.projectName = projectName;
}

/**
* Inherited constructor.
*
* {@inheritDoc}
*
* @param name name
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script used to generate the list of parameter values
* @param useDefaultMaxVisibleItemCount flag to enable the default maximum of visible item count
* @param visibleItemCount number of visible items on the UI
*/
protected AbstractScriptableParameter(String name, String description, String randomName, Script script,
Boolean useDefaultMaxVisibleItemCount, Integer visibleItemCount) {
super(name, description, randomName);
this.script = script;
this.visibleItemCount = visibleItemCount;
this.useDefaultMaxVisibleItemCount = useDefaultMaxVisibleItemCount;
// Try to get the project name from the current request. In case of being called in some other non-web way,
// the name will be fetched later via Jenkins.getInstance() and iterating through all items. This is for a
// performance wise approach first.
@@ -139,6 +188,15 @@ public Script getScript() {
return script;
}

/**
* Get the flag to use the default maximum visible item count.
*
* @return flag, that indicates whether to use the default maximum visible item count or not
*/
public Boolean getUseDefaultMaxVisibleItemCount() {
return useDefaultMaxVisibleItemCount;
}

/**
* Gets the current parameters, be it before or after other referenced parameters triggered an update. Populates
* parameters common to all evaluations, such as jenkinsProject, which is the current Jenkins project.
@@ -151,6 +209,7 @@ public Script getScript() {

/**
* Helper parameters used to render the parameter definition.
*
* @return Map with helper parameters
*/
private Map<Object, Object> getHelperParameters() {
@@ -175,12 +234,13 @@ public Script getScript() {

public Map<Object, Object> getChoices() {
Map<Object, Object> choices = this.getChoices(getParameters());
visibleItemCount = choices.size();
computedVisibleItemCount = choices.size();
return choices;
}

/*
* (non-Javadoc)
*
* @see org.biouno.unochoice.ScriptableParameter#getChoices(java.util.Map)
*/
@Override
@@ -189,7 +249,7 @@ public Script getScript() {
final Object value = eval(parameters);
if (value instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) value;
visibleItemCount = map.size();
computedVisibleItemCount = map.size();
return map;
}
if (value instanceof List) {
@@ -198,11 +258,12 @@ public Script getScript() {
for (Object o : (List<Object>) value) {
map.put(o, o);
}
visibleItemCount = map.size();
computedVisibleItemCount = map.size();
return map;
}
LOGGER.warning(String.format("Script parameter with name '%s' is not an instance of java.util.Map. The "
+ "parameter value is %s", getName(), value));
LOGGER.warning(String.format(
"Script parameter with name '%s' is not an instance of java.util.Map. The " + "parameter value is %s",
getName(), value));
return Collections.emptyMap();
}

@@ -232,6 +293,7 @@ private Object eval(Map<Object, Object> parameters) {

/*
* (non-Javadoc)
*
* @see hudson.model.ParameterDefinition#getDefaultParameterValue()
*/
@Override
@@ -255,17 +317,29 @@ public ParameterValue getDefaultParameterValue() {
/**
* Get the number of visible items in the select.
*
* @return the number of choices or, if it is higher than the default, then it returns the default maximum value
* @return the number of visible items
*/
public int getVisibleItemCount() {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.entering(AbstractUnoChoiceParameter.class.getName(), "getVisibleItemCount");
return visibleItemCount;
}

/**
* Get the computed number of visible items in the select.
*
* @return 1 if visibleItemCount is less or equal to 0, the visibleItemCount value if not using the default max
* visible item count, otherwise the number of choices or, if it is higher than the default, then it returns
* the default maximum value
*/
public int getComputedVisibleItemCount() {
if (useDefaultMaxVisibleItemCount) {
if (computedVisibleItemCount < DEFAULT_MAX_VISIBLE_ITEM_COUNT)
return computedVisibleItemCount;
return DEFAULT_MAX_VISIBLE_ITEM_COUNT;
}
if (visibleItemCount <= 0) {
return 1;
}
if (visibleItemCount <= 0)
visibleItemCount = 1;
if (visibleItemCount < DEFAULT_MAX_VISIBLE_ITEM_COUNT)
return visibleItemCount;
return DEFAULT_MAX_VISIBLE_ITEM_COUNT;
return visibleItemCount;
}

}
@@ -24,21 +24,26 @@

package org.biouno.unochoice;

import hudson.Extension;

import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.model.Script;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;

/**
* <p>A choice parameter, that gets updated when another parameter changes. The simplest
* use case for this, would be having a list of states, and when the user selected a
* state it would trigger an update of the city fields.</p>
* <p>
* A choice parameter, that gets updated when another parameter changes. The simplest use case for this, would be having
* a list of states, and when the user selected a state it would trigger an update of the city fields.
* </p>
*
* <p>The state parameter would be a choice parameter, and the city parameter would be a
* cascade choice parameter, that referenced the former.</p>
* <p>
* The state parameter would be a choice parameter, and the city parameter would be a cascade choice parameter, that
* referenced the former.
* </p>
*
* <p>Its options are retrieved from the evaluation of a Groovy script.</p>
* <p>
* Its options are retrieved from the evaluation of a Groovy script.
* </p>
*
* @author Bruno P. Kinoshita
* @since 0.1
@@ -71,8 +76,8 @@
* @param filterable filter flag
* @deprecated see JENKINS-32149
*/
public CascadeChoiceParameter(String name, String description, Script script,
String choiceType, String referencedParameters, Boolean filterable) {
public CascadeChoiceParameter(String name, String description, Script script, String choiceType,
String referencedParameters, Boolean filterable) {
super(name, description, script, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
@@ -88,17 +93,41 @@ public CascadeChoiceParameter(String name, String description, Script script,
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @deprecated see JENKINS-38889
*/
@DataBoundConstructor
public CascadeChoiceParameter(String name, String description, String randomName, Script script,
String choiceType, String referencedParameters, Boolean filterable) {
public CascadeChoiceParameter(String name, String description, String randomName, Script script, String choiceType,
String referencedParameters, Boolean filterable) {
super(name, description, randomName, script, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
}

/**
* Constructor called from Jelly with parameters.
*
* @param name name
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @param useDefaultMaxVisibleItemCount flag to enable the default maximum of visible item count
* @param visibleItemCount number of visible items on the UI
*/
@DataBoundConstructor
public CascadeChoiceParameter(String name, String description, String randomName, Script script, String choiceType,
String referencedParameters, Boolean filterable, Boolean useDefaultMaxVisibleItemCount,
Integer visibleItemCount) {
super(name, description, randomName, script, referencedParameters, useDefaultMaxVisibleItemCount,
visibleItemCount);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
}

/*
* (non-Javadoc)
*
* @see org.biouno.unochoice.AbstractUnoChoiceParameter#getChoiceType()
*/
@Override
@@ -78,15 +78,35 @@ public ChoiceParameter(String name, String description, Script script, String ch
* @param script script
* @param choiceType choice type
* @param filterable filter flag
* @deprecated see JENKINS-38889
*/
@DataBoundConstructor
public ChoiceParameter(String name, String description, String randomName, Script script, String choiceType,
Boolean filterable) {
super(name, description, randomName, script);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
}

/**
* Constructor called from Jelly with parameters.
*
* @param name name
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script
* @param choiceType choice type
* @param filterable filter flag
* @param useDefaultMaxVisibleItemCount flag to enable the default maximum of visible item count
* @param visibleItemCount number of visible items on the UI
*/
@DataBoundConstructor
public ChoiceParameter(String name, String description, String randomName, Script script, String choiceType,
Boolean filterable, Boolean useDefaultMaxVisibleItemCount, Integer visibleItemCount) {
super(name, description, randomName, script, useDefaultMaxVisibleItemCount, visibleItemCount);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
}

/*
* (non-Javadoc)
* @see org.biouno.unochoice.AbstractUnoChoiceParameter#getChoiceType()

0 comments on commit 83f53bd

Please sign in to comment.
You can’t perform that action at this time.