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
;
27 import org
.springframework
.remoting
.RemoteAccessException
;
29 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
30 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
31 import eu
.etaxonomy
.taxeditor
.model
.MementoHelper
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
33 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
36 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
39 * @created Sep 30, 2010
41 public class ContextManager
implements IWorkbenchListener
{
43 private final ListenerList
<IContextListener
> 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);
56 public void addContextListener(IContextListener listener
){
57 contextListeners
.add(listener
);
60 public void removeContextListener(IContextListener listener
) {
61 contextListeners
.remove(listener
);
64 public void notifyContextStartWithoutDialog(IProgressMonitor monitor
) {
65 MessagingUtils
.info("Notifying context listeners, that the context has started.");
69 for(final Object listener
: contextListeners
.getListeners()){
70 ((IContextListener
) listener
).contextStart(memento
, monitor
);
74 public void notifyContextStart() throws RemoteAccessException
{
75 MessagingUtils
.info("Notifying context listeners, that the context has started.");
76 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(AbstractUtility
.getShell());
79 dialog
.run(false, false, new IRunnableWithProgress() {
82 public void run(IProgressMonitor monitor
)
83 throws InvocationTargetException
, InterruptedException
{
84 monitor
.beginTask("Starting context", contextListeners
.size());
89 for(final Object listener
: contextListeners
.getListeners()){
90 ((IContextListener
) listener
).contextStart(memento
, monitor
);
96 } catch (InvocationTargetException e
) {
97 if (e
.getTargetException().getClass().equals(RemoteAccessException
.class)){
98 if (e
.getTargetException().getMessage().contains("403")){
99 // new CdmAuthenticationException("Access Denied", e.getTargetException());
100 throw new CdmAuthenticationException("You are logged in now but you are not permitted to use the TaxEditor with the selected data source", e
.getTargetException());
102 MessagingUtils
.error(getClass(), e
.getTargetException());
105 MessagingUtils
.error(getClass(), e
);
107 } catch (InterruptedException e
) {
108 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(AbstractUtility
.getShell());
117 dialog
.run(false, false, new IRunnableWithProgress() {
120 public void run(IProgressMonitor monitor
)
121 throws InvocationTargetException
, InterruptedException
{
122 monitor
.beginTask("Refreshing context", contextListeners
.size());
124 for(final Object listener
: contextListeners
.getListeners()){
125 ((IContextListener
) listener
).contextRefresh(monitor
);
131 } catch (InvocationTargetException e
) {
132 if (e
.getTargetException().getClass().equals(RemoteAccessException
.class)){
133 if (e
.getTargetException().getMessage().contains("403")){
134 MessagingUtils
.confirmDialog("Access denied", "Access denied");
136 MessagingUtils
.error(getClass(), e
.getTargetException());
139 MessagingUtils
.error(getClass(), e
);
141 } catch (InterruptedException e
) {
142 MessagingUtils
.error(getClass(), e
);
146 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
148 IProgressMonitor subMonitor
= AbstractUtility
.getSubProgressMonitor(monitor
, 1);
150 subMonitor
.beginTask("Stoping context", contextListeners
.size());
151 // we are creating the memento here; even if the context is not stopped
154 for(final Object listener
: contextListeners
.getListeners()){
155 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
156 subMonitor
.worked(1);
162 public void notifyContextStop(IProgressMonitor monitor
) {
164 IProgressMonitor subMonitor
= AbstractUtility
.getSubProgressMonitor(monitor
, 1);
166 subMonitor
.beginTask("Stoping context", contextListeners
.size());
167 MessagingUtils
.info("Notifying context listeners, that the context has stopped.");
169 for(Object listener
: contextListeners
.getListeners()){
170 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
171 subMonitor
.worked(1);
179 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
183 IProgressMonitor monitor
= null;
185 for(Object listener
: contextListeners
.getListeners()){
186 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
191 // return true in any case, otherwise the application will not stop
196 public void postShutdown(IWorkbench workbench
) {
199 private void readMemento(){
201 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
202 } catch (FileNotFoundException e
) {
203 // no memento -> no previous state
204 MessagingUtils
.info("No state file for datasource");
208 private void createMemento(){
210 if (CdmStore
.getActiveCdmSource() != null) {
213 String name
= CdmStore
.getActiveCdmSource().getName();
215 name
= name
.replace(" ", "_");
216 memento
= XMLMemento
.createWriteRoot(name
);
218 MessagingUtils
.info("DataSource found. Memento created.");
219 } catch (Exception e
) {
220 // The memento could not be created, but a not closable editor is avoided for this case.
221 MessagingUtils
.error(this.getClass(), "The memento could not be created", e
);
224 MessagingUtils
.info("Not storing state data, because no DataSource present.");
228 private boolean saveMemento(){
229 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
232 protected File
getStateFileForCurrentDatabase() {
233 if(CdmStore
.getActiveCdmSource() == null){
237 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
241 path
= path
.append("editor_state_" + CdmStore
.getActiveCdmSource().getName() + ".xml");
242 return path
.toFile();