/
MvcLogoExampleView.java
263 lines (235 loc) · 9.46 KB
/
MvcLogoExampleView.java
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
* Copyright (c) 2014, 2016 itemis AG 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:
* Alexander Nyßen (itemis AG) - initial API and implementation
*
*******************************************************************************/
package org.eclipse.gef4.mvc.examples.logo.ui.view;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.gef4.common.adapt.AdapterKey;
import org.eclipse.gef4.fx.anchors.DynamicAnchor;
import org.eclipse.gef4.fx.nodes.Connection;
import org.eclipse.gef4.geometry.planar.Point;
import org.eclipse.gef4.mvc.examples.logo.MvcLogoExample;
import org.eclipse.gef4.mvc.examples.logo.MvcLogoExampleModule;
import org.eclipse.gef4.mvc.examples.logo.MvcLogoExampleViewersComposite;
import org.eclipse.gef4.mvc.examples.logo.model.FXGeometricCurve;
import org.eclipse.gef4.mvc.examples.logo.ui.MvcLogoExampleUiModule;
import org.eclipse.gef4.mvc.examples.logo.ui.properties.FXCurvePropertySource;
import org.eclipse.gef4.mvc.fx.ui.parts.AbstractFXView;
import org.eclipse.gef4.mvc.fx.viewer.FXViewer;
import org.eclipse.gef4.mvc.models.ContentModel;
import org.eclipse.gef4.mvc.operations.AbstractCompositeOperation;
import org.eclipse.gef4.mvc.operations.DeselectOperation;
import org.eclipse.gef4.mvc.operations.ForwardUndoCompositeOperation;
import org.eclipse.gef4.mvc.operations.ITransactionalOperation;
import org.eclipse.gef4.mvc.operations.SelectOperation;
import org.eclipse.gef4.mvc.parts.IContentPart;
import org.eclipse.gef4.mvc.ui.properties.SetPropertyValueOperation;
import org.eclipse.gef4.mvc.ui.properties.UndoablePropertySheetEntry;
import org.eclipse.gef4.mvc.ui.properties.UndoablePropertySheetPage;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import com.google.inject.Guice;
import com.google.inject.util.Modules;
import javafx.scene.Node;
import javafx.scene.Scene;
public class MvcLogoExampleView extends AbstractFXView {
public static final class ChangeWayPointsOperation extends AbstractOperation
implements ITransactionalOperation {
private final FXGeometricCurve curve;
private final List<Point> newWayPoints;
private final List<Point> oldWayPoints;
public ChangeWayPointsOperation(String label, FXGeometricCurve curve,
List<Point> oldWayPoints, List<Point> newWayPoints) {
super(label);
this.curve = curve;
this.oldWayPoints = oldWayPoints;
this.newWayPoints = newWayPoints;
}
@Override
public IStatus execute(IProgressMonitor monitor, IAdaptable info) {
curve.setWayPoints(newWayPoints.toArray(new Point[] {}));
return Status.OK_STATUS;
}
@Override
public boolean isContentRelevant() {
return true;
}
@Override
public boolean isNoOp() {
return oldWayPoints == newWayPoints || (oldWayPoints != null
&& oldWayPoints.equals(newWayPoints));
}
@Override
public IStatus redo(IProgressMonitor monitor, IAdaptable info) {
return execute(monitor, info);
}
@Override
public IStatus undo(IProgressMonitor monitor, IAdaptable info) {
curve.setWayPoints(oldWayPoints.toArray(new Point[] {}));
return Status.OK_STATUS;
}
}
private UndoablePropertySheetEntry rootEntry;
// TODO: create AbstractFXView via an executable extension factory
// (obtaining the injector via the bundle)
public MvcLogoExampleView() {
super(Guice.createInjector(Modules.override(new MvcLogoExampleModule())
.with(new MvcLogoExampleUiModule())));
// set initial contents
getContentViewer()
.getAdapter(ContentModel.class).getContents()
.setAll(MvcLogoExample.createDefaultContents());
getPaletteViewer()
.getAdapter(ContentModel.class).getContents()
.setAll(MvcLogoExample.createPaletteContents());
}
@Override
public void dispose() {
// clear viewer contents
getContentViewer().getAdapter(ContentModel.class).contentsProperty().clear();
getPaletteViewer().getAdapter(ContentModel.class).contentsProperty().clear();
super.dispose();
}
@SuppressWarnings("rawtypes")
@Override
public Object getAdapter(Class key) {
if (IPropertySheetPage.class.equals(key)) {
// use another UndoablePropertySheetEntry, which chains undo of
// waypoint removal
UndoablePropertySheetPage propertySheetPage = (UndoablePropertySheetPage) super.getAdapter(
key);
if (rootEntry == null) {
rootEntry = new UndoablePropertySheetEntry(
(IOperationHistory) getAdapter(IOperationHistory.class),
(IUndoContext) getAdapter(IUndoContext.class)) {
// FIXME: Code copied from FXBendConnectionPolicy (see
// #494752)
private Point computeEndHint(Connection connection) {
if (connection.getEndAnchor() instanceof DynamicAnchor
&& connection.getPointsUnmodifiable()
.size() > 1) {
Point endPoint = connection.getEndPoint();
Point neighbor = connection.getPoint(
connection.getPointsUnmodifiable().size()
- 2);
Point translated = endPoint.getTranslated(endPoint
.getDifference(neighbor).getScaled(0.5));
return translated;
}
return null;
}
// FIXME: Code copied from FXBendConnectionPolicy (see
// #494752)
private Point computeStartHint(Connection connection) {
if (connection.getStartAnchor() instanceof DynamicAnchor
&& connection.getPointsUnmodifiable()
.size() > 1) {
Point startPoint = connection.getStartPoint();
Point neighbor = connection.getPoint(1);
Point translated = startPoint.getTranslated(
startPoint.getDifference(neighbor)
.getScaled(0.5));
return translated;
}
return null;
}
@Override
public void setValues(Object[] objects) {
if (objects == null || objects.length == 0) {
// TODO: test
objects = new Object[] { getContentViewer()
.getAdapter(ContentModel.class)
.getContents().get(0) };
}
super.setValues(objects);
}
@Override
protected void valueChanged(
UndoablePropertySheetEntry child,
ITransactionalOperation operation) {
// in case routing style is changed, clear the
// waypoints (chain into a composite operation)
if (operation instanceof SetPropertyValueOperation) {
SetPropertyValueOperation changeRoutingStyleOperation = (SetPropertyValueOperation) operation;
if (changeRoutingStyleOperation
.getPropertySource() instanceof FXCurvePropertySource
&& FXCurvePropertySource.ROUTING_STYLE_PROPERTY
.getId()
.equals(changeRoutingStyleOperation
.getPropertyId())) {
// clear way anchors using bend policy
FXCurvePropertySource ps = (FXCurvePropertySource) changeRoutingStyleOperation
.getPropertySource();
IContentPart<Node, ? extends Node> contentPart = getContentViewer()
.getContentPartMap().get(ps.getCurve());
// preserve first and last waypoint, but clear
// all intermediate points
List<Point> newWaypoints = new ArrayList<>();
List<Point> currentWaypoints = ps.getCurve()
.getWayPointsCopy();
// FIXME: Code copied from
// FXBendConnectionPolicy (see #494752)
newWaypoints.add(computeStartHint(
(Connection) contentPart.getVisual()));
newWaypoints.add(computeEndHint(
(Connection) contentPart.getVisual()));
ChangeWayPointsOperation clearWaypointsOperation = new ChangeWayPointsOperation(
"Clear waypoints", ps.getCurve(),
currentWaypoints, newWaypoints);
AbstractCompositeOperation c = new ForwardUndoCompositeOperation(
"Change routing style");
c.add(changeRoutingStyleOperation);
c.add(clearWaypointsOperation);
// reselect
IUndoableOperation deselectOperation = new DeselectOperation<>(
getContentViewer(),
Collections.singletonList(contentPart));
IUndoableOperation selectOperation = new SelectOperation<>(
getContentViewer(),
Collections.singletonList(contentPart));
c.add(deselectOperation);
c.add(selectOperation);
super.valueChanged(child, c);
} else {
super.valueChanged(child, operation);
}
} else {
super.valueChanged(child, operation);
}
}
};
propertySheetPage.setRootEntry(rootEntry);
}
return propertySheetPage;
}
return super.getAdapter(key);
}
protected FXViewer getPaletteViewer() {
return getDomain().getAdapter(AdapterKey.get(FXViewer.class,
MvcLogoExampleModule.PALETTE_VIEWER_ROLE));
}
@Override
protected void hookViewers() {
// build viewers composite
MvcLogoExampleViewersComposite viewersComposite = new MvcLogoExampleViewersComposite(
getContentViewer(), getPaletteViewer());
// create scene and populate canvas
getCanvas().setScene(new Scene(viewersComposite.getComposite()));
}
}