-
Notifications
You must be signed in to change notification settings - Fork 23
/
TypeEditPart.java
270 lines (237 loc) · 9 KB
/
TypeEditPart.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
264
265
266
267
268
269
270
/*******************************************************************************
* Copyright (c) 2011, 2024 Profactor GmbH, TU Wien ACIN, fortiss GmbH,
* Johannes Kepler University
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Gerhard Ebenhofer, Alois Zoitl
* - initial API and implementation and/or initial documentation
* Alois Zoitl - moved adapter search code to palette
* Alois Zoitl - added diagram font preference
* - added selection feedback
*******************************************************************************/
package org.eclipse.fordiac.ide.fbtypeeditor.editparts;
import java.util.List;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.fordiac.ide.gef.annotation.AnnotableGraphicalEditPart;
import org.eclipse.fordiac.ide.gef.annotation.FordiacAnnotationUtil;
import org.eclipse.fordiac.ide.gef.annotation.GraphicalAnnotationModelEvent;
import org.eclipse.fordiac.ide.gef.annotation.GraphicalAnnotationStyles;
import org.eclipse.fordiac.ide.gef.editparts.AbstractDirectEditableEditPart;
import org.eclipse.fordiac.ide.gef.editparts.ComboCellEditorLocator;
import org.eclipse.fordiac.ide.gef.editparts.ComboDirectEditManager;
import org.eclipse.fordiac.ide.gef.editparts.FigureCellEditorLocator;
import org.eclipse.fordiac.ide.gef.editparts.TypeDeclarationDirectEditManager;
import org.eclipse.fordiac.ide.gef.listeners.DiagramFontChangeListener;
import org.eclipse.fordiac.ide.gef.listeners.IFontUpdateListener;
import org.eclipse.fordiac.ide.gef.policies.ModifiedNonResizeableEditPolicy;
import org.eclipse.fordiac.ide.model.commands.change.ChangeDataTypeCommand;
import org.eclipse.fordiac.ide.model.data.DataType;
import org.eclipse.fordiac.ide.model.libraryElement.AdapterDeclaration;
import org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement;
import org.eclipse.fordiac.ide.model.libraryElement.INamedElement;
import org.eclipse.fordiac.ide.model.libraryElement.VarDeclaration;
import org.eclipse.fordiac.ide.model.libraryElement.impl.ErrorMarkerDataTypeImpl;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibrary;
import org.eclipse.fordiac.ide.ui.preferences.PreferenceConstants;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.swt.custom.CCombo;
public class TypeEditPart extends AbstractInterfaceElementEditPart implements AnnotableGraphicalEditPart {
private final TypeLibrary typeLib;
private DiagramFontChangeListener fontChangeListener;
public TypeEditPart(final TypeLibrary typeLib) {
this.typeLib = typeLib;
}
@Override
public void activate() {
super.activate();
JFaceResources.getFontRegistry().addListener(getFontChangeListener());
}
@Override
public void deactivate() {
super.deactivate();
JFaceResources.getFontRegistry().removeListener(getFontChangeListener());
}
@Override
protected Adapter createAdapter() {
return new AdapterImpl() {
@Override
public void notifyChanged(final Notification notification) {
super.notifyChanged(notification);
refresh();
}
};
}
private IPropertyChangeListener getFontChangeListener() {
if (null == fontChangeListener) {
fontChangeListener = new DiagramFontChangeListener(getFigure());
}
return fontChangeListener;
}
private static class TypeFigure extends Label implements IFontUpdateListener {
public TypeFigure() {
setTypeLabelFonts();
}
@Override
public void updateFonts() {
setTypeLabelFonts();
invalidateTree();
revalidate();
}
private void setTypeLabelFonts() {
setFont(JFaceResources.getFontRegistry().getItalic(PreferenceConstants.DIAGRAM_FONT));
}
}
@Override
public IInterfaceElement getCastedModel() {
return getModel().getReferencedElement();
}
@Override
public TypeField getModel() {
return (TypeField) super.getModel();
}
@Override
protected IFigure createFigure() {
final TypeFigure fig = new TypeFigure();
updateFigure(fig);
return fig;
}
@Override
public TypeFigure getFigure() {
return (TypeFigure) super.getFigure();
}
@Override
protected void refreshVisuals() {
super.refreshVisuals();
updateFigure(getFigure());
}
private void updateFigure(final TypeFigure typeFigure) {
typeFigure.setText(getTypeName());
}
@Override
public void updateAnnotations(final GraphicalAnnotationModelEvent event) {
GraphicalAnnotationStyles.updateAnnotationFeedback(getFigure(), getModel().getReferencedElement(), event,
FordiacAnnotationUtil::showOnTargetType);
}
private String getTypeName() {
return getModel().getLabel();
}
@Override
protected void createEditPolicies() {
final ModifiedNonResizeableEditPolicy handle = new ModifiedNonResizeableEditPolicy();
handle.setDragAllowed(false);
installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, handle);
if (isDirectEditable()) {
installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new DirectEditPolicy() {
@Override
protected void showCurrentEditValue(final DirectEditRequest request) {
// nothing to do
}
@Override
protected Command getDirectEditCommand(final DirectEditRequest request) {
if (getHost() instanceof AbstractDirectEditableEditPart) {
final Object value = request.getCellEditor().getValue();
final IInterfaceElement targetElement = getTargetInterfaceElement();
if (value instanceof final Integer intValue) {
final int index = intValue.intValue();
if (request.getCellEditor().getControl() instanceof final CCombo combo && index >= 0
&& index < combo.getItemCount()) {
final String typeName = combo.getItem(index);
return ChangeDataTypeCommand.forTypeName(targetElement, typeName);
}
} else if (value instanceof final String stringValue) {
return ChangeDataTypeCommand.forTypeDeclaration(targetElement, stringValue);
}
}
return null;
}
});
}
}
protected IInterfaceElement getTargetInterfaceElement() {
if (getCastedModel() instanceof final VarDeclaration varDecl && varDecl.isInOutVar() && !varDecl.isIsInput()) {
return varDecl.getInOutVarOpposite();
}
return getCastedModel();
}
@Override
public void performRequest(final Request request) {
// REQ_DIRECT_EDIT -> first select 0.4 sec pause -> click -> edit
// REQ_OPEN -> doubleclick
if (request.getType() == RequestConstants.REQ_OPEN) {
// transform doubleclick to direct edit
request.setType(RequestConstants.REQ_DIRECT_EDIT);
}
super.performRequest(request);
}
@Override
protected DirectEditManager createDirectEditManager() {
final IInterfaceElement interfaceElement = getCastedModel();
if (interfaceElement instanceof final VarDeclaration varDecl) {
return new TypeDeclarationDirectEditManager(this, new FigureCellEditorLocator(getFigure()), varDecl);
}
return new ComboDirectEditManager(this, ComboBoxCellEditor.class, new ComboCellEditorLocator(getFigure()),
getFigure());
}
@Override
public void performDirectEdit() {
final DirectEditManager editManager = createDirectEditManager();
if (editManager instanceof final ComboDirectEditManager comboEditManager) {
final List<String> dataTypeNames;
if (getCastedModel() instanceof AdapterDeclaration) {
dataTypeNames = typeLib.getAdapterTypesSorted().stream().map(TypeEntry::getTypeName).toList();
} else {
dataTypeNames = typeLib.getDataTypeLibrary().getDataTypesSorted().stream().map(DataType::getName)
.toList();
}
comboEditManager.updateComboData(dataTypeNames);
comboEditManager.setSelectedItem(dataTypeNames.indexOf(getTypeName()));
}
editManager.show();
}
@Override
public Label getNameLabel() {
return getFigure();
}
@Override
public INamedElement getINamedElement() {
return getCastedModel();
}
@Override
public boolean isDirectEditable() {
// allow direct edit only for VarDeclarations and AdapterDeclarations
return super.isDirectEditable()
&& (getCastedModel() instanceof VarDeclaration || getCastedModel() instanceof AdapterDeclaration);
}
@Override
public boolean isConnectable() {
return false;
}
@Override
public <T> T getAdapter(final Class<T> key) {
if (key == ErrorMarkerDataTypeImpl.class) {
final DataType marker = getCastedModel().getType();
return marker instanceof ErrorMarkerDataTypeImpl ? key.cast(marker) : null;
}
return super.getAdapter(key);
}
}