Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates - eclipse

I have a code template with a variable and I would like to capitalize(just the first letter) the value of this variable only in some occurrences. Is there a way to do this?
The template code is as follows - I would like to capitalize Property Name in my function names...
private $$${PropertyName};
${cursor}
public function get${PropertyName}()
{
return $$this->${PropertyName};
}
public function set${PropertyName}($$value)
{
$$this->${PropertyName} = $$value;
}
Please Note: This is a template for use with code templates in the IDE (not in PHP). For details see: http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/index.html

I also want this and tried to build a custom TemplateVariableResolver to do it. (I already have one custom resolver in place that generates new UUIDs a la http://dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/.)
I made a custom resolver bound to capitalize:
public class CapitalizingVariableResolver extends TemplateVariableResolver {
#Override
public void resolve(TemplateVariable variable, TemplateContext context) {
#SuppressWarnings("unchecked")
final List<String> params = variable.getVariableType().getParams();
if (params.isEmpty())
return;
final String currentValue = context.getVariable(params.get(0));
if (currentValue == null || currentValue.length() == 0)
return;
variable.setValue(currentValue.substring(0, 1).toUpperCase() + currentValue.substring(1));
}
}
(plugin.xml:)
<extension point="org.eclipse.ui.editors.templates">
<resolver
class="com.foo.CapitalizingVariableResolver"
contextTypeId="java"
description="Resolves to the value of the variable named by the first argument, but with its first letter capitalized."
name="capitalized"
type="capitalize">
</resolver>
</extension>
that I would use like this: (I am working in Java; I see that you do not appear to be)
public PropertyAccessor<${propertyType}> ${property:field}() {
return ${property};
}
public ${propertyType} get${capitalizedProperty:capitalize(property)}() {
return ${property}.get();
}
public void set${capitalizedProperty}(${propertyType} ${property}) {
this.${property}.set(${property});
}
As of Eclipse 3.5, the problem I am having is that my custom resolver does not get a chance to re-resolve once I've specified a value for the property variable. It appears that the Java Development Tools (Eclipse JDT) do this dependent template variable re-resolution via a mechanism called MultiVariableGuess within the JavaContext (see addDependency()). Unfortunately for us, that mechanism does not seem to be exposed, so I/we can't use it to do the same (without lots of copy-and-paste or other redundant work).
At this point, I am giving up again for a while and will keep typing the leading-lowercase and leading-uppercase names separately into two independent template variables.

Related

How to add a custom/dynamic target to a hyperlink

I am using jasperreports-6.14.0. As far as I can tell, there is only one way to add a custom hyperlink target to anything that allows hyperlinks. Please tell me there is a better way (other than putting javascript into my reference expression).
Implement the net.sf.jasperreports.engine.export.JRHyperlinkTargetProducer interface, looking in the hyperlink parameters for a specific, named parameter to return as your target string.
Extend net.sf.jasperreports.engine.export.HtmlExporter and set its targetProducerFactory protected field as an instance of your new custom hyperlink target producer.
It looks like this is the only option, but it just feels like there should be a way to skip step 2 by just setting the targetProducerFactory. It's almost like the Jasper devs started to do exactly that and thought "Nah, I just don't feel right about that. Let's take it out."
I am going to do the above unless some kind soul can show me a better way.
Custom target producers are loaded as extensions by the HTML exporter. You can register extensions either programmatically by creating the HTML exporter using your own JasperReportsContext instance, or package the extension in a jar and have it autodetected by the exporter.
If you control the HTML exporter creation you can pass the extension programmatically:
JRHyperlinkTargetProducer targetProducer = new JRHyperlinkTargetProducer() {
#Override
public String getHyperlinkTarget(JRPrintHyperlink hyperlink) {
return "foo";
}
};
JRHyperlinkTargetProducerMapFactory targetProducerFactory = new JRHyperlinkTargetProducerMapFactory();
targetProducerFactory.addProducer("mycustomtarget", targetProducer);
SimpleJasperReportsContext jasperReportsContext = new SimpleJasperReportsContext();
jasperReportsContext.setExtensions(JRHyperlinkTargetProducerFactory.class,
Collections.singletonList(targetProducerFactory));
HtmlExporter htmlExporter = new HtmlExporter(jasperReportsContext);
If you want to have the extension autodected you need to create a jar that contains a class like this:
public class CustomTargetProducerExtension implements ExtensionsRegistryFactory {
#Override
public ExtensionsRegistry createRegistry(String registryId, JRPropertiesMap properties) {
JRHyperlinkTargetProducer targetProducer = new JRHyperlinkTargetProducer() {
#Override
public String getHyperlinkTarget(JRPrintHyperlink hyperlink) {
return "bar";
}
};
JRHyperlinkTargetProducerMapFactory targetProducerFactory = new JRHyperlinkTargetProducerMapFactory();
targetProducerFactory.addProducer("mycustomtarget", targetProducer);
return new SingletonExtensionRegistry<>(JRHyperlinkTargetProducerFactory.class, targetProducerFactory);
}
}
And put a jasperreports_extension.properties resource in the root of the jar containing the line:
net.sf.jasperreports.extension.registry.factory.my.custom.target.producer=<package>.CustomTargetProducerExtension
Then your custom target producer would be automatically detected for elements that have hyperlinkTarget="mycustomtarget"

Extend ProposalProvider in external Eclipse Project via Extension Point

I try to extend my MyDSLProposalProvider from an external Eclipse RCP Project. I created an extension point schema which requires a class property which extends my ProposalProvider. In the new project I extend the class an overrode some methods justs to give me some output so I can see that the external method is called. But this is currently not happening. Is there anything I have to consider?
Currently the hirachy looks like:
MyDSLProposalProvider extends AbstractMyDSLProposalProvider
ExternalProposalProvider extends MyDSLProposalProvider
I rewrote a Method generated in the AbstractMyDSLProposalProvider but when its triggered the predefined Method in the AbstractMyDSLProposalProvider is called and not my new implementation.
public class ExternalMyDSLProposalPovider extends MyDSLProposalProvider
{
#Override
public void completeComponent_Name(EObject model, Assignment
assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
System.err.println("extern");
if(model instanceof Component)
{
createProposal("foo", "foo", context, acceptor);
}
super.completeComponent_Name(model, assignment, context, acceptor);
}
}
This is the class in the external Eclipse Project.
Thanks for the help.
When you declare an extension point using a schema that you have defined Eclipse puts that declaration in the extension point registry. That is all that is does, you must then write code to make uses of those declarations.
You read the extension point registry using something like:
IExtensionRegistry extRegistry = Platform.getExtensionRegistry();
IExtensionPoint extPoint = extRegistry.getExtensionPoint("your extension point id");
IConfigurationElement [] elements = extPoint.getConfigurationElements();
elements is now an array of the declarations in the various plugins using the extension point.
IConfigurationElement has various methods to get the values of the attributes of the declaration.
If you have defined a class in one of the attributes you can create an instance of the class using:
IConfigurationElement element = .... a config element
Object obj = element.createExecutableExtension("attribute name");
In your case the result should be your ExternalMyDSLProposalPovider.
You will then need to hook this object up with whatever is doing to proposals.

How can I use values from Eclipse Preference Pages to initialize external tool parameters?

I've created Eclipse Plugin from default template with Preference Pages. There are some preference parameters, two of them are String. I also created custom External Tool launch configuration in this Plugin and I use one of String parameters (let it be P_PATH) mentioned before to initialize one of tool's fields by default (using SetDefaults method). The problem is that it doesn't work as I suggested. When I launch this plugin (in another Eclipse instance) I go to Window->Preferences->Sample Preferences where I can edit and save field values. But after I'm done (when i enter something to field connected with P_PATH value there) I create new external tool of selected type and String parameter of selected field (the one tied to P_PATH, let it be "File Path") is inialized by the value that was specified in Plugin, not the one I entered into Preference Pages form. So, I want String value I enter in Preference Pages to be passed to External Tool as one of its parameters and when I create new External Tool of selected type it should be there (as default). How can I do that? I tied one External Tool field and one Preference Pages field to the same string parameter but looks like it's not properly passed to External Tool after all.
Added some code, there are three classes for Preference Pages and the forth one is for Launch Configuration Tabs. Here is only relevant code:
public class PreferenceConstants {
public static final String P_PATH = "pathPreference";
}
public class PreferenceInitializer extends AbstractPreferenceInitializer {org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
public void initializeDefaultPreferences() {
store.setDefault(PreferenceConstants.P_PATH,"Default value");
}
}
public class PreferencePage
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
public PreferencePage() {
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
setDescription("A demonstration of a preference page implementation");
}
public void createFieldEditors() {
addField(new FileFieldEditor(PreferenceConstants.P_PATH,
"&Console compiler path:", getFieldEditorParent()));
}
public void init(IWorkbench workbench) {
}
}
public class LaunchConfigurationTabs extends AbstractLaunchConfigurationTabGroup {
#Override
public void setDefaults(ILaunchConfigurationWorkingCopy configuration){
configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", PreferenceConstants.P_PATH);
}
}
The line:
configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", PreferenceConstants.P_PATH);
Just sets the attribute value to 'pathPreference' - this does not do anything to look up the value in the preferences.
You can look up the preference value at that point:
IPreferenceStore prefStore = Activator.getDefault().getPreferenceStore();
String value = prefStore.getString(PreferenceConstants.P_PATH);
configuration.setAttribute("org.eclipse.ui.externaltools.ATTR_LOCATION", value);
I don't think you can do anything that will make the attribute value automatically update if the preference changes.

