]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/SessionUndoHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / SessionUndoHandler.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/SessionUndoHandler.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/SessionUndoHandler.java
new file mode 100644 (file)
index 0000000..bcf0803
--- /dev/null
@@ -0,0 +1,145 @@
+/*******************************************************************************\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;\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.AbstractHandler;\r
+import org.eclipse.core.commands.Command;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.State;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\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.IActionBars;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.commands.ICommandService;\r
+import org.eclipse.ui.handlers.HandlerUtil;\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.common.CommentMetadata;\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.ui.states.TrackedTextState;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * @author Kalle Kondelin\r
+ */\r
+public class SessionUndoHandler extends AbstractHandler {\r
+    \r
+    private final boolean DEBUG = false;\r
+    private final boolean ENABLED = true;\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent e) throws ExecutionException {\r
+        if (DEBUG)\r
+            System.out.println("--\nUndo handler called.");\r
+\r
+        IWorkbenchPart part = HandlerUtil.getActivePart(e);\r
+        if (part == null)\r
+            return null;\r
+        IActionBars actionBars = WorkbenchUtils.getActionBars(part);\r
+        final Session session = Simantics.peekSession();\r
+        if (session == null)\r
+            return null;\r
+        \r
+        ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
+        Command command = service.getCommand( TrackedTextState.COMMAND_ID );\r
+        State state = command.getState( TrackedTextState.STATE_ID );\r
+        \r
+        boolean sessionUndoEnabled = true;\r
+        Object value = state.getValue();\r
+        if (value != null && value instanceof Boolean)\r
+            sessionUndoEnabled = (Boolean) value;\r
+        \r
+        if (ENABLED) {\r
+            if (sessionUndoEnabled) {\r
+                try {\r
+                    final AtomicReference<String> msg = new AtomicReference<String>();\r
+                    IRunnableWithProgress undo = new IRunnableWithProgress() {\r
+                        @Override\r
+                        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                            try {\r
+                                monitor.beginTask("Undo", IProgressMonitor.UNKNOWN);\r
+                                msg.set( undo( session ) );\r
+                            } catch (NoHistoryException e) {\r
+                                msg.set("Nothing to undo.");\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, undo);\r
+                    actionBars.getStatusLineManager().setMessage( msg.get() );\r
+                } catch (InvocationTargetException e1) {\r
+                    throw new ExecutionException("Undo failed, database failure.", e1);\r
+                } catch (InterruptedException e1) {\r
+                    throw new ExecutionException("Undo failed, interrupted.", e1);\r
+                }\r
+            }\r
+        } else {\r
+            ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);\r
+        }\r
+        return null;\r
+    }\r
+\r
+    public static String getComment(Session session, ChangeSetIdentifier id) {\r
+        byte[] data = id.getMetadata().get(CommentMetadata.class.getName());\r
+        if(data == null)\r
+            return "Undescribed operation.";\r
+        String comment = CommentMetadata.deserialise(session, data).toString().trim();\r
+        if(comment.isEmpty())\r
+            return "Undescribed operation.";\r
+        return comment;\r
+    }\r
+    \r
+    private String undo(Session session) throws DatabaseException {\r
+        UndoRedoSupport support = session.getService(UndoRedoSupport.class);\r
+        \r
+        List<Operation> ops = support.undoAndReturnOperations(session, 1);\r
+        if(ops.isEmpty())\r
+            return "Undo history is empty.";        \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 = "Undo reverted: " + getComment(session, id);\r
+\r
+        if (DEBUG)\r
+            System.out.println(msg);\r
+        return msg;\r
+    }\r
+\r
+}\r