Project

General

Profile

Download (6.2 KB) Statistics
| Branch: | Tag: | Revision:
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.model.IContextListener;
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
    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

    
79
    @ProcessRemovals
80
    private void processRemovals() {
81
    }
82

    
83
    @PreSave
84
    private void preSave(){
85
    }
86

    
87
    @Inject
88
    @Optional
89
    public void subscribeAppShutdownStarted(@UIEventTopic(UIEvents.UILifeCycle.APP_SHUTDOWN_STARTED) Event event,
90
            MApplication application) {
91
        IEclipseContext context = application.getContext();
92
        EPartService partService = context.get(EPartService.class);
93
        try {
94
            Collection<MPart> dirtyParts = partService.getDirtyParts();
95
            if(!dirtyParts.isEmpty()){
96
                partService.saveAll(true);
97
            }
98
        } catch (IllegalStateException e) {
99
            //FIXME E4
100
            //exception is ignored as this is a temporary fix until full e4 migration
101
        }
102
    }
103

    
104
    private void hideParts() {
105
        if (CdmStore.isActive()) {
106
            try{
107
                List<MPart> elements = modelService.findElements(application, null, MPart.class, Arrays.asList(NON_RESTORE));
108

    
109
                Collection<MPart> parts = partService.getParts();
110
                for (MPart element : elements) {
111
                    String elementId = element.getElementId();
112
                    for (MPart part : parts) {
113
                        String partID = part.getElementId();
114
                        if (elementId.equals(partID)){
115
                            partService.hidePart(part);
116
                        }
117
                    }
118
                }
119
            }catch(IllegalStateException e){
120
                //application does not have an active window
121
            }
122

    
123
        }
124

    
125

    
126

    
127
    }
128

    
129
    /**
130
     * {@inheritDoc}
131
     */
132
    @Override
133
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
134

    
135
    }
136

    
137
    /**
138
     * {@inheritDoc}
139
     */
140
    @Override
141
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
142
        hideParts();
143
        if (CdmStore.isActive()){
144
            IWorkbench workbench = PlatformUI.getWorkbench();
145
            IWorkbenchWindow window = workbench
146
                    .getActiveWorkbenchWindow();
147
            IViewDescriptor viewDescriptor = workbench.getViewRegistry().find(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVE_E4_FACTUALDATAPARTE4);
148
            if(viewDescriptor == null){
149
                memento.putBoolean("facts", false);
150
            }
151
            viewDescriptor = workbench.getViewRegistry().find(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_MEDIA_E4_MEDIAVIEWPARTE4);
152
            if(viewDescriptor == null){
153
                memento.putBoolean("media", false);
154
            }
155
        }
156
    }
157

    
158
    /**
159
     * {@inheritDoc}
160
     */
161
    @Override
162
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
163
        PreferencesUtil.setDefaults();
164
        PreferencesUtil.updateDBPreferences();
165

    
166
        hideParts();
167

    
168
        IWorkbench workbench = PlatformUI.getWorkbench();
169
        IWorkbenchWindow window = workbench
170
                .getActiveWorkbenchWindow();
171
        if (PreferencesUtil.getBooleanValue(PreferencePredicate.ShowChecklistPerspective.getKey())){
172

    
173

    
174
            IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
175
            IWorkbenchPage page = window.getActivePage();
176
            page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.perspective.checklistperspective"));
177
        }else{
178

    
179

    
180
            IPerspectiveRegistry registry = workbench.getPerspectiveRegistry();
181
            IWorkbenchPage page = window.getActivePage();
182
            page.setPerspective(registry.findPerspectiveWithId("eu.etaxonomy.taxeditor.application.perspective.taxonomic"));
183

    
184
        }
185
        hideParts();
186

    
187
        PreferencesUtil.checkNomenclaturalCode();
188
    }
189

    
190
    /**
191
     * {@inheritDoc}
192
     */
193
    @Override
194
    public void contextRefresh(IProgressMonitor monitor) {
195
    }
196

    
197
    /**
198
     * {@inheritDoc}
199
     */
200
    @Override
201
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
202
    }
203

    
204
}
(5-5/7)