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
|
import java.lang.reflect.InvocationTargetException;
|
16
|
|
17
|
import org.eclipse.core.runtime.IPath;
|
18
|
import org.eclipse.core.runtime.IProgressMonitor;
|
19
|
import org.eclipse.core.runtime.ListenerList;
|
20
|
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
|
21
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
22
|
import org.eclipse.ui.IMemento;
|
23
|
import org.eclipse.ui.IWorkbench;
|
24
|
import org.eclipse.ui.IWorkbenchListener;
|
25
|
import org.eclipse.ui.PlatformUI;
|
26
|
import org.eclipse.ui.XMLMemento;
|
27
|
|
28
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
29
|
import eu.etaxonomy.taxeditor.model.MementoHelper;
|
30
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
31
|
|
32
|
/**
|
33
|
* The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
|
34
|
*
|
35
|
* @author n.hoffmann
|
36
|
* @created Sep 30, 2010
|
37
|
* @version 1.0
|
38
|
*/
|
39
|
public class ContextManager implements IWorkbenchListener{
|
40
|
|
41
|
private ListenerList contextListeners = new ListenerList();
|
42
|
|
43
|
private IMemento memento;
|
44
|
|
45
|
/**
|
46
|
* <p>Constructor for ContextManager.</p>
|
47
|
*/
|
48
|
protected ContextManager() {
|
49
|
PlatformUI.getWorkbench().addWorkbenchListener(this);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* <p>addContextListener</p>
|
54
|
*
|
55
|
* @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
|
56
|
*/
|
57
|
public void addContextListener(IContextListener listener){
|
58
|
contextListeners.add(listener);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* <p>removeContextListener</p>
|
63
|
*
|
64
|
* @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
|
65
|
*/
|
66
|
public void removeContextListener(IContextListener listener) {
|
67
|
contextListeners.remove(listener);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* <p>notifyContextStart</p>
|
72
|
*/
|
73
|
public void notifyContextStart() {
|
74
|
StoreUtil.info("Notifying context listeners, that the context has started.");
|
75
|
ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
|
76
|
|
77
|
try {
|
78
|
dialog.run(false, false, new IRunnableWithProgress() {
|
79
|
/* (non-Javadoc)
|
80
|
* @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
|
81
|
*/
|
82
|
@Override
|
83
|
public void run(IProgressMonitor monitor)
|
84
|
throws InvocationTargetException, InterruptedException {
|
85
|
monitor.beginTask("Starting context", contextListeners.size());
|
86
|
|
87
|
|
88
|
readMemento();
|
89
|
|
90
|
for(final Object listener : contextListeners.getListeners()){
|
91
|
((IContextListener) listener).contextStart(memento, monitor);
|
92
|
monitor.worked(1);
|
93
|
}
|
94
|
monitor.done();
|
95
|
}
|
96
|
});
|
97
|
} catch (InvocationTargetException e) {
|
98
|
StoreUtil.error(getClass(), e);
|
99
|
} catch (InterruptedException e) {
|
100
|
StoreUtil.error(getClass(), e);
|
101
|
}
|
102
|
}
|
103
|
|
104
|
|
105
|
/**
|
106
|
*
|
107
|
*/
|
108
|
public void notifyContextRefresh() {
|
109
|
StoreUtil.info("Notifying context listeners, that the context needs to be refreshed.");
|
110
|
ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
|
111
|
|
112
|
try {
|
113
|
dialog.run(false, false, new IRunnableWithProgress() {
|
114
|
/* (non-Javadoc)
|
115
|
* @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
|
116
|
*/
|
117
|
@Override
|
118
|
public void run(IProgressMonitor monitor)
|
119
|
throws InvocationTargetException, InterruptedException {
|
120
|
monitor.beginTask("Refreshing context", contextListeners.size());
|
121
|
|
122
|
for(final Object listener : contextListeners.getListeners()){
|
123
|
((IContextListener) listener).contextRefresh(monitor);
|
124
|
monitor.worked(1);
|
125
|
}
|
126
|
monitor.done();
|
127
|
}
|
128
|
});
|
129
|
} catch (InvocationTargetException e) {
|
130
|
StoreUtil.error(getClass(), e);
|
131
|
} catch (InterruptedException e) {
|
132
|
StoreUtil.error(getClass(), e);
|
133
|
}
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* <p>notifyContextAboutToStop</p>
|
138
|
*
|
139
|
* @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
|
140
|
*/
|
141
|
public void notifyContextAboutToStop(final IProgressMonitor monitor){
|
142
|
|
143
|
IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
|
144
|
|
145
|
subMonitor.beginTask("Stoping context", contextListeners.size());
|
146
|
// we are creating the memento here; even if the context is not stopped
|
147
|
createMemento();
|
148
|
|
149
|
for(final Object listener : contextListeners.getListeners()){
|
150
|
((IContextListener) listener).contextAboutToStop(memento, subMonitor);
|
151
|
subMonitor.worked(1);
|
152
|
}
|
153
|
|
154
|
subMonitor.done();
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* <p>notifyContextStop</p>
|
159
|
*
|
160
|
* @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
|
161
|
*/
|
162
|
public void notifyContextStop(IProgressMonitor monitor) {
|
163
|
|
164
|
IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
|
165
|
|
166
|
subMonitor.beginTask("Stoping context", contextListeners.size());
|
167
|
StoreUtil.info("Notifying context listeners, that the context has stopped.");
|
168
|
|
169
|
for(Object listener : contextListeners.getListeners()){
|
170
|
((IContextListener) listener).contextStop(memento, subMonitor);
|
171
|
subMonitor.worked(1);
|
172
|
}
|
173
|
|
174
|
saveMemento();
|
175
|
subMonitor.done();
|
176
|
}
|
177
|
|
178
|
/* (non-Javadoc)
|
179
|
* @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
|
180
|
*/
|
181
|
/** {@inheritDoc} */
|
182
|
@Override
|
183
|
public boolean preShutdown(IWorkbench workbench, boolean forced) {
|
184
|
|
185
|
createMemento();
|
186
|
|
187
|
IProgressMonitor monitor = null;
|
188
|
|
189
|
for(Object listener : contextListeners.getListeners()){
|
190
|
((IContextListener) listener).workbenchShutdown(memento, monitor);
|
191
|
}
|
192
|
|
193
|
saveMemento();
|
194
|
|
195
|
// return true in any case, otherwise the application will not stop
|
196
|
return true;
|
197
|
}
|
198
|
|
199
|
/* (non-Javadoc)
|
200
|
* @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
|
201
|
*/
|
202
|
/** {@inheritDoc} */
|
203
|
@Override
|
204
|
public void postShutdown(IWorkbench workbench) {
|
205
|
|
206
|
|
207
|
}
|
208
|
|
209
|
|
210
|
private void readMemento(){
|
211
|
try {
|
212
|
memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase());
|
213
|
} catch (FileNotFoundException e) {
|
214
|
// no memento -> no previous state
|
215
|
StoreUtil.info("No state file for datasource");
|
216
|
}
|
217
|
}
|
218
|
|
219
|
private void createMemento(){
|
220
|
|
221
|
if (CdmStore.getDataSource() != null) {
|
222
|
|
223
|
try {
|
224
|
String name = CdmStore.getDataSource().getName();
|
225
|
name = name.trim();
|
226
|
name = name.replace(" ", "_");
|
227
|
memento = XMLMemento.createWriteRoot(name);
|
228
|
|
229
|
StoreUtil.info("DataSource found. Memento created.");
|
230
|
} catch (Exception e) {
|
231
|
// The memento could not be created, but a not closable editor is avoided for this case.
|
232
|
StoreUtil.error(this.getClass(), "The memento could not be created", e);
|
233
|
}
|
234
|
} else {
|
235
|
StoreUtil.info("Not storing state data, because no DataSource present.");
|
236
|
}
|
237
|
|
238
|
}
|
239
|
|
240
|
private boolean saveMemento(){
|
241
|
return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* <p>getStateFileForCurrentDatabase</p>
|
246
|
*
|
247
|
* @return a {@link java.io.File} object.
|
248
|
*/
|
249
|
protected File getStateFileForCurrentDatabase() {
|
250
|
if(CdmStore.getDataSource() == null){
|
251
|
return null;
|
252
|
}
|
253
|
|
254
|
IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
|
255
|
if (path == null) {
|
256
|
return null;
|
257
|
}
|
258
|
path = path.append("editor_state_" + CdmStore.getDataSource().getName() + ".xml");
|
259
|
return path.toFile();
|
260
|
}
|
261
|
|
262
|
}
|