Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to change the label of a ToolControlImpl
How to change the label of a ToolControlImpl [message #1033286] Thu, 04 April 2013 04:17 Go to next message
Mark Leone is currently offline Mark LeoneFriend
Messages: 69
Registered: April 2012
Member
I have a ToolControlImpl added to the trim for various views, and I'm able to retrieve it with the model service. Now I want to do a couple things with it, and I haven't been able to find out how:

I need to get a reference to the Control that was created as a child of 'parent' in ToolControlImpl#createPartControl. It happens to be a Label in my case. I don't see any way to get access to it. I tried casting the retrieved model object to its actual class, which is a subclass of ToolControlImpl, but the cast fails. WHat I'm trying to do is dynamically change the label text and the listener on the Label inside the ToolControlImpl.

If I call getWidget() on the ToolControlImpl, I get a Composite, but its list of children is empty. Yet a Label was added to it when the part control was created. Here's the code for my ToolControlImpl:

public class CustomButton1 extends ToolControlImpl {

	private Label label;

	@PostConstruct
	public void createPartControl(Composite parent) {
		GridLayoutFactory.fillDefaults().applyTo(parent);
		label = new Label(parent, SWT.NONE);
		label.setText("Label");
		GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER);
		label.addMouseMoveListener(new MouseMoveListener() {

			@Override
			public void mouseMove(MouseEvent e) {
				System.err.println("MOUSE MOVE Label 1");
			}

		});
	}

	public Control getControl() {
		return this.label;
	}
}

[Updated on: Thu, 04 April 2013 04:19]

Report message to a moderator

Re: How to change the label of a ToolControlImpl [message #1033748 is a reply to message #1033286] Thu, 04 April 2013 15:43 Go to previous messageGo to next message
Mark Leone is currently offline Mark LeoneFriend
Messages: 69
Registered: April 2012
Member
So I think the problem is that the model service is not returning the proper ToolControlImpl. In the model editor, I add a ToolControl to

Application -> Windows -> Trimmed Window -> TrimBars -> Window Trim -> Toolbar
.

I also add a ToolControl to

Application -> Windows -> Trimmed Window -> Main Menu -> Controls -> Perspective Stack -> Perspective -> Controls -> PartSashContainer -> Part Stack -> Part -> Toolbar

for each of three Parts.

But when I search via the model service, I only find the ToolControl added to the toolbar of the TrimBars, and not the ones added to the toolbar of each Part, even though the Part's ToolControls are displayed and active.

I tried searching by element type

elements = modelService.findElements(application, null, ToolControlImpl.class, null)


This returns only the TrimBars ToolControl.

by ID:

elements = modelService.findElements(application, controlId ,null, null)


This returns the TrimBars ToolControl when I give it that ID, empty list when I give it the ID of either of the Part ToolControl items.

and by tag

elements = modelService.findElements(application, null, null, tags)


This returns an empty list when I specify a tag that was assigned to one of the Part's ToolControl items.

Re: How to access the wrapped Control of a ToolControlImpl [message #1034499 is a reply to message #1033748] Fri, 05 April 2013 13:56 Go to previous messageGo to next message
Mark Leone is currently offline Mark LeoneFriend
Messages: 69
Registered: April 2012
Member
Since no one has any ideas, I guess I should submit a bug report. It's very surprising that there is no way to reference the Control wrapped by a ToolControlImpl, so I thought I might be doing something wrong, as I'm fairly new to using the E4 APIs.

Can anyone confirm whether they see the same behavior, or that they don't?

Specifically:

1. If you add a ToolControlImpl to the Toolbar of a Part (not the TrimBars of a Window) are you able to retrieve a reference to it from the model service? If so, what arguments are you passing in the findElements method?

2. If you subclass ToolControlImpl and add it to the ToolBar of a Window's TrimBars or to a Part's ToolBar, and you're able to retrieve the instance via the model service, is the model element an instance of the subclass, or of ToolControlImpl?
Re: How to access the wrapped Control of a ToolControlImpl [message #1037455 is a reply to message #1034499] Tue, 09 April 2013 15:19 Go to previous messageGo to next message
Mark Leone is currently offline Mark LeoneFriend
Messages: 69
Registered: April 2012
Member
The failure to find the ToolControl instances through the model service is because of bumping into this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=383403

The model service does not find Menu and ToolBar elements (though it seems to find them for the TrimBars but not a Trimmed Window). The model service will be enhanced to support this, and a workaround for now is to walk the model explicitly.

The failure to obtain the correct instance of the ToolControl implementation was because I was using the wrong class in my filter specification when invoking ModelService#findElements. I needed to specify MToolControl.class and not ToolControlImpl.class. Then invoke getObject() on the MToolControl instance returned by the model service, and it returns an instance of the ToolControlImpl subclass I added to the ToolBar in the model editor.

A working demo with E3/E4 single-source implementation is here
Re: How to access the wrapped Control of a ToolControlImpl [message #1038196 is a reply to message #1037455] Wed, 10 April 2013 14:32 Go to previous messageGo to next message
Eclipse UserFriend
Can't you find the items if you supply the trim bar as the search root? Also I'm not sure it's a good idea to subclass the default implementations of MToolControl, and for any MXXXXXX for that matter.
Re: How to access the wrapped Control of a ToolControlImpl [message #1038228 is a reply to message #1038196] Wed, 10 April 2013 15:25 Go to previous message
Mark Leone is currently offline Mark LeoneFriend
Messages: 69
Registered: April 2012
Member
Thanks for the reply, Sopot. The ToolControl in the TrimBars' ToolBar actually is returned by ModelService#findElements, even with the Application instance as the search root. It's the ToolControl in the Window's ToolBar that I was unable to find this way. I tried with the Application and the Window as the root.

Regarding what I subclassed, I think it's a bit different than what you commented on. I extended ToolControlImpl (let's call it MyToolControl), and I specified MyToolControl as the class for the ToolControl I added to the ToolBar via the model editor. Then I retrieve an MToolControl instance from the model (by walking the model starting with the Window), and call getObject() on this MToolControl instance. The object returned is an instance of MyToolControl, which is what I was after. What I was doing wrong before was retrieving ToolControlImpl from the model (via the Model Service) instead of retrieving MToolControl and calling getObject(). I assume this approach is not problematic?
Previous Topic:Icon URI
Next Topic:IEclipseContextDebugger debugger
Goto Forum:
  


Current Time: Fri Mar 29 09:47:11 GMT 2024

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

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

Back to the top