Skip to content

Package: AbstractSchemaExportCommandHandler

AbstractSchemaExportCommandHandler

nameinstructionbranchcomplexitylinemethod
AbstractSchemaExportCommandHandler()
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: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getLocationProposal(ExecutionEvent)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
handleRuntimeException(RuntimeException)
M: 74 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
openWizard(Collection, ExecutionEvent, Shell)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Stefan Dirix - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.emf2web.ui.handler;
15:
16: import java.io.PrintWriter;
17: import java.io.StringWriter;
18: import java.util.ArrayList;
19: import java.util.Collection;
20: import java.util.List;
21:
22: import org.eclipse.core.commands.AbstractHandler;
23: import org.eclipse.core.commands.ExecutionEvent;
24: import org.eclipse.core.commands.ExecutionException;
25: import org.eclipse.core.resources.IContainer;
26: import org.eclipse.core.resources.IFile;
27: import org.eclipse.core.runtime.IStatus;
28: import org.eclipse.core.runtime.MultiStatus;
29: import org.eclipse.core.runtime.Status;
30: import org.eclipse.emf.common.util.URI;
31: import org.eclipse.emf.ecp.emf2web.controller.xtend.GenerationController;
32: import org.eclipse.emf.ecp.emf2web.controller.xtend.GenerationInfo;
33: import org.eclipse.emf.ecp.emf2web.exporter.GenerationExporter;
34: import org.eclipse.emf.ecp.emf2web.ui.messages.Messages;
35: import org.eclipse.emf.ecp.emf2web.ui.wizard.ExportSchemasWizard;
36: import org.eclipse.emf.ecp.view.spi.model.VView;
37: import org.eclipse.jface.dialogs.ErrorDialog;
38: import org.eclipse.jface.viewers.IStructuredSelection;
39: import org.eclipse.jface.wizard.WizardDialog;
40: import org.eclipse.swt.graphics.Point;
41: import org.eclipse.swt.widgets.Shell;
42: import org.eclipse.ui.handlers.HandlerUtil;
43:
44: /**
45: * Abstract implementation for an handler responsible for exporting view models.
46: */
47: public abstract class AbstractSchemaExportCommandHandler extends AbstractHandler {
48:
49:         /**
50:          * This implementation uses the {@link #getViews(ExecutionEvent)}
51:          * and {@link #openWizard(Collection, ExecutionEvent, Shell)} methods to open an export wizard.
52:          *
53:          * @param event {@inheritDoc}
54:          * @return {@inheritDoc}
55:          * @throws ExecutionException {@inheritDoc}
56:          */
57:         @Override
58:         public Object execute(ExecutionEvent event) throws ExecutionException {
59:                 try {
60:                         final Collection<VView> views = getViews(event);
61:•                        if (views == null || views.isEmpty()) {
62:                                 return null;
63:                         }
64:
65:                         final Shell shell = HandlerUtil.getActiveShell(event);
66:                         openWizard(views, event, shell);
67:                         // BEGIN SUPRESS CATCH EXCEPTION
68:                 } catch (final RuntimeException e) {
69:                         // END SUPRESS CATCH EXCEPTION
70:                         handleRuntimeException(e);
71:                 }
72:
73:                 return null;
74:         }
75:
76:         /**
77:          * Is called if a RuntimeException occurs during execution.
78:          *
79:          * @param e the {@link RuntimeException}.
80:          * @throws ExecutionException
81:          * Wraps the given RuntimeException and throws it.
82:          */
83:         protected void handleRuntimeException(RuntimeException e) throws ExecutionException {
84:                 final StringWriter sw = new StringWriter();
85:                 e.printStackTrace(new PrintWriter(sw));
86:                 final String stackTrace = sw.toString();
87:
88:                 final List<Status> childStatuses = new ArrayList<Status>();
89:•                for (final String line : stackTrace.split(System.getProperty("line.separator"))) { //$NON-NLS-1$
90:                         childStatuses.add(new Status(IStatus.ERROR, "org.eclipse.emf.ecp.emf2web.ui", line)); //$NON-NLS-1$
91:                 }
92:                 final MultiStatus status = new MultiStatus("org.eclipse.emf.ecp.emf2web.ui", IStatus.ERROR, //$NON-NLS-1$
93:                         childStatuses.toArray(new Status[] {}),
94:                         e.getLocalizedMessage(), e);
95:
96:                 ErrorDialog.openError(null, Messages.getString("AbstractSchemaExportCommandHandler.Error_Title"), //$NON-NLS-1$
97:                         Messages.getString("AbstractSchemaExportCommandHandler.Error_Message"), //$NON-NLS-1$
98:                         status);
99:                 throw new ExecutionException(Messages.getString("AbstractSchemaExportCommandHandler.Error_Message"), e); //$NON-NLS-1$
100:         }
101:
102:         /**
103:          * The default implementation opens the {@link ExportSchemaWizard} using the generation handlers returned by
104:          * {@link #getGenerationController()} and {@link #getGenerationExporter()}.
105:          *
106:          * @param views
107:          * The views which shall be exported.
108:          * @param event
109:          * The {@link ExecutionEvent} which is given by the {@link #execute(ExecutionEvent)} method.
110:          * @param shell
111:          * The shell for the wizard.
112:          * @return
113:          *                 The return value of the {@link WizardDialog}.
114:          */
115:         protected int openWizard(Collection<VView> views, ExecutionEvent event, Shell shell) {
116:                 final List<GenerationInfo> generationInfos = getGenerationController().generate(views);
117:                 final URI locationProposal = getLocationProposal(event);
118:                 final ExportSchemasWizard wizard = new ExportSchemasWizard(generationInfos, getGenerationExporter(),
119:                         locationProposal);
120:                 final WizardDialog dialog = new WizardDialog(shell, wizard);
121:                 dialog.setPageSize(new Point(600, 600));
122:                 return dialog.open();
123:         }
124:
125:         /**
126:          * Returns a proposal for the export location. The default implementation returns the container of the first
127:          * selected element.
128:          *
129:          * @param event
130:          * The {@link ExecutionEvent} which is given by the {@link #execute(ExecutionEvent)} method.
131:          * @return
132:          *                 The location proposal for the export. {@code null} if no proposal could be determined.
133:          */
134:         protected URI getLocationProposal(ExecutionEvent event) {
135:                 final IStructuredSelection selection = (IStructuredSelection) HandlerUtil
136:                         .getCurrentSelection(event);
137:                 final Object firstElement = selection.getFirstElement();
138:•                if (IFile.class.isInstance(firstElement)) {
139:                         final IFile file = IFile.class.cast(firstElement);
140:                         final IContainer container = file.getParent();
141:                         return URI.createPlatformResourceURI(container.getFullPath().toString(), true);
142:                 }
143:                 return null;
144:         }
145:
146:         /**
147:          * Returns the views which shall be exported.
148:          *
149:          * @param event
150:          * The {@link ExecutionEvent} which is given by the {@link #execute(ExecutionEvent)} method.
151:          * @return
152:          *                 The collection of views which shall be exported.
153:          */
154:         protected abstract Collection<VView> getViews(ExecutionEvent event);
155:
156:         /**
157:          * Returns the {@link GenerationController} which shall be used to generate the files.
158:          *
159:          * @return
160:          *                 The {@link GenerationController} which shall be used.
161:          */
162:         protected abstract GenerationController getGenerationController();
163:
164:         /**
165:          * Returns the {@link GenerationExporter} responsible for creating the files generated by the
166:          * {@link GenerationController}.
167:          *
168:          * @return
169:          *                 The {@link GenerationExporter} which shall be used.
170:          */
171:         protected abstract GenerationExporter getGenerationExporter();
172: }