Revision 93b0eb6b
Added by Cherian Mathew almost 9 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/ContextManager.java | ||
---|---|---|
1 | 1 |
// $Id$ |
2 | 2 |
/** |
3 | 3 |
* Copyright (C) 2007 EDIT |
4 |
* European Distributed Institute of Taxonomy
|
|
4 |
* European Distributed Institute of Taxonomy |
|
5 | 5 |
* http://www.e-taxonomy.eu |
6 |
*
|
|
6 |
* |
|
7 | 7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
8 | 8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
9 | 9 |
*/ |
... | ... | |
24 | 24 |
import org.eclipse.ui.IWorkbenchListener; |
25 | 25 |
import org.eclipse.ui.PlatformUI; |
26 | 26 |
import org.eclipse.ui.XMLMemento; |
27 |
import org.eclipse.ui.internal.Workbench; |
|
27 | 28 |
|
28 | 29 |
import eu.etaxonomy.taxeditor.model.IContextListener; |
29 | 30 |
import eu.etaxonomy.taxeditor.model.MementoHelper; |
... | ... | |
38 | 39 |
* @version 1.0 |
39 | 40 |
*/ |
40 | 41 |
public class ContextManager implements IWorkbenchListener{ |
41 |
|
|
42 |
private ListenerList contextListeners = new ListenerList(); |
|
42 |
|
|
43 |
private final ListenerList contextListeners = new ListenerList();
|
|
43 | 44 |
|
44 | 45 |
private IMemento memento; |
45 |
|
|
46 |
|
|
46 | 47 |
/** |
47 | 48 |
* <p>Constructor for ContextManager.</p> |
48 | 49 |
*/ |
49 | 50 |
protected ContextManager() { |
50 |
PlatformUI.getWorkbench().addWorkbenchListener(this); |
|
51 |
if(Workbench.getInstance() != null) { |
|
52 |
PlatformUI.getWorkbench().addWorkbenchListener(this); |
|
53 |
} |
|
51 | 54 |
} |
52 |
|
|
55 |
|
|
53 | 56 |
/** |
54 | 57 |
* <p>addContextListener</p> |
55 | 58 |
* |
... | ... | |
58 | 61 |
public void addContextListener(IContextListener listener){ |
59 | 62 |
contextListeners.add(listener); |
60 | 63 |
} |
61 |
|
|
64 |
|
|
62 | 65 |
/** |
63 | 66 |
* <p>removeContextListener</p> |
64 | 67 |
* |
... | ... | |
67 | 70 |
public void removeContextListener(IContextListener listener) { |
68 | 71 |
contextListeners.remove(listener); |
69 | 72 |
} |
70 |
|
|
73 |
|
|
71 | 74 |
/** |
72 | 75 |
* <p>notifyContextStart</p> |
73 | 76 |
*/ |
74 | 77 |
public void notifyContextStart() { |
75 | 78 |
MessagingUtils.info("Notifying context listeners, that the context has started."); |
76 | 79 |
ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell()); |
77 |
|
|
80 |
|
|
78 | 81 |
try { |
79 | 82 |
dialog.run(false, false, new IRunnableWithProgress() { |
80 | 83 |
/* (non-Javadoc) |
... | ... | |
84 | 87 |
public void run(IProgressMonitor monitor) |
85 | 88 |
throws InvocationTargetException, InterruptedException { |
86 | 89 |
monitor.beginTask("Starting context", contextListeners.size()); |
87 |
|
|
88 |
|
|
89 |
readMemento();
|
|
90 |
|
|
90 |
|
|
91 |
|
|
92 |
readMemento(); |
|
93 |
|
|
91 | 94 |
for(final Object listener : contextListeners.getListeners()){ |
92 | 95 |
((IContextListener) listener).contextStart(memento, monitor); |
93 | 96 |
monitor.worked(1); |
... | ... | |
101 | 104 |
MessagingUtils.error(getClass(), e); |
102 | 105 |
} |
103 | 106 |
} |
104 |
|
|
107 |
|
|
105 | 108 |
|
106 | 109 |
/** |
107 |
*
|
|
110 |
* |
|
108 | 111 |
*/ |
109 | 112 |
public void notifyContextRefresh() { |
110 | 113 |
MessagingUtils.info("Notifying context listeners, that the context needs to be refreshed."); |
111 | 114 |
ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell()); |
112 |
|
|
115 |
|
|
113 | 116 |
try { |
114 | 117 |
dialog.run(false, false, new IRunnableWithProgress() { |
115 | 118 |
/* (non-Javadoc) |
... | ... | |
118 | 121 |
@Override |
119 | 122 |
public void run(IProgressMonitor monitor) |
120 | 123 |
throws InvocationTargetException, InterruptedException { |
121 |
monitor.beginTask("Refreshing context", contextListeners.size());
|
|
122 |
|
|
124 |
monitor.beginTask("Refreshing context", contextListeners.size()); |
|
125 |
|
|
123 | 126 |
for(final Object listener : contextListeners.getListeners()){ |
124 | 127 |
((IContextListener) listener).contextRefresh(monitor); |
125 | 128 |
monitor.worked(1); |
... | ... | |
133 | 136 |
MessagingUtils.error(getClass(), e); |
134 | 137 |
} |
135 | 138 |
} |
136 |
|
|
139 |
|
|
137 | 140 |
/** |
138 | 141 |
* <p>notifyContextAboutToStop</p> |
139 | 142 |
* |
... | ... | |
146 | 149 |
subMonitor.beginTask("Stoping context", contextListeners.size()); |
147 | 150 |
// we are creating the memento here; even if the context is not stopped |
148 | 151 |
createMemento(); |
149 |
|
|
152 |
|
|
150 | 153 |
for(final Object listener : contextListeners.getListeners()){ |
151 | 154 |
((IContextListener) listener).contextAboutToStop(memento, subMonitor); |
152 | 155 |
subMonitor.worked(1); |
153 |
}
|
|
154 |
|
|
156 |
} |
|
157 |
|
|
155 | 158 |
subMonitor.done(); |
156 | 159 |
} |
157 |
|
|
160 |
|
|
158 | 161 |
/** |
159 | 162 |
* <p>notifyContextStop</p> |
160 | 163 |
* |
161 | 164 |
* @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object. |
162 | 165 |
*/ |
163 | 166 |
public void notifyContextStop(IProgressMonitor monitor) { |
164 |
|
|
167 |
|
|
165 | 168 |
IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1); |
166 | 169 |
|
167 | 170 |
subMonitor.beginTask("Stoping context", contextListeners.size()); |
168 | 171 |
MessagingUtils.info("Notifying context listeners, that the context has stopped."); |
169 |
|
|
172 |
|
|
170 | 173 |
for(Object listener : contextListeners.getListeners()){ |
171 | 174 |
((IContextListener) listener).contextStop(memento, subMonitor); |
172 | 175 |
subMonitor.worked(1); |
173 |
}
|
|
176 |
} |
|
174 | 177 |
|
175 | 178 |
saveMemento(); |
176 | 179 |
subMonitor.done(); |
177 | 180 |
} |
178 |
|
|
181 |
|
|
179 | 182 |
/* (non-Javadoc) |
180 | 183 |
* @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean) |
181 | 184 |
*/ |
... | ... | |
184 | 187 |
public boolean preShutdown(IWorkbench workbench, boolean forced) { |
185 | 188 |
|
186 | 189 |
createMemento(); |
187 |
|
|
190 |
|
|
188 | 191 |
IProgressMonitor monitor = null; |
189 |
|
|
192 |
|
|
190 | 193 |
for(Object listener : contextListeners.getListeners()){ |
191 | 194 |
((IContextListener) listener).workbenchShutdown(memento, monitor); |
192 |
}
|
|
193 |
|
|
195 |
} |
|
196 |
|
|
194 | 197 |
saveMemento(); |
195 |
|
|
198 |
|
|
196 | 199 |
// return true in any case, otherwise the application will not stop |
197 | 200 |
return true; |
198 | 201 |
} |
... | ... | |
203 | 206 |
/** {@inheritDoc} */ |
204 | 207 |
@Override |
205 | 208 |
public void postShutdown(IWorkbench workbench) { |
206 |
|
|
207 |
|
|
209 |
|
|
210 |
|
|
208 | 211 |
} |
209 |
|
|
210 |
|
|
212 |
|
|
213 |
|
|
211 | 214 |
private void readMemento(){ |
212 | 215 |
try { |
213 | 216 |
memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase()); |
... | ... | |
216 | 219 |
MessagingUtils.info("No state file for datasource"); |
217 | 220 |
} |
218 | 221 |
} |
219 |
|
|
222 |
|
|
220 | 223 |
private void createMemento(){ |
221 |
|
|
224 |
|
|
222 | 225 |
if (CdmStore.getActiveCdmSource() != null) { |
223 |
|
|
226 |
|
|
224 | 227 |
try { |
225 | 228 |
String name = CdmStore.getActiveCdmSource().getName(); |
226 | 229 |
name = name.trim(); |
227 | 230 |
name = name.replace(" ", "_"); |
228 | 231 |
memento = XMLMemento.createWriteRoot(name); |
229 |
|
|
232 |
|
|
230 | 233 |
MessagingUtils.info("DataSource found. Memento created."); |
231 | 234 |
} catch (Exception e) { |
232 | 235 |
// The memento could not be created, but a not closable editor is avoided for this case. |
... | ... | |
235 | 238 |
} else { |
236 | 239 |
MessagingUtils.info("Not storing state data, because no DataSource present."); |
237 | 240 |
} |
238 |
|
|
241 |
|
|
239 | 242 |
} |
240 |
|
|
243 |
|
|
241 | 244 |
private boolean saveMemento(){ |
242 | 245 |
return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null; |
243 | 246 |
} |
244 |
|
|
247 |
|
|
245 | 248 |
/** |
246 | 249 |
* <p>getStateFileForCurrentDatabase</p> |
247 | 250 |
* |
... | ... | |
251 | 254 |
if(CdmStore.getActiveCdmSource() == null){ |
252 | 255 |
return null; |
253 | 256 |
} |
254 |
|
|
257 |
|
|
255 | 258 |
IPath path = TaxeditorStorePlugin.getDefault().getStateLocation(); |
256 | 259 |
if (path == null) { |
257 | 260 |
return null; |
Also available in: Unified diff
eu.etaxonomy.taxeditor.cdmlib/.classpath, eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF, eu.etaxonomy.taxeditor.test/META-INF/MANIFEST.MF : moved test jars to test project
eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF : added packages required by test project to exported package list
ContextManager.java : adding check to make sure the workbench is created before initialisation
eu.etaxonomy.taxeditor.test/.classpath : moved resource from src/main/resources to src/test/resourcesto be maven comliant
eu.etaxonomy.taxeditor.test/pom.xml : added tycho-surefire-plugin config to run tests
TaxonNameEditorTest : removed logger setting
BaseRemotingTest : removed unused call to disk cache
CdmPersistentRemoteSourceTest, AbstractLazyInitializerTest, RemoteLazyLoadingTest : changed assert calls to be compatible with junit 4.8.2
OperationTestBase, BaseRemotingTest : changed to abstract to not be included in test run
eu.etaxonomy.taxeditor/pom.xml : made the building of products as optional (separate profile)
pom.xml : added unitils version property
eu.etaxonomy.taxeditor.test/lib : added test dependency jars