Eclipse – GMF – Making Figures Sensitive To Attributes Of Semantic Elements

TASK:
Change a diagram editor generated by GMF to make its figures sensitive to changes of semantic elements’ attributes’ values. Changes of attributes’ values should be automatically detected and reflected in figures’ look.

SOLUTION:
You have to make a little change in *EditPart class. I’m going to change edit part for the semantic model element named Port, so I will edit my.package.diagram.edit.parts.PortEditPart class.

Add a new method to the PortFigure class, which is PortEditPart‘s inner class:

	public void updateFace() {
		Port port = (Port) ((Node) PortEditPart.this.getModel()).getElement();

		// set line width according to number of some children
		int lineWidth = 1;
		if (port.getLogicalUnits().size() > 1) {
			lineWidth = 2;
		}
		this.setLineWidth(lineWidth);

		// update tooltip
		String tooltipText;
		if (port.getDescription() != null
				&& port.getDescription().length() > 0) {
			tooltipText = port.getDescription();
		} else {
			tooltipText = port.getName();
		}
		if (getToolTip() == null) {
			setToolTip(new Label(tooltipText));
		} else if (getToolTip() instanceof Label) {
			((Label) getToolTip()).setText(tooltipText);
		}
	}

The updateFace method makes actual changes to the figure’s look. This example implementation sets the line’s width according to the number of Port’s children (port.getLogicalUnits().size()) and sets the tool-tip text to the value of element’s attribute named description.

Then you have to hook up this method in two places: PortFigure‘s constructor to update the look when editor starts and the handleNotificationEvent method to react to live changes.

Change PortFigure‘s constructor to make it use new method.

	/**
	 * @generated NOT
	 */
	public PortFigure() {
		this.setFill(true);
		this.setFillXOR(false);
		this.setOutline(true);
		this.setOutlineXOR(false);
		this.setLineStyle(Graphics.LINE_SOLID);
		//this.setLineWidth(1);
		//this.setForegroundColor(PORTFIGURE_FORE);
		//this.setBackgroundColor(PORTFIGURE_BACK);
		updateFace();
		createContents();
	}

Override handleNotificationEvent(Notification notification) method in PortEditPart.

	@Override
	protected void handleNotificationEvent(Notification notification) {
		if (notification.getNotifier() instanceof Port) {
			getPrimaryShape().updateFace();
		}
		super.handleNotificationEvent(notification);
	}

Without this method you would need to restart editor to reflect element’s changes in figure parameters.

Tested on GMF version 2.0.

2 thoughts on “Eclipse – GMF – Making Figures Sensitive To Attributes Of Semantic Elements”

  1. I’ve used these code snippets with success… But, when saving a diagram and reloading it again the figure is drawn on the canvas without the ‘modifications’ as specified in the updateFace() method, although the updateFace() method is called from the constructor of the figure… Do you experience the same problem as I’m describing here? Do know what the problem might be?

  2. Hi Ramon, may be its too late to answer your problem, but it will be useful for others who may facing this issue.
    The solution to your problem is call the “updateFace()” method from your editpart’s “activate()” method:
    public void activate() {
    getPrimaryShape().updateFace();
    super.activate();
    }

Comments are closed.