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
.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
);
74 public void notifyContextStartWithoutDialog(IProgressMonitor monitor
) {
75 MessagingUtils
.info("Notifying context listeners, that the context has started.");
79 for(final Object listener
: contextListeners
.getListeners()){
80 ((IContextListener
) listener
).contextStart(memento
, monitor
);
84 * <p>notifyContextStart</p>
87 public void notifyContextStart() throws RemoteAccessException
{
88 MessagingUtils
.info("Notifying context listeners, that the context has started.");
89 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
92 dialog
.run(false, false, new IRunnableWithProgress() {
94 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
97 public void run(IProgressMonitor monitor
)
98 throws InvocationTargetException
, InterruptedException
{
99 monitor
.beginTask("Starting context", contextListeners
.size());
104 for(final Object listener
: contextListeners
.getListeners()){
105 ((IContextListener
) listener
).contextStart(memento
, monitor
);
111 } catch (InvocationTargetException e
) {
112 if (e
.getTargetException().getClass().equals(RemoteAccessException
.class)){
113 if (e
.getTargetException().getMessage().contains("403")){
114 // new CdmAuthenticationException("Access Denied", e.getTargetException());
115 throw new CdmAuthenticationException("You are logged in now but you are not permitted to use the TaxEditor with the selected data source", e
.getTargetException());
117 MessagingUtils
.error(getClass(), e
.getTargetException());
120 MessagingUtils
.error(getClass(), e
);
122 } catch (InterruptedException e
) {
123 MessagingUtils
.error(getClass(), e
);
131 public void notifyContextRefresh() {
132 MessagingUtils
.info("Notifying context listeners, that the context needs to be refreshed.");
133 ProgressMonitorDialog dialog
= new ProgressMonitorDialog(StoreUtil
.getShell());
136 dialog
.run(false, false, new IRunnableWithProgress() {
138 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
141 public void run(IProgressMonitor monitor
)
142 throws InvocationTargetException
, InterruptedException
{
143 monitor
.beginTask("Refreshing context", contextListeners
.size());
145 for(final Object listener
: contextListeners
.getListeners()){
146 ((IContextListener
) listener
).contextRefresh(monitor
);
152 } catch (InvocationTargetException e
) {
153 if (e
.getTargetException().getClass().equals(RemoteAccessException
.class)){
154 if (e
.getTargetException().getMessage().contains("403")){
155 MessagingUtils
.confirmDialog("Access denied", "Access denied");
157 MessagingUtils
.error(getClass(), e
.getTargetException());
160 MessagingUtils
.error(getClass(), e
);
162 } catch (InterruptedException e
) {
163 MessagingUtils
.error(getClass(), e
);
168 * <p>notifyContextAboutToStop</p>
170 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
172 public void notifyContextAboutToStop(final IProgressMonitor monitor
){
174 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
176 subMonitor
.beginTask("Stoping context", contextListeners
.size());
177 // we are creating the memento here; even if the context is not stopped
180 for(final Object listener
: contextListeners
.getListeners()){
181 ((IContextListener
) listener
).contextAboutToStop(memento
, subMonitor
);
182 subMonitor
.worked(1);
189 * <p>notifyContextStop</p>
191 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
193 public void notifyContextStop(IProgressMonitor monitor
) {
195 IProgressMonitor subMonitor
= StoreUtil
.getSubProgressMonitor(monitor
, 1);
197 subMonitor
.beginTask("Stoping context", contextListeners
.size());
198 MessagingUtils
.info("Notifying context listeners, that the context has stopped.");
200 for(Object listener
: contextListeners
.getListeners()){
201 ((IContextListener
) listener
).contextStop(memento
, subMonitor
);
202 subMonitor
.worked(1);
210 * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
214 public boolean preShutdown(IWorkbench workbench
, boolean forced
) {
218 IProgressMonitor monitor
= null;
220 for(Object listener
: contextListeners
.getListeners()){
221 ((IContextListener
) listener
).workbenchShutdown(memento
, monitor
);
226 // return true in any case, otherwise the application will not stop
231 * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
235 public void postShutdown(IWorkbench workbench
) {
241 private void readMemento(){
243 memento
= MementoHelper
.readMementoFromFile(getStateFileForCurrentDatabase());
244 } catch (FileNotFoundException e
) {
245 // no memento -> no previous state
246 MessagingUtils
.info("No state file for datasource");
250 private void createMemento(){
252 if (CdmStore
.getActiveCdmSource() != null) {
255 String name
= CdmStore
.getActiveCdmSource().getName();
257 name
= name
.replace(" ", "_");
258 memento
= XMLMemento
.createWriteRoot(name
);
260 MessagingUtils
.info("DataSource found. Memento created.");
261 } catch (Exception e
) {
262 // The memento could not be created, but a not closable editor is avoided for this case.
263 MessagingUtils
.error(this.getClass(), "The memento could not be created", e
);
266 MessagingUtils
.info("Not storing state data, because no DataSource present.");
271 private boolean saveMemento(){
272 return MementoHelper
.saveMementoToFile(memento
, getStateFileForCurrentDatabase()) != null;
276 * <p>getStateFileForCurrentDatabase</p>
278 * @return a {@link java.io.File} object.
280 protected File
getStateFileForCurrentDatabase() {
281 if(CdmStore
.getActiveCdmSource() == null){
285 IPath path
= TaxeditorStorePlugin
.getDefault().getStateLocation();
289 path
= path
.append("editor_state_" + CdmStore
.getActiveCdmSource().getName() + ".xml");
290 return path
.toFile();