Bugfixes merged into trunk
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / model / AbstractUtility.java
index d62fc28f5e0bc213de3263b4e2a83398f40c04ea..105237af10a10d170cccef4d98f49f0c4d3635e7 100644 (file)
@@ -15,26 +15,29 @@ import java.lang.reflect.InvocationTargetException;
 import org.apache.log4j.Logger;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.operations.IOperationHistory;
-import org.eclipse.core.commands.operations.IUndoableOperation;
+import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
 import org.eclipse.jface.action.IStatusLineManager;
 import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.dialogs.MessageDialogWithToggle;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.resource.ColorRegistry;
 import org.eclipse.jface.resource.FontRegistry;
+import org.eclipse.jface.window.ApplicationWindow;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IViewReference;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
@@ -42,6 +45,8 @@ import org.eclipse.ui.progress.IProgressService;
 import org.eclipse.ui.themes.ITheme;
 import org.eclipse.ui.themes.IThemeManager;
 
+import eu.etaxonomy.taxeditor.operations.AbstractPostOperation;
+import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
@@ -63,10 +68,22 @@ public abstract class AbstractUtility {
        }
        
        public static IWorkbenchPage getActivePage(){
+               
                return TaxeditorStorePlugin.getDefault().getWorkbench()
                        .getActiveWorkbenchWindow().getActivePage();
        }
        
