/
SelectRepresentationDescriptionWizardPage.java
474 lines (408 loc) · 15 KB
/
SelectRepresentationDescriptionWizardPage.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*******************************************************************************
* Copyright (c) 2016 Obeo
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.ecoretools.design.ui.wizard.pages;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.sirius.business.api.componentization.ViewpointRegistry;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.ui.tools.api.views.ViewHelper;
import org.eclipse.sirius.viewpoint.description.RepresentationDescription;
import org.eclipse.sirius.viewpoint.description.Viewpoint;
import org.eclipse.sirius.viewpoint.provider.SiriusEditPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.osgi.framework.Bundle;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
* A wizard page to select representation descriptions based on the Viewpoints
* currently availables.
*
* @author Cedric Brun <cedric.brun@obeo.fr>
*/
public class SelectRepresentationDescriptionWizardPage extends WizardPage
implements Supplier<RepresentationDescription> {
/**
*
* @author mchauvin
*/
private class ViewpointsTableLabelProvider extends AdapterFactoryLabelProvider implements ITableLabelProvider {
public ViewpointsTableLabelProvider() {
super(ViewHelper.INSTANCE.createAdapterFactory());
}
/**
* {@inheritDoc}
*/
@Override
public Image getColumnImage(Object element, int columnIndex) {
Image image = null;
if (columnIndex == 0) {
if (element instanceof Viewpoint) {
final Viewpoint vp = (Viewpoint) element;
if (vp.getIcon() != null && vp.getIcon().length() > 0) {
final ImageDescriptor desc = SiriusEditPlugin.Implementation.findImageDescriptor(vp.getIcon());
if (desc != null) {
image = SiriusEditPlugin.getPlugin().getImage(desc);
image = getEnhancedImage(image, vp);
}
}
if (image == null) {
image = SiriusEditPlugin.getPlugin()
.getImage(SiriusEditPlugin.getPlugin().getItemImageDescriptor(vp));
image = getEnhancedImage(image, vp);
}
} else {
image = super.getImage(element);
}
}
return image;
}
private Image getEnhancedImage(final Image image, final Viewpoint viewpoint) {
if (ViewpointRegistry.getInstance().isFromPlugin(viewpoint) && image != null) {
return SiriusEditPlugin.getPlugin()
.getImage(getOverlayedDescriptor(image, "icons/full/ovr16/plugin_ovr.gif"));
}
return image;
}
private ImageDescriptor getOverlayedDescriptor(final Image baseImage, final String decoratorPath) {
final ImageDescriptor decoratorDescriptor = SiriusEditPlugin.Implementation
.getBundledImageDescriptor(decoratorPath);
return new DecorationOverlayIcon(baseImage, decoratorDescriptor, IDecoration.BOTTOM_LEFT);
}
}
private static final String PAGE_MESSAGE = "Select the type of representation to create.";
/** The title of the page. */
private static final String PAGE_TITLE = "Select type of representation";
/** The browser for documentation */
private Browser browser;
/**
* The list of selected representation descriptions
*/
private Set<RepresentationDescription> descriptions;
private Composite pageComposite;
/**
* List of description names that must be activate by default
*/
private Set<String> representationsURIsToActivateByDefault;
private Supplier<Session> sessionSupplier;
/** The table viewer. */
private TableViewer tableViewer;
/**
* Create a new <code>RepresentationSelectionWizardPage</code> with default
* viewpoints activation. This constructor makes this page optional.
*
* @param supplier
* the session
* @param representationsToSelectByDefault
* list of viewpoints URIS to activate by default (in the form of
* viewpoint:/someplugin/Some Viewpoint.
*/
public SelectRepresentationDescriptionWizardPage(final Supplier<Session> supplier,
Collection<String> representationsToSelectByDefault) {
super(PAGE_TITLE);
this.setTitle(PAGE_TITLE);
this.setMessage(PAGE_MESSAGE);
this.sessionSupplier = supplier;
this.descriptions = Sets.newLinkedHashSet();
this.representationsURIsToActivateByDefault = Sets.newHashSet(representationsToSelectByDefault);
}
private StringBuilder appendCss(StringBuilder content) {
Font currentFont = JFaceResources.getDialogFont();
FontData data = currentFont.getFontData()[0];
String fontName = data.getName();
int fontHeight = data.getHeight() + 3;
content.append("<style type=\"text/css\">");
content.append("body{font-family:" + fontName + ",Arial, sans-serif;}");
content.append("body{font-size:" + fontHeight + "px;}");
content.append("</style>");
return content;
}
private SelectRepresentationDescriptionWizardPage begin(StringBuilder content) {
content.append("<html>");
return this;
}
private SelectRepresentationDescriptionWizardPage body(StringBuilder content,
RepresentationDescription repDescription) {
content.append("<body>");
if (repDescription == null) {
content.append("<br><br><center><b>Documentation</b></center>");
} else {
final String endUserDocumentation = repDescription.getDocumentation();
if (!StringUtil.isEmpty(endUserDocumentation))
content.append(repDescription.getEndUserDocumentation());
else
content.append("no documentation for this representation");
}
content.append("</body>");
return this;
}
/*
* The following code (HTML handling ) and methods could move to another
* class.
*/
private boolean containsHTMLDocumentation(RepresentationDescription description) {
if (description != null) {
final String doc = description.getDocumentation();
if (!StringUtil.isEmpty(doc))
return doc.startsWith("<html>");
}
return false;
}
private Browser createBrowser(final Composite parent) {
try {
Browser aBrowser = new Browser(parent, SWT.NONE);
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
aBrowser.setLayoutData(gridData);
return aBrowser;
} catch (SWTError error) {
/*
* the browser could not be created, do not display further
* information
*/
return null;
}
}
/**
* {@inheritDoc}
*
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
*/
public void createControl(final Composite parent) {
initializeDialogUnits(parent);
pageComposite = new Composite(parent, SWT.NONE);
pageComposite.setLayout(GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(true).create());
pageComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
this.tableViewer = createTableViewer(pageComposite);
this.browser = createBrowser(pageComposite);
setBrowserInput(null);
setControl(pageComposite);
}
/**
* Create the table viewer.
*
* @param parent
* the parent composite.
* @return the table viewer.
*/
private TableViewer createTableViewer(final Composite parent) {
final int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
TableViewer viewer = new TableViewer(parent, style);
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
viewer.getControl().setLayoutData(gridData);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new ViewpointsTableLabelProvider());
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
if (event.getSelection() instanceof IStructuredSelection) {
descriptions.clear();
Iterators.addAll(descriptions, Iterators.filter(
((IStructuredSelection) event.getSelection()).iterator(), RepresentationDescription.class));
}
}
});
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
ISelection selection = event.getSelection();
if (selection instanceof IStructuredSelection) {
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
if (firstElement instanceof RepresentationDescription) {
setBrowserInput((RepresentationDescription) firstElement);
}
}
}
});
return viewer;
}
private String end(StringBuilder content) {
content.append("</html>");
return content.toString();
}
private void extractUrlToRewrite(String document, Set<String> urlToRewrite) {
String imgSrcPattern = "img src=\"";
int patternStartIndex = document.indexOf(imgSrcPattern);
if (patternStartIndex != -1) {
int imgSrcStartIndex = patternStartIndex + imgSrcPattern.length();
int imgSrcStopIndex = document.indexOf("\"", imgSrcStartIndex);
if (imgSrcStopIndex != -1) {
String newToRewrite = document.substring(imgSrcStartIndex, imgSrcStopIndex);
urlToRewrite.add(newToRewrite);
extractUrlToRewrite(document.substring(imgSrcStopIndex), urlToRewrite);
}
}
}
public RepresentationDescription get() {
if (descriptions.size() > 0) {
return descriptions.iterator().next();
}
return null;
}
private Collection<RepresentationDescription> getAvailableDescriptions(Session session) {
List<RepresentationDescription> result = Lists.newArrayList();
for (Viewpoint vp : session.getSelectedViewpoints(false)) {
result.addAll(vp.getOwnedRepresentations());
}
return result;
}
private String getContentWhenHtml(RepresentationDescription viewpoint) {
final String document = viewpoint.getDocumentation();
Set<String> urlToRewrite = Sets.newLinkedHashSet();
extractUrlToRewrite(document, urlToRewrite);
return rewriteURLs(viewpoint, document, urlToRewrite);
}
private String getContentWhenNoHtml(RepresentationDescription viewpoint) {
StringBuilder content = new StringBuilder();
return begin(content).head(content).body(content, viewpoint).end(content);
}
private SelectRepresentationDescriptionWizardPage head(StringBuilder content) {
content.append("<head>");
appendCss(content);
content.append("</head>");
return this;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
*/
@Override
public boolean isPageComplete() {
String errorMessage = null;
boolean complete = false;
if (descriptions.size() == 0) {
errorMessage = "You should select at least one type of representation.";
} else {
complete = true;
}
setErrorMessage(errorMessage);
return complete;
}
private String rewriteURL(RepresentationDescription viewpoint, String url) {
final URI uri = viewpoint.eResource().getURI();
String pluginId = uri.segment(1);
String rewrittenURL = "";
if (uri.isPlatformPlugin()) {
Bundle bundle = Platform.getBundle(pluginId);
URL imageURL = bundle.getEntry(url);
rewrittenURL = imageURL != null ? imageURL.toString() : rewrittenURL;
if (imageURL != null) {
try {
URL fileURL = FileLocator.toFileURL(imageURL);
rewrittenURL = fileURL.toString();
} catch (IOException e) {
// do nothing
}
}
} else {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IPath path = new Path("/" + pluginId + url);
if (workspace.getRoot().exists(path)) {
IResource resource = workspace.getRoot().findMember(path);
rewrittenURL = resource.getLocation().toFile().toURI().toString();
}
}
return rewrittenURL;
}
private String rewriteURLs(RepresentationDescription viewpoint, String document, Set<String> urls) {
String newDocument = document;
for (final String url : urls) {
newDocument = newDocument.replace(url, rewriteURL(viewpoint, url));
}
StringBuilder css = new StringBuilder();
appendCss(css);
String headClose = "</head>";
newDocument = newDocument.replace(headClose, css.append(headClose));
return newDocument;
}
/***
* Set the browser input.A jface like browser viewer would have been better.
*
* @param viewpoint
* the viewpoint to document
*/
protected void setBrowserInput(final RepresentationDescription viewpoint) {
/* browser may be null if its creation fail */
if (browser != null) {
String content = null;
if (containsHTMLDocumentation(viewpoint)) {
content = getContentWhenHtml(viewpoint);
} else {
content = getContentWhenNoHtml(viewpoint);
}
browser.setText(content);
}
}
@Override
public void setVisible(boolean visible) {
if (visible) {
/*
* shouldn't we make sure the "get" is called in a job ?
*/
Session s = sessionSupplier.get();
if (s != null) {
tableViewer.setInput(getAvailableDescriptions(s));
if (!representationsURIsToActivateByDefault.isEmpty()) {
// Search the viewpoints to activate by their name
for (int i = 0; i < tableViewer.getTable().getItemCount(); i++) {
Object object = tableViewer.getElementAt(i);
if (object instanceof RepresentationDescription) {
RepresentationDescription representationDescription = (RepresentationDescription) object;
if (representationDescription.getName() != null && representationsURIsToActivateByDefault
.contains(representationDescription.getName())) {
descriptions.add((RepresentationDescription) object);
}
}
}
if (!descriptions.isEmpty()) {
// Set the focus on the first one
tableViewer.setSelection(new StructuredSelection(descriptions.iterator().next()));
}
}
}
}
super.setVisible(visible);
}
}