]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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.e4;\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.ExecutionException;\r
18 import org.eclipse.core.runtime.IProgressMonitor;\r
19 import org.eclipse.e4.core.contexts.Active;\r
20 import org.eclipse.e4.core.di.annotations.CanExecute;\r
21 import org.eclipse.e4.core.di.annotations.Execute;\r
22 import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
23 import org.eclipse.jface.action.IStatusLineManager;\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.IWorkbenchPart;\r
28 import org.eclipse.ui.PlatformUI;\r
29 import org.simantics.Simantics;\r
30 import org.simantics.db.ChangeSetIdentifier;\r
31 import org.simantics.db.Operation;\r
32 import org.simantics.db.Session;\r
33 import org.simantics.db.exception.DatabaseException;\r
34 import org.simantics.db.exception.NoHistoryException;\r
35 import org.simantics.db.service.ManagementSupport;\r
36 import org.simantics.db.service.UndoRedoSupport;\r
37 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
38 \r
39 /**\r
40  * @author Kalle Kondelin\r
41  */\r
42 public class SessionRedoHandler {\r
43 \r
44     private final boolean DEBUG = false;\r
45 \r
46     @CanExecute\r
47     public boolean canExecute() {\r
48         return UndoRedoTester.canRedo();\r
49     }\r
50     \r
51     @Execute\r
52     public void execute(@Active MPart activePart) throws ExecutionException {\r
53         if (DEBUG)\r
54             System.out.println("--\nRedo handler called.");\r
55 \r
56         if (activePart == null)\r
57             return;\r
58         final Session session = Simantics.peekSession();\r
59         if (session == null)\r
60             return;\r
61 \r
62         try {\r
63             final AtomicReference<String> msg = new AtomicReference<String>();\r
64             IRunnableWithProgress redo = new IRunnableWithProgress() {\r
65                 @Override\r
66                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
67                     try {\r
68                         monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);\r
69                         msg.set( redo( session ) );\r
70                     } catch (NoHistoryException e) {\r
71                         msg.set("Nothing to redo.");\r
72                     } catch (DatabaseException e) {\r
73                         throw new InvocationTargetException(e);\r
74                     } finally {\r
75                         monitor.done();\r
76                     }\r
77                 }\r
78             };\r
79 \r
80             // busyCursorWhile does not work because locking the session for undo\r
81             // will also lock the UI completely.\r
82             //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);\r
83             Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
84             new ProgressMonitorDialog(shell).run(true, false, redo);\r
85             \r
86             //\r
87             // TODO Not the Eclipse 4 way of using IStatusLineManager!\r
88             // See:\r
89             // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332499\r
90             // https://www.eclipse.org/forums/index.php?t=msg&th=367379&goto=941232&#msg_941232\r
91             //\r
92             IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();\r
93             IStatusLineManager manager = WorkbenchUtils.getStatusLine(part);\r
94             manager.setMessage(msg.get());\r
95 //            actionBars.getStatusLineManager().setMessage( msg.get() );\r
96         } catch (InvocationTargetException e1) {\r
97             throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());\r
98         } catch (InterruptedException e1) {\r
99             throw new ExecutionException("Redo failed, interrupted.", e1);\r
100         }\r
101         return;\r
102     }\r
103 \r
104     private String redo(Session session) throws DatabaseException {\r
105         UndoRedoSupport support = session.getService(UndoRedoSupport.class);\r
106         List<Operation> ops = support.redo(session, 1);\r
107         if(ops.isEmpty())\r
108             return "Nothing to redo.";\r
109         \r
110         Operation mainOperation = ops.get(0);\r
111         \r
112         String msg = null;\r
113         long csId = mainOperation.getCSId();\r
114 \r
115         ManagementSupport management = session.getService(ManagementSupport.class);\r
116         for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))\r
117             if(msg == null)\r
118                 msg = "Redid: " + SessionUndoHandler.getComment(session, id);\r
119 \r
120         if (DEBUG)\r
121             System.out.println(msg);\r
122         return msg;\r
123     }\r
124 \r
125 }\r