Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » Access to variable name in creation-section of *.wbp-component.xml
Access to variable name in creation-section of *.wbp-component.xml [message #718400] Wed, 24 August 2011 08:26 Go to next message
Bernd  is currently offline Bernd Friend
Messages: 13
Registered: June 2011
Junior Member
Hey everyone,

I guess the title already explains pretty much what I want, still here are some details.
I am in a plugin project that extends WindowBuilder.
When I create a component I want to set its "name" property to the name of its variable. So I thought the creation-section of said component's wbp-component.xml would be a good place to do that via a invocation-tag.

Is there a way to do something like this:
<creation>
  <source><![CDATA[new javax.swing.JPanel()]]></source>
</creation>
<invocation signature="setName(java.lang.String)"><![CDATA[Variable_name_here]]>/invocation>


If not, is there an alternative way to achieve the same? So far, my solution workaround is a PropertyListProcessor, that sets the name when a single component is selected.

Thanks in advance for any ideas.
Re: Access to variable name in creation-section of *.wbp-component.xml [message #718533 is a reply to message #718400] Wed, 24 August 2011 15:42 Go to previous messageGo to next message
Eric Clayberg is currently offline Eric ClaybergFriend
Messages: 979
Registered: July 2009
Location: Boston, MA
Senior Member
Why not just turn on the Remember variable name in component preference?

http://code.google.com/javadevtools/wbpro/preferences/swing/images/preferences_variables_miscellaneous.gif
Re: Access to variable name in creation-section of *.wbp-component.xml [message #718801 is a reply to message #718533] Thu, 25 August 2011 11:41 Go to previous messageGo to next message
Bernd  is currently offline Bernd Friend
Messages: 13
Registered: June 2011
Junior Member
Uhm... yeah that is, erm... way to simple
Thanks.

But a problem that arises now is that I do not want the name property to contain underscores (I don't care if the variable name contains underscores, but the name should not). Is there a way to influence that?

[Updated on: Thu, 25 August 2011 11:46]

Report message to a moderator

Re: Access to variable name in creation-section of *.wbp-component.xml [message #718819 is a reply to message #718801] Thu, 25 August 2011 12:37 Go to previous messageGo to next message
Eric Clayberg is currently offline Eric ClaybergFriend
Messages: 979
Registered: July 2009
Location: Boston, MA
Senior Member
I'm guessing that you will need to roll your own version of that feature. At least all of the code is there to be reviewed, copied and modified as needed.
Re: Access to variable name in creation-section of *.wbp-component.xml [message #718874 is a reply to message #718819] Thu, 25 August 2011 14:37 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
See implementation for Swing in method
org.eclipse.wb.internal.swing.model.component.ComponentInfo.setVariableNameAs_setName(String)

To be able to add this broadcast listener from outside of Swing plugin
org.eclipse.wb.core.model.broadcast.JavaEventListener.variable_setName(AbstractNamedVariableSupport)

write plugin which contributes to the extension point
org.eclipse.wb.core.rootProcessors


Konstantin Scheglov,
Google, Inc.
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 Go to previous messageGo to next message
Bernd  is currently offline Bernd Friend
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? Wink

---
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 #720312 is a reply to message #720268] Tue, 30 August 2011 11:08 Go to previous messageGo to next message
Bernd  is currently offline Bernd Friend
Messages: 13
Registered: June 2011
Junior Member
Thanks for the help. One tiny question remains: If "Remember variable name in component" is checked and my processor is active too, the Remember option 'wins'. Is there a (simple) way to prevent that? If not, it's no big problem either, because I can tell users of my plugin to keep the Remember option off.
Apropos: Is there a preferred way my plugin can 'force' certain preferences/settings of WB to be set? Or will I have to do that like this
IPreferenceStore s = new ScopedPreferenceStore(new InstanceScope(), "org.eclipse.ui");
s.setValue("SHOW_MEMORY_MONITOR", true);

That code, however, will not be fully effective until I press apply in the preferences dialog or restart eclipse (I can live with that, but maybe WB offers something better?)
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 Go to previous message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
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.
Previous Topic:Freezing the pallet
Next Topic:Latest version?
Goto Forum:
  


Current Time: Thu Apr 25 06:40:18 GMT 2024

Powered by FUDForum. Page generated in 1.92785 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top