Skip to main content
summaryrefslogtreecommitdiffstats
blob: 38dd69df652e7097b0f4f0056310221119b6ef5d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************************************* 
 * Copyright (c) 2012 TESIS DYNAware GmbH and others. 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 *     Torsten Sommer <torsten.sommer@tesis.de> - initial API and implementation 
 *******************************************************************************/
package org.eclipse.fx.ecp;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.beans.property.Property;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

import javax.inject.Inject;

import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecp.edit.ECPControlContext;
import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.Control;
import org.eclipse.fx.ecp.ui.ModelElementEditor;
import org.eclipse.fx.ecp.ui.Control.Factory;
import org.eclipse.fx.ecp.ui.Control.Factory.Registry;
import org.eclipse.fx.emf.databinding.edit.EMFEditFXProperties;


@SuppressWarnings("restriction")
public class ModelEditorPart implements ModelElementEditor {

	private ScrollPane scrollPane;
	private MPart part;
	private Map<EStructuralFeature, Control> controls = new HashMap<>();

	@Inject
	public ModelEditorPart(BorderPane parent, final MApplication application, MPart part) {
		this.part = part;
		// part.setCloseable(true);

		// ECPControlContext modelElementContext = new
		// DummyControlContext(DummyWorkspace.INSTANCE.getPlayer());
		// ECPControlContext modelElementContext = new
		// DummyControlContext(DummyWorkspace.INSTANCE.getTournament());
		// ECPControlContext modelElementContext = new
		// DummyControlContext(DummyWorkspace.INSTANCE.getReferee());

		scrollPane = new ScrollPane();
		scrollPane.setFitToWidth(true);
		scrollPane.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

		parent.setCenter(scrollPane);
	}

	public void setInput(final ECPControlContext modelElementContext) {
		ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
		AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(adapterFactory);
		EObject modelElement = modelElementContext.getModelElement();

		IItemLabelProvider labelProvider = (IItemLabelProvider) adapterFactory.adapt(modelElement, IItemLabelProvider.class);
		part.setLabel(labelProvider.getText(modelElement));
		Object image = labelProvider.getImage(modelElement);
		if (image instanceof URL)
			part.setIconURI(((URL) image).toExternalForm());

		List<IItemPropertyDescriptor> propertyDescriptors = adapterFactoryItemDelegator.getPropertyDescriptors(modelElement);
		// FormControlFactory controlFactory = new FormControlFactory();

		GridPane gridPane = new GridPane();
		gridPane.getStyleClass().add("theForm");
		// gridPane.setStyle("-fx-alignment: top-left;");

		// VBox vBox = new VBox();
		// vBox.getStyleClass().add("theForm");

		Button button = new Button("validate");
		button.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				Diagnostic diagnostic = Diagnostician.INSTANCE.validate(modelElementContext.getModelElement());
				for (Diagnostic childDiagnostic : diagnostic.getChildren()) {
					Control control = controls.get(childDiagnostic.getData().get(1));
					if(control != null)
						control.handleValidation(childDiagnostic);
				}
				
			}

		});

		gridPane.add(button, 1, 0);

		Registry registry = Control.Factory.Registry.INSTANCE;

		int i = 1;
		for (IItemPropertyDescriptor propertyDescriptor : propertyDescriptors) {

			String displayName = propertyDescriptor.getDisplayName(modelElement);
			Label label = new Label(displayName);
			label.getStyleClass().add("controlLabel");
			// label.setStyle("-fx-alignment: top-left;");
			GridPane.setValignment(label, VPos.TOP);
			gridPane.add(label, 0, i);

			EStructuralFeature feature = (EStructuralFeature) propertyDescriptor.getFeature(modelElement);

			Factory factory = registry.getFactory(feature, modelElement, feature.isMany());
			
			if (factory != null) {
				Property<?> property = EMFEditFXProperties.value(modelElementContext.getEditingDomain(), modelElementContext.getModelElement(), feature);
				Control control = factory.createControl(property, feature, modelElementContext);
				Node node = (Node) control;
				gridPane.add(node, 1, i);
				GridPane.setHgrow(node, Priority.ALWAYS);
				controls.put(feature, control);
			}
			i++;
		}

		Label label = new Label("The End.");
		gridPane.add(label, 0, i);

		scrollPane.setContent(gridPane);
	}

}

Back to the top