Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » CombinedTemplateCreationEntry and drag and drop from the palette
CombinedTemplateCreationEntry and drag and drop from the palette [message #234021] Wed, 09 May 2007 19:44 Go to next message
Ian Leslie is currently offline Ian LeslieFriend
Messages: 137
Registered: July 2009
Senior Member
I have done some searching in this forum but cannot quite piece together
and answer. I had a very simple tool palette with only a few items in it
which were all create-able using the default constructor. I used this
constructor for my CombinedTemplateCreationEntry objects:

CombinedTemplateCreationEntry(String label, String shortDesc, Object
template, CreationFactory factory, ImageDescriptor iconSmall,
ImageDescriptor iconLarge)

Where the template was the type of my object and the factory was a
SimpleFactory constructed with that same type.

Then I needed to add a few palette entries that required some information
for the constructor. But I found that this did not work as expected.
Selecting the tool and then clicking on the diagram produced the correct
results but drag and dropping created instances of the object that used
the default constructor. It seems that the click placement was using my
factory but the drag and drop was using createInstance from the type - my
factory was never called.

I found this thread
http://dev.eclipse.org/newslists/news.eclipse.tools.gef/msg1 3863.html
which looked promising. It sounded like I could switch to using this
constructor and my factory woudl get called:

CombinedTemplateCreationEntry(String label, String shortDesc,
CreationFactory factory, ImageDescriptor iconSmall, ImageDescriptor
iconLarge)

But this meant that now drag and drop from the tool palette is not
allowed. I suppose that is slightly better than creating the wrong thing.

Shouldn't the factory be called in all cases when an object is created
from the tool palette?

On a more pragmatic note: What is the right thing for me to do in order to
get drag and drop to work for all my tool palette entries? It seems from
re-reading the above forum post and a few others I found that one
possibility is to actually change my diagram to accept instances of the
factory to be dropped and then switch back to the
CombinedTemplateCreationEntry constructor and use the factory as the
template. Then when one gets dropped I can invoke the getNewObject
method. Would I be going down the right track? Are there any changes in
3.3 that I should be aware of? My application will not be GA until after
3.3 is ready so I am planning to switch to 3.3 before my application ships.

Any help or advice would be appreciated.

Ian
Re: CombinedTemplateCreationEntry and drag and drop from the palette [message #234074 is a reply to message #234021] Fri, 11 May 2007 05:00 Go to previous messageGo to next message
Alex Boyko is currently offline Alex BoykoFriend
Messages: 200
Registered: July 2009
Senior Member
Hi,

The factory's #getNewObject() should be called when you create something
from the palette either by clicking or dragging and dropping. Hence, I'd
suggest you to debug YourFactory#getNewObject(), also verify that
DropTargetEvent data field contains your factory and check whether a valid
command is created for your CreateRequest.
Hope this helps.

Cheers,
Alex
Re: CombinedTemplateCreationEntry and drag and drop from the palette [message #234104 is a reply to message #234074] Fri, 11 May 2007 11:14 Go to previous messageGo to next message
Ian Leslie is currently offline Ian LeslieFriend
Messages: 137
Registered: July 2009
Senior Member
Thanks for the pointers. My factory's getNewObject was never called when
I drag and drop the tool. After trying to find a place to check the
DropTargetEvent as you suggested (which I could not) I checked how my
other drag and drop listener was setup and found the problem. My editor's
viewer needs to listen to two kinds of drag and drop - items from the
palette and items from another one of my views so I have two
DragTargetListeners setup:

protected void initializeGraphicalViewer () {
super.initializeGraphicalViewer ();

GraphicalViewer viewer = getGraphicalViewer ();

// Set the contents of the viewer
viewer.setContents (getModel ());

// Listen for dropped parts
viewer.addDropTargetListener
(createPaletteTransferDropTargetListener ());
viewer.addDropTargetListener (createCardTransferDropTargetListener
());
}

The one that exists to serve the palette is created like this:
private TransferDropTargetListener
createPaletteTransferDropTargetListener () {
return new TemplateTransferDropTargetListener (getGraphicalViewer
()) {

@Override
protected CreationFactory getFactory (Object template) {
return new SimpleFactory ((Class)template);
}
};
}

and sure enough the getFactory method is called when dragging and dropping
a palette tool and the template object is an instance of my
NamedCardFactory. Creating a SimpleFactory with an instance of
NamedCardFactory was not having the desired effect.

So I changed it to this:
private TransferDropTargetListener
createPaletteTransferDropTargetListener () {
return new TemplateTransferDropTargetListener (getGraphicalViewer
()) {

@Override
protected CreationFactory getFactory (Object template) {
if (template instanceof CreationFactory) {
// Use the passed in factory
return (CreationFactory)template;
} else {
// Create a simple factory for the template type
return new SimpleFactory ((Class)template);
}
}
};
}
Now things work. I got this code from a recipe or tutorial when I was
first setting up my editor. I remember this had to be there so the
palette tools would work. Is the new code I have now really appropriate?
The instanceof and casting seems less than ideal. Is there a better way?

Ian
Re: CombinedTemplateCreationEntry and drag and drop from the palette [message #234111 is a reply to message #234104] Fri, 11 May 2007 13:58 Go to previous messageGo to next message
Ian Leslie is currently offline Ian LeslieFriend
Messages: 137
Registered: July 2009
Senior Member
Ian Leslie wrote:

<snip>
> Creating a SimpleFactory with an instance of
> NamedCardFactory was not having the desired effect.
By which I meant - "Creating a SimpleFactory with an instance of
NamedCardFactory was *obviously* not having the desired effect."


<snip>
> I got this code from a recipe or tutorial when I was
> first setting up my editor.
Should have read "I got *the original* code from a recipe or tutorial when
I was first setting up my editor."
Re: CombinedTemplateCreationEntry and drag and drop from the palette [message #234141 is a reply to message #234111] Fri, 11 May 2007 20:24 Go to previous messageGo to next message
Alex Boyko is currently offline Alex BoykoFriend
Messages: 200
Registered: July 2009
Senior Member
Hi Ian,

Perhaps it would be better just to override createTargetRequest() and use
currently active tool to create the request. This way the same factory
will be used for drag and drop and clicking.

For example:
protected Request createTargetRequest() {

CreationTool tool = getCreationTool();

if (tool != null) {

tool.setViewer(getViewer());
tool.setEditDomain(getViewer().getEditDomain());

return tool.createCreateRequest();
}

return null;
}

private CreationTool getCreationTool() {
Object template = TemplateTransfer.getInstance().getTemplate();
if (template instanceof PaletteToolEntry) {
Tool tool = ((PaletteToolEntry) template).createTool();
if (tool instanceof CreationTool) {
return (CreationTool) tool;
}
}
return null;
}

Essentially, this is what you did in more correct way.
Hope this helps.

Cheers,
Alex
Re: CombinedTemplateCreationEntry and drag and drop from the palette [message #234149 is a reply to message #234141] Sat, 12 May 2007 13:00 Go to previous messageGo to next message
Ian Leslie is currently offline Ian LeslieFriend
Messages: 137
Registered: July 2009
Senior Member
Thank you that seems like a good way to go. I copied and pasted but had a
bit of trouble. The method createCreateRequest does not exist on
CreationTool
> return tool.createCreateRequest();
There is a getCreateRequest() and a createTargetRequest() method but they
are both protected to I cannot call them.

Also I cannot find a class called PaletteToolEntry:
> if (template instanceof PaletteToolEntry) {
My guess is it should be CreationToolEntry.

Ian
Re: CombinedTemplateCreationEntry and drag and drop from the palette [message #234173 is a reply to message #234149] Sat, 12 May 2007 17:44 Go to previous message
Alex Boyko is currently offline Alex BoykoFriend
Messages: 200
Registered: July 2009
Senior Member
Hi Ian,

The code snippet I gave you is GMF specific... so to make it work you'd
have to override CreationTool and provide a public method that would
return a CreateRequest (i.e. the createCreateRequest()

private CreationTool getCreationTool() {
Object template = TemplateTransfer.getInstance().getTemplate();
if (template instanceof CreationTool) {
return (CreationTool) tool;
}
return null;
}

However, maybe the solution you provided is even better, since it requires
less work.

Cheers,
Alex
Previous Topic:How to use the ComboBoxCellEditor to impl dierect edit?
Next Topic:GEF tutorials and Books
Goto Forum:
  


Current Time: Tue Apr 23 08:15:24 GMT 2024

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

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

Back to the top