X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.migration.ui%2Fsrc%2Forg%2Fsimantics%2Fmigration%2Fui%2FMigrateActionFactory.java;fp=bundles%2Forg.simantics.migration.ui%2Fsrc%2Forg%2Fsimantics%2Fmigration%2Fui%2FMigrateActionFactory.java;h=414b117390d6e1e5954b6f8d6e4be21d7ea8f329;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.migration.ui/src/org/simantics/migration/ui/MigrateActionFactory.java b/bundles/org.simantics.migration.ui/src/org/simantics/migration/ui/MigrateActionFactory.java new file mode 100644 index 000000000..414b11739 --- /dev/null +++ b/bundles/org.simantics.migration.ui/src/org/simantics/migration/ui/MigrateActionFactory.java @@ -0,0 +1,93 @@ +package org.simantics.migration.ui; + +import java.util.ArrayList; +import java.util.Collections; + +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.dialogs.ListDialog; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.layer0.SoftwareConfigurationResources; +import org.simantics.migration.ui.MigrationAnalysis.Update; + +public class MigrateActionFactory implements ActionFactory { + + @Override + public Runnable create(Object target) { + final Resource resource = (Resource)target; + return new Runnable() { + @Override + public void run() { + analyze(resource); + } + }; + } + + private void analyze(final Resource resource) { + Simantics.getSession().asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + SoftwareConfigurationResources SC = SoftwareConfigurationResources.getInstance(graph); + final ArrayList updates = new ArrayList(); + for(Resource version : graph.getObjects(resource, SC.IsCompatibleWith)) { + MigrationAnalysis analysis = new MigrationAnalysis(graph, version); + updates.addAll(analysis.getUpdates()); + } + Collections.sort(updates); + Display.getDefault().asyncExec(new Runnable() { + @Override + public void run() { + select(resource, updates); + } + }); + } + }); + } + + private void select(final Resource resource, final ArrayList updates) { + ListDialog listDialog = new ListDialog(Display.getCurrent().getActiveShell()); + listDialog.setTitle("Migrate"); + listDialog.setMessage("Choose the version to migrate to"); + listDialog.setContentProvider(new IStructuredContentProvider() { + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + } + + @Override + public void dispose() { + } + + @Override + public Object[] getElements(Object inputElement) { + return updates.toArray(); + } + }); + listDialog.setLabelProvider(new LabelProvider() { + @Override + public String getText(Object element) { + return ((Update)element).versionName; + } + }); + listDialog.setInput(updates); + if(listDialog.open() == ListDialog.OK) { + Object[] result = listDialog.getResult(); + if(result.length == 1) { + Update update = (Update)result[0]; + try { + update.execute(resource); + update.updateTargetVersion(resource); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + } + } + +}