Skip to content

Package: AddDescriptionTagHandler

AddDescriptionTagHandler

nameinstructionbranchcomplexitylinemethod
AddDescriptionTagHandler()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
execute(ExecutionEvent)
M: 115 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.genmodel.util.handler;
15:
16: import java.io.IOException;
17: import java.text.MessageFormat;
18: import java.util.Collections;
19:
20: import org.eclipse.core.commands.AbstractHandler;
21: import org.eclipse.core.commands.ExecutionEvent;
22: import org.eclipse.core.commands.ExecutionException;
23: import org.eclipse.core.resources.IFile;
24: import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
25: import org.eclipse.emf.common.util.URI;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.resource.Resource;
28: import org.eclipse.emf.ecore.resource.ResourceSet;
29: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
30: import org.eclipse.emf.ecore.xmi.XMLResource;
31: import org.eclipse.emfforms.editor.genmodel.util.GenModelUtil;
32: import org.eclipse.emfforms.internal.editor.genmodel.util.Activator;
33: import org.eclipse.jface.viewers.ISelection;
34: import org.eclipse.jface.viewers.IStructuredSelection;
35: import org.eclipse.ui.handlers.HandlerUtil;
36:
37: /**
38: * Handler for adding description placeholders to a {@link GenModel}.
39: *
40: * @author Johannes Faltermeier
41: *
42: */
43: public class AddDescriptionTagHandler extends AbstractHandler {
44:
45:         private static final String DESCRIPTION_PLACEHOLDER = "<DESCRIPTION-PLACEHOLDER>"; //$NON-NLS-1$
46:
47:         @Override
48:         public Object execute(ExecutionEvent event) throws ExecutionException {
49:                 try {
50:                         final ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
51:•                        if (!IStructuredSelection.class.isInstance(currentSelection)) {
52:                                 Activator.log("Unknown selection"); //$NON-NLS-1$
53:                                 return null;
54:                         }
55:                         final Object selectedElement = IStructuredSelection.class.cast(currentSelection).getFirstElement();
56:•                        if (!IFile.class.isInstance(selectedElement)) {
57:                                 Activator.log("Selection is not a file"); //$NON-NLS-1$
58:                                 return null;
59:                         }
60:                         final IFile file = IFile.class.cast(selectedElement);
61:                         final URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), false);
62:                         final ResourceSet resourceSet = new ResourceSetImpl();
63:                         final Resource resource = resourceSet.createResource(uri);
64:                         resource.load(null);
65:•                        if (resource.getContents().size() != 1) {
66:                                 Activator.log("Multiple contents detected"); //$NON-NLS-1$
67:                                 return null;
68:                         }
69:                         final EObject resourceContents = resource.getContents().get(0);
70:•                        if (!GenModel.class.isInstance(resourceContents)) {
71:                                 Activator.log("Selection is not a genmodel"); //$NON-NLS-1$
72:                                 return null;
73:                         }
74:                         final GenModel genModel = GenModel.class.cast(resourceContents);
75:                         GenModelUtil.addDescriptionTags(genModel, DESCRIPTION_PLACEHOLDER);
76:•                        for (final Resource r : resourceSet.getResources()) {
77:                                 try {
78:                                         r.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
79:                                 } catch (final IOException ex) {
80:                                         Activator.log(MessageFormat.format("Could not save resource with URI {0}.", resource.getURI()), ex); //$NON-NLS-1$
81:                                 }
82:                         }
83:                 } catch (final IOException ex) {
84:                         Activator.log("Could not load GenModel", ex); //$NON-NLS-1$
85:                 }
86:                 return null;
87:         }
88:
89: }