Project

General

Profile

Download (5.58 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 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

    
11
package eu.etaxonomy.taxeditor.store;
12

    
13
import java.io.File;
14
import java.io.FileNotFoundException;
15

    
16
import org.eclipse.core.runtime.IPath;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.ListenerList;
19
import org.eclipse.ui.IMemento;
20
import org.eclipse.ui.IWorkbench;
21
import org.eclipse.ui.IWorkbenchListener;
22
import org.eclipse.ui.PlatformUI;
23
import org.eclipse.ui.XMLMemento;
24

    
25
import eu.etaxonomy.taxeditor.model.IContextListener;
26
import eu.etaxonomy.taxeditor.model.MementoHelper;
27
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
28

    
29
/**
30
 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
31
 *
32
 * @author n.hoffmann
33
 * @created Sep 30, 2010
34
 * @version 1.0
35
 */
36
public class ContextManager implements IWorkbenchListener{
37
	
38
	private ListenerList contextListeners = new ListenerList();
39

    
40
	private IMemento memento;
41
		
42
	/**
43
	 * <p>Constructor for ContextManager.</p>
44
	 */
45
	protected ContextManager() {
46
		PlatformUI.getWorkbench().addWorkbenchListener(this);
47
	}
48
	
49
	/**
50
	 * <p>addContextListener</p>
51
	 *
52
	 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
53
	 */
54
	public void addContextListener(IContextListener listener){
55
		contextListeners.add(listener);
56
	}
57
	
58
	/**
59
	 * <p>removeContextListener</p>
60
	 *
61
	 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
62
	 */
63
	public void removeContextListener(IContextListener listener) {
64
		contextListeners.remove(listener);
65
	}
66
	
67
	/**
68
	 * <p>notifyContextStart</p>
69
	 *
70
	 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
71
	 */
72
	public void notifyContextStart(IProgressMonitor monitor) {
73
		IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
74

    
75
		subMonitor.beginTask("Starting context", contextListeners.size());
76
		StoreUtil.info("Notifying context listeners, that the context has started.");
77
				
78
		readMemento();				
79
		
80
		for(final Object listener : contextListeners.getListeners()){
81
			((IContextListener) listener).contextStart(memento, monitor);
82
			subMonitor.worked(1);
83
		}
84
			
85
		subMonitor.done();
86
	}
87
	
88
	/**
89
	 * <p>notifyContextAboutToStop</p>
90
	 *
91
	 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
92
	 */
93
	public void notifyContextAboutToStop(IProgressMonitor monitor){
94

    
95
		IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
96

    
97
		subMonitor.beginTask("Stoping context", contextListeners.size());
98
		// we are creating the memento here; even if the context is not stopped
99
		createMemento();
100
		
101
		for(Object listener : contextListeners.getListeners()){
102
			((IContextListener) listener).contextAboutToStop(memento, monitor);
103
			subMonitor.worked(1);
104
		}	
105
		
106
		subMonitor.done();
107
	}
108
	
109
	/**
110
	 * <p>notifyContextStop</p>
111
	 *
112
	 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
113
	 */
114
	public void notifyContextStop(IProgressMonitor monitor) {
115
		
116
		IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
117

    
118
		subMonitor.beginTask("Stoping context", contextListeners.size());
119
		StoreUtil.info("Notifying context listeners, that the context has stopped.");
120
				
121
		for(Object listener : contextListeners.getListeners()){
122
			((IContextListener) listener).contextStop(memento, subMonitor);
123
		}	
124

    
125
		saveMemento();
126
		subMonitor.done();
127
	}
128
	
129
	/* (non-Javadoc)
130
	 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
131
	 */
132
	/** {@inheritDoc} */
133
	@Override
134
	public boolean preShutdown(IWorkbench workbench, boolean forced) {
135

    
136
		createMemento();
137
		
138
		IProgressMonitor monitor = null;
139
		
140
		for(Object listener : contextListeners.getListeners()){
141
			((IContextListener) listener).workbenchShutdown(memento, monitor);
142
		}	
143
		
144
		saveMemento();
145
		
146
		// return true in any case, otherwise the application will not stop
147
		return true;
148
	}
149

    
150
	/* (non-Javadoc)
151
	 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
152
	 */
153
	/** {@inheritDoc} */
154
	@Override
155
	public void postShutdown(IWorkbench workbench) {
156
		
157
		
158
	}
159
	
160
	
161
	private void readMemento(){
162
		try {
163
			memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase());
164
		} catch (FileNotFoundException e) {
165
			// no memento -> no previous state
166
			StoreUtil.info("No state file for datasource");
167
		}
168
	}
169
	
170
	private void createMemento(){
171
		
172
		if (CdmStore.getDataSource() != null) {
173
			
174
			try {
175
				memento = XMLMemento.createWriteRoot(CdmStore.getDataSource().getName());
176
			
177
				StoreUtil.info("DataSource found. Memento created.");
178
			} catch (Exception e) {
179
				// The memento could not be created, but a not closable editor is avoided for this case.
180
				StoreUtil.error(this.getClass(), "The memento could not be created", e);
181
			}
182
		} else {
183
			StoreUtil.info("Not storing state data, because no DataSource present.");
184
		}
185
		
186
	}
187
	
188
	private boolean saveMemento(){
189
		return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null;
190
	}
191
	
192
	/**
193
	 * <p>getStateFileForCurrentDatabase</p>
194
	 *
195
	 * @return a {@link java.io.File} object.
196
	 */
197
	protected File getStateFileForCurrentDatabase() {
198
		if(CdmStore.getDataSource() == null){
199
			return null;
200
		}
201
		
202
		IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
203
		if (path == null) {
204
			return null;
205
		}
206
		path = path.append("editor_state_" + CdmStore.getDataSource().getName() + ".xml");
207
		return path.toFile();
208
	}
209
}
(2-2/6)