Ok, solution is to remove the constructor in the question post, and replace 
it with extra class and a constructor which takes that class. Also boolean 
enableText field is redundant, and can be replaced with text!=null check, 
both in Jelly and Java code. New full code:


<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:block>
        <f:optionalBlock name="enableText" title="Enable optional text" 
checked="${instance.text != null}">
            <f:entry title="Optional text" field="text">
                <f:textbox />
            </f:entry>
        </f:optionalBlock>
    </f:block>
</j:jelly>


and


package foo.hyde.jenkins.plugins;

public class OptionalBlockSampleBuilder extends hudson.tasks.Builder {

    public final String text;

    @org.kohsuke.stapler.DataBoundConstructor
    public OptionalBlockSampleBuilder(OptinalTextBlock enableText) {
        this.text = (enableText != null) ? enableText.text : null;
    }

    public static class OptinalTextBlock {
        private String text;

        @org.kohsuke.stapler.DataBoundConstructor
        public OptinalTextBlock(String text) {
            this.text = text;
        }
    }

    @Override
    public boolean perform(hudson.model.AbstractBuild build, hudson.Launcher 
launcher, hudson.model.BuildListener listener) {
        listener.getLogger().println("OptionalBlockSampleBuilder " + text);
        return true;
    }

    @hudson.Extension
    public static final class DescriptorImpl extends 
hudson.tasks.BuildStepDescriptor<hudson.tasks.Builder> {

        public boolean isApplicable(Class<? extends 
hudson.model.AbstractProject> aClass) {
            return true;
        }

        public String getDisplayName() {
            return "Optional Block Sample";
        }
    }
}


If somebody gets around to adding above code to wiki or something, feel 
free. Also any feedback/improvements here are welcome.


On Friday, October 26, 2012 10:36:17 AM UTC+3, ari_hyttinen wrote:
>
> I have trouble getting optionalBlock to work. Test code, 

SNIP
 

Reply via email to