]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/SessionUndoHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / e4 / SessionUndoHandler.java
index bccedb364a7a7177a3c530b3b382b7fcaf14a190..cbb97ef104a05461ab196b13a0fd24ed6230f1c2 100644 (file)
-/*******************************************************************************\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.Command;\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.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.MApplication;\r
-import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
-import org.eclipse.e4.ui.workbench.modeling.EModelService;\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.eclipse.ui.commands.ICommandService;\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
- * @author Jani Simomaa\r
- */\r
-public class SessionUndoHandler {\r
-    \r
-    private final static boolean DEBUG = false;\r
-    private final static boolean ENABLED = true;\r
-    \r
-    @CanExecute\r
-    public boolean canExecute() {\r
-        return UndoRedoTester.canUndo();\r
-    }\r
-    \r
-    @Execute\r
-    public void execute(@Active MPart activePart, MApplication mApplication, EModelService modelService) throws ExecutionException {\r
-        if (DEBUG)\r
-            System.out.println("--\nUndo handler called.");\r
-\r
-        if (activePart == null)\r
-            return;\r
-        final Session session = Simantics.peekSession();\r
-        if (session == null)\r
-            return;\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
-                    \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
-//                    statusManager.setMessage( msg.get() );\r
-                } catch (InvocationTargetException e1) {\r
-                    throw new ExecutionException("Undo failed, database failure.", e1.getCause());\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;\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
+/*******************************************************************************
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.workbench.handler.e4;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.State;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.e4.core.contexts.Active;
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.jface.action.IStatusLineManager;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.commands.ICommandService;
+import org.simantics.Simantics;
+import org.simantics.db.ChangeSetIdentifier;
+import org.simantics.db.Operation;
+import org.simantics.db.Session;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.NoHistoryException;
+import org.simantics.db.service.ManagementSupport;
+import org.simantics.db.service.UndoRedoSupport;
+import org.simantics.ui.states.TrackedTextState;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * @author Kalle Kondelin
+ * @author Jani Simomaa
+ */
+public class SessionUndoHandler {
+    
+    private final static boolean DEBUG = false;
+    private final static boolean ENABLED = true;
+    
+    @CanExecute
+    public boolean canExecute() {
+        return UndoRedoTester.canUndo();
+    }
+    
+    @Execute
+    public void execute(@Active MPart activePart, MApplication mApplication, EModelService modelService) throws ExecutionException {
+        if (DEBUG)
+            System.out.println("--\nUndo handler called.");
+
+        if (activePart == null)
+            return;
+        final Session session = Simantics.peekSession();
+        if (session == null)
+            return;
+        
+        ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
+        Command command = service.getCommand( TrackedTextState.COMMAND_ID );
+        State state = command.getState( TrackedTextState.STATE_ID );
+        
+        boolean sessionUndoEnabled = true;
+        Object value = state.getValue();
+        if (value != null && value instanceof Boolean)
+            sessionUndoEnabled = (Boolean) value;
+        
+        if (ENABLED) {
+            if (sessionUndoEnabled) {
+                try {
+                    final AtomicReference<String> msg = new AtomicReference<String>();
+                    IRunnableWithProgress undo = new IRunnableWithProgress() {
+                        @Override
+                        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+                            try {
+                                monitor.beginTask("Undo", IProgressMonitor.UNKNOWN);
+                                msg.set( undo( session ) );
+                            } catch (NoHistoryException e) {
+                                msg.set("Nothing to undo.");
+                            } catch (DatabaseException e) {
+                                throw new InvocationTargetException(e);
+                            } finally {
+                                monitor.done();
+                            }
+                        }
+                    };
+    
+                    // busyCursorWhile does not work because locking the session for undo
+                    // will also lock the UI completely.
+                    //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
+                    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+                    new ProgressMonitorDialog(shell).run(true, false, undo);
+                    
+                    //
+                    // TODO Not the Eclipse 4 way of using IStatusLineManager!
+                    // See:
+                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332499
+                    // https://www.eclipse.org/forums/index.php?t=msg&th=367379&goto=941232&#msg_941232
+                    //
+                    IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();
+                    IStatusLineManager manager = WorkbenchUtils.getStatusLine(part);
+                    manager.setMessage(msg.get());
+//                    statusManager.setMessage( msg.get() );
+                } catch (InvocationTargetException e1) {
+                    throw new ExecutionException("Undo failed, database failure.", e1.getCause());
+                } catch (InterruptedException e1) {
+                    throw new ExecutionException("Undo failed, interrupted.", e1);
+                }
+            }
+        } else {
+            ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);
+        }
+        return;
+    }
+
+    public static String getComment(Session session, ChangeSetIdentifier id) {
+        byte[] data = id.getMetadata().get(CommentMetadata.class.getName());
+        if(data == null)
+            return "Undescribed operation.";
+        String comment = CommentMetadata.deserialise(session, data).toString().trim();
+        if(comment.isEmpty())
+            return "Undescribed operation.";
+        return comment;
+    }
+    
+    private String undo(Session session) throws DatabaseException {
+        UndoRedoSupport support = session.getService(UndoRedoSupport.class);
+        
+        List<Operation> ops = support.undoAndReturnOperations(session, 1);
+        if(ops.isEmpty())
+            return "Undo history is empty.";        
+        
+        Operation mainOperation = ops.get(0);
+        
+        String msg = null;
+        long csId = mainOperation.getCSId();
+
+        ManagementSupport management = session.getService(ManagementSupport.class);
+        for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
+            if(msg == null)
+                msg = "Undo reverted: " + getComment(session, id);
+
+        if (DEBUG)
+            System.out.println(msg);
+        return msg;
+    }
+
+}