]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/SessionRedoHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / e4 / SessionRedoHandler.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/SessionRedoHandler.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/SessionRedoHandler.java
new file mode 100644 (file)
index 0000000..20208bb
--- /dev/null
@@ -0,0 +1,125 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench.handler.e4;\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.List;\r
+import java.util.concurrent.atomic.AtomicReference;\r
+\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.e4.core.contexts.Active;\r
+import org.eclipse.e4.core.di.annotations.CanExecute;\r
+import org.eclipse.e4.core.di.annotations.Execute;\r
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
+import org.eclipse.jface.action.IStatusLineManager;\r
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ChangeSetIdentifier;\r
+import org.simantics.db.Operation;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.NoHistoryException;\r
+import org.simantics.db.service.ManagementSupport;\r
+import org.simantics.db.service.UndoRedoSupport;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * @author Kalle Kondelin\r
+ */\r
+public class SessionRedoHandler {\r
+\r
+    private final boolean DEBUG = false;\r
+\r
+    @CanExecute\r
+    public boolean canExecute() {\r
+        return UndoRedoTester.canRedo();\r
+    }\r
+    \r
+    @Execute\r
+    public void execute(@Active MPart activePart) throws ExecutionException {\r
+        if (DEBUG)\r
+            System.out.println("--\nRedo handler called.");\r
+\r
+        if (activePart == null)\r
+            return;\r
+        final Session session = Simantics.peekSession();\r
+        if (session == null)\r
+            return;\r
+\r
+        try {\r
+            final AtomicReference<String> msg = new AtomicReference<String>();\r
+            IRunnableWithProgress redo = new IRunnableWithProgress() {\r
+                @Override\r
+                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                    try {\r
+                        monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);\r
+                        msg.set( redo( session ) );\r
+                    } catch (NoHistoryException e) {\r
+                        msg.set("Nothing to redo.");\r
+                    } catch (DatabaseException e) {\r
+                        throw new InvocationTargetException(e);\r
+                    } finally {\r
+                        monitor.done();\r
+                    }\r
+                }\r
+            };\r
+\r
+            // busyCursorWhile does not work because locking the session for undo\r
+            // will also lock the UI completely.\r
+            //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);\r
+            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
+            new ProgressMonitorDialog(shell).run(true, false, redo);\r
+            \r
+            //\r
+            // TODO Not the Eclipse 4 way of using IStatusLineManager!\r
+            // See:\r
+            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332499\r
+            // https://www.eclipse.org/forums/index.php?t=msg&th=367379&goto=941232&#msg_941232\r
+            //\r
+            IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();\r
+            IStatusLineManager manager = WorkbenchUtils.getStatusLine(part);\r
+            manager.setMessage(msg.get());\r
+//            actionBars.getStatusLineManager().setMessage( msg.get() );\r
+        } catch (InvocationTargetException e1) {\r
+            throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());\r
+        } catch (InterruptedException e1) {\r
+            throw new ExecutionException("Redo failed, interrupted.", e1);\r
+        }\r
+        return;\r
+    }\r
+\r
+    private String redo(Session session) throws DatabaseException {\r
+        UndoRedoSupport support = session.getService(UndoRedoSupport.class);\r
+        List<Operation> ops = support.redo(session, 1);\r
+        if(ops.isEmpty())\r
+            return "Nothing to redo.";\r
+        \r
+        Operation mainOperation = ops.get(0);\r
+        \r
+        String msg = null;\r
+        long csId = mainOperation.getCSId();\r
+\r
+        ManagementSupport management = session.getService(ManagementSupport.class);\r
+        for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))\r
+            if(msg == null)\r
+                msg = "Redid: " + SessionUndoHandler.getComment(session, id);\r
+\r
+        if (DEBUG)\r
+            System.out.println(msg);\r
+        return msg;\r
+    }\r
+\r
+}\r