Full circle implementation of the part property bridge idea

The last posting on this subject showed you how you can publish properties to other components using only Eclipse level API's. I got a few inquiries on how the Eclipse view part can receive property changes from other components.

Here is the complete round trip implementation. The view can receive property changes from the bridge using a part property listener – check out the code below. When the bridge needs to tell the eclipse part a property is changed it simple calls setPartProperty() which is part of the IWorkbenchPart3 interface. So this means your code only needs to register itself as a part property listener and process the changes as they come in.

So in short, to publish a property your part would call:

firePartPropertyChanged(propertyName.getText(), oldValue, propertyValue.getText());

To receive a property from another part you would implement a listener to yourself:

addPartPropertyListener(this);

public void propertyChange(PropertyChangeEvent arg0) {

int idx = getItemIndex(arg0.getProperty());

if (idx == -1){
propertyName.add(arg0.getProperty());
idx = getItemIndex(arg0.getProperty());
}

propertyName.select(idx);

propertyValue.setText(arg0.getNewValue() != null ? arg0.getNewValue().toString() : “”);
}

Here is the full source:

package net.balfes.partprop.bridge.views;

import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Combo;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.Text;import org.eclipse.ui.part.*;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.SelectionListener;import org.eclipse.swt.layout.RowData;import org.eclipse.swt.layout.RowLayout;import org.eclipse.swt.SWT;

public class PropertyBridgeTest extends ViewPart implements IPropertyChangeListener {

	Combo propertyName = null;	Text propertyValue = null;	String oldValue = new String();

	public PropertyBridgeTest() {	}

	public void createPartControl(Composite parent) {

		RowLayout rl = new RowLayout(SWT.VERTICAL);

		parent.setLayout(rl);

		Label l = new Label(parent, SWT.NONE);		l.setText("Enter property name");

		RowData rd = new RowData();		rd.width = 300;

		propertyName = new Combo(parent, SWT.READ_ONLY);		propertyName.setLayoutData(rd);

		l = new Label(parent, SWT.NONE);		l.setText("Enter property value");

		propertyValue = new Text(parent, SWT.NONE);		propertyValue.setLayoutData(rd);

		Button b = new Button(parent, SWT.PUSH);		b.setText("Publish");

		b.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent arg0) {

			}

			public void widgetSelected(SelectionEvent arg0) {				firePartPropertyChanged(propertyName.getText(), oldValue, propertyValue.getText());

				oldValue = propertyValue.getText();			}

		});

		addPartPropertyListener(this);

		//Setup what properties are in our view		createPartProperties();			}

	private void createPartProperties() {		this.setPartProperty("Name", "Bob Balfe");		this.setPartProperty("Phone", "555-543-1212");		this.setPartProperty("Manager", "George Bush");		this.setPartProperty("Computer", "Lenovo");	}

	public void setFocus() {			}

	int getItemIndex(String name){		String[] vals = propertyName.getItems();

		for (int x=0; x<vals.length; x++)			if (vals[x].equals(name))				return x;

		return -1;	}	public void propertyChange(PropertyChangeEvent arg0) {		int idx = getItemIndex(arg0.getProperty());

		if (idx == -1){			propertyName.add(arg0.getProperty());			idx = getItemIndex(arg0.getProperty());		}

		propertyName.select(idx);

		propertyValue.setText(arg0.getNewValue() != null ? arg0.getNewValue().toString() : "");	}

}
Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.