Skip to main content
summaryrefslogtreecommitdiffstats
blob: 887f7b96b32e7ac27eb79dead041cf56bc274004 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.eclipse.fx.emf.edit.ui;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Cell;
import javafx.scene.control.TextField;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.fx.emf.edit.ui.AdapterFactoryCellFactory.ICellEditHandler;


public class EAttributeCellEditHandler implements ICellEditHandler {

	EAttribute attribute;
	EditingDomain editingDomain;
	TextField textField;

	public EAttributeCellEditHandler(EAttribute attribute, EditingDomain editingDomain) {
		this.attribute = attribute;
		this.editingDomain = editingDomain;
	}

	@Override
	public boolean canEdit(Cell<?> treeCell) {
		Object item = treeCell.getItem();
		return item instanceof EObject && ((EObject) item).eClass().getEAllAttributes().contains(attribute);
	}

	@Override
	public void startEdit(final Cell<?> cell) {
		EObject item = (EObject) cell.getItem();
		String string = EcoreUtil.convertToString(attribute.getEAttributeType(), item.eGet(attribute));
		textField = new TextField();
		textField.focusedProperty().addListener(new ChangeListener<Boolean>() {

			@Override
			public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
				if (!newValue)
					commitEdit(cell, textField.getText());
			}

		});
		textField.setText(string);
		cell.setText(null);
		cell.setGraphic(textField);
		textField.selectAll();
	}

	@Override
	public void cancelEdit(Cell<?> treeCell) {
		// do nothing
	}

	@Override
	public void commitEdit(Cell<?> treeCell, Object newValue) {
		Object item = treeCell.getItem();
		Object value = EcoreUtil.createFromString(attribute.getEAttributeType(), (String) newValue);
		Command command = SetCommand.create(editingDomain, item, attribute, value);
		if (command.canExecute())
			editingDomain.getCommandStack().execute(command);
	}

}

Back to the top