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