Project

General

Profile

Download (3.96 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.store.CdmStore;
38

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

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

    
48
    private MApplication application;
49

    
50
    @Inject
51
    private EModelService modelService;
52

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

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

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

    
68
    @ProcessRemovals
69
    private void processRemovals() {
70
    }
71

    
72
    @PreSave
73
    private void preSave(){
74
    }
75

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

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

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

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

    
122
    /**
123
     * {@inheritDoc}
124
     */
125
    @Override
126
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
127
        hideParts();
128
    }
129

    
130
    /**
131
     * {@inheritDoc}
132
     */
133
    @Override
134
    public void contextRefresh(IProgressMonitor monitor) {
135
    }
136

    
137
    /**
138
     * {@inheritDoc}
139
     */
140
    @Override
141
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
142
    }
143

    
144
}
(5-5/8)