/
EMFModelHelpView.java
277 lines (242 loc) · 8.66 KB
/
EMFModelHelpView.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
271
272
273
274
275
276
277
/*******************************************************************************
* Copyright (C) 2016-2018 Institute of Automation, TU Dresden.
*
* 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:
* Institute of Automation, TU Dresden - initial API and implementation
******************************************************************************/
package de.tud.et.ifa.agtele.ui.views;
import java.util.Iterator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.edit.provider.ItemProvider;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IPersistable;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import de.tud.et.ifa.agtele.emf.AgteleEcoreUtil;
import de.tud.et.ifa.agtele.help.IEMFModelHelpItemProvider;
import de.tud.et.ifa.agtele.resources.BundleContentHelper;
import de.tud.et.ifa.agtele.ui.AgteleUIPlugin;
/**
* This {@link ViewPart view} provides help contents for {@link EObject
* EObjects} by displaying information about their {@link EObject#eClass()
* EClass} as well as about corresponding {@link EAttribute EAttributes} and
* {@link EReference EReferences}. <br />
* <b>Note</b>: In order to enable this for your meta-model, your generated
* {@link ItemProvider ItemProviders} (resp. the root ItemProvider) need to
* implement the {@link IEMFModelHelpItemProvider} interface. <br />
* <b>Note</b>: The view is able to display the content of 'documentation'
* annotations specified in your meta-model. However, you need to set the
* 'Suppress GenModel Annotations' setting in your genmodel to 'false' for this
* to work! Otherwise, the 'documentation' annotations are not present in the
* generated package impls and can thus not be evaluated by the 'EMF Model Help
* View'.
*/
public class EMFModelHelpView extends ViewPart implements IPersistable {
/**
* The ID of the view as specified by the extension.
*/
public static final String ID = "de.tud.et.ifa.agtele.ui.views.EMFModelHelpView";
private Browser browser;
private Action linkAction;
private String currentText;
private ISelectionListener selectionListener;
private Boolean linkEditor;
/**
* The constructor.
*/
public EMFModelHelpView() {
this.selectionListener = (IWorkbenchPart iw, ISelection selection) -> {
EMFModelHelpView.showHelp(selection, false);
};
this.linkEditor = AgteleUIPlugin.getPlugin().getDialogSettings().getBoolean("link");
if (this.linkEditor) {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
.addSelectionListener(this.selectionListener);
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
@Override
public void createPartControl(Composite parent) {
this.browser = new Browser(parent, SWT.NONE);
this.makeActions();
this.contributeToActionBars();
this.currentText = AgteleUIPlugin.getPlugin().getDialogSettings().get("browserText");
if (this.currentText != null) {
this.browser.setText(this.currentText);
}
if (this.linkEditor) {
EMFModelHelpView.show(false);
}
}
/**
* Passing the focus request to the viewer's control.
*/
@Override
public void setFocus() {
this.browser.setFocus();
}
@Override
public void dispose() {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
.removeSelectionListener(this.selectionListener);
AgteleUIPlugin.getPlugin().getDialogSettings().put("link", this.linkEditor);
AgteleUIPlugin.getPlugin().getDialogSettings().put("browserText", this.currentText);
super.dispose();
}
private void contributeToActionBars() {
IActionBars bars = this.getViewSite().getActionBars();
this.fillLocalToolBar(bars.getToolBarManager());
}
private void fillLocalToolBar(IToolBarManager manager) {
manager.add(this.linkAction);
}
private void makeActions() {
this.linkAction = new Action("Link Editor",
BundleContentHelper.getBundleImageDescriptor(AgteleUIPlugin.PLUGIN_ID, "icons/synced.gif")) {
@Override
public void run() {
EMFModelHelpView.this.linkEditor = !EMFModelHelpView.this.linkEditor;
if (EMFModelHelpView.this.linkEditor) {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
.addSelectionListener(EMFModelHelpView.this.selectionListener);
} else {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
.removeSelectionListener(EMFModelHelpView.this.selectionListener);
}
EMFModelHelpView.this.linkAction.setChecked(EMFModelHelpView.this.linkEditor);
}
};
this.linkAction.setToolTipText("Link with Selection");
this.linkAction.setChecked(this.linkEditor);
}
/**
* Opens the view and shows help based on the current selection
*
* @param activateView
* bring view to front on text update
*/
public static void show(Boolean activateView) {
EMFModelHelpView.showHelp(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(),
activateView);
}
/**
* Opens the {@link EMFModelHelpView}, generates the help page for the given
* AminoUI {@link EObject} and displays it
*
* @param eObject
* AminoUI model element
* @param activateView
* bring view to front on text update
*/
public static void showHelp(EObject eObject, Boolean activateView) {
EMFModelHelpView.showText(EMFModelHelpView.getHtml(eObject), activateView);
}
/**
* Opens the {@link EMFModelHelpView} and displays text in it
*
* @param text
* text to be displayed
* @param activateView
* bring view to front on text update
*/
public static void showText(String text, Boolean activateView) {
try {
int viewMode = IWorkbenchPage.VIEW_CREATE;
if (activateView) {
viewMode = IWorkbenchPage.VIEW_ACTIVATE;
}
EMFModelHelpView helpView = (EMFModelHelpView) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().showView(EMFModelHelpView.ID, null, viewMode);
helpView.browser.setText(text);
helpView.currentText = text;
} catch (PartInitException e) {
e.printStackTrace();
}
}
/**
* Determine the current selection and show the help if it is an
* {@link EObject}
*
* @param selection
*/
private static void showHelp(ISelection selection, Boolean activateView) {
if (selection instanceof StructuredSelection) {
StructuredSelection structuredSelection = (StructuredSelection) selection;
// show Help if one EObject is selected
if (structuredSelection.size() == 1) {
if (structuredSelection.getFirstElement() instanceof EObject) {
EMFModelHelpView.showHelp((EObject) structuredSelection.getFirstElement(), activateView);
}
}
// else show nothing new
else {
Iterator<?> it = structuredSelection.iterator();
while (it.hasNext()) {
Object type = it.next();
if (!(type instanceof EObject)) {
return;
}
}
if (!structuredSelection.isEmpty()) {
EMFModelHelpView.showText(
AgteleUIPlugin.getPlugin().getString("_UI_EMFModelHelpView_TooManyElementsSelected"),
false);
}
}
}
}
/**
* Generates the documentation page of an {@link EObject} based on the
* {@link IEMFModelHelpItemProvider} matching the element
*
* @param eObject
* @return
*/
private static String getHtml(EObject eObject) {
IEMFModelHelpItemProvider iEMFModelHelpItemProvider = AgteleEcoreUtil.adapt(eObject,
IEMFModelHelpItemProvider.class);
// Create a new provider if necessary
//
if (iEMFModelHelpItemProvider == null) {
iEMFModelHelpItemProvider = new IEMFModelHelpItemProvider() {
};
}
return iEMFModelHelpItemProvider.render(iEMFModelHelpItemProvider.getHelpItemDescription(eObject));
}
/**
* Adding this listener will show the {@link EMFModelHelpView} when pressing
* F1
*
* @author martin
*
*/
public static class HelpListener implements org.eclipse.swt.events.HelpListener {
@Override
public void helpRequested(HelpEvent e) {
EMFModelHelpView.show(true);
}
}
}