Skip to content

Commit

Permalink
[SAFRAN-906] Initialize path in file selection dialog for Swagger Imp…
Browse files Browse the repository at this point in the history
…ort/Export
  • Loading branch information
vrichard12 committed Feb 11, 2021
1 parent 8c888b9 commit 13fced9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Require-Bundle: org.eclipse.ui,
org.obeonetwork.dsl.environment.design.services,
org.eclipse.sirius,
org.eclipse.emf.transaction,
org.obeonetwork.utils.common
org.obeonetwork.utils.common,
org.obeonetwork.utils.sirius
Import-Package: org.eclipse.jface.databinding.swt,
org.obeonetwork.utils.sirius.transaction
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.sirius.business.api.modelingproject.ModelingProject;
import org.eclipse.sirius.business.api.query.EObjectQuery;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
Expand All @@ -32,6 +35,7 @@
import org.obeonetwork.dsl.environment.design.services.ModelServices;
import org.obeonetwork.dsl.soa.System;
import org.obeonetwork.dsl.soa.gen.swagger.SwaggerImporter;
import org.obeonetwork.utils.sirius.session.SessionUtils;
import org.obeonetwork.utils.sirius.transaction.RecordingCommandWithResult;

public class ImportSwaggerHandler extends AbstractHandler implements IHandler {
Expand All @@ -44,6 +48,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
Shell shell = HandlerUtil.getActiveShell(event);
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(new String [] { "*.yaml;*.json", "*.yaml", "*.json" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
dialog.setFilterPath(getDefaultInputDirPath(system));
String swaggerFilePath = dialog.open();

if(swaggerFilePath != null) {
Expand Down Expand Up @@ -92,6 +97,19 @@ protected Integer doExecuteWithResult() {

}

public String getDefaultInputDirPath(System system) {
String defaultInputDirPath = null;
if(system != null) {
Session session = new EObjectQuery(system).getSession();
ModelingProject modelingProject = SessionUtils.getModelingProjectFromSession(session);
defaultInputDirPath = modelingProject.getProject().getLocation().toOSString();
} else {
defaultInputDirPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
}

return defaultInputDirPath;
}

private System unwrapSelection(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
ISelectionService service = window.getSelectionService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,40 @@
import java.io.File;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.sirius.business.api.modelingproject.ModelingProject;
import org.eclipse.sirius.business.api.query.EObjectQuery;
import org.eclipse.sirius.business.api.session.Session;
import org.obeonetwork.dsl.soa.Component;
import org.obeonetwork.dsl.soa.gen.swagger.SwaggerExporter;
import org.obeonetwork.dsl.soa.gen.swagger.SwaggerExporter.MapperType;
import org.obeonetwork.dsl.soa.gen.swagger.ui.handlers.Messages;
import org.obeonetwork.dsl.soa.gen.swagger.utils.ComponentGenUtil;
import org.obeonetwork.utils.sirius.session.SessionUtils;

public class GenerateComponentsSwaggerWizard extends Wizard {

private List<Component> components;
private ModelingProject enclosingModelingProject = null;

private GenerateComponentsSwaggerWizardOptionsPage generateComponentsSwaggerWizardOptionsPage;

public GenerateComponentsSwaggerWizard(List<Component> components) {
super();
this.components = components;

if(components != null && !components.isEmpty()) {
Component firstComponent = components.get(0);
Session session = new EObjectQuery(firstComponent).getSession();
enclosingModelingProject = SessionUtils.getModelingProjectFromSession(session);
}

setWindowTitle(Messages.GenerateComponentsSwaggerWizard_Title);

generateComponentsSwaggerWizardOptionsPage = new GenerateComponentsSwaggerWizardOptionsPage(this);
Expand All @@ -47,6 +62,17 @@ public boolean canFinish() {
return !components.isEmpty() && generateComponentsSwaggerWizardOptionsPage.isComplete();
}

public String getDefaultOutputDirPath() {
String defaultOutputDirPath = null;
if(enclosingModelingProject != null) {
defaultOutputDirPath = enclosingModelingProject.getProject().getLocation().toOSString();
} else {
defaultOutputDirPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
}

return defaultOutputDirPath;
}

@Override
public boolean performFinish() {
boolean exitStatus = true;
Expand Down Expand Up @@ -112,6 +138,14 @@ public boolean performFinish() {
break;
}

if(status != IStatus.ERROR) {
try {
enclosingModelingProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
} catch (CoreException e) {
// That can't be that critical
}
}

return exitStatus;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@ public void createControl(Composite parent) {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(GenerateComponentsSwaggerWizardOptionsPage.this.getShell(), SWT.OPEN);
model.setOutputDirPath(dialog.open());
dialog.setFilterPath(model.getOutputDirPath());
String outputDirPath = dialog.open();
if(outputDirPath != null) {
model.setOutputDirPath(outputDirPath);
}
}
});
btnSelectOutputDirPath.setText("..."); //$NON-NLS-1$

model.setOutputDirPath(wizard.getDefaultOutputDirPath());

LabelProvider enumLabelProvider = new LabelProvider() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
*******************************************************************************/
package org.obeonetwork.utils.sirius.session;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.sirius.business.api.modelingproject.ModelingProject;
import org.eclipse.sirius.business.api.query.AirDResouceQuery;
import org.eclipse.sirius.business.api.query.URIQuery;
import org.eclipse.sirius.business.api.session.Session;
Expand Down Expand Up @@ -77,4 +82,20 @@ private boolean isRemoteResource(Resource resource) {
}
return false;
}

public static ModelingProject getModelingProjectFromSession(Session session) {
if (session == null || session.getSessionResource() == null) {
return null;
}
URI uri = session.getSessionResource().getURI();
if (!uri.isPlatformResource()) {
return null;
}

IPath path = new Path(uri.toPlatformString(true));
IProject project = ResourcesPlugin.getWorkspace().getRoot().getFile(path).getProject();

return ModelingProject.asModelingProject(project).get();
}

}

0 comments on commit 13fced9

Please sign in to comment.