Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Visual Editor (VE) » Adding a component programmatically
Adding a component programmatically [message #104463] Wed, 31 August 2005 01:21 Go to next message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
After a brief hiatus I am back to work on this thing. I have a few questions:

1. What is the best way to get the current (VE relevant) coordinates of
the mouse cursor given only the editDomain and root edit part objects ?
I am cooking up my own CreateRequest and it needs a Location. I do not have
a Tool object as I am not doing this through my own installed editpolicy.

2. My goal is to add a component to a jpanel based on the location of the
mouse but not using any of the built in Tool types. I would like the code
generation to automatically take into account the registered component and
layout policies for the target editpart. I think the way you do this it
to cook up a CreateRequest and pass it into the getCommand() method of the
editPart. This isn't working for me. My test code so far is as follows:
<code>
BeanSubclassComposition bean = (BeanSubclassComposition) editDomain.getDiagramData();
IJavaObjectInstance rootInstance = bean.getThisPart();
EditPart part = primaryViewer.getRootEditPart().getContents();
//TODO: get the current editPart

CreateRequest request = new CreateRequest();

IJavaInstance fieldInstance = BeanUtilities.createJavaObject("javax.swing.JTextField",
rootInstance.eResource().getResourceSet(), "");

request.setFactory(new SimpleFactory(fieldInstance.getClass()));


request.setLocation(new org.eclipse.draw2d.geometry.Point(20,20));
/*dummy coordinates for now*/
Command command = part.getCommand(request);

editDomain.getCommandStack().execute(command);
</code>

This is creating the following stack trace after the execute method:

java.lang.ClassCastException: org.eclipse.emf.ecore.impl.EClassImpl
at org.eclipse.jem.internal.instantiation.base.JavaObjectInstan ce.getJavaType(JavaObjectInstance.java:36)
at org.eclipse.ve.internal.java.core.BeanProxyAdapterFactory.cr eateAdapter(BeanProxyAdapterFactory.java:99)
at org.eclipse.emf.common.notify.impl.AdapterFactoryImpl.create Adapter(AdapterFactoryImpl.java:138)
at org.eclipse.emf.common.notify.impl.AdapterFactoryImpl.adaptN ew(AdapterFactoryImpl.java:112)
at org.eclipse.emf.ecore.util.EcoreUtil.getRegisteredAdapter(Ec oreUtil.java:100)
at org.eclipse.ve.internal.java.core.BeanProxyUtilities.getBean ProxyHost(BeanProxyUtilities.java:481)
at org.eclipse.ve.internal.java.core.CompositionProxyAdapter.in itSetting(CompositionProxyAdapter.java:365)
at org.eclipse.ve.internal.java.core.CompositionProxyAdapter.no tifyChanged(CompositionProxyAdapter.java:113)
at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify (BasicNotifierImpl.java:229)
at org.eclipse.emf.ecore.util.EcoreEList.dispatchNotification(E coreEList.java:211)
at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addAllU nique(NotifyingListImpl.java:423)
at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addAllU nique(NotifyingListImpl.java:367)
at org.eclipse.emf.common.util.BasicEList.addAll(BasicEList.jav a:686)
at org.eclipse.ve.internal.cde.commands.ApplyAttributeSettingCo mmand.execute(ApplyAttributeSettingCommand.java:92)
at org.eclipse.ve.internal.propertysheet.common.commands.Compou ndCommand.execute(CompoundCommand.java:217)
at org.eclipse.ve.internal.propertysheet.common.commands.Compou ndCommand.execute(CompoundCommand.java:217)
at org.eclipse.ve.internal.java.codegen.editorpart.JavaVisualEd itorCommandStack$1.run(JavaVisualEditorCommandStack.java:52)
at org.eclipse.ve.internal.cde.core.ModelChangeController.doMod elChanges(ModelChangeController.java:166)
at org.eclipse.ve.internal.java.codegen.editorpart.JavaVisualEd itorCommandStack.execute(JavaVisualEditorCommandStack.java:4 9)
at com.dvs.rad.views.visualeditor.DVSVisualEditorPart$1.safeDro p(DVSVisualEditorPart.java:80)
at com.dvs.rad.views.bindingsource.dnd.TransferDropTargetAdapte r.drop(TransferDropTargetAdapter.java:59)
at org.eclipse.jface.util.DelegatingDropAdapter$3.run(Delegatin gDropAdapter.java:211)
at org.eclipse.core.internal.runtime.InternalPlatform.run(Inter nalPlatform.java:1044)
at org.eclipse.core.runtime.Platform.run(Platform.java:783)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:44)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:14 8)
at org.eclipse.jface.util.DelegatingDropAdapter.drop(Delegating DropAdapter.java:209)
at org.eclipse.swt.dnd.DNDListener.handleEvent(DNDListener.java :65)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:867)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:852)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:6 60)
at org.eclipse.swt.dnd.DropTarget.notifyListeners(DropTarget.ja va:480)
at org.eclipse.swt.dnd.DropTarget.Drop(DropTarget.java:405)
at org.eclipse.swt.dnd.DropTarget.access$7(DropTarget.java:350)
at org.eclipse.swt.dnd.DropTarget$3.method6(DropTarget.java:236 )
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMOb ject.java:117)
at org.eclipse.swt.internal.ole.win32.COM.DoDragDrop(Native Method)
at org.eclipse.swt.dnd.DragSource.drag(DragSource.java:277)
at org.eclipse.swt.dnd.DragSource.access$0(DragSource.java:267)
at org.eclipse.swt.dnd.DragSource$1.handleEvent(DragSource.java :161)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3080)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :2713)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:1699)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1663)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:367)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:143)
at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplicatio n.java:103)
at org.eclipse.core.internal.runtime.PlatformActivator$1.run(Pl atformActivator.java:226)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:376)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:163)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334 )
at org.eclipse.core.launcher.Main.basicRun(Main.java:278)
at org.eclipse.core.launcher.Main.run(Main.java:973)
at org.eclipse.core.launcher.Main.main(Main.java:948)

