Skip to content

Package: ECPImportHandlerHelper$1

ECPImportHandlerHelper$1

nameinstructionbranchcomplexitylinemethod
doExecute()
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * David Soto Setzke - initial API and implementation
13: * Johannes Faltermeier - moved file dialog dependent code to helper class
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.internal.ui.util;
16:
17: import org.eclipse.emf.common.util.URI;
18: import org.eclipse.emf.ecore.EObject;
19: import org.eclipse.emf.ecore.EReference;
20: import org.eclipse.emf.ecore.resource.Resource;
21: import org.eclipse.emf.ecore.resource.ResourceSet;
22: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
23: import org.eclipse.emf.ecore.util.EcoreUtil;
24: import org.eclipse.emf.ecp.core.ECPProject;
25: import org.eclipse.emf.ecp.internal.ui.Activator;
26: import org.eclipse.emf.edit.command.AddCommand;
27: import org.eclipse.emf.edit.command.ChangeCommand;
28: import org.eclipse.emf.edit.command.SetCommand;
29: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
30: import org.eclipse.emf.edit.domain.EditingDomain;
31: import org.eclipse.jface.dialogs.MessageDialog;
32: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
33: import org.eclipse.swt.widgets.Shell;
34: import org.osgi.framework.BundleContext;
35: import org.osgi.framework.FrameworkUtil;
36: import org.osgi.framework.ServiceReference;
37:
38: /**
39: * A utility class which provides support for importing {@link EObject}s.
40: *
41: * @author David
42: *
43: */
44: public final class ECPImportHandlerHelper {
45:
46:         private ECPImportHandlerHelper() {
47:         }
48:
49:         /**
50:          * These filter extensions are used to filter which files are displayed.
51:          */
52:         public static final String[] FILTER_EXTS = { "*.xmi" }; //$NON-NLS-1$
53:
54:         /**
55:          * These filter names are used to filter which files are displayed.
56:          */
57:         public static final String[] FILTER_NAMES = { "Model Files (*.xmi)" }; //$NON-NLS-1$
58:
59:         private static boolean imported;
60:
61:         /**
62:          * Connects an {@link EObject} with another imported {@link EObject} which will be selected via a dialog.
63:          *
64:          * @param shell The {@link Shell} which should be used for the dialog
65:          * @param eObject The {@link EObject} which should be connected with the imported {@link EObject}
66:          */
67:         public static void importElement(Shell shell, EObject eObject) {
68:                 importElement(shell, (Object) eObject);
69:         }
70:
71:         /**
72:          * Connects an {@link EObject} with another imported {@link EObject} which will be selected via a dialog.
73:          *
74:          * @param shell The {@link Shell} which should be used for the dialog
75:          * @param ecpProject The {@link ECPProject} where the {@link EObject} should be imported into
76:          */
77:         public static void importElement(Shell shell, ECPProject ecpProject) {
78:                 importElement(shell, (Object) ecpProject);
79:         }
80:
81:         private static void importElement(Shell shell, Object object) {
82:
83:                 // ECPModelContextProvider contextProvider =
84:                 // (ECPModelContextProvider)((TreeView)HandlerUtil.getActivePart(event))
85:                 // .getViewer().getContentProvider();
86:                 // IStructuredSelection selection=(IStructuredSelection)HandlerUtil.getCurrentSelection(event);
87:                 // final ECPProject project = (ECPProject)ECPUtil.getModelContext(contextProvider, selectedModelElement);
88:
89:                 if (object == null) {// project == null ||
90:                         return;
91:                 }
92:
93:                 final String fileName = getFileName(shell);
94:                 if (fileName == null) {
95:                         return;
96:                 }
97:
98:                 final URI fileURI = URI.createFileURI(fileName);
99:
100:                 // create resource set and resource
101:                 final ResourceSet resourceSet = new ResourceSetImpl();
102:
103:                 final Resource resource = resourceSet.getResource(fileURI, true);
104:
105:                 importFile(object, fileURI, resource, shell);
106:         }
107:
108:         private static void importFile(final Object parentObject, final URI fileURI, final Resource resource,
109:                 final Shell shell) {
110:                 imported = false;
111:                 final ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(shell);
112:                 try {
113:
114:                         progressDialog.open();
115:                         progressDialog.getProgressMonitor().beginTask("Import model...", 100); //$NON-NLS-1$
116:
117:                         // Set<EObject> importElements = validation(resource);
118:                         final EObject eObjectImport = resource.getContents().get(0);
119:
120:                         if (parentObject instanceof EObject) {
121:
122:                                 for (final EReference ref : ((EObject) parentObject).eClass().getEAllContainments()) {
123:                                         if (ref.getEReferenceType().isInstance(eObjectImport)) {
124:                                                 final EditingDomain editingDomain = AdapterFactoryEditingDomain
125:                                                         .getEditingDomainFor(parentObject);
126:                                                 if (ref.isMany()) {
127:                                                         editingDomain.getCommandStack().execute(
128:                                                                 new AddCommand(editingDomain, (EObject) parentObject, ref, EcoreUtil
129:                                                                         .copy(eObjectImport)));
130:                                                 } else {
131:                                                         editingDomain.getCommandStack().execute(
132:                                                                 new SetCommand(editingDomain, (EObject) parentObject, ref, EcoreUtil
133:                                                                         .copy(eObjectImport)));
134:                                                 }
135:                                                 imported = true;
136:                                                 break;
137:                                         }
138:                                 }
139:                         } else if (parentObject instanceof ECPProject) {
140:                                 final EditingDomain editingDomain = ((ECPProject) parentObject).getEditingDomain();
141:                                 editingDomain.getCommandStack().execute(new ChangeCommand(eObjectImport) {
142:
143:                                         @Override
144:                                         protected void doExecute() {
145:                                                 ((ECPProject) parentObject).getContents().add(EcoreUtil.copy(eObjectImport));
146:                                                 imported = true;
147:                                         }
148:                                 });
149:                         }
150:
151:                         // if (importElements.size() > 0) {
152:                         // for (EObject eObject : importElements) {
153:                         // project.addModelElement(EcoreUtil.copy(eObject));
154:                         // progressDialog.getProgressMonitor().worked(10);
155:                         // }
156:                         // }
157:                         // BEGIN SUPRESS CATCH EXCEPTION
158:                 } catch (final RuntimeException e) {
159:                         Activator.log(e.getMessage(), e);
160:                         // END SUPRESS CATCH EXCEPTION
161:                 } finally {
162:                         progressDialog.getProgressMonitor().done();
163:                         progressDialog.close();
164:                 }
165:                 if (!imported) {
166:                         MessageDialog
167:                                 .openWarning(shell, "No Imports", //$NON-NLS-1$
168:                                         "No Objects were imported, the model element you selected probably can't contain the element you try to import."); //$NON-NLS-1$
169:                 }
170:         }
171:
172:         // TODO ask jonas
173:
174:         // Validates if the EObjects can be imported
175:         // private Set<EObject> validation(Resource resource) {
176:         // Set<EObject> childrenSet = new HashSet<EObject>();
177:         // Set<EObject> rootNodes = new HashSet<EObject>();
178:         //
179:         // EList<EObject> rootContent = resource.getContents();
180:         //
181:         // for (EObject rootNode : rootContent) {
182:         // TreeIterator<EObject> contents = rootNode.eAllContents();
183:         // // 1. Run: Put all children in set
184:         // while (contents.hasNext()) {
185:         // EObject content = contents.next();
186:         // if (!(content != null)) {
187:         // continue;
188:         // }
189:         // childrenSet.add(content);
190:         // }
191:         // }
192:         //
193:         // // 2. Run: Check if RootNodes are children -> set.contains(RootNode) -- no: RootNode in rootNode-Set -- yes:
194:         // // Drop RootNode, will be imported as a child
195:         // for (EObject rootNode : rootContent) {
196:         //
197:         // if (!(rootNode != null)) {
198:         // // No report to Console, because Run 1 will do this
199:         // continue;
200:         // }
201:         //
202:         // if (!childrenSet.contains(rootNode)) {
203:         // rootNodes.add(rootNode);
204:         // }
205:         // }
206:         //
207:         // // 3. Check if RootNodes are SelfContained -- yes: import -- no: error
208:         // Set<EObject> notSelfContained = new HashSet<EObject>();
209:         // for (EObject rootNode : rootNodes) {
210:         // if (!CommonUtil.isSelfContained(rootNode)) {
211:         // // TODO: Report to Console //System.out.println(rootNode + " is not selfcontained");
212:         // notSelfContained.add(rootNode);
213:         // }
214:         // }
215:         // rootNodes.removeAll(notSelfContained);
216:         //
217:         // return rootNodes;
218:         // }
219:
220:         private static String getFileName(Shell shell) {
221:                 final BundleContext bundleContext = FrameworkUtil.getBundle(ECPExportHandlerHelper.class).getBundleContext();
222:                 final ServiceReference<ECPFileDialogHelper> serviceReference = bundleContext
223:                         .getServiceReference(ECPFileDialogHelper.class);
224:                 final ECPFileDialogHelper fileDialogHelper = bundleContext.getService(serviceReference);
225:                 final String result = fileDialogHelper.getPathForImport(shell);
226:                 bundleContext.ungetService(serviceReference);
227:                 return result;
228:         }
229: }