| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Regent L'Archeveque - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| *******************************************************************************/ |
| package org.eclipse.apogy.workspace.ui.wizards; |
| |
| import java.lang.reflect.InvocationTargetException; |
| |
| import org.eclipse.apogy.common.ApogyCommonOSGiUtilities; |
| import org.eclipse.apogy.common.emf.ui.wizards.NamedDescribedWizardPage; |
| import org.eclipse.apogy.common.ui.ApogyCommonUiFacade; |
| import org.eclipse.apogy.workspace.ApogyWorkspaceFacade; |
| import org.eclipse.apogy.workspace.ui.ApogyWorkspaceUiFactory; |
| import org.eclipse.apogy.workspace.ui.NewProjectSettings; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.jface.wizard.Wizard; |
| import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| import org.osgi.framework.Bundle; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class ImportRegisteredProjectWizard extends Wizard { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(ImportRegisteredProjectWizard.class); |
| |
| private final NamedDescribedWizardPage namedDescribedWizardPage; |
| private NewProjectSettings newProjectSettings; |
| private final Bundle selectedBundle; |
| |
| public ImportRegisteredProjectWizard(Bundle selectedBundle) { |
| super(); |
| this.selectedBundle = selectedBundle; |
| setWindowTitle("New Apogy Project"); |
| setNeedsProgressMonitor(true); |
| // FIXME Remove dependencies to org.eclipse.ui. |
| setDefaultPageImageDescriptor(ApogyCommonUiFacade.INSTANCE |
| .getImageDescriptor("platform:/plugin/" + ApogyCommonOSGiUtilities.INSTANCE.getBundleSymbolicName(this.getClass()) + "/icons/wizban/apogy.png")); |
| |
| this.namedDescribedWizardPage = new NamedDescribedWizardPage(getNewApogyProjectSettings(), |
| getNewApogyProjectSettings()) { |
| @Override |
| protected void validate() { |
| super.validate(); |
| if (ApogyWorkspaceFacade.INSTANCE.isProjectExists(getNewApogyProjectSettings().getName())) { |
| setErrorMessage("A project with the same name already exists"); |
| setPageComplete(false); |
| } |
| } |
| }; |
| } |
| |
| /** |
| * Adding the page to the wizard. |
| */ |
| @Override |
| public void addPages() { |
| addPage(this.namedDescribedWizardPage); |
| } |
| |
| private NewProjectSettings getNewApogyProjectSettings() { |
| if (this.newProjectSettings == null) { |
| this.newProjectSettings = ApogyWorkspaceUiFactory.eINSTANCE.createNewProjectSettings(); |
| |
| /* Find an available name. */ |
| String name = this.selectedBundle.getSymbolicName(); |
| int i = 1; |
| while (ApogyWorkspaceFacade.INSTANCE.isProjectExists(name)) { |
| name = this.selectedBundle.getSymbolicName() + "_" + i; |
| i++; |
| } |
| this.newProjectSettings.setName(name); |
| } |
| return this.newProjectSettings; |
| } |
| |
| @Override |
| public boolean performFinish() { |
| WorkspaceModifyOperation createApogyProjectOperation = new WorkspaceModifyOperation() { |
| @Override |
| protected void execute(IProgressMonitor monitor) |
| throws CoreException, InvocationTargetException, InterruptedException { |
| try { |
| // Create the project. |
| ApogyWorkspaceFacade.INSTANCE.importApogyProject(ImportRegisteredProjectWizard.this.selectedBundle, |
| getNewApogyProjectSettings().getName()); |
| } catch (Exception e) { |
| Logger.error("Problems occured while importing project <" |
| + getNewApogyProjectSettings().getName() + ">.", e); |
| } finally { |
| monitor.done(); |
| } |
| } |
| }; |
| |
| /** |
| * Execute Jobs. |
| */ |
| try { |
| getContainer().run(false, false, createApogyProjectOperation); |
| } catch (Exception e) { |
| Logger.error("Problems occured while importing project <" + getNewApogyProjectSettings().getName() + ">.", |
| e); |
| } |
| return true; |
| } |
| } |