I'm sure I'm missing something simple here. I've already stepped through
the creation tool and layout policies. My addled brain is finding little
in the way of enlightenment there. CreationTool doesn't seem to HAVE a getCommand
method as stated in an earlier post, and the methods I could find trigger
on annotationcreation. By the time I can catch the event the request object
has already been created, so that isn't terribly useful. Anyplace higher
up the chain I can watch ?

Thanks for any help you can throw my way.

Dylan Bruzenak
Re: Adding a component programmatically - figured - one more question [message #104612 is a reply to message #104463] Wed, 31 August 2005 23:50 Go to previous messageGo to next message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Ok, I figured this out. A combination of re-reading all the code generation
posts I could find on this board and putting breakpoints higher up the food
chain. My code is appended to the end of this message for anyone else trying
to accomplish the same thing.

I have two more questions.

1. Using a combination of EMFClassCreationFactory and the AnnotationCreationFactory
decorator causes a popup to appear asking for the control name. I want to
specify the name and not bother the user with the popup. I see a constructor
that takes a list of keys, but I have no idea what to populate that with ;)

2. Is there an easy way to specify the component width and height for a
newly added component ? My current technique involved finding the existing
line of code and manually editing it using the AST. God help me.

I greatly appreciate any response.

My current code follows:

<code>
BeanSubclassComposition bean = (BeanSubclassComposition) editDomain.getDiagramData();
IJavaObjectInstance rootInstance = bean.getThisPart();
EditPart part = primaryViewer.getRootEditPart().getContents();
part = (EditPart)part.getChildren().get(0); /* get the jpanel */

CreateRequest request = new CreateRequest();

IJavaInstance fieldInstance = BeanUtilities.createJavaObject("javax.swing.JTextField",
rootInstance.eResource().getResourceSet(), "");

/* AnnotationCreationFactory wraps another CreationFactory and
lets the user set the fieldname of the added object with a
popup. The EMFClassCreationFactory takes a namespace URI
and creates object instances based on it.

*/
AnnotationCreationFactory factory = new AnnotationCreationFactory(new
EMFClassCreationFactory(getNSUri(fieldInstance)));

factory.setEditDomain(editDomain);

request.setFactory(factory);

/* The coordinates below are adjusted for the position of the component that
the object is dropped on. If you've moved the panel within the editor the
x,y coordinates here need to be adjusted accordingly. Default is 10,10 I
believe, so the following would actually appear at point 0,0 inside of the
panel. Using the position of the mouse pointer or somesuch is highly recommended
here, as the offset is in no way guaranteed.
*/
request.setLocation(new org.eclipse.draw2d.geometry.Point(10,10));


Command command = part.getCommand(request);

editDomain.getCommandStack().execute(command);

/** This returns a namespace uri for the given field instance. This is the
uri that ve uses to create the eClass object in the factory
*/
private String getNSUri(IJavaInstance fieldInstance) {
return ((EPackageImpl)fieldInstance.eClass().eContainer()).getNsURI ()
+ "#" + fieldInstance.eClass().getName();
}
</code>

Sorry for the hacked out code, my newsgroup client crushes my attempts and
good editing.