+       public static IWorkbenchPart getActivePart(){
+               return getActivePage().getActivePart();
+       }
+       
+       public static ApplicationWindow getWorkbenchWindow(){
+               if(TaxeditorStorePlugin.getDefault().getWorkbench().getWorkbenchWindowCount() > 1){
+                       throw new IllegalStateException("More than one workbench window");
+               }
+               return (ApplicationWindow) TaxeditorStorePlugin.getDefault().getWorkbench().getWorkbenchWindows()[0];
+       }
+       
        public static IViewPart showView(String id){
                try {
                        return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
@@ -145,54 +162,102 @@ public abstract class AbstractUtility {
                warningDialog("Not yet implemented", "This functionality is not yet implemented.");
        }
        
-       public static void informationDialog(String title, String message){
-               MessageDialog.openInformation(getShell(), title, message);
+       public static void informationDialog(final String title, final String message){
+               Display.getDefault().asyncExec(new Runnable(){
+
+                       public void run() {
+                               MessageDialog.openInformation(getShell(), title, message);
+                       }
+               });
        }
        
-       public static void warningDialog(String title, String message){
-               MessageDialog.openWarning(getShell(), title, message);
+       public static void warningDialog(final String title, final String message){
+               Display.getDefault().asyncExec(new Runnable(){
+
+                       public void run() {
+                               MessageDialog.openWarning(getShell(), title, message);
+                       }
+               });
        }
        
-       public static void errorDialog(String title, String message){
-               MessageDialog.openError(getShell(), title, message);
+       public static void errorDialog(final String title, final String message){
+               Display.getDefault().asyncExec(new Runnable(){
+
+                       public void run() {
+                               MessageDialog.openError(getShell(), title, message);
+                       }
+               });
        }
 
        public static boolean confirmDialog(String title, String message) {
                return MessageDialog.openQuestion(getShell(), title, message);
        }
        
-       public static IStatus executeOperation(IUndoableOperation operation){
+       public static IStatus executeOperation(final AbstractPostOperation operation){
                if(getOperationHistory() == null){
                        throw new IllegalArgumentException("There is no operation history for this context");
                }
                
-               // Start the main progress monitor.
-        IProgressMonitor newMonitor = startMainMonitor(getMonitor(),operation.getLabel(), 100);
-
-               // Check whether operation was canceled and do some steps.
-        workedChecked(newMonitor, 10);
-
-        try {
-                       IStatus status = getOperationHistory().execute(operation, newMonitor,
-                                       WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
-
-                       // Check whether operation was canceled and do some steps.
-                       workedChecked(newMonitor, 30);
-
-               String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
-                       setStatusLine(operation.getLabel() + " " + statusString + ".");
-
-               return status;
-               } catch (ExecutionException e) {
-                       logger.error("Error executing operation: " + operation.getLabel(), e);
-                       errorDialog("Error executing operation: " + operation.getLabel(), "Please refer to the error log.");
+               final IAdaptable uiInfoAdapter = WorkspaceUndoUtil.getUIInfoAdapter(getShell());
+               
+               
+               
+               IRunnableWithProgress runnable = new IRunnableWithProgress() {
+                       
+                       public void run(IProgressMonitor monitor) throws InvocationTargetException,
+                                       InterruptedException {
+                               monitor.beginTask(operation.getLabel(), 100);
+                               IStatus status;
+                               try {
+                                       status = getOperationHistory().execute(operation, monitor, uiInfoAdapter);
+                               } catch (ExecutionException e) {
+                                       throw new RuntimeException(e);
+                               }
+                               monitor.done();
+                       String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
+                               setStatusLine(operation.getLabel() + " " + statusString + ".");
+                               
+                       }
+               };
+               
+               try {
+                       runInUI(runnable, null);
+               } catch (InvocationTargetException e) {
+                       throw new RuntimeException(e);
+               } catch (InterruptedException e) {
+                       throw new RuntimeException(e);
                }
-        finally {
-               
-               // Stop the progress monitor.
-            newMonitor.done();
-        }
-               return null;
+               
+//             // Start the main progress monitor.
+//        IProgressMonitor newMonitor = startMainMonitor(getMonitor(),operation.getLabel(), 100);
+//
+//             // Check whether operation was canceled and do some steps.
+//        workedChecked(newMonitor, 10);
+//
+//        try {
+//                     IStatus status = getOperationHistory().execute(operation, newMonitor,
+//                                     WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
+//
+//                     // Check whether operation was canceled and do some steps.
+//                     workedChecked(newMonitor, 30);
+//
+//             String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
+//                     setStatusLine(operation.getLabel() + " " + statusString + ".");
+//
+//             return status;
+//             } catch (ExecutionException e) {
+//                     logger.error("Error executing operation: " + operation.getLabel(), e);
+//                     errorDialog("Error executing operation: " + operation.getLabel(), "Please refer to the error log.");
+//             }
+//        finally {
+//             
+//             // Stop the progress monitor.
+//            newMonitor.done();
+//        }
+               
+               IPostOperationEnabled postOperationEnabled = operation.getPostOperationEnabled();
+               postOperationEnabled.onComplete();
+               return Status.OK_STATUS;
        }
        
        public static IOperationHistory getOperationHistory(){
@@ -200,8 +265,15 @@ public abstract class AbstractUtility {
                                                        getOperationSupport().getOperationHistory();
        }
        
-       public static void setStatusLine(String message) {
-               statusLineManager.setMessage(message);
+       public static void setStatusLine(final String message) {
+               Display.getDefault().asyncExec(new Runnable(){
+
+                       public void run() {
+                               statusLineManager.setMessage(message);
+                       }
+                       
+               });
+               
        }
        
        protected static IProgressMonitor getMonitor() {
@@ -262,30 +334,24 @@ public abstract class AbstractUtility {
         * Present a progress dialog to the user. This dialog will block the UI
         * 
         * @param runnable an implementation of {@link IRunnableWithProgress}
+        * @throws InterruptedException 
+        * @throws InvocationTargetException 
         */
-       public static void busyCursorWhile(IRunnableWithProgress runnable){
-               IWorkbench workbench = PlatformUI.getWorkbench();
-               IProgressService progressService = workbench.getProgressService();
-               try {
-                       progressService.busyCursorWhile(runnable);
-               } catch (InvocationTargetException e) {
-                       logger.error("InvocationTargetException while running busy cursor", e);
-               } catch (InterruptedException e) {
-                       logger.error("InterruptedException while running busy cursor", e);
-               }
+       public static void busyCursorWhile(IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException{
+               getProgressService().busyCursorWhile(runnable);
+       }
+       
+       public static void runInUI(IRunnableWithProgress runnable, ISchedulingRule rule) throws InvocationTargetException, InterruptedException{
+               getProgressService().runInUI(getWorkbenchWindow(), runnable, rule);             
+       }
+       
+       public static void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException{
+               getProgressService().run(fork, cancelable, runnable);
        }
        
-       public static void runAsynchroneously(IRunnableWithProgress runnable){
+       public static IProgressService getProgressService(){
                IWorkbench workbench = PlatformUI.getWorkbench();
-               IProgressService progressService = workbench.getProgressService();
-               
-               try {
-                       progressService.run(true, false, runnable);
-               } catch (InvocationTargetException e) {
-                       logger.error("InvocationTargetException while running busy cursor", e);
-               } catch (InterruptedException e) {
-                       logger.error("InterruptedException while running busy cursor", e);
-               }
+               return workbench.getProgressService();
        }
 
 }