Skip to content

Package: ECPExportHandlerHelper

ECPExportHandlerHelper

nameinstructionbranchcomplexitylinemethod
export(Shell, List)
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getFilePathByFileDialog(Shell, String)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getNameForModelElement(EObject)
M: 36 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
runCommand(List, String, Shell)
M: 53 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
saveEObjectToResource(List, URI)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
showExceptionDialog(String, Exception, Shell)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 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 java.io.File;
18: import java.io.IOException;
19: import java.util.Collections;
20: import java.util.List;
21:
22: import org.eclipse.emf.common.notify.AdapterFactory;
23: import org.eclipse.emf.common.util.EList;
24: import org.eclipse.emf.common.util.URI;
25: import org.eclipse.emf.ecore.EObject;
26: import org.eclipse.emf.ecore.resource.Resource;
27: import org.eclipse.emf.ecore.resource.ResourceSet;
28: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
29: import org.eclipse.emf.ecore.xmi.XMLResource;
30: import org.eclipse.emf.ecp.internal.ui.Activator;
31: import org.eclipse.emf.ecp.internal.ui.PreferenceHelper;
32: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
33: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
34: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
35: import org.eclipse.jface.dialogs.MessageDialog;
36: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
37: import org.eclipse.swt.widgets.Shell;
38: import org.osgi.framework.BundleContext;
39: import org.osgi.framework.FrameworkUtil;
40: import org.osgi.framework.ServiceReference;
41:
42: /**
43: * A utility class which provides support for exporting model elements.
44: *
45: * @author David
46: *
47: */
48: public final class ECPExportHandlerHelper {
49:
50:         private ECPExportHandlerHelper() {
51:         }
52:
53:         private static final String UNEXPECTED_ERROR_MESSAGE = "An unexpected error in a ECP plugin occured."; //$NON-NLS-1$
54:
55:         private static final String FILE_EXTENSION = "xmi"; //$NON-NLS-1$
56:
57:         /**
58:          * These filter names are used to filter which files are displayed.
59:          */
60:         public static final String[] FILTER_NAMES = { "Model Files (*." + FILE_EXTENSION + ")" }; //$NON-NLS-1$ //$NON-NLS-2$
61:
62:         /**
63:          * These filter extensions are used to filter which files are displayed.
64:          */
65:         public static final String[] FILTER_EXTS = { "*." + FILE_EXTENSION }; //$NON-NLS-1$
66:
67:         private static final String EXPORT_MODEL_PATH = "org.eclipse.emf.emfstore.client.ui.exportModelPath"; //$NON-NLS-1$
68:
69:         /**
70:          * Export a list of model elements.
71:          *
72:          * @param shell The shell which should be used for the file dialog
73:          * @param eObjects The {@link EObject}s which should be exported
74:          */
75:         public static void export(Shell shell, List<EObject> eObjects) {
76:•                if (eObjects.size() > 0) {
77:                         final String filePath = getFilePathByFileDialog(shell, getNameForModelElement(eObjects.get(0)));
78:•                        if (filePath == null) {
79:                                 return;
80:                         }
81:                         PreferenceHelper.setPreference(EXPORT_MODEL_PATH, new File(filePath).getParent());
82:                         runCommand(eObjects, filePath, shell);
83:                 }
84:         }
85:
86:         /**
87:          * Get the name of a model element.
88:          *
89:          * @param modelElement the model element
90:          * @return the name for the model element
91:          */
92:         private static String getNameForModelElement(EObject modelElement) {
93:                 ComposedAdapterFactory adapterFactory = null;
94:
95:                 adapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
96:                         new ReflectiveItemProviderAdapterFactory(),
97:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
98:                 final AdapterFactoryLabelProvider labelProvider = new AdapterFactoryLabelProvider(adapterFactory);
99:
100:                 final String text = labelProvider.getText(modelElement);
101:                 adapterFactory.dispose();
102:                 labelProvider.dispose();
103:                 return text;
104:         }
105:
106:         private static void runCommand(final List<EObject> exportModelElements, String filePath, Shell shell) {
107:                 final File file = new File(filePath);
108:
109:                 final URI uri = URI.createFileURI(filePath);
110:
111:                 final ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(shell);
112:
113:                 progressDialog.open();
114:                 progressDialog.getProgressMonitor().beginTask("Export modelelement...", 100); //$NON-NLS-1$
115:                 progressDialog.getProgressMonitor().worked(10);
116:
117:                 try {
118:                         saveEObjectToResource(exportModelElements, uri);
119:                 } catch (final IOException e) {
120:                         showExceptionDialog(e.getMessage(), e, shell);
121:                 }
122:                 progressDialog.getProgressMonitor().done();
123:                 progressDialog.close();
124:
125:                 MessageDialog.openInformation(null, "Export", "Exported modelelement to file " + file.getName()); //$NON-NLS-1$ //$NON-NLS-2$
126:         }
127:
128:         /**
129:          * This method opens a standard error dialog displaying an exception to the user.
130:          *
131:          * @param cause the exception to be shown.
132:          * @param message the message to be shown.
133:          */
134:         private static void showExceptionDialog(String message, Exception cause, Shell shell) {
135:                 final StringBuilder stringBuilder = new StringBuilder();
136:                 stringBuilder.append(message);
137:                 String title = "Error"; //$NON-NLS-1$
138:•                if (cause != null) {
139:                         stringBuilder.append(": "); //$NON-NLS-1$
140:                         stringBuilder.append(cause.getMessage());
141:                         title = cause.getClass().getName();
142:                 }
143:                 final String string = stringBuilder.toString();
144:                 MessageDialog.openError(shell, title, string);
145:                 Activator.log(UNEXPECTED_ERROR_MESSAGE, cause);
146:         }
147:
148:         private static String getFilePathByFileDialog(Shell shell, String modelElementName) {
149:                 final BundleContext bundleContext = FrameworkUtil.getBundle(ECPExportHandlerHelper.class).getBundleContext();
150:                 final ServiceReference<ECPFileDialogHelper> serviceReference = bundleContext
151:                         .getServiceReference(ECPFileDialogHelper.class);
152:                 final ECPFileDialogHelper fileDialogHelper = bundleContext.getService(serviceReference);
153:                 final String result = fileDialogHelper.getPathForExport(shell, modelElementName);
154:                 bundleContext.ungetService(serviceReference);
155:                 return result;
156:         }
157:
158:         /**
159:          * Save a list of EObjects to the resource with the given URI.
160:          *
161:          * @param eObjects the EObjects to be saved
162:          * @param resourceURI the URI of the resource, which should be used to save the EObjects
163:          * @throws IOException if saving to the resource fails
164:          */
165:         private static void saveEObjectToResource(List<? extends EObject> eObjects, URI resourceURI) throws IOException {
166:                 final ResourceSet resourceSet = new ResourceSetImpl();
167:                 final Resource resource = resourceSet.createResource(resourceURI);
168:                 final EList<EObject> contents = resource.getContents();
169:
170:•                for (final EObject eObject : eObjects) {
171:                         contents.add(eObject);
172:                 }
173:
174:                 contents.addAll(eObjects);
175:                 resource.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
176:         }
177:
178: }