Dylan Bruzenak
Re: Adding a component programmatically - figured - one more question [message #104679 is a reply to message #104612] Thu, 01 September 2005 14:10 Go to previous messageGo to next message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Ok, number 2 was easy. Set size on the CreationRequest. I don't know how
I missed that - everything seems so much simpler in the morning. Still stumped
on #1. Once I get all this figured out I may do a write up for the eclipse
wiki, if anyone else is interested. I don't know if that page has any traffic.


Sorry for the noise.

Dylan

> Ok, I figured this out. A combination of re-reading all the code
> generation posts I could find on this board and putting breakpoints
> higher up the food chain. My code is appended to the end of this
> message for anyone else trying to accomplish the same thing.
>
> I have two more questions.
>
> 1. Using a combination of EMFClassCreationFactory and the
> AnnotationCreationFactory decorator causes a popup to appear asking
> for the control name. I want to specify the name and not bother the
> user with the popup. I see a constructor that takes a list of keys,
> but I have no idea what to populate that with ;)
>
> 2. Is there an easy way to specify the component width and height for
> a newly added component ? My current technique involved finding the
> existing line of code and manually editing it using the AST. God help
> me.
>
> I greatly appreciate any response.
>
> My current code follows:
>
> <code>
> BeanSubclassComposition bean = (BeanSubclassComposition)
> editDomain.getDiagramData();
> IJavaObjectInstance rootInstance = bean.getThisPart();
> EditPart part =
> primaryViewer.getRootEditPart().getContents();
> part = (EditPart)part.getChildren().get(0); /* get the
> jpanel */
> CreateRequest request = new CreateRequest();
>
> IJavaInstance fieldInstance =
> BeanUtilities.createJavaObject("javax.swing.JTextField",
> rootInstance.eResource().getResourceSet(), "");
> /* AnnotationCreationFactory wraps another
> CreationFactory and
> lets the user set the fieldname of the added object with a
> popup. The EMFClassCreationFactory takes a namespace
> URI
> and creates object instances based on it.
> */
> AnnotationCreationFactory factory = new
> AnnotationCreationFactory(new
> EMFClassCreationFactory(getNSUri(fieldInstance)));
> factory.setEditDomain(editDomain);
>
> request.setFactory(factory);
>
> /* The coordinates below are adjusted for the position of the
> component that
> the object is dropped on. If you've moved the panel within the editor
> the
> x,y coordinates here need to be adjusted accordingly. Default is
> 10,10 I
> believe, so the following would actually appear at point 0,0 inside of
> the
> panel. Using the position of the mouse pointer or somesuch is highly
> recommended
> here, as the offset is in no way guaranteed.
> */
> request.setLocation(new
> org.eclipse.draw2d.geometry.Point(10,10));
> Command command = part.getCommand(request);
>
> editDomain.getCommandStack().execute(command);
>
> /** This returns a namespace uri for the given field instance. This
> is the
> uri that ve uses to create the eClass object in the factory
> */
> private String getNSUri(IJavaInstance fieldInstance) {
> return
> ((EPackageImpl)fieldInstance.eClass().eContainer()).getNsURI ()
> + "#" + fieldInstance.eClass().getName();
> }
> </code>
> Sorry for the hacked out code, my newsgroup client crushes my attempts
> and good editing.
>
> Dylan Bruzenak
>
Re: Adding a component programmatically - figured - one more question [message #105194 is a reply to message #104679] Mon, 05 September 2005 10:10 Go to previous messageGo to next message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi Dylan,

> Still stumped on #1.

The VE uses Annotations (in the JEM model) to just store arbitrary bits
of information that aren't part of the Java instance but might be useful
to something else. The field name is held as an instance of an
EStringToStringMapEntry with a key of
"org.eclipse.ve.internal.cde.code.nameincomposition" and a value that is
a String that represents the field name.

If you switch the VE into the mode where it shows its XML (do this on
your launch configuration by enabling tracing for
org.eclipse.ve.java.core and selecting "debug/xmltext" and then in the
workbench you launch visit the preferences page and select "Show XML
Window") you can see the annotation for a field. Another way to know is
to look at one of the palette template files like RatioButton.xmi that
shows the serailized form of the annotation with the map entry for the
field name.

To create an EStringToStringMapEntry you go to the package and get its
instance you need something like the following code snippet.

EStringToStringMapEntryImpl entry
((EcoreFactoryImpl)EcorePackage.eINSTANCE.getEcoreFactory()) .createEStringToStringMapEntry();
entry.setValue("myFieldName");
entry.setKey("org.eclipse.ve.internal.cde.code.nameincomposition ");

There might be an easier code path to do this as usually on EMFFactories
there are more helper methods but ECoreFactory doesn't seem to have one.
I'll ping Rich to see why there isn't and we should either add one or
else he can point us to the right method to use.

> Once I get all this figured out I may do a write
> up for the eclipse wiki, if anyone else is interested. I don't know if
> that page has any traffic.

This sounds like a great idea. We can link to your wiki from the
org.eclipse.vep homepage. One of the things we're doing right now is
trying very hard to create more tutorials to do the kind of thing you
are doing but failing basically cause we are very stretched between
trying to make the VE a) work for users who want to build GUIs and b)
work for users who want to extend it. Would you be interested in
helping to co-author a tutorial for b) that we can work off-line to get
the contents right and you'd be a great input based on where you are in
the learning curve and your list of requirements ?

Best regards,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #105210 is a reply to message #105194] Mon, 05 September 2005 13:12 Go to previous messageGo to next message
hanys is currently offline hanysFriend
Messages: 188
Registered: July 2009
Senior Member
Hi all,

1) How can I get Annotations to JavaObjectInstance model object.
2) How can I get java String value of allocation object
I have for example:
JavaObjectInstance@83bbd2{java.lang.String}: org.eclipse.jem.internal.instantiation.impl.InitStringAlloca tionImpl @f6ab36
(initString: "MY VALUE")

I want to get result MY VALUE without quotes.

