]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/SessionRedoHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / SessionRedoHandler.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.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
22 import org.eclipse.jface.operation.IRunnableWithProgress;\r
23 import org.eclipse.swt.widgets.Shell;\r
24 import org.eclipse.ui.IActionBars;\r
25 import org.eclipse.ui.IWorkbenchPart;\r
26 import org.eclipse.ui.PlatformUI;\r
27 import org.eclipse.ui.handlers.HandlerUtil;\r
28 import org.simantics.Simantics;\r
29 import org.simantics.db.ChangeSetIdentifier;\r
30 import org.simantics.db.Operation;\r
31 import org.simantics.db.Session;\r
32 import org.simantics.db.exception.DatabaseException;\r
33 import org.simantics.db.exception.NoHistoryException;\r
34 import org.simantics.db.service.ManagementSupport;\r
35 import org.simantics.db.service.UndoRedoSupport;\r
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
37 \r
38 /**\r
39  * @author Kalle Kondelin\r
40  */\r
41 public class SessionRedoHandler extends AbstractHandler {\r
42 \r
43     private final boolean DEBUG = false;\r
44 \r
45     @Override\r
46     public Object execute(ExecutionEvent e) throws ExecutionException {\r
47         if (DEBUG)\r
48             System.out.println("--\nRedo handler called.");\r
49 \r
50         IWorkbenchPart part = HandlerUtil.getActivePart(e);\r
51         if (part == null)\r
52             return null;\r
53         IActionBars actionBars = WorkbenchUtils.getActionBars(part);\r
54         final Session session = Simantics.peekSession();\r
55         if (session == null)\r
56             return null;\r
57 \r
58         try {\r
59             final AtomicReference<String> msg = new AtomicReference<String>();\r
60             IRunnableWithProgress redo = new IRunnableWithProgress() {\r
61                 @Override\r
62                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
63                     try {\r
64                         monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);\r
65                         msg.set( redo( session ) );\r
66                     } catch (NoHistoryException e) {\r
67                         msg.set("Nothing to redo.");\r
68                     } catch (DatabaseException e) {\r
69                         throw new InvocationTargetException(e);\r
70                     } finally {\r
71                         monitor.done();\r
72                     }\r
73                 }\r
74             };\r
75 \r
76             // busyCursorWhile does not work because locking the session for undo\r
77             // will also lock the UI completely.\r
78             //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);\r
79             Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
80             new ProgressMonitorDialog(shell).run(true, false, redo);\r
81             actionBars.getStatusLineManager().setMessage( msg.get() );\r
82         } catch (InvocationTargetException e1) {\r
83             throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());\r
84         } catch (InterruptedException e1) {\r
85             throw new ExecutionException("Redo failed, interrupted.", e1);\r
86         }\r
87         return null;\r
88     }\r
89 \r
90     private String redo(Session session) throws DatabaseException {\r
91         UndoRedoSupport support = session.getService(UndoRedoSupport.class);\r
92         List<Operation> ops = support.redo(session, 1);\r
93         if(ops.isEmpty())\r
94             return "Nothing to redo.";\r
95         \r
96         Operation mainOperation = ops.get(0);\r
97         \r
98         String msg = null;\r
99         long csId = mainOperation.getCSId();\r
100 \r
101         ManagementSupport management = session.getService(ManagementSupport.class);\r
102         for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))\r
103             if(msg == null)\r
104                 msg = "Redid: " + SessionUndoHandler.getComment(session, id);\r
105 \r
106         if (DEBUG)\r
107             System.out.println(msg);\r
108         return msg;\r
109     }\r
110 \r
111 }\r