Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / handler / FeatureTreeExportWordHandler.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
13 import javax.inject.Named;
14
15 import org.eclipse.core.runtime.jobs.Job;
16 import org.eclipse.e4.core.di.annotations.CanExecute;
17 import org.eclipse.e4.core.di.annotations.Execute;
18 import org.eclipse.e4.core.di.annotations.Optional;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
21 import org.eclipse.e4.ui.services.IServiceConstants;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.widgets.DirectoryDialog;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.progress.IProgressConstants;
26
27 import eu.etaxonomy.cdm.io.descriptive.word.out.WordExportConfigurator;
28 import eu.etaxonomy.cdm.model.term.TermTree;
29 import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
30 import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
31 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
32 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34 import eu.etaxonomy.taxeditor.store.StoreUtil;
35
36 /**
37 * @author pplitzner
38 * @since Jul 12, 2017
39 *
40 */
41 public class FeatureTreeExportWordHandler {
42
43 @Execute
44 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
45 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
46 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart){
47 IFeatureTreeEditor editor = (IFeatureTreeEditor) thisPart.getObject();
48
49 if(StoreUtil.promptCheckIsDirty(editor)){
50 return;
51 }
52
53 TermTree selectedFeatureTree = (TermTree) selection.getFirstElement();
54 DirectoryDialog dialog = new DirectoryDialog(shell);
55 String directoryString = dialog.open();
56 if(directoryString!=null){
57 // create job
58 Job job = CdmStore.getExportManager().createIOServiceJob(WordExportConfigurator.NewInstance(null, null, selectedFeatureTree), new File(directoryString+"/"+selectedFeatureTree.getTitleCache()+".docx"));
59 // configure the job
60 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
61 job.setUser(true);
62 // schedule job
63 job.schedule();
64 }
65 }
66
67 @CanExecute
68 public boolean canExecute(
69 @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
70 MHandledMenuItem menuItem) {
71 boolean canExecute = false;
72 canExecute = PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)
73 && selection!=null
74 && selection.size()==1
75 && selection.getFirstElement() instanceof TermTreeDto;
76 menuItem.setVisible(canExecute);
77 return canExecute;
78 }
79
80 }