Thanks,
John Cage
Re: Adding a component programmatically - figured - one more question [message #105235 is a reply to message #105194] Mon, 05 September 2005 16:06 Go to previous messageGo to next message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Thanks Joe, this works well. Just pop it into the Annotation factory and
it comes up.

I also found a workaround for the label problem I was having (seperate post)
so everything is working wonderfully. I'm trying to imagine what it would
be like to have to build all of this stuff from scratch. The stuff nightmares
and time sinkholes are made of. If only Hawking were a comp sci major.

As to the tutorial I am interested. Everyone is tight for time it seems
( I squeeze this stuff in during my limited free time). How do we go about
organizing a co-authorship ? Let me know who to email and I'll get a work
address out to them, one that isn't as cluttered with spam as the above.

In the meantime I'll work out a content outline for a potential wiki posting.


thanks,

Dylan

> Hi Dylan,
>
>> Still stumped on #1.
>>
> The VE uses Annotations (in the JEM model) to just store arbitrary
> bits of information that aren't part of the Java instance but might be
> useful to something else. The field name is held as an instance of an
> EStringToStringMapEntry with a key of
> "org.eclipse.ve.internal.cde.code.nameincomposition" and a value that
> is a String that represents the field name.
>
> If you switch the VE into the mode where it shows its XML (do this on
> your launch configuration by enabling tracing for
> org.eclipse.ve.java.core and selecting "debug/xmltext" and then in the
> workbench you launch visit the preferences page and select "Show XML
> Window") you can see the annotation for a field. Another way to know
> is to look at one of the palette template files like RatioButton.xmi
> that shows the serailized form of the annotation with the map entry
> for the field name.
>
> To create an EStringToStringMapEntry you go to the package and get its
> instance you need something like the following code snippet.
>
> EStringToStringMapEntryImpl entry
> ((EcoreFactoryImpl)EcorePackage.eINSTANCE.getEcoreFactory()) .createESt
> ringToStringMapEntry(); entry.setValue("myFieldName");
> entry.setKey("org.eclipse.ve.internal.cde.code.nameincomposition ");
>
> There might be an easier code path to do this as usually on
> EMFFactories
> there are more helper methods but ECoreFactory doesn't seem to have
> one.
> I'll ping Rich to see why there isn't and we should either add one
> or
> else he can point us to the right method to use.
>> Once I get all this figured out I may do a write up for the eclipse
>> wiki, if anyone else is interested. I don't know if that page has
>> any traffic.
>>
> This sounds like a great idea. We can link to your wiki from the
> org.eclipse.vep homepage. One of the things we're doing right now is
> trying very hard to create more tutorials to do the kind of thing you
> are doing but failing basically cause we are very stretched between
> trying to make the VE a) work for users who want to build GUIs and b)
> work for users who want to extend it. Would you be interested in
> helping to co-author a tutorial for b) that we can work off-line to
> get the contents right and you'd be a great input based on where you
> are in the learning curve and your list of requirements ?
>
> Best regards,
>
> Joe Winchester
>
Re: Adding a component programmatically - figured - one more question [message #105712 is a reply to message #105235] Thu, 08 September 2005 13:18 Go to previous messageGo to next message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi Dylan,

> As to the tutorial I am interested. Everyone is tight for time it seems
> ( I squeeze this stuff in during my limited free time). How do we go
> about organizing a co-authorship ? Let me know who to email and I'll
> get a work address out to them, one that isn't as cluttered with spam as
> the above.

Post a query to the ve-dev newsgroup and we'll get a list of names of
people to work on the tutorial. The way we have written these in the
past is to get the code working first and iterate this and then write
the doc for it. I definitely would want to work on the tutorial as it
has been on my to-do-list for about the last year, and I think there are
others on the VE newsgroup how would be able to help either with
authorship or by doing a review and sanity check on the piece.

> In the meantime I'll work out a content outline for a potential wiki
> posting.

Fantastic.

Best regards,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #105728 is a reply to message #105210] Thu, 08 September 2005 13:30 Go to previous messageGo to next message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi John,

> 1) How can I get Annotations to JavaObjectInstance model object.

To create one you basically need to do the following

1 - Create an Annotation object
2 - Point it to the JavaObjectInstance
2 - Add it to the BeanComposition

Code for this might look something like:

AnnotationEMF a =
CDMPackage.eInstance.getCDMFactory().createAnnotationEMF();
a.setAnnotates(myJavaObjectInstance);
beanComposition.getAnnotations().add(a);

To get the BeanComposition (who is the root of the whole model) there
are several ways. If you are in an edit part you can do

EditDomain editDomain = EditDomain.getEditDomain(editPart);
BeanComposition b = (BeanComposition)editDomain.getDiagramData();

another is to call getContainer() in a loop till you get it, e.g.

EObject container = javaObjectInstance.eContainer();
while(container != null){
container = container.eContainer();
if(container instance of BeanComposition){
// bingo
}
}

> 2) How can I get java String value of allocation object
> I have for example:
> JavaObjectInstance@83bbd2{java.lang.String}: org.eclipse.jem.internal.instantiation.impl.InitStringAlloca tionImpl @f6ab36
> (initString: "MY VALUE")

Usually you don't do this but you get a property value instead. For
example, with the code like

JButton b = new JButton("Frog");

this gives you the kind of code you've described, however the "Frog"
becomes the text property. To get it from the JavaBean you call
getText(). In VE terms you just do something like

textJavaObjectInstance =
BeanUtilities.getFeatureValue(javaObjectInstance,"text");

which returns an IJavaObjectInstance for the String whose value is
"Frog". To get the "Frog" value you then go and get the actual value on
the VM

IBeanProxy textBeanProxy =
BeanProxyUtilities.getBeanProxy(textJavaObjectInstance):

Cause you know it is a String you cast and get the value

String myValue = ((IStringBeanProxy)testBeanProxy).stringValue();

Having said all this, it assumes that is a get method property that can
give you the real value. Is there one ?

Best regards,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #105763 is a reply to message #105728] Thu, 08 September 2005 13:51 Go to previous messageGo to next message
hanys is currently offline hanysFriend
Messages: 188
Registered: July 2009
Senior Member
Hi Joe,

Thank You for your explanation of the problem,
it is very clear to understand.


Best Regards,

