3 * Copyright (C) 2017 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.List
;
16 import javax
.inject
.Inject
;
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
;
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
;
51 public class LifeCycleManager
implements IContextListener
{
53 private static final String NON_RESTORE
= "nonRestore";
55 private MApplication application
;
58 private EModelService modelService
;
61 private EPartService partService
;
64 public LifeCycleManager() {
65 CdmStore
.getContextManager().addContextListener(this);
69 private void postContextCreate() {
73 private void processAdditions(MApplication application
) {
74 this.application
= application
;
79 private void processRemovals() {
83 private void preSave(){
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);
93 Collection
<MPart
> dirtyParts
= partService
.getDirtyParts();
94 if(!dirtyParts
.isEmpty()){
95 partService
.saveAll(true);
97 } catch (IllegalStateException e
) {
99 //exception is ignored as this is a temporary fix until full e4 migration
103 private void hideParts() {
104 if (CdmStore
.isActive()) {
106 List
<MPart
> elements
= modelService
.findElements(application
, null, MPart
.class, Arrays
.asList(NON_RESTORE
));
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
);
118 }catch(IllegalStateException e
){
119 //application does not have an active window
125 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
130 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
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);
140 viewDescriptor
= workbench
.getViewRegistry().find(AppModelId
.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4
);
141 if(viewDescriptor
== null){
142 memento
.putBoolean("media", false);
148 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
149 PreferencesUtil
.setDefaults();
150 PreferencesUtil
.updateDBPreferences();
154 IWorkbench workbench
= PlatformUI
.getWorkbench();
155 IWorkbenchWindow window
= workbench
156 .getActiveWorkbenchWindow();
157 if (PreferencesUtil
.getBooleanValue(PreferencePredicate
.ShowChecklistPerspective
.getKey())){
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);
168 IPerspectiveRegistry registry
= workbench
.getPerspectiveRegistry();
169 IWorkbenchPage page
= window
.getActivePage();
170 page
.setPerspective(registry
.findPerspectiveWithId("eu.etaxonomy.taxeditor.application.perspective.taxonomic"));
175 PreferencesUtil
.checkNomenclaturalCode();
183 public void contextRefresh(IProgressMonitor monitor
) {
190 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {