]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/SessionUndoHandler.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / SessionUndoHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.ui.workbench.handler;\r
12 \r
13 import java.lang.reflect.InvocationTargetException;\r
14 import java.util.List;\r
15 import java.util.concurrent.atomic.AtomicReference;\r
16 \r
17 import org.eclipse.core.commands.AbstractHandler;\r
18 import org.eclipse.core.commands.Command;\r
19 import org.eclipse.core.commands.ExecutionEvent;\r
20 import org.eclipse.core.commands.ExecutionException;\r
21 import org.eclipse.core.commands.State;\r
22 import org.eclipse.core.runtime.IProgressMonitor;\r
23 import org.eclipse.core.runtime.IStatus;\r
24 import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
25 import org.eclipse.jface.operation.IRunnableWithProgress;\r
26 import org.eclipse.swt.widgets.Shell;\r
27 import org.eclipse.ui.IActionBars;\r
28 import org.eclipse.ui.IWorkbenchPart;\r
29 import org.eclipse.ui.PlatformUI;\r
30 import org.eclipse.ui.commands.ICommandService;\r
31 import org.eclipse.ui.handlers.HandlerUtil;\r
32 import org.simantics.Simantics;\r
33 import org.simantics.db.ChangeSetIdentifier;\r
34 import org.simantics.db.Operation;\r
35 import org.simantics.db.Session;\r
36 import org.simantics.db.common.CommentMetadata;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.exception.NoHistoryException;\r
39 import org.simantics.db.service.ManagementSupport;\r
40 import org.simantics.db.service.UndoRedoSupport;\r
41 import org.simantics.ui.states.TrackedTextState;\r
42 import org.simantics.utils.ui.ErrorLogger;\r
43 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
44 \r
45 /**\r
46  * @author Kalle Kondelin\r
47  */\r
48 public class SessionUndoHandler extends AbstractHandler {\r
49     \r
50     private final boolean DEBUG = false;\r
51     private final boolean ENABLED = true;\r
52 \r
53     @Override\r
54     public Object execute(ExecutionEvent e) throws ExecutionException {\r
55         if (DEBUG)\r
56             System.out.println("--\nUndo handler called.");\r
57 \r
58         IWorkbenchPart part = HandlerUtil.getActivePart(e);\r
59         if (part == null)\r
60             return null;\r
61         IActionBars actionBars = WorkbenchUtils.getActionBars(part);\r
62         final Session session = Simantics.peekSession();\r
63         if (session == null)\r
64             return null;\r
65         \r
66         ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
67         Command command = service.getCommand( TrackedTextState.COMMAND_ID );\r
68         State state = command.getState( TrackedTextState.STATE_ID );\r
69         \r
70         boolean sessionUndoEnabled = true;\r
71         Object value = state.getValue();\r
72         if (value != null && value instanceof Boolean)\r
73             sessionUndoEnabled = (Boolean) value;\r
74         \r
75         if (ENABLED) {\r
76             if (sessionUndoEnabled) {\r
77                 try {\r
78                     final AtomicReference<String> msg = new AtomicReference<String>();\r
79                     IRunnableWithProgress undo = new IRunnableWithProgress() {\r
80                         @Override\r
81                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
82                             try {\r
83                                 monitor.beginTask("Undo", IProgressMonitor.UNKNOWN);\r
84                                 msg.set( undo( session ) );\r
85                             } catch (NoHistoryException e) {\r
86                                 msg.set("Nothing to undo.");\r
87                             } catch (DatabaseException e) {\r
88                                 throw new InvocationTargetException(e);\r
89                             } finally {\r
90                                 monitor.done();\r
91                             }\r
92                         }\r
93                     };\r
94     \r
95                     // busyCursorWhile does not work because locking the session for undo\r
96                     // will also lock the UI completely.\r
97                     //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);\r
98                     Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
99                     new ProgressMonitorDialog(shell).run(true, false, undo);\r
100                     actionBars.getStatusLineManager().setMessage( msg.get() );\r
101                 } catch (InvocationTargetException e1) {\r
102                     throw new ExecutionException("Undo failed, database failure.", e1.getCause());\r
103                 } catch (InterruptedException e1) {\r
104                     throw new ExecutionException("Undo failed, interrupted.", e1);\r
105                 }\r
106             }\r
107         } else {\r
108             ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);\r
109         }\r
110         return null;\r
111     }\r
112 \r
113     public static String getComment(Session session, ChangeSetIdentifier id) {\r
114         byte[] data = id.getMetadata().get(CommentMetadata.class.getName());\r
115         if(data == null)\r
116             return "Undescribed operation.";\r
117         String comment = CommentMetadata.deserialise(session, data).toString().trim();\r
118         if(comment.isEmpty())\r
119             return "Undescribed operation.";\r
120         return comment;\r
121     }\r
122     \r
123     private String undo(Session session) throws DatabaseException {\r
124         UndoRedoSupport support = session.getService(UndoRedoSupport.class);\r
125         \r
126         List<Operation> ops = support.undoAndReturnOperations(session, 1);\r
127         if(ops.isEmpty())\r
128             return "Undo history is empty.";        \r
129         \r
130         Operation mainOperation = ops.get(0);\r
131         \r
132         String msg = null;\r
133         long csId = mainOperation.getCSId();\r
134 \r
135         ManagementSupport management = session.getService(ManagementSupport.class);\r
136         for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))\r
137             if(msg == null)\r
138                 msg = "Undo reverted: " + getComment(session, id);\r
139 \r
140         if (DEBUG)\r
141             System.out.println(msg);\r
142         return msg;\r
143     }\r
144 \r
145 }\r