-// $Id$
/**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
-* http://www.e-taxonomy.eu
-*
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
+ * Copyright (C) 2007 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
package eu.etaxonomy.taxeditor.store;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.internal.Workbench;
+import org.springframework.remoting.RemoteAccessException;
import eu.etaxonomy.taxeditor.model.IContextListener;
import eu.etaxonomy.taxeditor.model.MementoHelper;
*/
public class ContextManager implements IWorkbenchListener{
- private final ListenerList contextListeners = new ListenerList();
-
- private IMemento memento;
-
- /**
- * <p>Constructor for ContextManager.</p>
- */
- protected ContextManager() {
- if(Workbench.getInstance() != null) {
- PlatformUI.getWorkbench().addWorkbenchListener(this);
- }
- }
-
- /**
- * <p>addContextListener</p>
- *
- * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
- */
- public void addContextListener(IContextListener listener){
- contextListeners.add(listener);
- }
-
- /**
- * <p>removeContextListener</p>
- *
- * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
- */
- public void removeContextListener(IContextListener listener) {
- contextListeners.remove(listener);
- }
-
- /**
- * <p>notifyContextStart</p>
- */
- public void notifyContextStart() {
- MessagingUtils.info("Notifying context listeners, that the context has started.");
- ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
-
- try {
- dialog.run(false, false, new IRunnableWithProgress() {
- /* (non-Javadoc)
- * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
- */
- @Override
- public void run(IProgressMonitor monitor)
- throws InvocationTargetException, InterruptedException {
- monitor.beginTask("Starting context", contextListeners.size());
-
-
- readMemento();
-
- for(final Object listener : contextListeners.getListeners()){
- ((IContextListener) listener).contextStart(memento, monitor);
- monitor.worked(1);
- }
- monitor.done();
- }
- });
- } catch (InvocationTargetException e) {
- MessagingUtils.error(getClass(), e);
- } catch (InterruptedException e) {
- MessagingUtils.error(getClass(), e);
- }
- }
-
-
- /**
- *
- */
- public void notifyContextRefresh() {
- MessagingUtils.info("Notifying context listeners, that the context needs to be refreshed.");
- ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
-
- try {
- dialog.run(false, false, new IRunnableWithProgress() {
- /* (non-Javadoc)
- * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
- */
- @Override
- public void run(IProgressMonitor monitor)
- throws InvocationTargetException, InterruptedException {
- monitor.beginTask("Refreshing context", contextListeners.size());
-
- for(final Object listener : contextListeners.getListeners()){
- ((IContextListener) listener).contextRefresh(monitor);
- monitor.worked(1);
- }
- monitor.done();
- }
- });
- } catch (InvocationTargetException e) {
- MessagingUtils.error(getClass(), e);
- } catch (InterruptedException e) {
- MessagingUtils.error(getClass(), e);
- }
- }
-
- /**
- * <p>notifyContextAboutToStop</p>
- *
- * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
- */
- public void notifyContextAboutToStop(final IProgressMonitor monitor){
-
- IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
-
- subMonitor.beginTask("Stoping context", contextListeners.size());
- // we are creating the memento here; even if the context is not stopped
- createMemento();
-
- for(final Object listener : contextListeners.getListeners()){
- ((IContextListener) listener).contextAboutToStop(memento, subMonitor);
- subMonitor.worked(1);
- }
-
- subMonitor.done();
- }
-
- /**
- * <p>notifyContextStop</p>
- *
- * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
- */
- public void notifyContextStop(IProgressMonitor monitor) {
-
- IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
-
- subMonitor.beginTask("Stoping context", contextListeners.size());
- MessagingUtils.info("Notifying context listeners, that the context has stopped.");
-
- for(Object listener : contextListeners.getListeners()){
- ((IContextListener) listener).contextStop(memento, subMonitor);
- subMonitor.worked(1);
- }
-
- saveMemento();
- subMonitor.done();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
- */
- /** {@inheritDoc} */
- @Override
- public boolean preShutdown(IWorkbench workbench, boolean forced) {
-
- createMemento();
-
- IProgressMonitor monitor = null;
-
- for(Object listener : contextListeners.getListeners()){
- ((IContextListener) listener).workbenchShutdown(memento, monitor);
- }
-
- saveMemento();
-
- // return true in any case, otherwise the application will not stop
- return true;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
- */
- /** {@inheritDoc} */
- @Override
- public void postShutdown(IWorkbench workbench) {
-
-
- }
-
-
- private void readMemento(){
- try {
- memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase());
- } catch (FileNotFoundException e) {
- // no memento -> no previous state
- MessagingUtils.info("No state file for datasource");
- }
- }
-
- private void createMemento(){
-
- if (CdmStore.getActiveCdmSource() != null) {
-
- try {
- String name = CdmStore.getActiveCdmSource().getName();
- name = name.trim();
- name = name.replace(" ", "_");
- memento = XMLMemento.createWriteRoot(name);
-
- MessagingUtils.info("DataSource found. Memento created.");
- } catch (Exception e) {
- // The memento could not be created, but a not closable editor is avoided for this case.
- MessagingUtils.error(this.getClass(), "The memento could not be created", e);
- }
- } else {
- MessagingUtils.info("Not storing state data, because no DataSource present.");
- }
-
- }
-
- private boolean saveMemento(){
- return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null;
- }
-
- /**
- * <p>getStateFileForCurrentDatabase</p>
- *
- * @return a {@link java.io.File} object.
- */
- protected File getStateFileForCurrentDatabase() {
- if(CdmStore.getActiveCdmSource() == null){
- return null;
- }
-
- IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
- if (path == null) {
- return null;
- }
- path = path.append("editor_state_" + CdmStore.getActiveCdmSource().getName() + ".xml");
- return path.toFile();
- }
+ private final ListenerList contextListeners = new ListenerList();
+
+ private IMemento memento;
+
+ /**
+ * <p>Constructor for ContextManager.</p>
+ */
+ protected ContextManager() {
+ if(Workbench.getInstance() != null) {
+ PlatformUI.getWorkbench().addWorkbenchListener(this);
+ }
+ }
+
+ /**
+ * <p>addContextListener</p>
+ *
+ * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
+ */
+ public void addContextListener(IContextListener listener){
+ contextListeners.add(listener);
+ }
+
+ /**
+ * <p>removeContextListener</p>
+ *
+ * @param listener a {@link eu.etaxonomy.taxeditor.model.IContextListener} object.
+ */
+ public void removeContextListener(IContextListener listener) {
+ contextListeners.remove(listener);
+ }
+
+ public void notifyContextStartWithoutDialog(IProgressMonitor monitor) {
+ MessagingUtils.info("Notifying context listeners, that the context has started.");
+
+ readMemento();
+
+ for(final Object listener : contextListeners.getListeners()){
+ ((IContextListener) listener).contextStart(memento, monitor);
+ }
+ }
+ /**
+ * <p>notifyContextStart</p>
+ * @throws Throwable
+ */
+ public void notifyContextStart() throws RemoteAccessException {
+ MessagingUtils.info("Notifying context listeners, that the context has started.");
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
+
+ try {
+ dialog.run(false, false, new IRunnableWithProgress() {
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public void run(IProgressMonitor monitor)
+ throws InvocationTargetException, InterruptedException {
+ monitor.beginTask("Starting context", contextListeners.size());
+
+
+ readMemento();
+
+ for(final Object listener : contextListeners.getListeners()){
+ ((IContextListener) listener).contextStart(memento, monitor);
+ monitor.worked(1);
+ }
+ monitor.done();
+ }
+ });
+ } catch (InvocationTargetException e) {
+ if (e.getTargetException().getClass().equals(RemoteAccessException.class)){
+ if (e.getTargetException().getMessage().contains("403")){
+ // new CdmAuthenticationException("Access Denied", e.getTargetException());
+ throw new CdmAuthenticationException("You are logged in now but you are not permitted to use the TaxEditor with the selected data source", e.getTargetException());
+ }else{
+ MessagingUtils.error(getClass(), e.getTargetException());
+ }
+ }else{
+ MessagingUtils.error(getClass(), e);
+ }
+ } catch (InterruptedException e) {
+ MessagingUtils.error(getClass(), e);
+ }
+ }
+
+
+ /**
+ *
+ */
+ public void notifyContextRefresh() {
+ MessagingUtils.info("Notifying context listeners, that the context needs to be refreshed.");
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
+
+ try {
+ dialog.run(false, false, new IRunnableWithProgress() {
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public void run(IProgressMonitor monitor)
+ throws InvocationTargetException, InterruptedException {
+ monitor.beginTask("Refreshing context", contextListeners.size());
+
+ for(final Object listener : contextListeners.getListeners()){
+ ((IContextListener) listener).contextRefresh(monitor);
+ monitor.worked(1);
+ }
+ monitor.done();
+ }
+ });
+ } catch (InvocationTargetException e) {
+ if (e.getTargetException().getClass().equals(RemoteAccessException.class)){
+ if (e.getTargetException().getMessage().contains("403")){
+ MessagingUtils.confirmDialog("Access denied", "Access denied");
+ }else{
+ MessagingUtils.error(getClass(), e.getTargetException());
+ }
+ }else{
+ MessagingUtils.error(getClass(), e);
+ }
+ } catch (InterruptedException e) {
+ MessagingUtils.error(getClass(), e);
+ }
+ }
+
+ /**
+ * <p>notifyContextAboutToStop</p>
+ *
+ * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
+ */
+ public void notifyContextAboutToStop(final IProgressMonitor monitor){
+
+ IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
+
+ subMonitor.beginTask("Stoping context", contextListeners.size());
+ // we are creating the memento here; even if the context is not stopped
+ createMemento();
+
+ for(final Object listener : contextListeners.getListeners()){
+ ((IContextListener) listener).contextAboutToStop(memento, subMonitor);
+ subMonitor.worked(1);
+ }
+
+ subMonitor.done();
+ }
+
+ /**
+ * <p>notifyContextStop</p>
+ *
+ * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
+ */
+ public void notifyContextStop(IProgressMonitor monitor) {
+
+ IProgressMonitor subMonitor = StoreUtil.getSubProgressMonitor(monitor, 1);
+
+ subMonitor.beginTask("Stoping context", contextListeners.size());
+ MessagingUtils.info("Notifying context listeners, that the context has stopped.");
+
+ for(Object listener : contextListeners.getListeners()){
+ ((IContextListener) listener).contextStop(memento, subMonitor);
+ subMonitor.worked(1);
+ }
+
+ saveMemento();
+ subMonitor.done();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IWorkbenchListener#preShutdown(org.eclipse.ui.IWorkbench, boolean)
+ */
+ /** {@inheritDoc} */
+ @Override
+ public boolean preShutdown(IWorkbench workbench, boolean forced) {
+
+ createMemento();
+
+ IProgressMonitor monitor = null;
+
+ for(Object listener : contextListeners.getListeners()){
+ ((IContextListener) listener).workbenchShutdown(memento, monitor);
+ }
+
+ saveMemento();
+
+ // return true in any case, otherwise the application will not stop
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IWorkbenchListener#postShutdown(org.eclipse.ui.IWorkbench)
+ */
+ /** {@inheritDoc} */
+ @Override
+ public void postShutdown(IWorkbench workbench) {
+
+
+ }
+
+
+ private void readMemento(){
+ try {
+ memento = MementoHelper.readMementoFromFile(getStateFileForCurrentDatabase());
+ } catch (FileNotFoundException e) {
+ // no memento -> no previous state
+ MessagingUtils.info("No state file for datasource");
+ }
+ }
+
+ private void createMemento(){
+
+ if (CdmStore.getActiveCdmSource() != null) {
+
+ try {
+ String name = CdmStore.getActiveCdmSource().getName();
+ name = name.trim();
+ name = name.replace(" ", "_");
+ memento = XMLMemento.createWriteRoot(name);
+
+ MessagingUtils.info("DataSource found. Memento created.");
+ } catch (Exception e) {
+ // The memento could not be created, but a not closable editor is avoided for this case.
+ MessagingUtils.error(this.getClass(), "The memento could not be created", e);
+ }
+ } else {
+ MessagingUtils.info("Not storing state data, because no DataSource present.");
+ }
+
+ }
+
+ private boolean saveMemento(){
+ return MementoHelper.saveMementoToFile(memento, getStateFileForCurrentDatabase()) != null;
+ }
+
+ /**
+ * <p>getStateFileForCurrentDatabase</p>
+ *
+ * @return a {@link java.io.File} object.
+ */
+ protected File getStateFileForCurrentDatabase() {
+ if(CdmStore.getActiveCdmSource() == null){
+ return null;
+ }
+
+ IPath path = TaxeditorStorePlugin.getDefault().getStateLocation();
+ if (path == null) {
+ return null;
+ }
+ path = path.append("editor_state_" + CdmStore.getActiveCdmSource().getName() + ".xml");
+ return path.toFile();
+ }
}