John Cage
Re: Adding a component programmatically - figured - one more question [message #105776 is a reply to message #105712] Thu, 08 September 2005 13:59 Go to previous messageGo to next message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Newsgroup or mailing list ? I don't see a ve-dev newsgroup, so I'm assuming
the latter.

Dylan

> Hi Dylan,
>
>> As to the tutorial I am interested. Everyone is tight for time it
>> seems ( I squeeze this stuff in during my limited free time). How do
>> we go about organizing a co-authorship ? Let me know who to email
>> and I'll get a work address out to them, one that isn't as cluttered
>> with spam as the above.
>>
> Post a query to the ve-dev newsgroup and we'll get a list of names of
> people to work on the tutorial. The way we have written these in the
> past is to get the code working first and iterate this and then write
> the doc for it. I definitely would want to work on the tutorial as it
> has been on my to-do-list for about the last year, and I think there
> are others on the VE newsgroup how would be able to help either with
> authorship or by doing a review and sanity check on the piece.
>
>> In the meantime I'll work out a content outline for a potential wiki
>> posting.
>>
> Fantastic.
>
> Best regards,
>
> Joe Winchester
>
Re: Adding a component programmatically - figured - one more question [message #105817 is a reply to message #105763] Thu, 08 September 2005 14:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi,

There is one important complication to Joe's description. In the VE you
would never do subclasscomposition.add(something) or
javainstance.eSet(feature, value) to the subclasscomposition or to any
javainstance that is in the model. This is because changing it and not
using the command stack and commands will cause bad things to occur.

That is why everytime we change things we do it through commands.

John Cage wrote:
> Hi Joe,
>
> Thank You for your explanation of the problem, it is very clear to
> understand.
>
>
> Best Regards,
>
> John Cage
>

--
Thanks,
Rich Kulp
Re: Adding a component programmatically - figured - one more question [message #105992 is a reply to message #105817] Thu, 08 September 2005 22:52 Go to previous messageGo to next message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
> There is one important complication to Joe's description. In the VE you
> would never do subclasscomposition.add(something) or
> javainstance.eSet(feature, value) to the subclasscomposition or to any
> javainstance that is in the model. This is because changing it and not
> using the command stack and commands will cause bad things to occur.
> That is why everytime we change things we do it through commands.

Good point. Once you know the target, feature and source to change the
technique is to use RuledCommandBuilder so a line that was

target.eSet(feature,source);

now becomes

RuledCommandBuilder biulder = new RuledCommandBuilder();
builder.applyAttributeSetting(target,feature,source);
editDomain.getCommandStack().execute(builder.getCommand());

Best regards,

Joe winchester
Re: Adding a component programmatically - figured - one more question [message #106033 is a reply to message #105992] Fri, 09 September 2005 07:19 Go to previous messageGo to next message
hanys is currently offline hanysFriend
Messages: 188
Registered: July 2009
Senior Member
Hi joe,
Now it's absolutely clear :)

Thank you,
John Cage
Re: Adding a component programmatically - figured - one more question [message #106060 is a reply to message #105776] Fri, 09 September 2005 10:14 Go to previous message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi Dylan,

> Newsgroup or mailing list ? I don't see a ve-dev newsgroup, so I'm
> assuming the latter.

Yup - I think the mailing list is a good place to try to pick up people
for interest. To begin with I think you and I can make a stab at
writing the piece and I know who else from the VE comitters we can use
as reviewers, but hopefully there are others who would like to give
input to steer the content and contribute and review.

I am actually quite surprised by a lot of the traffic on this newsgroup
from people who are wanting to extend the VE and also who have gotten
pretty far and succeeded without any kind of formal tutorial or API docs.

One thing we are thinking of doing is submitting a tutorial to
EclipseCon 2006 on extending the VE and its internals. It'd also be
good to get input into what areas this should cover - it could vary from
the persistence (a lot of people want to read/write XML instead of
Java - it could be about custom layout managers that people want - it
could be about custom model manipulation such as group/ungroup features.
The more ideas and input the better it can be and, from this, formal
Eclipsecorner tutorials can be spun off.

Best regards and many thanks,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #610470 is a reply to message #104463] Wed, 31 August 2005 23:50 Go to previous message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Ok, I figured this out. A combination of re-reading all the code generation
posts I could find on this board and putting breakpoints higher up the food
chain. My code is appended to the end of this message for anyone else trying
to accomplish the same thing.

I have two more questions.

1. Using a combination of EMFClassCreationFactory and the AnnotationCreationFactory
decorator causes a popup to appear asking for the control name. I want to
specify the name and not bother the user with the popup. I see a constructor
that takes a list of keys, but I have no idea what to populate that with ;)

2. Is there an easy way to specify the component width and height for a
newly added component ? My current technique involved finding the existing
line of code and manually editing it using the AST. God help me.

I greatly appreciate any response.

My current code follows:

<code>
BeanSubclassComposition bean = (BeanSubclassComposition) editDomain.getDiagramData();
IJavaObjectInstance rootInstance = bean.getThisPart();
EditPart part = primaryViewer.getRootEditPart().getContents();
part = (EditPart)part.getChildren().get(0); /* get the jpanel */

CreateRequest request = new CreateRequest();

IJavaInstance fieldInstance = BeanUtilities.createJavaObject("javax.swing.JTextField",
rootInstance.eResource().getResourceSet(), "");

/* AnnotationCreationFactory wraps another CreationFactory and
lets the user set the fieldname of the added object with a
popup. The EMFClassCreationFactory takes a namespace URI
and creates object instances based on it.

*/
AnnotationCreationFactory factory = new AnnotationCreationFactory(new
EMFClassCreationFactory(getNSUri(fieldInstance)));

factory.setEditDomain(editDomain);

request.setFactory(factory);

/* The coordinates below are adjusted for the position of the component that
the object is dropped on. If you've moved the panel within the editor the
x,y coordinates here need to be adjusted accordingly. Default is 10,10 I
believe, so the following would actually appear at point 0,0 inside of the
panel. Using the position of the mouse pointer or somesuch is highly recommended
here, as the offset is in no way guaranteed.
*/
request.setLocation(new org.eclipse.draw2d.geometry.Point(10,10));


Command command = part.getCommand(request);

editDomain.getCommandStack().execute(command);

/** This returns a namespace uri for the given field instance. This is the
uri that ve uses to create the eClass object in the factory
*/
private String getNSUri(IJavaInstance fieldInstance) {
return ((EPackageImpl)fieldInstance.eClass().eContainer()).getNsURI ()
+ "#" + fieldInstance.eClass().getName();
}
</code>

Sorry for the hacked out code, my newsgroup client crushes my attempts and
good editing.

Dylan Bruzenak
Re: Adding a component programmatically - figured - one more question [message #610478 is a reply to message #104612] Thu, 01 September 2005 14:10 Go to previous message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Ok, number 2 was easy. Set size on the CreationRequest. I don't know how
I missed that - everything seems so much simpler in the morning. Still stumped
on #1. Once I get all this figured out I may do a write up for the eclipse
wiki, if anyone else is interested. I don't know if that page has any traffic.


Sorry for the noise.

Dylan

> Ok, I figured this out. A combination of re-reading all the code
> generation posts I could find on this board and putting breakpoints
> higher up the food chain. My code is appended to the end of this
> message for anyone else trying to accomplish the same thing.
>
> I have two more questions.
>
> 1. Using a combination of EMFClassCreationFactory and the
> AnnotationCreationFactory decorator causes a popup to appear asking
> for the control name. I want to specify the name and not bother the
> user with the popup. I see a constructor that takes a list of keys,
> but I have no idea what to populate that with ;)
>
> 2. Is there an easy way to specify the component width and height for
> a newly added component ? My current technique involved finding the
> existing line of code and manually editing it using the AST. God help
> me.
>
> I greatly appreciate any response.
>
> My current code follows:
>
> <code>
> BeanSubclassComposition bean = (BeanSubclassComposition)
> editDomain.getDiagramData();
> IJavaObjectInstance rootInstance = bean.getThisPart();
> EditPart part =
> primaryViewer.getRootEditPart().getContents();
> part = (EditPart)part.getChildren().get(0); /* get the
> jpanel */
> CreateRequest request = new CreateRequest();
>
> IJavaInstance fieldInstance =
> BeanUtilities.createJavaObject("javax.swing.JTextField",
> rootInstance.eResource().getResourceSet(), "");
> /* AnnotationCreationFactory wraps another
> CreationFactory and
> lets the user set the fieldname of the added object with a
> popup. The EMFClassCreationFactory takes a namespace
> URI
> and creates object instances based on it.
> */
> AnnotationCreationFactory factory = new
> AnnotationCreationFactory(new
> EMFClassCreationFactory(getNSUri(fieldInstance)));
> factory.setEditDomain(editDomain);
>
> request.setFactory(factory);
>
> /* The coordinates below are adjusted for the position of the
> component that
> the object is dropped on. If you've moved the panel within the editor
> the
> x,y coordinates here need to be adjusted accordingly. Default is
> 10,10 I
> believe, so the following would actually appear at point 0,0 inside of
> the
> panel. Using the position of the mouse pointer or somesuch is highly
> recommended
> here, as the offset is in no way guaranteed.
> */
> request.setLocation(new
> org.eclipse.draw2d.geometry.Point(10,10));
> Command command = part.getCommand(request);
>
> editDomain.getCommandStack().execute(command);
>
> /** This returns a namespace uri for the given field instance. This
> is the
> uri that ve uses to create the eClass object in the factory
> */
> private String getNSUri(IJavaInstance fieldInstance) {
> return
> ((EPackageImpl)fieldInstance.eClass().eContainer()).getNsURI ()
> + "#" + fieldInstance.eClass().getName();
> }
> </code>
> Sorry for the hacked out code, my newsgroup client crushes my attempts
> and good editing.
>
> Dylan Bruzenak
>
Re: Adding a component programmatically - figured - one more question [message #610650 is a reply to message #104679] Mon, 05 September 2005 10:10 Go to previous message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi Dylan,

> Still stumped on #1.

The VE uses Annotations (in the JEM model) to just store arbitrary bits
of information that aren't part of the Java instance but might be useful
to something else. The field name is held as an instance of an
EStringToStringMapEntry with a key of
"org.eclipse.ve.internal.cde.code.nameincomposition" and a value that is
a String that represents the field name.

If you switch the VE into the mode where it shows its XML (do this on
your launch configuration by enabling tracing for
org.eclipse.ve.java.core and selecting "debug/xmltext" and then in the
workbench you launch visit the preferences page and select "Show XML
Window") you can see the annotation for a field. Another way to know is
to look at one of the palette template files like RatioButton.xmi that
shows the serailized form of the annotation with the map entry for the
field name.

To create an EStringToStringMapEntry you go to the package and get its
instance you need something like the following code snippet.

EStringToStringMapEntryImpl entry
((EcoreFactoryImpl)EcorePackage.eINSTANCE.getEcoreFactory()) .createEStringToStringMapEntry();
entry.setValue("myFieldName");
entry.setKey("org.eclipse.ve.internal.cde.code.nameincomposition ");

There might be an easier code path to do this as usually on EMFFactories
there are more helper methods but ECoreFactory doesn't seem to have one.
I'll ping Rich to see why there isn't and we should either add one or
else he can point us to the right method to use.

> Once I get all this figured out I may do a write
> up for the eclipse wiki, if anyone else is interested. I don't know if
> that page has any traffic.

This sounds like a great idea. We can link to your wiki from the
org.eclipse.vep homepage. One of the things we're doing right now is
trying very hard to create more tutorials to do the kind of thing you
are doing but failing basically cause we are very stretched between
trying to make the VE a) work for users who want to build GUIs and b)
work for users who want to extend it. Would you be interested in
helping to co-author a tutorial for b) that we can work off-line to get
the contents right and you'd be a great input based on where you are in
the learning curve and your list of requirements ?

Best regards,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #610652 is a reply to message #105194] Mon, 05 September 2005 13:12 Go to previous message
hanys is currently offline hanysFriend
Messages: 188
Registered: July 2009
Senior Member
Hi all,

1) How can I get Annotations to JavaObjectInstance model object.
2) How can I get java String value of allocation object
I have for example:
JavaObjectInstance@83bbd2{java.lang.String}: org.eclipse.jem.internal.instantiation.impl.InitStringAlloca tionImpl @f6ab36
(initString: "MY VALUE")

I want to get result MY VALUE without quotes.

Thanks,
John Cage
Re: Adding a component programmatically - figured - one more question [message #610655 is a reply to message #105194] Mon, 05 September 2005 16:06 Go to previous message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Thanks Joe, this works well. Just pop it into the Annotation factory and
it comes up.

I also found a workaround for the label problem I was having (seperate post)
so everything is working wonderfully. I'm trying to imagine what it would
be like to have to build all of this stuff from scratch. The stuff nightmares
and time sinkholes are made of. If only Hawking were a comp sci major.

As to the tutorial I am interested. Everyone is tight for time it seems
( I squeeze this stuff in during my limited free time). How do we go about
organizing a co-authorship ? Let me know who to email and I'll get a work
address out to them, one that isn't as cluttered with spam as the above.

In the meantime I'll work out a content outline for a potential wiki posting.


thanks,

Dylan

> Hi Dylan,
>
>> Still stumped on #1.
>>
> The VE uses Annotations (in the JEM model) to just store arbitrary
> bits of information that aren't part of the Java instance but might be
> useful to something else. The field name is held as an instance of an
> EStringToStringMapEntry with a key of
> "org.eclipse.ve.internal.cde.code.nameincomposition" and a value that
> is a String that represents the field name.
>
> If you switch the VE into the mode where it shows its XML (do this on
> your launch configuration by enabling tracing for
> org.eclipse.ve.java.core and selecting "debug/xmltext" and then in the
> workbench you launch visit the preferences page and select "Show XML
> Window") you can see the annotation for a field. Another way to know
> is to look at one of the palette template files like RatioButton.xmi
> that shows the serailized form of the annotation with the map entry
> for the field name.
>
> To create an EStringToStringMapEntry you go to the package and get its
> instance you need something like the following code snippet.
>
> EStringToStringMapEntryImpl entry
> ((EcoreFactoryImpl)EcorePackage.eINSTANCE.getEcoreFactory()) .createESt
> ringToStringMapEntry(); entry.setValue("myFieldName");
> entry.setKey("org.eclipse.ve.internal.cde.code.nameincomposition ");
>
> There might be an easier code path to do this as usually on
> EMFFactories
> there are more helper methods but ECoreFactory doesn't seem to have
> one.
> I'll ping Rich to see why there isn't and we should either add one
> or
> else he can point us to the right method to use.
>> Once I get all this figured out I may do a write up for the eclipse
>> wiki, if anyone else is interested. I don't know if that page has
>> any traffic.
>>
> This sounds like a great idea. We can link to your wiki from the
> org.eclipse.vep homepage. One of the things we're doing right now is
> trying very hard to create more tutorials to do the kind of thing you
> are doing but failing basically cause we are very stretched between
> trying to make the VE a) work for users who want to build GUIs and b)
> work for users who want to extend it. Would you be interested in
> helping to co-author a tutorial for b) that we can work off-line to
> get the contents right and you'd be a great input based on where you
> are in the learning curve and your list of requirements ?
>
> Best regards,
>
> Joe Winchester
>
Re: Adding a component programmatically - figured - one more question [message #610701 is a reply to message #105235] Thu, 08 September 2005 13:18 Go to previous message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi Dylan,

> As to the tutorial I am interested. Everyone is tight for time it seems
> ( I squeeze this stuff in during my limited free time). How do we go
> about organizing a co-authorship ? Let me know who to email and I'll
> get a work address out to them, one that isn't as cluttered with spam as
> the above.

Post a query to the ve-dev newsgroup and we'll get a list of names of
people to work on the tutorial. The way we have written these in the
past is to get the code working first and iterate this and then write
the doc for it. I definitely would want to work on the tutorial as it
has been on my to-do-list for about the last year, and I think there are
others on the VE newsgroup how would be able to help either with
authorship or by doing a review and sanity check on the piece.

> In the meantime I'll work out a content outline for a potential wiki
> posting.

Fantastic.

Best regards,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #610702 is a reply to message #105210] Thu, 08 September 2005 13:30 Go to previous message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi John,

> 1) How can I get Annotations to JavaObjectInstance model object.

To create one you basically need to do the following

1 - Create an Annotation object
2 - Point it to the JavaObjectInstance
2 - Add it to the BeanComposition

Code for this might look something like:

AnnotationEMF a =
CDMPackage.eInstance.getCDMFactory().createAnnotationEMF();
a.setAnnotates(myJavaObjectInstance);
beanComposition.getAnnotations().add(a);

To get the BeanComposition (who is the root of the whole model) there
are several ways. If you are in an edit part you can do

EditDomain editDomain = EditDomain.getEditDomain(editPart);
BeanComposition b = (BeanComposition)editDomain.getDiagramData();

another is to call getContainer() in a loop till you get it, e.g.

EObject container = javaObjectInstance.eContainer();
while(container != null){
container = container.eContainer();
if(container instance of BeanComposition){
// bingo
}
}

> 2) How can I get java String value of allocation object
> I have for example:
> JavaObjectInstance@83bbd2{java.lang.String}: org.eclipse.jem.internal.instantiation.impl.InitStringAlloca tionImpl @f6ab36
> (initString: "MY VALUE")

