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