2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.store
;
13 import java
.io
.FileNotFoundException
;
14 import java
.lang
.reflect
.InvocationTargetException
;
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
.jface
.dialogs
.ProgressMonitorDialog
;
20 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
21 import org
.eclipse
.ui
.IMemento
;
22 import org
.eclipse
.ui
.IWorkbench
;
23 import org
.eclipse
.ui
.IWorkbenchListener
;
24 import org
.eclipse
.ui
.PlatformUI
;
25 import org
.eclipse
.ui
.XMLMemento
;
26 import org
.eclipse
.ui
.internal
.Workbench
;
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 final ListenerList contextListeners
= new ListenerList();
44 private IMemento memento
;
47 * <p>Constructor for ContextManager.</p>
49 protected ContextManager() {
50 if(Workbench
.getInstance() != null) {
51 PlatformUI
.getWorkbench().addWorkbenchListener(this);
56 * <p>addContextListener</p>
58 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
60 public void addContextListener(IContextListener listener
){
61 contextListeners
.add(listener
);
65 * <p>removeContextListener</p>
67 * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
69 public void removeContextListener(IContextListener listener
) {
70 contextListeners
.remove(listener
);
73 public void notifyContextStartWithoutDialog(IProgressMonitor monitor
) {
74 MessagingUtils
.info("Notifying context listeners, that the context has started.");
78 for(final Object listener
: contextListeners
.getListeners()){
79 ((IContextListener
) listener
).contextStart(memento
, monitor
);
83 * <p>notifyContextStart</p>
85 public void notifyContextStart() {
86 MessagingUtils
.info("Notifying context listeners, that the context has started.");
87 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
90 dialog
.run(false, false, new IRunnableWithProgress() {
92 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
95 public void run(IProgressMonitor monitor
)
96 throws InvocationTargetException
, InterruptedException
{
97 monitor
.beginTask("Starting context", contextListeners
.size());
102 for(final Object listener
: contextListeners
.getListeners()){
103 ((IContextListener
) listener
).contextStart(memento
, monitor
);
109 } catch (InvocationTargetException e
) {
110 MessagingUtils
.error(getClass(), e
);
111 } catch (InterruptedException e
) {
112 MessagingUtils
.error(getClass(), e
);
120 public void notifyContextRefresh() {
121 MessagingUtils
.info("Notifying context listeners, that the context needs to be refreshed.");
122 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
125 dialog
.run(false, false, new IRunnableWithProgress() {
127 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
130 public void run(IProgressMonitor monitor
)
131 throws InvocationTargetException
, InterruptedException
{
132 monitor
.beginTask("Refreshing context", contextListeners
.size());
134 for(final Object listener
: contextListeners
.getListeners()){
135 ((IContextListener
) listener
).contextRefresh(monitor
);
141 } catch (InvocationTargetException e
) {
142 MessagingUtils
.error(getClass(), e
);
143 } catch (InterruptedException e
) {
144 MessagingUtils
.error(getClass(), e
);
149 * <p>notifyContextAboutToStop</p>
151 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
153 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
155 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
157 subMonitor
.beginTask("Stoping context", contextListeners
.size());
158 // we are creating the memento here; even if the context is not stopped
161 for(final Object listener
: contextListeners
.getListeners()){
162 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
163 subMonitor
.worked(1);
170 * <p>notifyContextStop</p>
172 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
174 public void notifyContextStop(IProgressMonitor monitor
) {
176 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
178 subMonitor
.beginTask("Stoping context", contextListeners
.size());
179 MessagingUtils
.info("Notifying context listeners, that the context has stopped.");
181 for(Object listener
: contextListeners
.getListeners()){
182 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
183 subMonitor
.worked(1);
191 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
195 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
199 IProgressMonitor monitor
= null;
201 for(Object listener
: contextListeners
.getListeners()){
202 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
207 // return true in any case, otherwise the application will not stop
212 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
216 public void postShutdown(IWorkbench workbench
) {
222 private void readMemento(){
224 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
225 } catch (FileNotFoundException e
) {
226 // no memento -> no previous state
227 MessagingUtils
.info("No state file for datasource");
231 private void createMemento(){
233 if (CdmStore
.getActiveCdmSource() != null) {
236 String name
= CdmStore
.getActiveCdmSource().getName();
238 name
= name
.replace(" ", "_");
239 memento
= XMLMemento
.createWriteRoot(name
);
241 MessagingUtils
.info("DataSource found. Memento created.");
242 } catch (Exception e
) {
243 // The memento could not be created, but a not closable editor is avoided for this case.
244 MessagingUtils
.error(this.getClass(), "The memento could not be created", e
);
247 MessagingUtils
.info("Not storing state data, because no DataSource present.");
252 private boolean saveMemento(){
253 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
257 * <p>getStateFileForCurrentDatabase</p>
259 * @return a {@link java.io.File} object.
261 protected File
getStateFileForCurrentDatabase() {
262 if(CdmStore
.getActiveCdmSource() == null){
266 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
270 path
= path
.append("editor_state_" + CdmStore
.getActiveCdmSource().getName() + ".xml");
271 return path
.toFile();