Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / handler / FeatureTreeExportOntologyHandler.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.featuretree.e4.handler;
10
11 import java.io.File;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.UUID;
15
16 import javax.inject.Named;
17
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.e4.core.di.annotations.CanExecute;
20 import org.eclipse.e4.core.di.annotations.Execute;
21 import org.eclipse.e4.core.di.annotations.Optional;
22 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
24 import org.eclipse.e4.ui.services.IServiceConstants;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.swt.widgets.FileDialog;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.progress.IProgressConstants;
29
30 import eu.etaxonomy.cdm.io.descriptive.owl.out.StructureTreeOwlExportConfigurator;
31 import eu.etaxonomy.cdm.model.term.TermTree;
32 import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
33 import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35 import eu.etaxonomy.taxeditor.store.StoreUtil;
36
37 /**
38 * @author pplitzner
39 * @since Jul 12, 2017
40 *
41 */
42 public class FeatureTreeExportOntologyHandler {
43
44 @Execute
45 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
46 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
47 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart){
48 IFeatureTreeEditor editor = (IFeatureTreeEditor) thisPart.getObject();
49
50 if(StoreUtil.promptCheckIsDirty(editor)){
51 return;
52 }
53
54 List<UUID> featureTreeUuids = new ArrayList<>();
55 selection.toList().stream().filter(o->o instanceof TermTree).forEach(tree->featureTreeUuids.add(((TermTree)tree).getUuid()));
56
57 FileDialog dialog = new FileDialog(shell);
58 String[] filterNames = {
59 "Web Ontology Language (*.owl)",
60 "Extensible Markup Language (*.xml)",
61 "All files (*.*)"
62 };
63 String[] filterExtensions = { "*.owl", "*.xml", "*.*"};
64
65 dialog.setOverwrite(true);
66 dialog.setFilterNames(filterNames);
67 dialog.setFilterExtensions(filterExtensions);
68 String fileString = dialog.open();
69 if(fileString!=null){
70 // create job
71 StructureTreeOwlExportConfigurator configurator = StructureTreeOwlExportConfigurator.NewInstance();
72
73 configurator.setFeatureTreeUuids(featureTreeUuids);
74
75 Job job = CdmStore.getExportManager().createIOServiceJob(configurator, new File(fileString));
76 // configure the job
77 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
78 job.setUser(true);
79 // schedule job
80 job.schedule();
81 }
82 }
83
84 @CanExecute
85 public boolean canExecute(
86 @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
87 MHandledMenuItem menuItem) {
88 boolean canExecute = false;
89 canExecute = selection!=null
90 && selection.size()==1
91 && selection.getFirstElement() instanceof TermTreeDto;
92 menuItem.setVisible(canExecute);
93 return canExecute;
94 }
95
96 }