Skip to content

Package: ImportProjectHelper$1

ImportProjectHelper$1

nameinstructionbranchcomplexitylinemethod
isValid(String)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
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%

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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.emfstore.internal.ui.handler;
15:
16: import java.io.File;
17: import java.io.IOException;
18: import java.util.List;
19:
20: import org.eclipse.core.runtime.NullProgressMonitor;
21: import org.eclipse.emf.ecp.core.exceptions.ECPProjectWithNameExistsException;
22: import org.eclipse.emf.ecp.core.util.ECPProperties;
23: import org.eclipse.emf.ecp.core.util.ECPUtil;
24: import org.eclipse.emf.ecp.emfstore.core.internal.ECPEMFUtils;
25: import org.eclipse.emf.ecp.emfstore.core.internal.EMFStoreProvider;
26: import org.eclipse.emf.ecp.emfstore.internal.ui.Activator;
27: import org.eclipse.emf.ecp.internal.ui.PreferenceHelper;
28: import org.eclipse.emf.ecp.internal.ui.util.ECPExportHandlerHelper;
29: import org.eclipse.emf.ecp.internal.ui.util.ECPFileDialogHelper;
30: import org.eclipse.emf.emfstore.client.ESLocalProject;
31: import org.eclipse.emf.emfstore.internal.client.importexport.ExportImportControllerExecutor;
32: import org.eclipse.emf.emfstore.internal.client.importexport.ExportImportControllerFactory;
33: import org.eclipse.emf.emfstore.internal.client.importexport.impl.ExportImportDataUnits;
34: import org.eclipse.jface.dialogs.IInputValidator;
35: import org.eclipse.jface.dialogs.InputDialog;
36: import org.eclipse.jface.window.Window;
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 helper class that can be used to import projects.
44: *
45: * @author Eugen Neufeld, David Soto Setzke
46: *
47: */
48: public final class ImportProjectHelper {
49:
50:         private static final String FILE_EXTENSION = ExportImportDataUnits.ProjectSpace.getExtension();
51:
52:         /**
53:          * These filter names are used to filter which files are displayed.
54:          */
55:         public static final String[] FILTER_NAMES = { "Model Files (*" + FILE_EXTENSION + ")" }; //$NON-NLS-1$ //$NON-NLS-2$
56:
57:         /**
58:          * These filter extensions are used to filter which files are displayed.
59:          */
60:         public static final String[] FILTER_EXTS = { "*" + FILE_EXTENSION }; //$NON-NLS-1$
61:
62:         private static final String EXPORT_MODEL_PATH = "org.eclipse.emf.ecp.exportProjectModelPath"; //$NON-NLS-1$
63:
64:         private ImportProjectHelper() {
65:         }
66:
67:         /**
68:          * Imports a project.
69:          *
70:          * @param shell The active shell
71:          */
72:         public static void importProject(Shell shell) {
73:                 final File file = getFile(shell);
74:                 if (file == null) {
75:                         return;
76:                 }
77:
78:                 try {
79:                         new ExportImportControllerExecutor(file, new NullProgressMonitor())
80:                                 .execute(ExportImportControllerFactory.Import.getImportProjectSpaceController());
81:                         PreferenceHelper.setPreference(EXPORT_MODEL_PATH, file.getParent());
82:
83:                         final List<ESLocalProject> localProjects = ECPEMFUtils.getESWorkspaceProviderInstance().getWorkspace()
84:                                 .getLocalProjects();
85:                         final ESLocalProject localProject = localProjects.get(localProjects.size() - 1);
86:                         final ECPProperties properties = ECPUtil.createProperties();
87:                         properties.addProperty(EMFStoreProvider.PROP_PROJECTSPACEID, localProject.getLocalProjectId().getId());
88:                         final InputDialog id = new InputDialog(shell, "Project Name", //$NON-NLS-1$
89:                                 "Enter the name for the imported project.", localProject.getProjectName(), new IInputValidator() { //$NON-NLS-1$
90:
91:                                         @Override
92:                                         public String isValid(String newText) {
93:•                                                return ECPUtil.getECPProjectManager().getProject(newText) == null ? null
94:                                                         : String.format(
95:                                                                 "A project with the name %s already exists.", newText); //$NON-NLS-1$
96:                                         }
97:                                 });
98:                         final int result = id.open();
99:                         if (Window.CANCEL == result) {
100:                                 return;
101:                         }
102:                         final String projectName = id.getValue();
103:                         ECPUtil.getECPProjectManager().createProject(
104:                                 ECPUtil.getECPProviderRegistry().getProvider(EMFStoreProvider.NAME), projectName, properties);
105:
106:                 } catch (final IOException ex) {
107:                         Activator.log(ex);
108:                 } catch (final ECPProjectWithNameExistsException ex) {
109:                         Activator.log(ex);
110:                 }
111:         }
112:
113:         private static File getFile(Shell shell) {
114:                 final BundleContext bundleContext = FrameworkUtil.getBundle(ECPExportHandlerHelper.class).getBundleContext();
115:                 final ServiceReference<ECPFileDialogHelper> serviceReference = bundleContext
116:                         .getServiceReference(ECPFileDialogHelper.class);
117:                 final ECPFileDialogHelper fileDialogHelper = bundleContext.getService(serviceReference);
118:                 final String result = fileDialogHelper.getPathForImport(shell);
119:                 bundleContext.ungetService(serviceReference);
120:                 if (result == null) {
121:                         return null;
122:                 }
123:                 return new File(result);
124:         }
125:
126: }