Skip to main content



      Home
Home » Eclipse Projects » Eclipse 4 » Edit a part context with an handler([Beginner])
Edit a part context with an handler [message #833302] Sat, 31 March 2012 04:38 Go to next message
Eclipse UserFriend
Goodmorning everyone,
I've got a problem.

I have an application with one Part who is linked to class editor.java
In this part there is a textarea and I'm trying to create a button menu who clean the textarea.

In editor.java there is this method:
  @Inject
	@Optional
	public void setEditorTxt( @Named("setTxt") String text) {
		txt.setText(text);
	}


and in button's handler I've added this method:
        @Execute
	public <MApplication> void execute() {
		MPart editor = partService.findPart("org.unifi.inf.tds.editorpart");
		IEclipseContext cEditor = editor.getContext();
		cEditor.set("setTxt", "");		

	}


First time I use the button, it works. Next time it doen't nothing. Sad

Any suggestions?

Very thank you,
regards.
Re: Edit a part context with an handler [message #833392 is a reply to message #833302] Sat, 31 March 2012 07:56 Go to previous messageGo to next message
Eclipse UserFriend
My guess, without looking at the code, is that your second set is being treated as a null op by the context as the value hasn't changed. There's no point firing events if nothing's different, right?

If your underlying text area has changed, then you need to feed the changed value back into the context.

Otherwise consider using the IEventBroker instead: it's intended for communicating events. The IEclipseContext is for storing and retrieving values, and which. Happens to trigger events when those values change.

Brian.
Re: Edit a part context with an handler [message #833557 is a reply to message #833302] Sat, 31 March 2012 13:52 Go to previous messageGo to next message
Eclipse UserFriend
A better way to do it as Brian suggested is to use IEventBroker.

Put this in the part class:
@Inject
public void clear(@Optional @UIEventTopic("CLEAR_IT") Object dummy){
     if (txt!=null)
     txt.setText("");
}


and this in the handler:
        @Inject
	IEventBroker eventBroker;
	
	@Execute
	public void execute() {	
		eventBroker.post("CLEAR_IT", null);
	}


As to why your code doesn't work it's probably because the value in the context doesn't change and so it doesn't trigger a notification to the requester. But that isn't the proper way to handle events, IEventBroker is.
Re: Edit a part context with an handler [message #834158 is a reply to message #833557] Sun, 01 April 2012 09:54 Go to previous messageGo to next message
Eclipse UserFriend
Ok, very thank you for your reply.

Tomorrow I'll try this solution.
Re: Edit a part context with an handler [message #834680 is a reply to message #833557] Mon, 02 April 2012 04:56 Go to previous messageGo to next message
Eclipse UserFriend
Sopot Cela wrote on Sat, 31 March 2012 19:52

Put this in the part class:
@Inject
public void clear(@Optional @UIEventTopic("CLEAR_IT") Object dummy){
     if (txt!=null)
     txt.setText("");
}


and this in the handler:
        @Inject
	IEventBroker eventBroker;
	
	@Execute
	public void execute() {	
		eventBroker.post("CLEAR_IT", null);
	}



I've tried to do this but method clear not be executed. eventBroker.post("CLEAR_IT", null); returns true but nothing happens.

Thank you for your kind,
TDS.
Re: Edit a part context with an handler [message #834726 is a reply to message #834680] Mon, 02 April 2012 06:12 Go to previous messageGo to next message
Eclipse UserFriend
Please post the full part and handler code.
Re: Edit a part context with an handler [message #837121 is a reply to message #833302] Thu, 05 April 2012 05:43 Go to previous messageGo to next message
Eclipse UserFriend
Finally, it works.
Thanks to all for help.

Now I'm going to create save handler. Confused
Re: Edit a part context with an handler [message #856125 is a reply to message #837121] Wed, 25 April 2012 07:45 Go to previous messageGo to next message
Eclipse UserFriend
I'll just point out that your handler should not inject any fields. Handlers need to run in ever changing contexts, but they'll only be instantiated in one.

The correct pattern would be:

@Execute
public void execute(IEventBroker eventBroker) {
....
}


The same thing would have applied for getting the MPart or the MApplication during execution. Ask for it in the parameters of the execute method.

PW
Re: Edit a part context with an handler [message #857450 is a reply to message #856125] Thu, 26 April 2012 10:49 Go to previous messageGo to next message
Eclipse UserFriend
I'm working again with part.
I want to create an infrastructure that opens part and gives them some information in a class called "EditorMaker".

I've seen this example on Vogella:
@Execute
	public void open(
			MyInput input
			MApplication application, 
			EModelService modelService, 
			EPartService partService) {
		// Assuming that all editors should open in the the stack with the
		// ID "org.eclipse.e4.primaryDataStack"
		
		MPartStack stack = (MPartStack) modelService.find("org.eclipse.e4.primaryDataStack", application);
		
		MInputPart part = MBasicFactory.INSTANCE.createInputPart();
		// Pointing to the contributing class
		part.setContributionURI("bundleclass://de.vogella.rcp.e4.todo/de.vogella.rcp.e4.parts.Part1");
		part.setInputURI(input.getInputURI());
		part.setIconURI("platform:/plugin/de.vogella.rcp.e4.todo/icons/sample.gif");
		part.setLabel(input.getName());
		part.setTooltip(input.getTooltip());
		part.setCloseable(true);
		stack.getChildren().add(part);
		partService.showPart(part, PartState.ACTIVATE);
	}

so I've tryed to do a code like that in my part's class:
@PostConstruct
	public void createControls(Composite parent, EditorMaker em) {   
		System.out.println(em!=null);
	}

EditorMaker is created by the handler which open the part.
But I've got this error:
org.eclipse.e4.core.di.InjectionException: Unable to process "Editor#createControls()": no actual value was found for the argument "EditorMaker".
Where is the mistake?

Thank you.
Re: Edit a part context with an handler [message #857472 is a reply to message #857450] Thu, 26 April 2012 11:10 Go to previous messageGo to next message
Eclipse UserFriend
That error shows that there is no value in the context which is an instance of EditorMarker. Putting 'em' as a parameter of a method annotated with @PostConstruct tells the framework to look for an instance of EditorMarker in the contexts up to the OSGi layer. Thus it does not make sense creating it in the handler and getting it injected at the same time.

In order for the injection to work, at some moment you should put it on a context accessible by the handler.
Re: Edit a part context with an handler [message #860749 is a reply to message #857472] Sat, 28 April 2012 03:20 Go to previous messageGo to next message
Eclipse UserFriend
I understand what do you say, so how can realize my idea?
Re: Edit a part context with an handler [message #870605 is a reply to message #860749] Tue, 08 May 2012 19:36 Go to previous message
Eclipse UserFriend
in the handler method annotated with @Execute (similar to the open() method above), before creating the part, add
application.getContext().set(EditorMarker.class,yourEditorMarkerInstance);


Previous Topic:Eclipse 4 Training in the Netherlands (English)
Next Topic:WorkbenchAdvisor in an RCP e4 Application
Goto Forum:
  


Current Time: Wed Jul 23 06:49:28 EDT 2025

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

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

Back to the top