Usually you don't do this but you get a property value instead. For
example, with the code like

JButton b = new JButton("Frog");

this gives you the kind of code you've described, however the "Frog"
becomes the text property. To get it from the JavaBean you call
getText(). In VE terms you just do something like

textJavaObjectInstance =
BeanUtilities.getFeatureValue(javaObjectInstance,"text");

which returns an IJavaObjectInstance for the String whose value is
"Frog". To get the "Frog" value you then go and get the actual value on
the VM

IBeanProxy textBeanProxy =
BeanProxyUtilities.getBeanProxy(textJavaObjectInstance):

Cause you know it is a String you cast and get the value

String myValue = ((IStringBeanProxy)testBeanProxy).stringValue();

Having said all this, it assumes that is a get method property that can
give you the real value. Is there one ?

Best regards,

Joe Winchester
Re: Adding a component programmatically - figured - one more question [message #610705 is a reply to message #105728] Thu, 08 September 2005 13:51 Go to previous message
hanys is currently offline hanysFriend
Messages: 188
Registered: July 2009
Senior Member
Hi Joe,

Thank You for your explanation of the problem,
it is very clear to understand.


Best Regards,

John Cage
Re: Adding a component programmatically - figured - one more question [message #610706 is a reply to message #105712] Thu, 08 September 2005 13:59 Go to previous message
Dylan Bruzenak is currently offline Dylan BruzenakFriend
Messages: 48
Registered: July 2009
Member
Newsgroup or mailing list ? I don't see a ve-dev newsgroup, so I'm assuming
the latter.

Dylan

> Hi Dylan,
>
>> As to the tutorial I am interested. Everyone is tight for time it
>> seems ( I squeeze this stuff in during my limited free time). How do
>> we go about organizing a co-authorship ? Let me know who to email
>> and I'll get a work address out to them, one that isn't as cluttered
>> with spam as the above.
>>
> Post a query to the ve-dev newsgroup and we'll get a list of names of
> people to work on the tutorial. The way we have written these in the
> past is to get the code working first and iterate this and then write
> the doc for it. I definitely would want to work on the tutorial as it
> has been on my to-do-list for about the last year, and I think there
> are others on the VE newsgroup how would be able to help either with
> authorship or by doing a review and sanity check on the piece.
>
>> In the meantime I'll work out a content outline for a potential wiki
>> posting.
>>
> Fantastic.
>
> Best regards,
>
> Joe Winchester
>
Re: Adding a component programmatically - figured - one more question [message #610709 is a reply to message #105763] Thu, 08 September 2005 14:23 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

Hi,

There is one important complication to Joe's description. In the VE you
would never do subclasscomposition.add(something) or
javainstance.eSet(feature, value) to the subclasscomposition or to any
javainstance that is in the model. This is because changing it and not
using the command stack and commands will cause bad things to occur.

That is why everytime we change things we do it through commands.

John Cage wrote:
> Hi Joe,
>
> Thank You for your explanation of the problem, it is very clear to
> understand.
>
>
> Best Regards,
>
> John Cage
>

--
Thanks,
Rich Kulp
Re: Adding a component programmatically - figured - one more question [message #610721 is a reply to message #105817] Thu, 08 September 2005 22:52 Go to previous message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
> There is one important complication to Joe's description. In the VE you
> would never do subclasscomposition.add(something) or
> javainstance.eSet(feature, value) to the subclasscomposition or to any
> javainstance that is in the model. This is because changing it and not
> using the command stack and commands will cause bad things to occur.
> That is why everytime we change things we do it through commands.

Good point. Once you know the target, feature and source to change the
technique is to use RuledCommandBuilder so a line that was

target.eSet(feature,source);

now becomes

RuledCommandBuilder biulder = new RuledCommandBuilder();
builder.applyAttributeSetting(target,feature,source);
editDomain.getCommandStack().execute(builder.getCommand());

Best regards,

Joe winchester
Re: Adding a component programmatically - figured - one more question [message #610724 is a reply to message #105992] Fri, 09 September 2005 07:19 Go to previous message
hanys is currently offline hanysFriend
Messages: 188
Registered: July 2009
Senior Member
Hi joe,
Now it's absolutely clear :)

Thank you,
John Cage
Re: Adding a component programmatically - figured - one more question [message #610726 is a reply to message #105776] Fri, 09 September 2005 10:14 Go to previous message
Joe Winchester is currently offline Joe WinchesterFriend
Messages: 496
Registered: July 2009
Senior Member
Hi Dylan,

> Newsgroup or mailing list ? I don't see a ve-dev newsgroup, so I'm
> assuming the latter.

Yup - I think the mailing list is a good place to try to pick up people
for interest. To begin with I think you and I can make a stab at
writing the piece and I know who else from the VE comitters we can use
as reviewers, but hopefully there are others who would like to give
input to steer the content and contribute and review.

I am actually quite surprised by a lot of the traffic on this newsgroup
from people who are wanting to extend the VE and also who have gotten
pretty far and succeeded without any kind of formal tutorial or API docs.

One thing we are thinking of doing is submitting a tutorial to
EclipseCon 2006 on extending the VE and its internals. It'd also be
good to get input into what areas this should cover - it could vary from
the persistence (a lot of people want to read/write XML instead of
Java - it could be about custom layout managers that people want - it
could be about custom model manipulation such as group/ungroup features.
The more ideas and input the better it can be and, from this, formal
Eclipsecorner tutorials can be spun off.

Best regards and many thanks,

Joe Winchester
Previous Topic:VE with Swing problem
Next Topic:Details of "Parse Error"
Goto Forum:
  


Current Time: Thu Apr 25 14:02:25 GMT 2024

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

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

Back to the top