Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Dynamically changing the label of a Part
Dynamically changing the label of a Part [message #948911] Thu, 18 October 2012 10:04 Go to next message
Istvan Meszaros is currently offline Istvan Meszaros
Messages: 41
Registered: October 2009
Member
Hello!

How do i change the label of a Part dynamically?

I tried to retrieve the MPart via injection or via the EPartService, and call setLabel() on it. It worked but the change was not reflected on the rendered part. How do i update the rendered part to apply the model change?

My Part contains a CTabFolder, and i would like to set the active tab's text as the Part label:

	public void showTab(String tab) {
		CTabItem item = tabs.get(tab);
		if (item != null) {
			tabFolder.setSelection(item);
			createContentsForActiveTab();
			partService.findPart(PART_ID).setLabel(item.getText());
		}
	}


Thanks!
Re: Dynamically changing the label of a Part [message #948939 is a reply to message #948911] Thu, 18 October 2012 10:32 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Are you in a handler? Try injecting the MPart directly.
Re: Dynamically changing the label of a Part [message #948962 is a reply to message #948939] Thu, 18 October 2012 10:54 Go to previous messageGo to next message
Istvan Meszaros is currently offline Istvan Meszaros
Messages: 41
Registered: October 2009
Member
I'm in the part implementation itself.
Re: Dynamically changing the label of a Part [message #948996 is a reply to message #948962] Thu, 18 October 2012 11:30 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 346
Registered: December 2010
Location: Germany
Senior Member
Try something like this:
public class PartOne {
	@Inject private MPart part;
	public void setPartLabel(String label) {
		part.setLabel(label);
	}
	
	...
}
Re: Dynamically changing the label of a Part [message #949636 is a reply to message #948996] Fri, 19 October 2012 02:28 Go to previous messageGo to next message
Istvan Meszaros is currently offline Istvan Meszaros
Messages: 41
Registered: October 2009
Member
Just figured out what the problem is. If i run the code from a handler, it works perfectly. I guess the framework can detect the change after running the handler. The only problem is that i need to change the label not only from a handler. There is a SelectionListener bound to the CTabFolder i use in the part, and the label change is triggered from there.

Is there any way to manually tell the framework to scan for model changes and apply them?
Re: Dynamically changing the label of a Part [message #949678 is a reply to message #949636] Fri, 19 October 2012 03:26 Go to previous message
Christoph Keimel is currently offline Christoph Keimel
Messages: 346
Registered: December 2010
Location: Germany
Senior Member
The framework will listen to model changes and apply them. Like Sopot said, you should inject the MPart directly into your contribution class.

Try this example:

public class MyPart {
	
	@Inject private MPart part;
	
	@PostConstruct
	public void postConstruct(Composite parent) {
		final Text text = new Text(parent, SWT.MULTI);
		text.setText("New Part Label");
		final Button btn = new Button(parent, SWT.PUSH);
		btn.setText("Change Part Label");
		btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				part.setLabel(text.getText());
			}
		});
	}
	
}
Previous Topic:Creating menu items at runtime
Next Topic:Problem with events and lazy part rendering
Goto Forum:
  


Current Time: Thu Aug 21 20:03:52 EDT 2014

Powered by FUDForum. Page generated in 0.01622 seconds