-
Notifications
You must be signed in to change notification settings - Fork 85
/
ManageReferencedLibrariesHandler.java
154 lines (138 loc) · 6.96 KB
/
ManageReferencedLibrariesHandler.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
/*******************************************************************************
* Copyright (c) 2006, 2023 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.libraries.ui.handlers;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.polarsys.capella.common.flexibility.properties.loader.PropertiesLoader;
import org.polarsys.capella.common.flexibility.properties.property.PropertyContext;
import org.polarsys.capella.common.flexibility.properties.schema.IProperties;
import org.polarsys.capella.common.flexibility.properties.schema.IPropertyContext;
import org.polarsys.capella.common.flexibility.wizards.loader.RenderersLoader;
import org.polarsys.capella.common.flexibility.wizards.renderer.RendererContext;
import org.polarsys.capella.common.flexibility.wizards.schema.IRendererContext;
import org.polarsys.capella.common.flexibility.wizards.schema.IRenderers;
import org.polarsys.capella.common.flexibility.wizards.ui.PropertyDialog;
import org.polarsys.capella.common.flexibility.wizards.ui.PropertyWizard;
import org.polarsys.capella.common.flexibility.wizards.ui.PropertyWizardPage;
import org.polarsys.capella.common.helpers.EcoreUtil2;
import org.polarsys.capella.common.libraries.ILibraryManager;
import org.polarsys.capella.common.libraries.IModel;
import org.polarsys.capella.core.libraries.properties.FlexibilityIds;
import org.polarsys.capella.core.libraries.properties.LibraryManagerModel;
import org.polarsys.capella.core.model.handler.command.CapellaResourceHelper;
public class ManageReferencedLibrariesHandler extends AbstractHandler {
public static String LIBRARY_DETACHMENT_DOC_URI = "org.polarsys.capella.re.doc/html/09.%20Libraries/9.5.%20Advanced%20Operations.html?cp=6_1_20_4";//$NON-NLS-1$
@Override
public void setEnabled(Object evaluationContext) {
super.setEnabled(evaluationContext);
IEvaluationContext context = (IEvaluationContext) evaluationContext;
Collection<Object> selectedElements = (Collection<Object>) context.getDefaultVariable();
boolean isVisible = false;
if (selectedElements.size() == 1) {
Object element = selectedElements.iterator().next();
if (element instanceof IFile) {
IFile file = (IFile) element;
if (CapellaResourceHelper.isAirdResource(file, true)) {
isVisible = getExistingSession(file) != null;
}
}
}
setBaseEnabled(isVisible);
}
@Override
public boolean isEnabled() {
return super.isEnabled();
}
@Override
public boolean isHandled() {
return true;
}
protected Session getExistingSession(IFile file) {
return SessionManager.INSTANCE.getExistingSession(EcoreUtil2.getURI(file));
}
@Override
public void removeHandlerListener(IHandlerListener handlerListener_p) {
super.removeHandlerListener(handlerListener_p);
}
@Override
public Object execute(ExecutionEvent event_p) throws ExecutionException {
TreeSelection selection = (TreeSelection) HandlerUtil.getCurrentSelection(event_p);
IFile file = (IFile) selection.getFirstElement();
Session session = getExistingSession(file);
return openManageReferencesWizard(session);
}
public static Object openManageReferencesWizard(Session session) {
IModel rootModel = ILibraryManager.INSTANCE.getModel(session.getTransactionalEditingDomain());
if ((rootModel == null) || !(rootModel instanceof IModel.Edit)) {
return null;
}
final String modelName = rootModel.getIdentifier().getName();
IProperties properties = new PropertiesLoader().getProperties(FlexibilityIds.MANAGE_REFERENCES_PROPERTIES);
LibraryManagerModel managerModel = new LibraryManagerModel(session.getTransactionalEditingDomain(),
(IModel.Edit) rootModel);
IPropertyContext context = new PropertyContext(properties, managerModel);
IRenderers renderers = new RenderersLoader().getRenderers(properties);
IRendererContext rendererContext = new RendererContext(renderers, context);
// Instantiates and initializes the wizard
PropertyWizard wizard = new PropertyWizard(context, rendererContext) {
@Override
public void addPages() {
PropertyWizardPage page = new PropertyWizardPage("propertiesEditor", getContext(), getRendererContext()); //$NON-NLS-1$
page.setTitle(modelName + " management");
page.setDescription(
"This wizard helps you to define the libraries that are referenced by the current project (first tab).\nYou can also specify among these libraries which ones must be considered in capella query scopes (second tab).");
addPage(page);
}
};
// Instantiates the wizard container with the wizard and opens it
PropertyDialog dialog = new PropertyDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(), wizard);
dialog.setHelpAvailable(false);
dialog.setPageSize(200, 300);
// dialog.setTitle();
dialog.create();
int result = dialog.open();
Collection<IModel> newReferencedLibraries = managerModel.getAllReferencedLibrariesByRootModel();
List<IModel> removedLibraries = new ArrayList<IModel>(managerModel.getInitialAllReferencedLibrariesByRootModel());
removedLibraries.removeAll(newReferencedLibraries);
if (result == Window.OK) {
if (!removedLibraries.isEmpty()) {
if (MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "Read the documentation", //$NON-NLS-1$
"Unreferencing libraries may corrupt your model if not done in the proper way.\nMake sure to follow the procedure\nWould you like to open the documentation?")) {//$NON-NLS-1$
String localHRef = PlatformUI.getWorkbench().getHelpSystem().resolve(LIBRARY_DETACHMENT_DOC_URI, true)
.toExternalForm();
PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(localHRef);
}
}
session.save(new NullProgressMonitor());
return Status.OK_STATUS;
}
return null;
}
}