Project

General

Profile

Download (4.07 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.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.osgi.service.event.Event;
35

    
36
import eu.etaxonomy.taxeditor.model.IContextListener;
37
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39

    
40
/**
41
 * @author pplitzner
42
 * @date 05.07.2017
43
 *
44
 */
45
public class LifeCycleManager implements IContextListener{
46

    
47
    private static final String NON_RESTORE = "nonRestore";
48

    
49
    private MApplication application;
50

    
51
    @Inject
52
    private EModelService modelService;
53

    
54
    @Inject
55
    public LifeCycleManager() {
56
        CdmStore.getContextManager().addContextListener(this);
57
    }
58

    
59
    @PostContextCreate
60
    private void postContextCreate() {
61
    }
62

    
63
    @ProcessAdditions
64
    private void processAdditions(MApplication application) {
65
        this.application = application;
66
        hideParts();
67

    
68
    }
69

    
70
    @ProcessRemovals
71
    private void processRemovals() {
72
    }
73

    
74
    @PreSave
75
    private void preSave(){
76
    }
77

    
78
    @Inject
79
    @Optional
80
    public void subscribeAppShutdownStarted(@UIEventTopic(UIEvents.UILifeCycle.APP_SHUTDOWN_STARTED) Event event,
81
            MApplication application) {
82
        IEclipseContext context = application.getContext();
83
        EPartService partService = context.get(EPartService.class);
84
        try {
85
            Collection<MPart> dirtyParts = partService.getDirtyParts();
86
            if(!dirtyParts.isEmpty()){
87
                partService.saveAll(true);
88
            }
89
        } catch (IllegalStateException e) {
90
            //FIXME E4
91
            //exception is ignored as this is a temporary fix until full e4 migration
92
        }
93
    }
94

    
95
    private void hideParts() {
96
        List<MPart> elements = modelService.findElements(application, null, MPart.class, Arrays.asList(NON_RESTORE));
97
        for (MPart part : elements) {
98
            MElementContainer<MUIElement> parent = part.getParent();
99
            if(parent!=null){
100
                if(parent.getSelectedElement()!=null
101
                        && parent.getSelectedElement().equals(part)){
102
                    parent.setSelectedElement(null);
103
                }
104
                parent.getChildren().remove(part);
105
            }
106
        }
107
    }
108

    
109
    /**
110
     * {@inheritDoc}
111
     */
112
    @Override
113
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
114
    }
115

    
116
    /**
117
     * {@inheritDoc}
118
     */
119
    @Override
120
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
121
        hideParts();
122
    }
123

    
124
    /**
125
     * {@inheritDoc}
126
     */
127
    @Override
128
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
129
        PreferencesUtil.updateDBPreferences();
130
        hideParts();
131
    }
132

    
133
    /**
134
     * {@inheritDoc}
135
     */
136
    @Override
137
    public void contextRefresh(IProgressMonitor monitor) {
138
    }
139

    
140
    /**
141
     * {@inheritDoc}
142
     */
143
    @Override
144
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
145
    }
146

    
147
}
(5-5/7)