|
|
|
|
|
Re: Access to variable name in creation-section of *.wbp-component.xml [message #720268 is a reply to message #718874] |
Tue, 30 August 2011 09:20 |
Bernd Messages: 13 Registered: June 2011 |
Junior Member |
|
|
Edit: Just found what I was missing. See my code below the original post.
I think I don't fully understand what to do. I have a class NamingProcessor implements IRootProcessor that is contributes to the extension point you gave.
Now in that NamingProcessor I am able to add a JavaEventListener to all JavaInfo in the method process(JavaInfo arg0, List<JavaInfo> arg1) Correct so far?
So I override variable_setName(AbstractNamedVariableSupport variableSupport) in the abovementioned JavaEventListener.
But here I am at a loss. That method sets the name of the variable, not the property "setName" of that component. I thought looking at the ComponentInfo class would help, but I seem to be missing something.
Can you lend me a machete to my intellectual thicket?
---
public class NamingProcessor implements IRootProcessor
{
NameListener nameListener = new NameListener();
@Override
public void process(JavaInfo base, List<JavaInfo> components) throws Exception
{
base.addBroadcastListener(nameListener);
for (JavaInfo i : components)
{
i.addBroadcastListener(nameListener);
}
}
private class NameListener extends JavaEventListener
{
@Override
public void variable_setName(AbstractNamedVariableSupport variableSupport) throws Exception
{
ComponentInfo ci = (ComponentInfo) variableSupport.getJavaInfo();
String newName = variableSupport.getName().replace("_", "");
newName = StringUtils.capitalize(newName);
String valueSource = StringConverter.INSTANCE.toJavaSource(ci, newName);
MethodInvocation invocation = ci.getMethodInvocation("setName(java.lang.String)");
if (invocation != null)
{
Expression valueExpression = (Expression) invocation.arguments().get(0);
ci.getEditor().replaceExpression(valueExpression, valueSource);
}
ci.addMethodInvocation("setName(java.lang.String)", valueSource);
}
}
}
[Updated on: Tue, 30 August 2011 09:49] Report message to a moderator
|
|
|
|
Re: Access to variable name in creation-section of *.wbp-component.xml [message #720950 is a reply to message #720312] |
Wed, 31 August 2011 14:30 |
Konstantin Scheglov Messages: 555 Registered: July 2009 |
Senior Member |
|
|
Problem is that broadcast listeners are registered in some order.
1. When we parse, we create ComponentInfo models, which register variable listener.
2. Then your processor registers variable listener at then end of parse.
3. When user drops new component, new ComponentInfo is created and registers variable listener (after your processor).
So, your processor wins for existing components, but loose for new components.
Unfortunately, we don't have priorities for registering broadcast listeners.
Possible solutions:
1. Use hack. Register some other listener, which happens after adding new component, like
org.eclipse.wb.core.model.broadcast.ObjectInfoChildAddAfter
and remove/add your variable listener to ensure that it is last one.
2. Contribute patch to WindowBuilder to don't use separate variable listener for each ComponentInfo instance, and add single listener in same way as your processor does this. For external contributions we support "priority" attribute (looks in WB plugin.xml files), so you can contribute your processor after standard one and add variable broadcast listener after standard one. This will have two position things for WB: less broadcast listeners (they can be expensive sometimes); extract feature (name in component) into separate class.
Konstantin Scheglov,
Google, Inc.
|
|
|
Powered by
FUDForum. Page generated in 0.03690 seconds