Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Listen to selection changes in properties view
Listen to selection changes in properties view [message #507948] Fri, 15 January 2010 09:48 Go to next message
mnhg is currently offline mnhgFriend
Messages: 3
Registered: January 2010
Junior Member
How can I add a listener (and which one) to listen to selection changes in the default properties view. There will no modification, just a selection change.

I tried the IPropertyChangeListener, but this does't seems to work.


mnhg
Re: Listen to selection changes in properties view [message #509006 is a reply to message #507948] Thu, 21 January 2010 05:37 Go to previous messageGo to next message
mnhg is currently offline mnhgFriend
Messages: 3
Registered: January 2010
Junior Member
This problem ist still open. I even tried to get the tree inside the view and listen to its selection change. This works so far, but there ist nur way to get back from a tree item to its property source or the original object in the view.

I really need some suggestions, otherwises I have to implement my own property view and this ist not my prefered solution, because i need no other additional feature which would justify to build a new view.

Martin
Re: Listen to selection changes in properties view [message #509389 is a reply to message #507948] Fri, 22 January 2010 10:52 Go to previous messageGo to next message
mnhg is currently offline mnhgFriend
Messages: 3
Registered: January 2010
Junior Member
I found a solution, which seems to work.

First i created my own SelectionProvider to get the selection from my treeviewer and from the propertyview in one provider.

TreeViewer treeViewer=....

MergedSelectionProvider selectionProvider = new MergedSelectionProvider();

treeViewer.addSelectionChangedListener(new ViewerSelectionForwarder(selectionProvider, treeViewer));

getSite().getPage().addSelectionListener(new PropertySelectionForwarder(selectionProvider, treeViewer));

getSite().setSelectionProvider(selectionProvider);





public class MergedSelectionProvider implements ISelectionProvider {
	Set<ISelectionChangedListener> listeners = new HashSet<ISelectionChangedListener>();
	IStructuredSelection selection = new StructuredSelection();

	@Override
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		listeners.add(listener);

	}

	@Override
	public void removeSelectionChangedListener(
			ISelectionChangedListener listener) {
		// listeners.remove(listener);
		// Not allowed do remove any listener, otherwise nobody listen to the property view
	}

	@Override
	public ISelection getSelection() {
		return selection;
	}

	@Override
	public void setSelection(ISelection selection) {
		setSelection(this, selection);
	}

	public void setSelection(ISelectionProvider provider, ISelection selection) {
		if (selection != this.selection) {
			this.selection = (IStructuredSelection) selection;

			fireSelectionChanged(new SelectionChangedEvent(provider,
					this.selection));
		}
	}

	private void fireSelectionChanged(final SelectionChangedEvent event) {
		for (final ISelectionChangedListener listener : listeners) {
			SafeRunnable.run(new SafeRunnable() {
				public void run() {
					listener.selectionChanged(event);
				}
			});
		}
	}

}





The following class only forwards the selection changes of the viewer.

public class ViewerSelectionForwarder implements ISelectionChangedListener {

	MergedSelectionProvider provider;
	Viewer viewer;

	public ViewerSelectionForwarder(MergedSelectionProvider provider,
			Viewer viewer) {

		this.provider = provider;
		this.viewer = viewer;
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event) {

		provider.setSelection(viewer, event.getSelection());
		
	}
}


This class get the selection in the property view directly from the tree control. In my conrete scenario i could get by the name of the tree item and the selected parent element in the viewer to the selected attribute. It's not the nices solution but it's seem to work for my case.

public class PropertySelectionForwarder implements ISelectionListener,
		SelectionListener {

	Set<PropertySheet> properties = new HashSet<PropertySheet>();

	Viewer viewer;
	MergedSelectionProvider provider;

	public PropertySelectionForwarder(MergedSelectionProvider provider,
			Viewer viewer) {
		this.viewer = viewer;
		this.provider = provider;
	}

	// Register property view 
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (part instanceof PropertySheet && !properties.contains(part)) {
			PropertySheetPage p = (PropertySheetPage) ((PropertySheet) part)
					.getCurrentPage();
			Tree t = (Tree) p.getControl();
			t.addSelectionListener(this);
			properties.add((PropertySheet) part);

		}
	}

	// selection change in property view
	@Override
	public void widgetDefaultSelected(SelectionEvent e) { // ignore

	}

	@Override
	public void widgetSelected(SelectionEvent e) {

		if (!(e.item instanceof TreeItem)
				|| !(viewer.getSelection() instanceof IStructuredSelection)) {
			fireSelection(new StructuredSelection());
			return;
		}

		IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
		if (sel.size() != 1
				|| !(sel.getFirstElement() instanceof XMLEditorElement)) {
			fireSelection(new StructuredSelection());
			return;

		}

		TreeItem t = (TreeItem) e.item;

		if (!(t.getData() instanceof PropertySheetEntry)) {
			fireSelection(new StructuredSelection());
			return;

		}

		XMLEditorElement parent = (XMLEditorElement) sel.getFirstElement();
		PropertySheetEntry pse = (PropertySheetEntry) t.getData();
		XMLEditorAttribute attribute = parent.getAttributeByName(pse
				.getDisplayName());
		fireSelection(new StructuredSelection(attribute));

	}

	private void fireSelection(IStructuredSelection selection) {
		provider.setSelection(selection);
	}

}




Any opinions, suggestions or error?

Re: Listen to selection changes in properties view [message #731291 is a reply to message #509389] Fri, 30 September 2011 14:27 Go to previous message
Martin BurgerFriend
Messages: 21
Registered: July 2009
Junior Member
This is by design: http://stackoverflow.com/questions/6073869/rcp-selectionservice-question/7611738#7611738
Previous Topic:how to add a status line to org.eclipse.jface.wizard
Next Topic:Wizards + IRunnableWithProgress + Avoiding disabling controls
Goto Forum:
  


Current Time: Fri Apr 26 06:23:52 GMT 2024

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

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

Back to the top