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
);
105 * <p>notifyContextAboutToStop</p>
107 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
109 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
111 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
113 subMonitor
.beginTask("Stoping context", contextListeners
.size());
114 // we are creating the memento here; even if the context is not stopped
117 for(final Object listener
: contextListeners
.getListeners()){
118 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
119 subMonitor
.worked(1);
126 * <p>notifyContextStop</p>
128 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
130 public void notifyContextStop(IProgressMonitor monitor
) {
132 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
134 subMonitor
.beginTask("Stoping context", contextListeners
.size());
135 StoreUtil
.info("Notifying context listeners, that the context has stopped.");
137 for(Object listener
: contextListeners
.getListeners()){
138 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
139 subMonitor
.worked(1);
147 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
151 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
155 IProgressMonitor monitor
= null;
157 for(Object listener
: contextListeners
.getListeners()){
158 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
163 // return true in any case, otherwise the application will not stop
168 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
172 public void postShutdown(IWorkbench workbench
) {
178 private void readMemento(){
180 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
181 } catch (FileNotFoundException e
) {
182 // no memento -> no previous state
183 StoreUtil
.info("No state file for datasource");
187 private void createMemento(){
189 if (CdmStore
.getDataSource() != null) {
192 String name
= CdmStore
.getDataSource().getName();
194 name
= name
.replace(" ", "_");
195 memento
= XMLMemento
.createWriteRoot(name
);
197 StoreUtil
.info("DataSource found. Memento created.");
198 } catch (Exception e
) {
199 // The memento could not be created, but a not closable editor is avoided for this case.
200 StoreUtil
.error(this.getClass(), "The memento could not be created", e
);
203 StoreUtil
.info("Not storing state data, because no DataSource present.");
208 private boolean saveMemento(){
209 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
213 * <p>getStateFileForCurrentDatabase</p>
215 * @return a {@link java.io.File} object.
217 protected File
getStateFileForCurrentDatabase() {
218 if(CdmStore
.getDataSource() == null){
222 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
226 path
= path
.append("editor_state_" + CdmStore
.getDataSource().getName() + ".xml");
227 return path
.toFile();