-
Notifications
You must be signed in to change notification settings - Fork 85
/
EditCapellaCustomPropertyWizardPage.java
312 lines (269 loc) · 10.8 KB
/
EditCapellaCustomPropertyWizardPage.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
/*******************************************************************************
* Copyright (c) 2006, 2020 THALES GLOBAL SERVICES.
*
* 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:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.ui.properties.wizards;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.help.HelpSystem;
import org.eclipse.help.IContext;
import org.eclipse.help.IHelpResource;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.help.IWorkbenchHelpSystem;
import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyRegistry;
import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyRegistryFactory;
import org.eclipse.ui.views.properties.tabbed.ISection;
import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor;
import org.eclipse.ui.views.properties.tabbed.ITabDescriptor;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.polarsys.capella.common.mdsofa.common.constant.ICommonConstants;
import org.polarsys.capella.common.ui.services.helper.FormHelper;
import org.polarsys.capella.core.model.utils.NamingHelper;
import org.polarsys.capella.core.ui.properties.CapellaUIPropertiesPlugin;
import org.polarsys.capella.core.ui.properties.sections.IAbstractSection;
import org.polarsys.capella.core.ui.resources.CapellaUIResourcesPlugin;
/**
*/
public class EditCapellaCustomPropertyWizardPage extends WizardPage implements ITabbedPropertySheetPageContributor {
private FormToolkit toolkit;
private HelpListener helpListener = null;
private Composite comp = null;
private Collection<ISection> sections = null;
private IWorkbenchPart part;
/**
* Constructor.
*
* @param pageName
* @param element
* @param metaclassLabel
*/
public EditCapellaCustomPropertyWizardPage(IWorkbenchPart part, String pageName, EObject element,
String metaclassLabel) {
super(pageName);
// Configure page title and description.
setTitle(NamingHelper.getTitleLabel(element));
setDescription(NLS.bind(Messages.editCapellaCustomPropertyWizardPage_Description, metaclassLabel));
ImageDescriptor imageDescriptor = CapellaUIResourcesPlugin.getDefault().getPNGImage(element);
if (imageDescriptor != null) {
setImageDescriptor(imageDescriptor);
}
this.part = part;
}
/**
* Create a default composite to host page widgets.
*
* @param parent
* @param numColumns
* @return
*/
private Composite createComposite(Composite parent, FormToolkit toolkit) {
ScrolledForm scrolledContainer = toolkit.createScrolledForm(parent);
setControl(scrolledContainer);
// Get the content of this scrolled form.
Composite content = scrolledContainer.getBody();
content.setLayoutData(new GridData(GridData.FILL_BOTH));
content.setLayout(new GridLayout());
return content;
}
protected boolean sectionDisplayedOnWizard(ISectionDescriptor section) {
// Expert view is not synchronized with other sections, so if the user change a value in it.
// it is not reflected on other tabs.
return !CapellaUIPropertiesPlugin.CAPELLA_EXPERT_SECTION.equals(section.getId());
}
/**
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
*/
public void createControl(Composite parent) {
TabbedPropertyRegistry registry = TabbedPropertyRegistryFactory.getInstance().createRegistry(this);
IStructuredSelection selection = new StructuredSelection(getEObject());
sections = new ArrayList<ISection>();
toolkit = new FormToolkit(parent.getDisplay());
toolkit.setBackground(parent.getBackground());
// Create parent composite.
Composite content = createComposite(parent, toolkit);
// Initialize dialog units.
initializeDialogUnits(content);
// Get the edited model element.
EObject object = getEObject();
// Create a Tab folder to store each section per tab.
CTabFolder tabFolder = new CTabFolder(content, SWT.BORDER);
tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
tabFolder.setSimple(false);
tabFolder.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
Object data = tabFolder.getSelection().getData();
if (data instanceof ISection) {
((ISection) data).refresh();
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
});
for (ITabDescriptor descriptor : registry.getTabDescriptors(part, selection)) {
Composite tabItemContent = new Composite(tabFolder, SWT.NONE);
tabItemContent.setLayout(new GridLayout());
tabItemContent.setLayoutData(new GridData(GridData.FILL_BOTH));
List<ISectionDescriptor> descriptors = ((List<Object>) descriptor.getSectionDescriptors()).stream()
.map(ISectionDescriptor.class::cast).filter(x -> sectionDisplayedOnWizard(x)).collect(Collectors.toList());
if (!descriptors.isEmpty()) {
CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
tabItem.setText(descriptor.getLabel());
tabItem.setControl(tabItemContent);
for (ISectionDescriptor sd : descriptors) {
ISectionDescriptor sectionDescriptor = (ISectionDescriptor) sd;
ISection section = sectionDescriptor.getSectionClass();
if (section != null) {
Composite sectionComposite = new Composite(tabItemContent, SWT.NONE);
sectionComposite.setLayout(new FillLayout());
int style = (section.shouldUseExtraSpace()) ? GridData.FILL_BOTH : GridData.FILL_HORIZONTAL;
GridData data = new GridData(style);
data.heightHint = section.getMinimumHeight();
sectionComposite.setLayoutData(data);
section.createControls(sectionComposite, null);
section.setInput(part, new StructuredSelection(object));
tabItem.setData(section);
sections.add(section);
}
}
}
}
FormHelper.adaptBackgroundColor((Control) content, content.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND),
false);
//
// Help on wizards
//
final IWorkbenchHelpSystem helpSystem = PlatformUI.getWorkbench().getHelpSystem();
final EClass eclass = object.eClass();
if ((null != comp) && (null != helpListener)) {
comp.removeHelpListener(helpListener);
}
helpListener = new HelpListener() {
private boolean isHelpAvailable() {
return checkIfHelpIsAvailable(eclass);
}
protected boolean checkIfHelpIsAvailable(EClass eclass) {
try (InputStream inputStream = HelpSystem.getHelpContent(generateHTMLLink(eclass))) {
return ((null != eclass) && (null != inputStream));
} catch (Exception e) {
// Fail silently
}
return false;
}
public void helpRequested(HelpEvent e) {
IContext c = new IContext() {
@SuppressWarnings("synthetic-access")
public String getText() {
if (!isHelpAvailable()) {
return NLS.bind(Messages.editCapellaCustomPropertyWizardPage_MainDescriptionKO, eclass.getName());
}
return null;
}
@SuppressWarnings("synthetic-access")
public IHelpResource[] getRelatedTopics() {
IHelpResource hr = null;
if (isHelpAvailable()) {
hr = new IHelpResource() {
public String getLabel() {
return NLS.bind(Messages.editCapellaCustomPropertyWizardPage_RLLinkName, eclass.getName());
}
public String getHref() {
return generateHTMLLink(eclass);
}
};
}
return (null != hr ? new IHelpResource[] { hr } : null);
}
};
helpSystem.displayContext(c, 0, 0);
}
protected String generateHTMLLink(EClass eclass) {
return "/org.polarsys.capella.core.doc.user/html/editors/" + //$NON-NLS-1$
eclass.getEPackage().getName() + ICommonConstants.SLASH_CHARACTER + eclass.getName()
+ ICommonConstants.POINT_CHARACTER + "html"; //$NON-NLS-1$
}
};
comp = parent;
comp.addHelpListener(helpListener);
}
/**
* @see org.eclipse.jface.dialogs.DialogPage#dispose()
*/
@Override
public void dispose() {
if (null != toolkit) {
toolkit.dispose();
toolkit = null;
}
if ((null != comp) && (null != helpListener)) {
comp.removeHelpListener(helpListener);
helpListener = null;
comp.dispose();
comp = null;
}
// memory leaks, sections weren't disposed
if (null != sections) {
for (ISection section : sections) {
section.dispose();
}
sections = null;
}
getImage().dispose();
TabbedPropertyRegistryFactory.getInstance().disposeRegistry(this);
}
/**
* Get the model element that the wizard is open for.
*
* @return a not <code>null</code> element.
*/
protected EObject getEObject() {
return ((EditCapellaCustomPropertyWizard) getWizard()).getEObject();
}
public void performFinish() {
// Propagate the Finish action to sections
sections.stream().filter(IAbstractSection.class::isInstance).map(IAbstractSection.class::cast)
.forEach(IAbstractSection::performFinish);
}
public Collection<ISection> getSections() {
return sections;
}
@Override
public String getContributorId() {
return CapellaUIPropertiesPlugin.PROPERTIES_CONTRIBUTOR;
}
}