Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7c0767ea3e8db4e651b339e4e37208b95bc56ded (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.eclipse.fx.ecp.ui.form;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import javafx.animation.Transition;
import javafx.beans.property.ObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.SkinBase;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import jidefx.animation.AnimationType;
import jidefx.animation.AnimationUtils;
import jidefx.scene.control.decoration.DecorationPane;
import jidefx.scene.control.decoration.DecorationUtils;
import jidefx.scene.control.decoration.Decorator;
import jidefx.scene.control.decoration.MutableDecorator;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
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.edit.provider.AdapterFactoryItemDelegator;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.fx.ecp.ui.ECPControl;
import org.eclipse.fx.ecp.ui.ECPControlContext;
import org.eclipse.fx.ecp.ui.ECPControl.Factory.Registry;
import org.eclipse.fx.ecp.ui.controls.ControlDecoration;
import org.eclipse.fx.ecp.ui.controls.ECPControlBase;

public class DefaultModelElementForm extends Control implements ModelElementForm {

	ECPControlContext modelElementContext;
	private Map<EStructuralFeature, ValidationDecoration> controls = new HashMap<>();

	public DefaultModelElementForm(final ECPControlContext modelElementContext) {
		this.modelElementContext = modelElementContext;
		setSkin(new Skin(this));
		getStyleClass().add("model-element-form");

		modelElementContext.getModelElement().eAdapters().add(new AdapterImpl() {

			@Override
			public void notifyChanged(Notification msg) {
				validate();
			}

		});

		validate();
	}

	protected void validate() {
		Diagnostic diagnostic = Diagnostician.INSTANCE.validate(modelElementContext.getModelElement());
		for (ValidationDecoration controlDecoration : controls.values())
			controlDecoration.handleValidation(diagnostic);
	}

	public class Skin extends SkinBase<DefaultModelElementForm> {

		private GridPane gridPane;

		protected Skin(DefaultModelElementForm modelElementForm) {
			super(modelElementForm);

			gridPane = new GridPane();
			getChildren().add(new DecorationPane(gridPane));
			gridPane.getStyleClass().add("grid");

			EObject modelElement = modelElementContext.getModelElement();
			ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
			AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(adapterFactory);

			List<IItemPropertyDescriptor> propertyDescriptors = adapterFactoryItemDelegator.getPropertyDescriptors(modelElement);

			if (propertyDescriptors != null) {

				Registry registry = ECPControl.Factory.Registry.INSTANCE;

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

					String displayName = propertyDescriptor.getDisplayName(modelElement);
					Label label = new Label(displayName);
					label.getStyleClass().add("control-label");
					GridPane.setValignment(label, VPos.TOP);
					gridPane.add(label, 0, i);

					EStructuralFeature feature = (EStructuralFeature) propertyDescriptor.getFeature(modelElement);
					ECPControl.Factory factory = registry.getFactory(propertyDescriptor, modelElement);

					if (factory != null) {
						ECPControlBase control = factory.createControl(propertyDescriptor, modelElementContext);
						gridPane.add(control, 1, i);

						GridPane.setHgrow(control, Priority.ALWAYS);

						ValidationDecoration validationDecorator = new ValidationDecoration(control, feature);
//						DecorationUtils.install(control, validationDecorator);

						// AnchorPane.setLeftAnchor(control, 0.0);
						// AnchorPane.setRightAnchor(control, 0.0);
						//
						// ControlDecoration controlDecoration = new ControlDecoration(feature, control);
						 controls.put(feature, validationDecorator);

					}

					i++;
				}

			}

		}

	}

	@Override
	protected String getUserAgentStylesheet() {
		return getClass().getResource("../controls/ECPControls.css").toExternalForm();
	}

	@Override
	public void dispose() {
//		for (ControlDecoration controlDecoration : controls.values()) {
//			controlDecoration.dispose();
//		}
	}

	public static class Factory implements ModelElementForm.Factory {

		@Override
		public Node createModelElementForm(ECPControlContext modelElementContext) {
			return new DefaultModelElementForm(modelElementContext);
		}

	}

	public static class ValidationDecorator extends MutableDecorator<Node> {

		Image infoImage = new Image(Decorator.class.getResourceAsStream("overlay_info.png"));
		Image warningImage = new Image(Decorator.class.getResourceAsStream("overlay_warning.png"));
		Image errorImage = new Image(Decorator.class.getResourceAsStream("overlay_error.png"));

		public ValidationDecorator() {
			ImageView imageView = new ImageView(errorImage);
			Label label = new Label(null, imageView);
			nodeProperty().set(label);
			
			label.setOpacity(0);

			posProperty().set(Pos.TOP_LEFT);

			Transition transition = AnimationUtils.createTransition(label, AnimationType.BOUNCE_IN);
			
			transitionProperty().set(transition);
			
			paddingProperty().set(new Insets(0));
		}

	}

}

Back to the top