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
;
16 import org
.eclipse
.core
.runtime
.IPath
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.ListenerList
;
19 import org
.eclipse
.ui
.IMemento
;
20 import org
.eclipse
.ui
.IWorkbench
;
21 import org
.eclipse
.ui
.IWorkbenchListener
;
22 import org
.eclipse
.ui
.PlatformUI
;
23 import org
.eclipse
.ui
.XMLMemento
;
25 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
26 import eu
.etaxonomy
.taxeditor
.model
.MementoHelper
;
27 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
30 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
33 * @created Sep 30, 2010
36 public class ContextManager
implements IWorkbenchListener
{
38 private ListenerList contextListeners
= new ListenerList();
40 private IMemento memento
;
43 * <p>Constructor for ContextManager.</p>
45 protected ContextManager() {
46 PlatformUI
.getWorkbench().addWorkbenchListener(this);
50 * <p>addContextListener</p>
52 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
54 public void addContextListener(IContextListener listener
){
55 contextListeners
.add(listener
);
59 * <p>removeContextListener</p>
61 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
63 public void removeContextListener(IContextListener listener
) {
64 contextListeners
.remove(listener
);
68 * <p>notifyContextStart</p>
70 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
72 public void notifyContextStart(IProgressMonitor monitor
) {
73 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
75 subMonitor
.beginTask("Starting context", contextListeners
.size());
76 StoreUtil
.info("Notifying context listeners, that the context has started.");
80 for(final Object listener
: contextListeners
.getListeners()){
81 ((IContextListener
) listener
).contextStart(memento
, monitor
);
89 * <p>notifyContextAboutToStop</p>
91 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
93 public void notifyContextAboutToStop(IProgressMonitor monitor
){
95 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
97 subMonitor
.beginTask("Stoping context", contextListeners
.size());
98 // we are creating the memento here; even if the context is not stopped
101 for(Object listener
: contextListeners
.getListeners()){
102 ((IContextListener
) listener
).contextAboutToStop(memento
, monitor
);
103 subMonitor
.worked(1);
110 * <p>notifyContextStop</p>
112 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
114 public void notifyContextStop(IProgressMonitor monitor
) {
116 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
118 subMonitor
.beginTask("Stoping context", contextListeners
.size());
119 StoreUtil
.info("Notifying context listeners, that the context has stopped.");
121 for(Object listener
: contextListeners
.getListeners()){
122 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
130 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
134 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
138 IProgressMonitor monitor
= null;
140 for(Object listener
: contextListeners
.getListeners()){
141 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
146 // return true in any case, otherwise the application will not stop
151 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
155 public void postShutdown(IWorkbench workbench
) {
161 private void readMemento(){
163 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
164 } catch (FileNotFoundException e
) {
165 // no memento -> no previous state
166 StoreUtil
.info("No state file for datasource");
170 private void createMemento(){
172 if (CdmStore
.getDataSource() != null) {
175 memento
= XMLMemento
.createWriteRoot(CdmStore
.getDataSource().getName());
177 StoreUtil
.info("DataSource found. Memento created.");
178 } catch (Exception e
) {
179 // The memento could not be created, but a not closable editor is avoided for this case.
180 StoreUtil
.error(this.getClass(), "The memento could not be created", e
);
183 StoreUtil
.info("Not storing state data, because no DataSource present.");
188 private boolean saveMemento(){
189 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
193 * <p>getStateFileForCurrentDatabase</p>
195 * @return a {@link java.io.File} object.
197 protected File
getStateFileForCurrentDatabase() {
198 if(CdmStore
.getDataSource() == null){
202 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
206 path
= path
.append("editor_state_" + CdmStore
.getDataSource().getName() + ".xml");
207 return path
.toFile();