3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.store
;
14 import java
.io
.FileNotFoundException
;
15 import java
.lang
.reflect
.InvocationTargetException
;
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
;
28 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
29 import eu
.etaxonomy
.taxeditor
.model
.MementoHelper
;
30 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
33 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
36 * @created Sep 30, 2010
39 public class ContextManager
implements IWorkbenchListener
{
41 private ListenerList contextListeners
= new ListenerList();
43 private IMemento memento
;
46 * <p>Constructor for ContextManager.</p>
48 protected ContextManager() {
49 PlatformUI
.getWorkbench().addWorkbenchListener(this);
53 * <p>addContextListener</p>
55 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
57 public void addContextListener(IContextListener listener
){
58 contextListeners
.add(listener
);
62 * <p>removeContextListener</p>
64 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
66 public void removeContextListener(IContextListener listener
) {
67 contextListeners
.remove(listener
);
71 * <p>notifyContextStart</p>
73 public void notifyContextStart() {
74 StoreUtil
.info("Notifying context listeners, that the context has started.");
75 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
78 dialog
.run(false, false, new IRunnableWithProgress() {
80 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
83 public void run(IProgressMonitor monitor
)
84 throws InvocationTargetException
, InterruptedException
{
85 monitor
.beginTask("Starting context", contextListeners
.size());
90 for(final Object listener
: contextListeners
.getListeners()){
91 ((IContextListener
) listener
).contextStart(memento
, monitor
);
97 } catch (InvocationTargetException e
) {
98 StoreUtil
.error(getClass(), e
);
99 } catch (InterruptedException e
) {
100 StoreUtil
.error(getClass(), e
);
108 public void notifyContextRefresh() {
109 StoreUtil
.info("Notifying context listeners, that the context needs to be refreshed.");
110 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
113 dialog
.run(false, false, new IRunnableWithProgress() {
115 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
118 public void run(IProgressMonitor monitor
)
119 throws InvocationTargetException
, InterruptedException
{
120 monitor
.beginTask("Refreshing context", contextListeners
.size());
122 for(final Object listener
: contextListeners
.getListeners()){
123 ((IContextListener
) listener
).contextRefresh(monitor
);
129 } catch (InvocationTargetException e
) {
130 StoreUtil
.error(getClass(), e
);
131 } catch (InterruptedException e
) {
132 StoreUtil
.error(getClass(), e
);
137 * <p>notifyContextAboutToStop</p>
139 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
141 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
143 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
145 subMonitor
.beginTask("Stoping context", contextListeners
.size());
146 // we are creating the memento here; even if the context is not stopped
149 for(final Object listener
: contextListeners
.getListeners()){
150 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
151 subMonitor
.worked(1);
158 * <p>notifyContextStop</p>
160 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
162 public void notifyContextStop(IProgressMonitor monitor
) {
164 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
166 subMonitor
.beginTask("Stoping context", contextListeners
.size());
167 StoreUtil
.info("Notifying context listeners, that the context has stopped.");
169 for(Object listener
: contextListeners
.getListeners()){
170 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
171 subMonitor
.worked(1);
179 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
183 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
187 IProgressMonitor monitor
= null;
189 for(Object listener
: contextListeners
.getListeners()){
190 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
195 // return true in any case, otherwise the application will not stop
200 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
204 public void postShutdown(IWorkbench workbench
) {
210 private void readMemento(){
212 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
213 } catch (FileNotFoundException e
) {
214 // no memento -> no previous state
215 StoreUtil
.info("No state file for datasource");
219 private void createMemento(){
221 if (CdmStore
.getDataSource() != null) {
224 String name
= CdmStore
.getDataSource().getName();
226 name
= name
.replace(" ", "_");
227 memento
= XMLMemento
.createWriteRoot(name
);
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
);
235 StoreUtil
.info("Not storing state data, because no DataSource present.");
240 private boolean saveMemento(){
241 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
245 * <p>getStateFileForCurrentDatabase</p>
247 * @return a {@link java.io.File} object.
249 protected File
getStateFileForCurrentDatabase() {
250 if(CdmStore
.getDataSource() == null){
254 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
258 path
= path
.append("editor_state_" + CdmStore
.getDataSource().getName() + ".xml");
259 return path
.toFile();