Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / LifeCycleManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2017 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor;
11
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.List;
15
16 import javax.inject.Inject;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.e4.core.contexts.IEclipseContext;
20 import org.eclipse.e4.core.di.annotations.Optional;
21 import org.eclipse.e4.ui.di.UIEventTopic;
22 import org.eclipse.e4.ui.model.application.MApplication;
23 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
24 import org.eclipse.e4.ui.workbench.UIEvents;
25 import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
26 import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
27 import org.eclipse.e4.ui.workbench.lifecycle.ProcessAdditions;
28 import org.eclipse.e4.ui.workbench.lifecycle.ProcessRemovals;
29 import org.eclipse.e4.ui.workbench.modeling.EModelService;
30 import org.eclipse.e4.ui.workbench.modeling.EPartService;
31 import org.eclipse.ui.IMemento;
32 import org.eclipse.ui.IPerspectiveRegistry;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.views.IViewDescriptor;
38 import org.osgi.service.event.Event;
39
40 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
41 import eu.etaxonomy.taxeditor.editor.AppModelId;
42 import eu.etaxonomy.taxeditor.editor.EditorUtil;
43 import eu.etaxonomy.taxeditor.model.IContextListener;
44 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45 import eu.etaxonomy.taxeditor.store.CdmStore;
46
47 /**
48 * @author pplitzner
49 * @date 05.07.2017
50 */
51 public class LifeCycleManager implements IContextListener{
52
53 private static final String NON_RESTORE = "nonRestore";
54
55 private MApplication application;
56
57 @Inject
58 private EModelService modelService;
59
60 @Inject
61 private EPartService partService;
62
63 @Inject
64 public LifeCycleManager() {
65 CdmStore.getContextManager().addContextListener(this);
66 }
67
68 @PostContextCreate
69 private void postContextCreate() {
70 }
71
72 @ProcessAdditions
73 private void processAdditions(MApplication application) {
74 this.application = application;
75 hideParts();
76 }
77
78 @ProcessRemovals
79 private void processRemovals() {
80 }
81
82 @PreSave
83 private void preSave(){
84 }
85
86 @Inject
87 @Optional
88 public void subscribeAppShutdownStarted(@UIEventTopic(UIEvents.UILifeCycle.APP_SHUTDOWN_STARTED) Event event,
89 MApplication application) {
90 IEclipseContext context = application.getContext();
91 EPartService partService = context.get(EPartService.class);
92 try {
93 Collection<MPart> dirtyParts = partService.getDirtyParts();
94 if(!dirtyParts.isEmpty()){
95 partService.saveAll(true);
96 }
97 } catch (IllegalStateException e) {
98 //FIXME E4
99 //exception is ignored as this is a temporary fix until full e4 migration
100 }
101 }
102
103 private void hideParts() {
104 if (CdmStore.isActive()) {
105 try{
106 List<MPart> elements = modelService.findElements(application, null, MPart.class, Arrays.asList(NON_RESTORE));
107
108 Collection<MPart> parts = partService.getParts();
109 for (MPart element : elements) {
110 String elementId = element.getElementId();
111 for (MPart part : parts) {
112 String partID = part.getElementId();
113 if (elementId.equals(partID)){
114 partService.hidePart(part);
115 }
116 }
117 }
118 }catch(IllegalStateException e){
119 //application does not have an active window
120 }
121 }
122 }
123
124 @Override
125 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
126
127 }
128
129 @Override
130 public void contextStop(IMemento memento, IProgressMonitor monitor) {
131 hideParts();
132 if (CdmStore.isActive()){
133 IWorkbench workbench = PlatformUI.getWorkbench();
134 IWorkbenchWindow window = workbench
135 .getActiveWorkbenchWindow();
136 IViewDescriptor viewDescriptor = workbench.getViewRegistry().find(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4);
137 if(viewDescriptor == null){
138 memento.putBoolean("facts", false);
139 }
140 viewDescriptor = workbench.getViewRegistry().find(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4);
141 if(viewDescriptor == null){
142 memento.putBoolean("media", false);
143 }
144 }
145 }
146
147 @Override
148 public void contextStart(IMemento memento, IProgressMonitor monitor) {
149 PreferencesUtil.setDefaults();
150 PreferencesUtil.updateDBPreferences();
151
152 hideParts();
153
154 IWorkbench workbench = PlatformUI.getWorkbench();
155 IWorkbenchWindow window = workbench
156 .getActiveWorkbenchWindow();
157 if (PreferencesUtil.getBooleanValue(PreferencePredicate.ShowChecklistPerspective.getKey())){
158
159
160 IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
161 IWorkbenchPage page = window.getActivePage();
162 page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.perspective.checklistperspective"));
163 EditorUtil.setFactsVisible(false);
164 EditorUtil.setMediaVisible(false);
165 }else{
166
167
168 IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
169 IWorkbenchPage page = window.getActivePage();
170 page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.application.perspective.taxonomic"));
171
172 }
173 hideParts();
174
175 PreferencesUtil.checkNomenclaturalCode();
176
177 }
178
179 /**
180 * {@inheritDoc}
181 */
182 @Override
183 public void contextRefresh(IProgressMonitor monitor) {
184 }
185
186 /**
187 * {@inheritDoc}
188 */
189 @Override
190 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
191 }
192
193 }