Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » How to enable "Edit" actions in GraphicalEditor?(I'm facing the problem how to make the edit actions (copy, paste, find, ...) enabled in graphical editor)
How to enable "Edit" actions in GraphicalEditor? [message #1809891] Thu, 25 July 2019 11:05
M. J. is currently offline M. J.Friend
Messages: 6
Registered: September 2018
Junior Member
Hello,


I'm facing quite strange problem. I'm having muti-page editor ( MultiPageEditorPart ) in eclipse Oxygen 3.a Plug-in application, we're using 3.* API. It contains two editors, first one is StructuredTextEditor , the second one is child of GraphicalEditor . The idea is to have both textual and graphical editor for the data file (it's like HTML editor with both source editor and the WYSIWYG editor).

The source viewer works just fine, however, when I switch to the graphical editor. all the editor actions like undo, redo, copy, paste, find & replace does not work. The corresponding key shortcuts does nothing and if I open the Edit menu, all the items are beeing disabled. I would like to have such actions to be working in the same manner as in the source editor.

I've created testing project (with the only graphical editor) and spent a few days by trying, looking for similar projects and inspecting how they have the graphical editor implemented. My first surprise - absolutelly not all the projects I found (about twenty of them from various examples and tutorials, eclipse marketplace, github, sourceforge) have this feature working. This lead me to the idea it may be somehow tricky to make it working.

However, after some reverse engeneering I was able to make the actions enabled. The Undo and Redo actions works 100% fine (even really undoes and redoes), however the remaining does not work. I've tryied many things (like to extract actions from the text editor, use menu contributions, setup various kinds of actions in many places, try to setEnabled(true) mostly everywhere) and finally found a way. To have them working, I found out I have to do two things. First is to specify each action in the editor's createActions

public class MyEditorGraphicalEditorPart extends GraphicalEditor {
	// ...

	@Override
	protected void createActions() {
		super.createActions();

		ActionRegistry registry = this.getActionRegistry();

		registry.registerAction(new MyEditorSomeSelectionAction(this));

	}
	// ...
}


where the MyEditorSomeSelectionAction looks like:
public static final class MyEditorSomeSelectionAction extends SelectionAction {
	public MyEditorSomeSelectionAction(IWorkbenchPart part) {
		super(part);
		this.setId(ActionFactory.XYZ.getId()); // replace XYZ with FIND, COPY, PASTE, DELETE etc.
	}

	@Override
	protected boolean calculateEnabled() {
		return true;
	}

	@Override
	public void run() {
		System.out.println("This will be executed");
	}
}


The second required thing is to specify the ActionBarContributor :
public static final class MyEditorSomeSelectionAction extends SelectionAction {
	// ...

	private PlayingEditorActionBarContributor actionBarContributor;
	// ...

	@Override
	protected void initializeGraphicalViewer() {

		actionBarContributor = new MyEditorActionBarContributor(this);
		actionBarContributor.init(this.getEditorSite().getActionBars(), this.getSite().getPage());
	}
	// ...
}


which looks like:
public class MyEditorActionBarContributor extends ActionBarContributor {
	// ...
	@Override
	protected void buildActions() {
		addRetargetAction(new MyEditorSomeRetargetAction());
	}
	// ...
}


and:
public class MyEditorSomeRetargetAction extends RetargetAction {

	public MyEditorSomeRetargetAction() {
		super(ActionFactory.XYZ.getId(), "Do something"); // replace XYZ with FIND, COPY, PASTE, DELETE etc.
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}

From my observations only this combination makes the action "working" (enabled). Obviously, they're just outputing that the action have been executed (the System.out.println in the MyEditorSomeSelectionAction#run()).

I tried to try to use eclipse classes (FindReplaceAction, PasteAction, etc.) but they didn't work. Some of them didn't even make the action enabled, some of them did, but its execution did nothing.

For example - the FindReplaceAction: It was enabled, but did nothing. I've inspected the code, of the action and:
public class FindReplaceAction extends ResourceAction implements IUpdate {
	// ...
	private IFindReplaceTarget fTarget;
	// ...
	@Override
	public void run() {
		if (fTarget == null)
			return;

		// ...

		dialog.open();
	}
	// ...
	@Override
	public void update() {
		// ...
				fWorkbenchPart= fWorkbenchWindow.getPartService().getActivePart();
		// ...
				fTarget= fWorkbenchPart.getAdapter(IFindReplaceTarget.class);
		// ...
	}
}

That means the action does nothing if the action's find-replace target is not beeing set. The target is obtained from the part via getAdapter . I simply tricky hacky added support of that:
public class MyEditorGraphicalEditorPart extends GraphicalEditor {
	// ...
	@Override
	public Object getAdapter(Class type) {
		if (type.equals(IFindReplaceTarget.class)) {
			return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getEditorReferences()[1]
				.getEditor(true).getAdapter(IFindReplaceTarget.class);
		}
		
		return super.getAdapter(type);
	}
	// ...
}

Now the condition is met and the dialog pops up. But still does not search - even when the find-replace target points to existing text editor.

My question is whether it really is so complicated to add support of such actions in the graphical editor, even when it is just "an different view" to source file. Also, if there some hints how to solve it and make the graphical editor behave the same as the source one.

I know it is possible to implement all the mine actions from scratch, but I'd like to avoid that. Why to implement copy, paste and find, while it is already implemented in the source editor, which is linked with the graphical?
Previous Topic:GEF 5.1.0 is released!
Next Topic:Upgrade from GEF 3.11.0 to 5.1.0
Goto Forum:
  


Current Time: Sun Sep 25 14:50:09 GMT 2022

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

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

Back to the top