PropertyAcess : Define condition values for label in PropertyAcess

I have a model called Field which has id and label.
I have defined PropertyAcess as below and it works. I would like to change it in such a way that I can show label based on condition ie if field.getLabel() is null, use field.getId() as label. How can I acheieve that
interface FieldProperties extends PropertyAccess<Field> {
ModelKeyProvider<Field> id();
LabelProvider<Field> label();
#Path("label")
ValueProvider<Field, String> labelProp();
}
Thanks
The PropertyAccess tool is meant to make it easy to quickly build ValueProvider, ModelKeyProvider, and LabelProvider instances that are based on a specific getter/setter on a bean-like object. If you don't want just access to a single property, then implement the interface directly.
In your case, since you want a LabelProvider that returns getLabel() unless it is null, then getId(), you might do something like this:
public LabelOrIdLabelProvider implements LabelProvider<Field> {
#Override
public String getLabel(Object item) {
return item.getLabel() == null ? item.getId() : item.getLabel();
}
}
If you want custom behavior, build it out yourself to do exactly what you need. If you just want the simple behavior of reading a single getter, the PropertyAccess is there to help save you a few lines of code.

NUnit TestCaseSource pass value to factory

I'm using the NUnit 2.5.3 TestCaseSource attribute and creating a factory to generate my tests. Something like this:
[Test, TestCaseSource(typeof(TestCaseFactories), "VariableString")]
public void Does_Pass_Standard_Description_Tests(string text)
{
Item obj = new Item();
obj.Description = text;
}
My source is this:
public static IEnumerable<TestCaseData> VariableString
{
get
{
yield return new TestCaseData(string.Empty).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Empty_Text");
yield return new TestCaseData(null).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Null_Text");
yield return new TestCaseData(" ").Throws(typeof(PreconditionException))
.SetName("Does_Reject_Whitespace_Text");
}
}
What I need to be able to do is to add a maximum length check to the Variable String, but this maximum length is defined in the contracts in the class under test. In our case its a simple public struct:
public struct ItemLengths
{
public const int Description = 255;
}
I can't find any way of passing a value to the test case generator. I've tried static shared values and these are not picked up. I don't want to save stuff to a file, as then I'd need to regenerate this file every time the code changed.
I want to add the following line to my testcase:
yield return new TestCaseData(new string('A', MAX_LENGTH_HERE + 1))
.Throws(typeof(PreconditionException));
Something fairly simple in concept, but something I'm finding impossible to do. Any suggestions?
Change the parameter of your test as class instead of a string. Like so:
public class StringTest {
public string testString;
public int maxLength;
}
Then construct this class to pass as an argument to TestCaseData constructor. That way you can pass the string and any other arguments you like.
Another option is to make the test have 2 arguments of string and int.
Then for the TestCaseData( "mystring", 255). Did you realize they can have multiple arguments?
Wayne
I faced a similar problem like yours and ended up writing a small NUnit addin and a custom attribute that extends the NUnit TestCaseSourceAttribute. In my particular case I wasn't interested in passing parameters to the factory method but you could easily use the same technique to achieve what you want.
It wasn't all that hard and only required me to write something like three small classes. You can read more about my solution at: blackbox testing with nunit using a custom testcasesource.
PS. In order to use this technique you have to use NUnit 2.5 (at least) Good luck.