Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Add key listener on an element of diagram
Add key listener on an element of diagram [message #1726618] Tue, 15 March 2016 07:23 Go to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi all,

I'd like to add a key listener on elements. For example, when user click on an element on diagram, then he click Tab button on keyboard, the diagram will generate a new element (or doing something).

Can anyone give me a solution?

Thanks and BRs,
Tuan Hoang
Re: Add key listener on an element of diagram [message #1726630 is a reply to message #1726618] Tue, 15 March 2016 09:21 Go to previous messageGo to next message
Esteban Dugueperoux is currently offline Esteban DugueperouxFriend
Messages: 472
Registered: July 2009
Senior Member
Hi,

Sirius doesn't offer mean to do that through odesign. But as Sirius is
based on GMF, you can use GMF API to do that, or GEF as GMF is based on GMF.
Each swt/draw2d event is intercepted by the org.eclipse.gef.EditDomain,
for key press interception EditDomain.keyDown() is called. Looking at
key press after a node selection, the
org.eclipse.gef.tools.SelectionTool is called and this last call
DirectEditKeyHandler which allow to associate jface action to key.
This is not a solution but a clue to a solution.

Best Regards.

Le 15/03/2016 08:24, Tuan Hoang Anh a écrit :
> Hi all,
>
> I'd like to add a key listener on elements. For example, when user click
> on an element on diagram, then he click Tab button on keyboard, the
> diagram will generate a new element (or doing something).
>
> Can anyone give me a solution?
>
> Thanks and BRs,
> Tuan Hoang



--
Esteban Dugueperoux - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Add key listener on an element of diagram [message #1726742 is a reply to message #1726630] Wed, 16 March 2016 05:06 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Esteban,

Could you please guide me how to intercept swt events by org.eclipse.gef.EditDomain? Does eclipse plugins provide any extension to add more implementation for EditDomain? Is it possible if I want to overwrite a default implementation of Sirius in EditDomain?

Thanks and BRs,
Tuan Hoang

Esteban Dugueperoux wrote on Tue, 15 March 2016 09:21
Hi,

Sirius doesn't offer mean to do that through odesign. But as Sirius is
based on GMF, you can use GMF API to do that, or GEF as GMF is based on GMF.
Each swt/draw2d event is intercepted by the org.eclipse.gef.EditDomain,
for key press interception EditDomain.keyDown() is called. Looking at
key press after a node selection, the
org.eclipse.gef.tools.SelectionTool is called and this last call
DirectEditKeyHandler which allow to associate jface action to key.
This is not a solution but a clue to a solution.

Best Regards.

Le 15/03/2016 08:24, Tuan Hoang Anh a écrit :
> Hi all,
>
> I'd like to add a key listener on elements. For example, when user click
> on an element on diagram, then he click Tab button on keyboard, the
> diagram will generate a new element (or doing something).
>
> Can anyone give me a solution?
>
> Thanks and BRs,
> Tuan Hoang



--
Esteban Dugueperoux - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius

Re: Add key listener on an element of diagram [message #1726757 is a reply to message #1726742] Wed, 16 March 2016 08:24 Go to previous messageGo to next message
Esteban Dugueperoux is currently offline Esteban DugueperouxFriend
Messages: 472
Registered: July 2009
Senior Member
Hi Tuan,

As your question is not specific to Sirius but more GEF/GMF related, I
advice you to look at GEF/GMF documentation, for example [1], [2] and [3].
From what I have seen in looking at GEF/GMF code, a KeyHandler is
referenced by EditPartViewer and this last as the EditDomain are
accessible through EditPart and GMF DiagramEditor.


[1] https://wiki.eclipse.org/GEF_Description
[2] https://wiki.eclipse.org/GEF_Description2#EditDomain
[3] https://www.eclipse.org/articles/Article-Introducing-GMF/article.html

Best Regards.

Le 16/03/2016 06:06, Tuan Hoang Anh a écrit :
> Hi Esteban,
>
> Could you please guide me how to intercept swt events by
> org.eclipse.gef.EditDomain? Does eclipse plugins provide any extension
> to add more implementation for EditDomain? Is it possible if I want to
> overwrite a default implementation of Sirius in EditDomain?
>
> Thanks and BRs,
> Tuan Hoang
>
> Esteban Dugueperoux wrote on Tue, 15 March 2016 09:21
>> Hi,
>>
>> Sirius doesn't offer mean to do that through odesign. But as Sirius is
>> based on GMF, you can use GMF API to do that, or GEF as GMF is based
>> on GMF.
>> Each swt/draw2d event is intercepted by the
>> org.eclipse.gef.EditDomain, for key press interception
>> EditDomain.keyDown() is called. Looking at key press after a node
>> selection, the org.eclipse.gef.tools.SelectionTool is called and this
>> last call DirectEditKeyHandler which allow to associate jface action
>> to key.
>> This is not a solution but a clue to a solution.
>>
>> Best Regards.
>>
>> Le 15/03/2016 08:24, Tuan Hoang Anh a écrit :
>> > Hi all,
>> >
>> > I'd like to add a key listener on elements. For example, when user
>> click
>> > on an element on diagram, then he click Tab button on keyboard, the
>> > diagram will generate a new element (or doing something).
>> >
>> > Can anyone give me a solution?
>> >
>> > Thanks and BRs,
>> > Tuan Hoang
>>
>>
>>
>> --
>> Esteban Dugueperoux - Obeo
>>
>> Need training or professional services for Sirius?
>> http://www.obeodesigner.com/sirius
>
>



--
Esteban Dugueperoux - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Add key listener on an element of diagram [message #1726857 is a reply to message #1726757] Thu, 17 March 2016 05:01 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Esteban,

Thank for your support.
As my investigation, I have to create a new org.eclipse.ui.editors extension and override the implementation of EditDomain function to reach my target.
But I want to add the KeyHandler to the Sirius Diagram (Representation), not the Model File, and I don't know the extension of that file.

Is my understanding right or there is another way to do it?

Thanks and BRs,
Tuan Hoang
Esteban Dugueperoux wrote on Wed, 16 March 2016 08:24
Hi Tuan,

As your question is not specific to Sirius but more GEF/GMF related, I
advice you to look at GEF/GMF documentation, for example [1], [2] and [3].
From what I have seen in looking at GEF/GMF code, a KeyHandler is
referenced by EditPartViewer and this last as the EditDomain are
accessible through EditPart and GMF DiagramEditor.


[1] https://wiki.eclipse.org/GEF_Description
[2] https://wiki.eclipse.org/GEF_Description2#EditDomain
[3] https://www.eclipse.org/articles/Article-Introducing-GMF/article.html

Best Regards.

Le 16/03/2016 06:06, Tuan Hoang Anh a écrit :
> Hi Esteban,
>
> Could you please guide me how to intercept swt events by
> org.eclipse.gef.EditDomain? Does eclipse plugins provide any extension
> to add more implementation for EditDomain? Is it possible if I want to
> overwrite a default implementation of Sirius in EditDomain?
>
> Thanks and BRs,
> Tuan Hoang
>
> Esteban Dugueperoux wrote on Tue, 15 March 2016 09:21
>> Hi,
>>
>> Sirius doesn't offer mean to do that through odesign. But as Sirius is
>> based on GMF, you can use GMF API to do that, or GEF as GMF is based
>> on GMF.
>> Each swt/draw2d event is intercepted by the
>> org.eclipse.gef.EditDomain, for key press interception
>> EditDomain.keyDown() is called. Looking at key press after a node
>> selection, the org.eclipse.gef.tools.SelectionTool is called and this
>> last call DirectEditKeyHandler which allow to associate jface action
>> to key.
>> This is not a solution but a clue to a solution.
>>
>> Best Regards.
>>
>> Le 15/03/2016 08:24, Tuan Hoang Anh a écrit :
>> > Hi all,
>> >
>> > I'd like to add a key listener on elements. For example, when user
>> click
>> > on an element on diagram, then he click Tab button on keyboard, the
>> > diagram will generate a new element (or doing something).
>> >
>> > Can anyone give me a solution?
>> >
>> > Thanks and BRs,
>> > Tuan Hoang
>>
>>
>>
>> --
>> Esteban Dugueperoux - Obeo
>>
>> Need training or professional services for Sirius?
>> http://www.obeodesigner.com/sirius
>
>



--
Esteban Dugueperoux - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius

Re: Add key listener on an element of diagram [message #1726880 is a reply to message #1726857] Thu, 17 March 2016 08:44 Go to previous messageGo to next message
Esteban Dugueperoux is currently offline Esteban DugueperouxFriend
Messages: 472
Registered: July 2009
Senior Member
Hi Tuan,

You have no need to declare a new editor, you must reuse
SiriusDiagramGraphicalViewer which is the Sirius implementation of
EditPartViewer to put your action associated to a key press in the
KeyHandler. To do that you could put a IPartListener to listens
DDiagramEditorImpl opening, get the SiriusDiagramGraphicalViewer, get
the KeyHandler and put your action to an associated KeyStroke.

Best Regards.

Le 17/03/2016 06:01, Tuan Hoang Anh a écrit :
> Hi Esteban,
>
> Thank for your support.
> As my investigation, I have to create a new org.eclipse.ui.editors
> extension and override the implementation of EditDomain function to
> reach my target. But I want to add the KeyHandler to the Sirius Diagram
> (Representation), not the Model File, and I don't know the extension of
> that file.
>
> Is my understanding right or there is another way to do it?
>
> Thanks and BRs,
> Tuan Hoang
> Esteban Dugueperoux wrote on Wed, 16 March 2016 08:24
>> Hi Tuan,
>>
>> As your question is not specific to Sirius but more GEF/GMF related, I
>> advice you to look at GEF/GMF documentation, for example [1], [2] and
>> [3].
>> From what I have seen in looking at GEF/GMF code, a KeyHandler is
>> referenced by EditPartViewer and this last as the EditDomain are
>> accessible through EditPart and GMF DiagramEditor.
>>
>>
>> [1] https://wiki.eclipse.org/GEF_Description
>> [2] https://wiki.eclipse.org/GEF_Description2#EditDomain
>> [3] https://www.eclipse.org/articles/Article-Introducing-GMF/article.html
>>
>> Best Regards.
>>
>> Le 16/03/2016 06:06, Tuan Hoang Anh a écrit :
>> > Hi Esteban,
>> >
>> > Could you please guide me how to intercept swt events by
>> > org.eclipse.gef.EditDomain? Does eclipse plugins provide any extension
>> > to add more implementation for EditDomain? Is it possible if I want to
>> > overwrite a default implementation of Sirius in EditDomain?
>> >
>> > Thanks and BRs,
>> > Tuan Hoang
>> >
>> > Esteban Dugueperoux wrote on Tue, 15 March 2016 09:21
>> >> Hi,
>> >>
>> >> Sirius doesn't offer mean to do that through odesign. But as Sirius is
>> >> based on GMF, you can use GMF API to do that, or GEF as GMF is based
>> >> on GMF.
>> >> Each swt/draw2d event is intercepted by the
>> >> org.eclipse.gef.EditDomain, for key press interception
>> >> EditDomain.keyDown() is called. Looking at key press after a node
>> >> selection, the org.eclipse.gef.tools.SelectionTool is called and this
>> >> last call DirectEditKeyHandler which allow to associate jface action
>> >> to key.
>> >> This is not a solution but a clue to a solution.
>> >>
>> >> Best Regards.
>> >>
>> >> Le 15/03/2016 08:24, Tuan Hoang Anh a écrit :
>> >> > Hi all,
>> >> >
>> >> > I'd like to add a key listener on elements. For example, when user
>> >> click
>> >> > on an element on diagram, then he click Tab button on keyboard, the
>> >> > diagram will generate a new element (or doing something).
>> >> >
>> >> > Can anyone give me a solution?
>> >> >
>> >> > Thanks and BRs,
>> >> > Tuan Hoang
>> >>
>> >>
>> >>
>> >> --
>> >> Esteban Dugueperoux - Obeo
>> >>
>> >> Need training or professional services for Sirius?
>> >> http://www.obeodesigner.com/sirius
>> >
>> >
>>
>>
>>
>> --
>> Esteban Dugueperoux - Obeo
>>
>> Need training or professional services for Sirius?
>> http://www.obeodesigner.com/sirius
>
>



--
Esteban Dugueperoux - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Add key listener on an element of diagram [message #1726900 is a reply to message #1726880] Thu, 17 March 2016 11:12 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Esteban,

I did follow your suggestion, and get the KeyHandler successfully.
But it didn't work when I add a new action to an associated KeyStroke.
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
	if (editorPart != null) {		
		if (editorPart instanceof DiagramEditor) {
			EditPartViewer viewer = ((DiagramEditor) editorPart).getDiagramEditPart().getViewer();
			if (viewer instanceof SiriusDiagramGraphicalViewer) {
				KeyHandler keyHandler = viewer.getKeyHandler();
				if (keyHandler == null) {
					keyHandler = new KeyHandler();
				}
				keyHandler.put(KeyStroke.getPressed(SWT.TAB, 0), new AutoSuggestionElement());
				viewer.setKeyHandler(keyHandler);
			}								
		}
	}


The AutoSuggestionElement class implements IAction, but its run() function isn't called when I presses Tab on element.
Can you tell me if I miss something?

Thanks and BRs,
Tuan Hoang
Quote:
Hi Tuan,

You have no need to declare a new editor, you must reuse
SiriusDiagramGraphicalViewer which is the Sirius implementation of
EditPartViewer to put your action associated to a key press in the
KeyHandler. To do that you could put a IPartListener to listens
DDiagramEditorImpl opening, get the SiriusDiagramGraphicalViewer, get
the KeyHandler and put your action to an associated KeyStroke.

Best Regards.
Re: Add key listener on an element of diagram [message #1726919 is a reply to message #1726900] Thu, 17 March 2016 12:33 Go to previous messageGo to next message
Esteban Dugueperoux is currently offline Esteban DugueperouxFriend
Messages: 472
Registered: July 2009
Senior Member
I have never tested that, I only give you a mean reading the GMF code.
Then I cannot tell you if you missed something, it is to you to debug in
GMF code to look why it doesn't works.

Best Regards.

Le 17/03/2016 12:12, Tuan Hoang Anh a écrit :
> Hi Esteban,
>
> I did follow your suggestion, and get the KeyHandler successfully.
> But it didn't work when I add a new action to an associated KeyStroke.
> IEditorPart editorPart =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
>
> if (editorPart != null) {
> if (editorPart instanceof DiagramEditor) {
> EditPartViewer viewer = ((DiagramEditor)
> editorPart).getDiagramEditPart().getViewer();
> if (viewer instanceof SiriusDiagramGraphicalViewer) {
> KeyHandler keyHandler = viewer.getKeyHandler();
> if (keyHandler == null) {
> keyHandler = new KeyHandler();
> }
> keyHandler.put(KeyStroke.getPressed(SWT.TAB, 0), new
> AutoSuggestionElement());
> viewer.setKeyHandler(keyHandler);
> }
> }
> }
>
> The AutoSuggestionElement class implements IAction, but its run()
> function isn't called when I presses Tab on element.
> Can you tell me if I miss something?
>
> Thanks and BRs,
> Tuan Hoang
> Quote:
>> Hi Tuan,
>>
>> You have no need to declare a new editor, you must reuse
>> SiriusDiagramGraphicalViewer which is the Sirius implementation of
>> EditPartViewer to put your action associated to a key press in the
>> KeyHandler. To do that you could put a IPartListener to listens
>> DDiagramEditorImpl opening, get the SiriusDiagramGraphicalViewer, get
>> the KeyHandler and put your action to an associated KeyStroke.
>>
>> Best Regards.
>



--
Esteban Dugueperoux - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Add key listener on an element of diagram [message #1728827 is a reply to message #1726900] Thu, 07 April 2016 17:06 Go to previous messageGo to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 210
Registered: February 2014
Senior Member
Hi Tuan,

Could you please provide me with some info on how did you solve this problem ? I'm also looking for a way to capture key press on a Sirius diagram.

Do we need to use any extension points ? Sad

It would be much appreciated if you could kindly help me on this,
Best Regards,
Parsa
Re: Add key listener on an element of diagram [message #1728886 is a reply to message #1728827] Fri, 08 April 2016 09:05 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Parsa,

The suggestion from Esteban helped me a lot. I created a Listener whenever new editor was activated and put my action to an associated KeyStroke to viewer's KeyHandler.
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(new IPartListener2() {

			@Override
			public void partVisible(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partOpened(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partInputChanged(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partHidden(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partDeactivated(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partClosed(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partBroughtToTop(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partActivated(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub
				IEditorPart editorPart = partRef.getPage().getActiveEditor();
				if (editorPart != null && editorPart instanceof DiagramEditor) {
					if (editorPart instanceof DiagramEditor) {
						EditPartViewer viewer = ((DiagramEditor) editorPart).getDiagramEditPart().getViewer();
						if (viewer instanceof SiriusDiagramGraphicalViewer) {
							KeyHandler keyHandler = viewer.getKeyHandler();
							if (keyHandler == null) {
								keyHandler = new KeyHandler();
							}

							// Add action when pressing Ctrl + Space
							keyHandler.put(KeyStroke.getPressed(SWT.SPACE, 0x20, SWT.CTRL), new YourActionClass());
							viewer.setKeyHandler(keyHandler);
						}
					}
				}
			}
		});

You can write YourActionClass by implementing IAction interface.
There are some default action that you can refer in org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor.

Best Regards,
Tuan Hoang
Parsa Pourali wrote on Thu, 07 April 2016 17:06
Hi Tuan,

Could you please provide me with some info on how did you solve this problem ? I'm also looking for a way to capture key press on a Sirius diagram.

Do we need to use any extension points ? Sad

It would be much appreciated if you could kindly help me on this,
Best Regards,
Parsa

Re: Add key listener on an element of diagram [message #1729041 is a reply to message #1728886] Sun, 10 April 2016 04:37 Go to previous messageGo to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 210
Registered: February 2014
Senior Member
Hi Tuan,

Thanks a lot for your reply. I tried the solution but not working. It seems that I am missing something ! I have added a java service on diagram open (Begin->ChangeContext->service:addKeyHandlerService() ).

I also tried adding an extension for org.eclipse.ui.editors and added the method in the init method of the extension. It didn't work as well. Am I missing something ? Should I point the extension from my .odesign as well ?

Thank you,
Bests,
Parsa
Re: Add key listener on an element of diagram [message #1729081 is a reply to message #1729041] Mon, 11 April 2016 04:09 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Parsa,

I don't need any additional Java service, or extension, or changes in .odesign file.
Just only the listener as I wrote before.

Could you show your KeyStroke that you registered and your action class in this topic?

Best Regards,
Tuan Hoang
Parsa Pourali wrote on Sun, 10 April 2016 04:37
Hi Tuan,

Thanks a lot for your reply. I tried the solution but not working. It seems that I am missing something ! I have added a java service on diagram open (Begin->ChangeContext->service:addKeyHandlerService() ).

I also tried adding an extension for org.eclipse.ui.editors and added the method in the init method of the extension. It didn't work as well. Am I missing something ? Should I point the extension from my .odesign as well ?

Thank you,
Bests,
Parsa

Re: Add key listener on an element of diagram [message #1729161 is a reply to message #1729081] Mon, 11 April 2016 15:42 Go to previous messageGo to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 210
Registered: February 2014
Senior Member
Hi tuan,

Now I'm sure I'm doing sth wrong Rolling Eyes

This is my key stroke class:
public class myeditor extends EditorPart {

	public myeditor() {
		
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		JOptionPane.showMessageDialog(null, input);
		site.getPage().addPartListener(new IPartListener2() {
			@Override
			public void partVisible(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partOpened(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partInputChanged(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partHidden(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partDeactivated(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partClosed(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partBroughtToTop(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partActivated(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub
				IEditorPart editorPart = partRef.getPage().getActiveEditor();
				if (editorPart != null && editorPart instanceof DiagramEditor) {
					if (editorPart instanceof DiagramEditor) {
						EditPartViewer viewer = ((DiagramEditor) editorPart).getDiagramEditPart().getViewer();
						if (viewer instanceof SiriusDiagramGraphicalViewer) {
							KeyHandler keyHandler = viewer.getKeyHandler();
							if (keyHandler == null) {
								keyHandler = new KeyHandler();
							}

							// Add action when pressing Ctrl + Space
							JOptionPane.showMessageDialog(null, "ctrl+Space");

							keyHandler.put(KeyStroke.getPressed(SWT.SPACE, 0x20, SWT.CTRL), new YourActionClass());
							viewer.setKeyHandler(keyHandler);
						}
					}
				}
			}
		});
	
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		JOptionPane.showMessageDialog(null, parent);

	}

	@Override
	public void setFocus() {
		JOptionPane.showMessageDialog(null, "focus");

	}

}


and my action class is:
public class YourActionClass implements IAction {

	@Override
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getAccelerator() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getActionDefinitionId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ImageDescriptor getDisabledImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HelpListener getHelpListener() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ImageDescriptor getHoverImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IMenuCreator getMenuCreator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getStyle() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getToolTipText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isChecked() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run() {
		JOptionPane.showMessageDialog(null, "ctrlSpace");

	}

	@Override
	public void runWithEvent(Event event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setActionDefinitionId(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setChecked(boolean checked) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDescription(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDisabledImageDescriptor(ImageDescriptor newImage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEnabled(boolean enabled) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setHelpListener(HelpListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setHoverImageDescriptor(ImageDescriptor newImage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setId(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setImageDescriptor(ImageDescriptor newImage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setMenuCreator(IMenuCreator creator) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setText(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setToolTipText(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setAccelerator(int keycode) {
		// TODO Auto-generated method stub

	}

}


you can see that in the run method I just used JOptionpane to show a dialog just to test. and it is not working. I mean the class myeditor is just a class, how can the designer be aware of it if we don't define extensions ?

Thanks a lot,
Bests,
Parsa
Re: Add key listener on an element of diagram [message #1729166 is a reply to message #1729161] Mon, 11 April 2016 16:08 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Parsa,

The problem is in YourActionClass.

You have to enable the IAction class to do run() method.
public class YourActionClass implements IAction {

	...

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	...

	@Override
	public void run() {
		JOptionPane.showMessageDialog(null, "ctrlSpace");

	}

	...
}

Hope your code works fine now.

Best Regards,
Tuan Hoang
Parsa Pourali wrote on Mon, 11 April 2016 15:42
Hi tuan,

Now I'm sure I'm doing sth wrong Rolling Eyes

This is my key stroke class:
public class myeditor extends EditorPart {

	public myeditor() {
		
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		JOptionPane.showMessageDialog(null, input);
		site.getPage().addPartListener(new IPartListener2() {
			@Override
			public void partVisible(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partOpened(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partInputChanged(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partHidden(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partDeactivated(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partClosed(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partBroughtToTop(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub

			}

			@Override
			public void partActivated(IWorkbenchPartReference partRef) {
				// TODO Auto-generated method stub
				IEditorPart editorPart = partRef.getPage().getActiveEditor();
				if (editorPart != null && editorPart instanceof DiagramEditor) {
					if (editorPart instanceof DiagramEditor) {
						EditPartViewer viewer = ((DiagramEditor) editorPart).getDiagramEditPart().getViewer();
						if (viewer instanceof SiriusDiagramGraphicalViewer) {
							KeyHandler keyHandler = viewer.getKeyHandler();
							if (keyHandler == null) {
								keyHandler = new KeyHandler();
							}

							// Add action when pressing Ctrl + Space
							JOptionPane.showMessageDialog(null, "ctrl+Space");

							keyHandler.put(KeyStroke.getPressed(SWT.SPACE, 0x20, SWT.CTRL), new YourActionClass());
							viewer.setKeyHandler(keyHandler);
						}
					}
				}
			}
		});
	
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		JOptionPane.showMessageDialog(null, parent);

	}

	@Override
	public void setFocus() {
		JOptionPane.showMessageDialog(null, "focus");

	}

}


and my action class is:
public class YourActionClass implements IAction {

	@Override
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getAccelerator() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getActionDefinitionId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ImageDescriptor getDisabledImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HelpListener getHelpListener() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ImageDescriptor getHoverImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IMenuCreator getMenuCreator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getStyle() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getToolTipText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isChecked() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run() {
		JOptionPane.showMessageDialog(null, "ctrlSpace");

	}

	@Override
	public void runWithEvent(Event event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setActionDefinitionId(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setChecked(boolean checked) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDescription(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDisabledImageDescriptor(ImageDescriptor newImage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEnabled(boolean enabled) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setHelpListener(HelpListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setHoverImageDescriptor(ImageDescriptor newImage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setId(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setImageDescriptor(ImageDescriptor newImage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setMenuCreator(IMenuCreator creator) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setText(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setToolTipText(String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setAccelerator(int keycode) {
		// TODO Auto-generated method stub

	}

}


you can see that in the run method I just used JOptionpane to show a dialog just to test. and it is not working. I mean the class myeditor is just a class, how can the designer be aware of it if we don't define extensions ?

Thanks a lot,
Bests,
Parsa
Re: Add key listener on an element of diagram [message #1729182 is a reply to message #1729166] Mon, 11 April 2016 20:36 Go to previous messageGo to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 210
Registered: February 2014
Senior Member
Hi Tuan,

Thank you , I applied your suggestion but I don't still get it work ! I think I am implementing the keystroke method in some stupid class that has no relation to what I want to do : Confused

Where do you exactly implement the keystroke listener:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(new IPartListener2() { ...

I was reading your previous posts and thought that I need to use org.eclipse.ui.editors extension point to register the keystroke, but apparently it is not correct. Could you please let me know how do you register your keystroke class ? or what is the extension that I should use ? What do you mean by "viewer's KeyHandler" ?

Thank you for your time and concern,
Bests,
Parsa

[Updated on: Mon, 11 April 2016 20:37]

Report message to a moderator

Re: Add key listener on an element of diagram [message #1729193 is a reply to message #1729182] Tue, 12 April 2016 03:01 Go to previous messageGo to next message
Tuan Hoang Anh is currently offline Tuan Hoang AnhFriend
Messages: 42
Registered: May 2015
Member
Hi Parsa,

Quote:

Where do you exactly implement the keystroke listener:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(new IPartListener2() { ...

I added this Listener to listen whenever user activates a new Diagram Editor. And my code will register a KeyStroke and associated Action.
You can add it into Activator class of plugin.
public void start(BundleContext context) throws Exception {
		super.start(context);
		...
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		// adding a editor part listener
		page.addPartListener(new IPartListener2() { ...
	}


Quote:
What do you mean by "viewer's KeyHandler" ?

The KeyHandler handles all normal keystrokes on an EditPartViewer. You can register a new KeyStroke and associated Action, then put to to the KeyHandler. When you focus on an element on diagram, and press the combine KeyStroke (in this case Ctrl + Space), the KeyHandler will call the run() method of associated Action.

Best Regards,
Tuan Hoang
Parsa Pourali wrote on Mon, 11 April 2016 20:36
Hi Tuan,

Thank you , I applied your suggestion but I don't still get it work ! I think I am implementing the keystroke method in some stupid class that has no relation to what I want to do : Confused

Where do you exactly implement the keystroke listener:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(new IPartListener2() { ...

I was reading your previous posts and thought that I need to use org.eclipse.ui.editors extension point to register the keystroke, but apparently it is not correct. Could you please let me know how do you register your keystroke class ? or what is the extension that I should use ? What do you mean by "viewer's KeyHandler" ?

Thank you for your time and concern,
Bests,
Parsa

Re: Add key listener on an element of diagram [message #1729248 is a reply to message #1729193] Tue, 12 April 2016 11:59 Go to previous message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 210
Registered: February 2014
Senior Member
Hi Tuan,

Thank you soooooooo much, it is working now : Smile

Thanks again,
Cheers,
Parsa
Previous Topic:Service to query itemprovider combobox elements when connecting reference based edge
Next Topic:Capturing/Listening to the start point of a Label Edit Event on a diagram
Goto Forum:
  


Current Time: Thu Apr 18 11:07:09 GMT 2024

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

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

Back to the top