Merge branch 'release/5.4.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.MElementContainer;
24 import org.eclipse.e4.ui.model.application.ui.MUIElement;
25 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26 import org.eclipse.e4.ui.workbench.UIEvents;
27 import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
28 import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
29 import org.eclipse.e4.ui.workbench.lifecycle.ProcessAdditions;
30 import org.eclipse.e4.ui.workbench.lifecycle.ProcessRemovals;
31 import org.eclipse.e4.ui.workbench.modeling.EModelService;
32 import org.eclipse.e4.ui.workbench.modeling.EPartService;
33 import org.eclipse.ui.IMemento;
34 import org.eclipse.ui.IPerspectiveRegistry;
35 import org.eclipse.ui.IWorkbench;
36 import org.eclipse.ui.IWorkbenchPage;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.PlatformUI;
39 import org.osgi.service.event.Event;
40
41 import eu.etaxonomy.taxeditor.model.IContextListener;
42 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
43 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
44 import eu.etaxonomy.taxeditor.store.CdmStore;
45
46 /**
47 * @author pplitzner
48 * @date 05.07.2017
49 *
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 public LifeCycleManager() {
62 CdmStore.getContextManager().addContextListener(this);
63 }
64
65 @PostContextCreate
66 private void postContextCreate() {
67 }
68
69 @ProcessAdditions
70 private void processAdditions(MApplication application) {
71 this.application = application;
72 hideParts();
73
74 }
75
76 @ProcessRemovals
77 private void processRemovals() {
78 }
79
80 @PreSave
81 private void preSave(){
82 }
83
84 @Inject
85 @Optional
86 public void subscribeAppShutdownStarted(@UIEventTopic(UIEvents.UILifeCycle.APP_SHUTDOWN_STARTED) Event event,
87 MApplication application) {
88 IEclipseContext context = application.getContext();
89 EPartService partService = context.get(EPartService.class);
90 try {
91 Collection<MPart> dirtyParts = partService.getDirtyParts();
92 if(!dirtyParts.isEmpty()){
93 partService.saveAll(true);
94 }
95 } catch (IllegalStateException e) {
96 //FIXME E4
97 //exception is ignored as this is a temporary fix until full e4 migration
98 }
99 }
100
101 private void hideParts() {
102 List<MPart> elements = modelService.findElements(application, null, MPart.class, Arrays.asList(NON_RESTORE));
103 for (MPart part : elements) {
104 MElementContainer<MUIElement> parent = part.getParent();
105 if(parent!=null){
106 if(parent.getSelectedElement()!=null
107 && parent.getSelectedElement().equals(part)){
108 parent.setSelectedElement(null);
109 }
110 parent.getChildren().remove(part);
111 }
112 }
113
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 @Override
120 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public void contextStop(IMemento memento, IProgressMonitor monitor) {
128 hideParts();
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public void contextStart(IMemento memento, IProgressMonitor monitor) {
136 PreferencesUtil.setDefaults();
137 PreferencesUtil.updateDBPreferences();
138
139 hideParts();
140 if (PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_CHECKLIST_PERSPECTIVE)){
141 IWorkbench workbench = PlatformUI.getWorkbench();
142 IWorkbenchWindow window = workbench
143 .getActiveWorkbenchWindow();
144
145 IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
146 IWorkbenchPage page = window.getActivePage();
147 page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.perspective.checklistperspective"));
148 }
149 PreferencesUtil.checkNomenclaturalCode();
150 }
151
152 /**
153 * {@inheritDoc}
154 */
155 @Override
156 public void contextRefresh(IProgressMonitor monitor) {
157 }
158
159 /**
160 * {@inheritDoc}
161 */
162 @Override
163 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
164 }
165
166 }