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
.model
.MessagingUtils
;
31 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
34 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
37 * @created Sep 30, 2010
40 public class ContextManager
implements IWorkbenchListener
{
42 private ListenerList contextListeners
= new ListenerList();
44 private IMemento memento
;
47 * <p>Constructor for ContextManager.</p>
49 protected ContextManager() {
50 PlatformUI
.getWorkbench().addWorkbenchListener(this);
54 * <p>addContextListener</p>
56 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
58 public void addContextListener(IContextListener listener
){
59 contextListeners
.add(listener
);
63 * <p>removeContextListener</p>
65 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
67 public void removeContextListener(IContextListener listener
) {
68 contextListeners
.remove(listener
);
72 * <p>notifyContextStart</p>
74 public void notifyContextStart() {
75 MessagingUtils
.info("Notifying context listeners, that the context has started.");
76 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
79 dialog
.run(false, false, new IRunnableWithProgress() {
81 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
84 public void run(IProgressMonitor monitor
)
85 throws InvocationTargetException
, InterruptedException
{
86 monitor
.beginTask("Starting context", contextListeners
.size());
91 for(final Object listener
: contextListeners
.getListeners()){
92 ((IContextListener
) listener
).contextStart(memento
, monitor
);
98 } catch (InvocationTargetException e
) {
99 MessagingUtils
.error(getClass(), e
);
100 } catch (InterruptedException e
) {
101 MessagingUtils
.error(getClass(), e
);
109 public void notifyContextRefresh() {
110 MessagingUtils
.info("Notifying context listeners, that the context needs to be refreshed.");
111 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
114 dialog
.run(false, false, new IRunnableWithProgress() {
116 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
119 public void run(IProgressMonitor monitor
)
120 throws InvocationTargetException
, InterruptedException
{
121 monitor
.beginTask("Refreshing context", contextListeners
.size());
123 for(final Object listener
: contextListeners
.getListeners()){
124 ((IContextListener
) listener
).contextRefresh(monitor
);
130 } catch (InvocationTargetException e
) {
131 MessagingUtils
.error(getClass(), e
);
132 } catch (InterruptedException e
) {
133 MessagingUtils
.error(getClass(), e
);
138 * <p>notifyContextAboutToStop</p>
140 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
142 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
144 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
146 subMonitor
.beginTask("Stoping context", contextListeners
.size());
147 // we are creating the memento here; even if the context is not stopped
150 for(final Object listener
: contextListeners
.getListeners()){
151 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
152 subMonitor
.worked(1);
159 * <p>notifyContextStop</p>
161 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
163 public void notifyContextStop(IProgressMonitor monitor
) {
165 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
167 subMonitor
.beginTask("Stoping context", contextListeners
.size());
168 MessagingUtils
.info("Notifying context listeners, that the context has stopped.");
170 for(Object listener
: contextListeners
.getListeners()){
171 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
172 subMonitor
.worked(1);
180 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
184 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
188 IProgressMonitor monitor
= null;
190 for(Object listener
: contextListeners
.getListeners()){
191 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
196 // return true in any case, otherwise the application will not stop
201 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
205 public void postShutdown(IWorkbench workbench
) {
211 private void readMemento(){
213 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
214 } catch (FileNotFoundException e
) {
215 // no memento -> no previous state
216 MessagingUtils
.info("No state file for datasource");
220 private void createMemento(){
222 if (CdmStore
.getActiveCdmSource() != null) {
225 String name
= CdmStore
.getActiveCdmSource().getName();
227 name
= name
.replace(" ", "_");
228 memento
= XMLMemento
.createWriteRoot(name
);
230 MessagingUtils
.info("DataSource found. Memento created.");
231 } catch (Exception e
) {
232 // The memento could not be created, but a not closable editor is avoided for this case.
233 MessagingUtils
.error(this.getClass(), "The memento could not be created", e
);
236 MessagingUtils
.info("Not storing state data, because no DataSource present.");
241 private boolean saveMemento(){
242 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
246 * <p>getStateFileForCurrentDatabase</p>
248 * @return a {@link java.io.File} object.
250 protected File
getStateFileForCurrentDatabase() {
251 if(CdmStore
.getActiveCdmSource() == null){
255 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
259 path
= path
.append("editor_state_" + CdmStore
.getActiveCdmSource().getName() + ".xml");
260 return path
.toFile();