Skip to content

Package: ExportSchemasWizard

ExportSchemasWizard

nameinstructionbranchcomplexitylinemethod
ExportSchemasWizard(Collection, GenerationExporter, URI)
M: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
addNotAlreadyVisibleLocationPage(IWizardPage, Set)
M: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
addPages()
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getNewLocationProposal(URI, String)
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getNotVisitedLocationPages()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
performFinish()
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
propertyChange(PropertyChangeEvent)
M: 56 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 12 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.wizard;
15:
16: import java.beans.PropertyChangeEvent;
17: import java.beans.PropertyChangeListener;
18: import java.io.IOException;
19: import java.util.Collection;
20: import java.util.HashSet;
21: import java.util.Set;
22:
23: import org.eclipse.emf.common.util.URI;
24: import org.eclipse.emf.ecp.emf2web.controller.xtend.GenerationInfo;
25: import org.eclipse.emf.ecp.emf2web.exporter.DialogToggleInteraction;
26: import org.eclipse.emf.ecp.emf2web.exporter.GenerationExporter;
27: import org.eclipse.emf.ecp.emf2web.ui.messages.Messages;
28: import org.eclipse.jface.dialogs.MessageDialog;
29: import org.eclipse.jface.wizard.IWizardPage;
30: import org.eclipse.jface.wizard.Wizard;
31:
32: /**
33: * Generic wizards for {@link GenerationExporter}s.
34: *
35: * @author Stefan Dirix
36: *
37: */
38: public class ExportSchemasWizard extends Wizard implements PropertyChangeListener {
39:
40:         /**
41:          * Collection of {@link GenerationInfo}s.
42:          */
43:         private final Collection<? extends GenerationInfo> generationInfos;
44:         /**
45:          * The {@link GenerationExporter} responsible for export.
46:          */
47:         private final GenerationExporter exporter;
48:
49:         /**
50:          * Constructor.
51:          *
52:          * @param generationInfos Collection of information used by the exporter to export.
53:          * @param exporter The exporter responsible to export the given generation information.
54:          * @param locationProposal The locationProposal for the export. If {@code null} uses "user.home".
55:          */
56:         public ExportSchemasWizard(Collection<? extends GenerationInfo> generationInfos, GenerationExporter exporter,
57:                 URI locationProposal) {
58:                 setWindowTitle(Messages.getString("ExportSchemasWizard.WindowTitle")); //$NON-NLS-1$
59:                 this.generationInfos = generationInfos;
60:                 this.exporter = exporter;
61:
62:•                if (locationProposal == null) {
63:                         locationProposal = URI.createFileURI(System.getProperty("user.home", "")); //$NON-NLS-1$ //$NON-NLS-2$
64:                 }
65:
66:•                for (final GenerationInfo generationInfo : generationInfos) {
67:                         // Initialize location
68:                         final URI proposal = locationProposal.appendSegment(generationInfo.getNameProposal());
69:                         generationInfo.setLocation(proposal);
70:
71:                         // Observe generationInfos to update default values
72:                         generationInfo.addPropertyChangeListener(this);
73:                 }
74:         }
75:
76:         @Override
77:         public void addPages() {
78:•                for (final GenerationInfo generationInfo : generationInfos) {
79:                         addPage(new SelectLocationPage(generationInfo));
80:                 }
81:         }
82:
83:         @Override
84:         public boolean performFinish() {
85:                 try {
86:                         exporter.export(generationInfos, new DialogToggleInteraction());
87:                         MessageDialog.openInformation(getShell(), Messages.getString("ExportSchemasWizard.DialogTitle_Success"), //$NON-NLS-1$
88:                                 Messages.getString("ExportSchemasWizard.DialogMessage_Success")); //$NON-NLS-1$
89:                         return true;
90:                 } catch (final IOException e) {
91:                         MessageDialog.openError(getShell(), Messages.getString("ExportSchemasWizard.DialogTitle_Error"), //$NON-NLS-1$
92:                                 e.getMessage());
93:                 }
94:                 return false;
95:         }
96:
97:         @Override
98:         public void propertyChange(PropertyChangeEvent event) {
99:•                for (final SelectLocationPage page : getNotVisitedLocationPages()) {
100:                         final GenerationInfo generationInfo = page.getGenerationInfo();
101:                         generationInfo.removePropertyChangeListener(this);
102:•                        if ("location".equals(event.getPropertyName())) { //$NON-NLS-1$
103:                                 final URI newLocation = getNewLocationProposal(URI.class.cast(event.getNewValue()),
104:                                         generationInfo.getNameProposal());
105:                                 generationInfo.setLocation(newLocation);
106:                         }
107:•                        if ("wrap".equals(event.getPropertyName())) { //$NON-NLS-1$
108:                                 generationInfo.setWrap(Boolean.class.cast(event.getNewValue()));
109:                         }
110:                         generationInfo.addPropertyChangeListener(this);
111:                         page.getBindingContext().updateTargets();
112:                 }
113:         }
114:
115:         private Set<SelectLocationPage> getNotVisitedLocationPages() {
116:                 final Set<SelectLocationPage> locationPages = new HashSet<SelectLocationPage>();
117:•                for (final IWizardPage page : getPages()) {
118:                         addNotAlreadyVisibleLocationPage(page, locationPages);
119:                 }
120:                 return locationPages;
121:         }
122:
123:         private void addNotAlreadyVisibleLocationPage(IWizardPage page, Set<SelectLocationPage> locationPages) {
124:•                if (SelectLocationPage.class.isInstance(page)) {
125:                         final SelectLocationPage locationPage = SelectLocationPage.class.cast(page);
126:•                        if (!locationPage.wasAlreadyVisible()) {
127:                                 locationPages.add(locationPage);
128:                         }
129:                 }
130:         }
131:
132:         private URI getNewLocationProposal(URI location, String fileName) {
133:                 URI result = location;
134:                 result = result.trimSegments(1);
135:                 result = result.appendSegment(fileName).trimFileExtension();
136:•                if (location.fileExtension() != null) {
137:                         result = result.appendFileExtension(location.fileExtension());
138:                 }
139:                 return result;
140:         }
141:
142: }