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
;
27 import org
.eclipse
.ui
.internal
.Workbench
;
29 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
30 import eu
.etaxonomy
.taxeditor
.model
.MementoHelper
;
31 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
32 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
35 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
38 * @created Sep 30, 2010
41 public class ContextManager
implements IWorkbenchListener
{
43 private final ListenerList contextListeners
= new ListenerList();
45 private IMemento memento
;
48 * <p>Constructor for ContextManager.</p>
50 protected ContextManager() {
51 if(Workbench
.getInstance() != null) {
52 PlatformUI
.getWorkbench().addWorkbenchListener(this);
57 * <p>addContextListener</p>
59 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
61 public void addContextListener(IContextListener listener
){
62 contextListeners
.add(listener
);
66 * <p>removeContextListener</p>
68 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
70 public void removeContextListener(IContextListener listener
) {
71 contextListeners
.remove(listener
);
75 * <p>notifyContextStart</p>
77 public void notifyContextStart() {
78 MessagingUtils
.info("Notifying context listeners, that the context has started.");
79 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
82 dialog
.run(false, false, new IRunnableWithProgress() {
84 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
87 public void run(IProgressMonitor monitor
)
88 throws InvocationTargetException
, InterruptedException
{
89 monitor
.beginTask("Starting context", contextListeners
.size());
94 for(final Object listener
: contextListeners
.getListeners()){
95 ((IContextListener
) listener
).contextStart(memento
, monitor
);
101 } catch (InvocationTargetException e
) {
102 MessagingUtils
.error(getClass(), e
);
103 } catch (InterruptedException e
) {
104 MessagingUtils
.error(getClass(), e
);
112 public void notifyContextRefresh() {
113 MessagingUtils
.info("Notifying context listeners, that the context needs to be refreshed.");
114 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
117 dialog
.run(false, false, new IRunnableWithProgress() {
119 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
122 public void run(IProgressMonitor monitor
)
123 throws InvocationTargetException
, InterruptedException
{
124 monitor
.beginTask("Refreshing context", contextListeners
.size());
126 for(final Object listener
: contextListeners
.getListeners()){
127 ((IContextListener
) listener
).contextRefresh(monitor
);
133 } catch (InvocationTargetException e
) {
134 MessagingUtils
.error(getClass(), e
);
135 } catch (InterruptedException e
) {
136 MessagingUtils
.error(getClass(), e
);
141 * <p>notifyContextAboutToStop</p>
143 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
145 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
147 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
149 subMonitor
.beginTask("Stoping context", contextListeners
.size());
150 // we are creating the memento here; even if the context is not stopped
153 for(final Object listener
: contextListeners
.getListeners()){
154 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
155 subMonitor
.worked(1);
162 * <p>notifyContextStop</p>
164 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
166 public void notifyContextStop(IProgressMonitor monitor
) {
168 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
170 subMonitor
.beginTask("Stoping context", contextListeners
.size());
171 MessagingUtils
.info("Notifying context listeners, that the context has stopped.");
173 for(Object listener
: contextListeners
.getListeners()){
174 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
175 subMonitor
.worked(1);
183 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
187 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
191 IProgressMonitor monitor
= null;
193 for(Object listener
: contextListeners
.getListeners()){
194 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
199 // return true in any case, otherwise the application will not stop
204 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
208 public void postShutdown(IWorkbench workbench
) {
214 private void readMemento(){
216 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
217 } catch (FileNotFoundException e
) {
218 // no memento -> no previous state
219 MessagingUtils
.info("No state file for datasource");
223 private void createMemento(){
225 if (CdmStore
.getActiveCdmSource() != null) {
228 String name
= CdmStore
.getActiveCdmSource().getName();
230 name
= name
.replace(" ", "_");
231 memento
= XMLMemento
.createWriteRoot(name
);
233 MessagingUtils
.info("DataSource found. Memento created.");
234 } catch (Exception e
) {
235 // The memento could not be created, but a not closable editor is avoided for this case.
236 MessagingUtils
.error(this.getClass(), "The memento could not be created", e
);
239 MessagingUtils
.info("Not storing state data, because no DataSource present.");
244 private boolean saveMemento(){
245 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
249 * <p>getStateFileForCurrentDatabase</p>
251 * @return a {@link java.io.File} object.
253 protected File
getStateFileForCurrentDatabase() {
254 if(CdmStore
.getActiveCdmSource() == null){
258 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
262 path
= path
.append("editor_state_" + CdmStore
.getActiveCdmSource().getName() + ".xml");
263 return path
.toFile();