Adding buttons to the elements [message #853762] |
Mon, 23 April 2012 06:00  |
Eclipse User |
|
|
|
Hello,
I want to add a button to some of my elements (the squares/figures of class A.), that when clicked will create a special Node (class B), and will link A to B using an already defined connection between the two.
The idea is :
- click
+- create node
+- create link
and all this will be done automatically.
Can you give me a hint on how to do this?
Thanks in advance
|
|
|
|
|
|
|
Re: Adding buttons to the elements [message #853958 is a reply to message #853904] |
Mon, 23 April 2012 10:04   |
Eclipse User |
|
|
|
Hi,
For a pop up you need to do the following:
Create a class inherited from PopupBarEditPolicy and overwrite the fillPopupBarDescriptorsMethod:
@Override
protected void fillPopupBarDescriptors() {
super.fillPopupBarDescriptors();
AbstractPopupBarTool tool = null;
ImageDescriptor imageDescriptor = null;
Image image = null;
tool = new XXXPopupBarTool(getHost(), null);
imageDescriptor = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "/icons/tool.gif");
image = imageDescriptor.createImage();
addPopupBarDescriptor(null, image, tool, "ToolTip");
}
Create a class XXXPopupTollBarTool inherited from AbstractPopupBarTool with the followinf methods overriden
public XXXPopupBarTool(EditPart epHost, CreateRequest theRequest) {
super(epHost, theRequest);
}
@Override
protected Request createTargetRequest() {
return new RequestForThisTool();
}
@Override
protected Command getCommand() {
return new CommandForThisTool();
}
Now you need to install your editpolicy into the editpart
/**
* @generated
*/
protected void createDefaultEditPolicies() {
//Generated code before...
removeEditPolicy(EditPolicyRoles.POPUPBAR_ROLE);
installEditPolicy(EditPolicyRoles.POPUPBAR_ROLE, new YourPopupBarEditPolicy());
}
That's all.
Ralph
|
|
|
|
Re: Adding buttons to the elements [message #856082 is a reply to message #854882] |
Wed, 25 April 2012 06:56  |
Eclipse User |
|
|
|
Hi,
to implement the tool I do the following:
public IntrospectPopupBarTool(EditPart epHost, CreateRequest theRequest) {
super(epHost, theRequest);
}
@Override
protected Request createTargetRequest() {
ChangePropertyValueRequest req = new ChangePropertyValueRequest("XXXX", "XXX");
return req;
}
@Override
protected Command getCommand() {
return new XXXCommand((IGraphicalEditPart)getHost());
}
I do not use the ChangePropertyValueRequest in my command I just create anyone to implement this abstract method. On getCommand I return my custom command. It is implemented like this:
private IGraphicalEditPart host = null;
/**
*
* @param host Element to introspect
*/
public IntrospectCommand(IGraphicalEditPart host){
this.host = host;
}
@Override
public void execute() {
super.execute();
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
MessageBox m = new MessageBox(shell);
m.setMessage("Hello World");
m.open();
}
You can place whatever code you want in the execute method.
Michael Golubev postest this code a view days ago to create new model element in the way the palette does:
List types = Collections.singletonList(ActivityElementTypes.Action_2001);
CreateUnspecifiedTypeRequest gefRequest = new CreateUnspecifiedTypeRequest(types, PreferencesHint.USE_DEFAULTS);
gefRequest.setLocation(...);
gefRequest.setSize(...);
EditPart containerEP = .... //find EP that should host the new element, DiagramEditPart for top-level elements.
Command gefCommand = containerEP.getCommand(gefRequest);
if (gefCommand.canExecute()) {
//execute
}
Ralph
|
|
|
Powered by
FUDForum. Page generated in 0.10144 seconds