Jump to content

Changing the default Scriptlet


GoncaloSete

Recommended Posts

Hi!

I've searched the forum for a subject like this, but I couldn't find one thread.

I have a Master report and several subreports and I want them all to share the same scriptlet class.

The first solution I came up with was:

          parameters.put(JRParameter.REPORT_SCRIPTLET, new MyScriptlet());

but this only applies MyScriptlet to the master report.

Every other subreport continue to have JRDefaultScriptlet instantiated.

My challenge is to avoid passing the built-in parameter REPORT_SCRIPTLET to all subreports. In the future, I might have several more and I wouldn't like to worry about passing it all the time.

Is there a way of defining that the default scriptlet is MyScriptlet? (instead of JRDefaultScriptlet)

 

Thanks in advance

Gonçalo

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Hi again

I found that there is this JRFillDataset class that loads a default scriptlet if none is defined in the report.

Is there a way of doing this with "Extensions"?

 

Thank you

Code:
/**	 * Creates the scriptlets.	 * 	 * @return the scriptlets list	 * @throws JRException	 */	protected List createScriptlets(Map parameterValues) throws JRException	{		ScriptletFactoryContext context = new ScriptletFactoryContext(parameterValues);				scriptlets = new ArrayList();				List factories = ExtensionsEnvironment.getExtensionsRegistry().getExtensions(ScriptletFactory.class);		for (Iterator it = factories.iterator(); it.hasNext();)		{			ScriptletFactory factory = (ScriptletFactory)it.next();			List tmpScriptlets = factory.getScriplets(context);			if (tmpScriptlets != null)			{				scriptlets.addAll(tmpScriptlets);			}		}				if (scriptlets.size() == 0)		{			scriptlets.add(0, new JRDefaultScriptlet());		}		return scriptlets;	}
Link to comment
Share on other sites

Well, I reverse engineered the jasperreports code and found out a (potentially dirty) solution.

I will share with you, even if I feel like talking to myself.

  • Your scriptlet class should extend and implement the classes below.
  • In your jasperreports_extension.properties define the following line:

net.sf.jasperreports.extension.registry.factory.scriptlet=com.alert.pfh.reports.business.MyScriplet

 

Of course you should separate your Scriptlet class from the Factory class. That what I've done.

 

Cheers

 

Code:
Code:
private static final ExtensionsRegistry scriptletExtensionsRegistry = new ExtensionsRegistry(){		public List getExtensions(Class extensionType){			if(ScriptletFactory.class.equals(extensionType)){				return Collections.singletonList(new MyScriplet());			}			return null;		}	};	public ExtensionsRegistry createRegistry(String registryId,			JRPropertiesMap properties){		return scriptletExtensionsRegistry;	}

Post Edited by GoncaloSete at 10/06/2009 08:11
Link to comment
Share on other sites

  • 5 years later...
  • 5 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...