]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/SessionUndoHandler.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / e4 / 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.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.Command;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.core.commands.State;\r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.eclipse.core.runtime.IStatus;\r
22 import org.eclipse.e4.core.contexts.Active;\r
23 import org.eclipse.e4.core.di.annotations.CanExecute;\r
24 import org.eclipse.e4.core.di.annotations.Execute;\r
25 import org.eclipse.e4.ui.model.application.MApplication;\r
26 import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
27 import org.eclipse.e4.ui.workbench.modeling.EModelService;\r
28 import org.eclipse.jface.action.IStatusLineManager;\r
29 import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
30 import org.eclipse.jface.operation.IRunnableWithProgress;\r
31 import org.eclipse.swt.widgets.Shell;\r
32 import org.eclipse.ui.IWorkbenchPart;\r
33 import org.eclipse.ui.PlatformUI;\r
34 import org.eclipse.ui.commands.ICommandService;\r
35 import org.simantics.Simantics;\r
36 import org.simantics.db.ChangeSetIdentifier;\r
37 import org.simantics.db.Operation;\r
38 import org.simantics.db.Session;\r
39 import org.simantics.db.common.CommentMetadata;\r
40 import org.simantics.db.exception.DatabaseException;\r
41 import org.simantics.db.exception.NoHistoryException;\r
42 import org.simantics.db.service.ManagementSupport;\r
43 import org.simantics.db.service.UndoRedoSupport;\r
44 import org.simantics.ui.states.TrackedTextState;\r
45 import org.simantics.utils.ui.ErrorLogger;\r
46 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
47 \r
48 /**\r
49  * @author Kalle Kondelin\r
50  * @author Jani Simomaa\r
51  */\r
52 public class SessionUndoHandler {\r
53     \r
54     private final static boolean DEBUG = false;\r
55     private final static boolean ENABLED = true;\r
56     \r
57     @CanExecute\r
58     public boolean canExecute() {\r
59         return UndoRedoTester.canUndo();\r
60     }\r
61     \r
62     @Execute\r
63     public void execute(@Active MPart activePart, MApplication mApplication, EModelService modelService) throws ExecutionException {\r
64         if (DEBUG)\r
65             System.out.println("--\nUndo handler called.");\r
66 \r
67         if (activePart == null)\r
68             return;\r
69         final Session session = Simantics.peekSession();\r
70         if (session == null)\r
71             return;\r
72         \r
73         ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
74         Command command = service.getCommand( TrackedTextState.COMMAND_ID );\r
75         State state = command.getState( TrackedTextState.STATE_ID );\r
76         \r
77         boolean sessionUndoEnabled = true;\r
78         Object value = state.getValue();\r
79         if (value != null && value instanceof Boolean)\r
80             sessionUndoEnabled = (Boolean) value;\r
81         \r
82         if (ENABLED) {\r
83             if (sessionUndoEnabled) {\r
84                 try {\r
85                     final AtomicReference<String> msg = new AtomicReference<String>();\r
86                     IRunnableWithProgress undo = new IRunnableWithProgress() {\r
87                         @Override\r
88                         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
89                             try {\r
90                                 monitor.beginTask("Undo", IProgressMonitor.UNKNOWN);\r
91                                 msg.set( undo( session ) );\r
92                             } catch (NoHistoryException e) {\r
93                                 msg.set("Nothing to undo.");\r
94                             } catch (DatabaseException e) {\r
95                                 throw new InvocationTargetException(e);\r
96                             } finally {\r
97                                 monitor.done();\r
98                             }\r
99                         }\r
100                     };\r
101     \r
102                     // busyCursorWhile does not work because locking the session for undo\r
103                     // will also lock the UI completely.\r
104                     //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);\r
105                     Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
106                     new ProgressMonitorDialog(shell).run(true, false, undo);\r
107                     \r
108                     //\r
109                     // TODO Not the Eclipse 4 way of using IStatusLineManager!\r
110                     // See:\r
111                     // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332499\r
112                     // https://www.eclipse.org/forums/index.php?t=msg&th=367379&goto=941232&#msg_941232\r
113                     //\r
114                     IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();\r
115                     IStatusLineManager manager = WorkbenchUtils.getStatusLine(part);\r
116                     manager.setMessage(msg.get());\r
117 //                    statusManager.setMessage( msg.get() );\r
118                 } catch (InvocationTargetException e1) {\r
119                     throw new ExecutionException("Undo failed, database failure.", e1.getCause());\r
120                 } catch (InterruptedException e1) {\r
121                     throw new ExecutionException("Undo failed, interrupted.", e1);\r
122                 }\r
123             }\r
124         } else {\r
125             ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);\r
126         }\r
127         return;\r
128     }\r
129 \r
130     public static String getComment(Session session, ChangeSetIdentifier id) {\r
131         byte[] data = id.getMetadata().get(CommentMetadata.class.getName());\r
132         if(data == null)\r
133             return "Undescribed operation.";\r
134         String comment = CommentMetadata.deserialise(session, data).toString().trim();\r
135         if(comment.isEmpty())\r
136             return "Undescribed operation.";\r
137         return comment;\r
138     }\r
139     \r
140     private String undo(Session session) throws DatabaseException {\r
141         UndoRedoSupport support = session.getService(UndoRedoSupport.class);\r
142         \r
143         List<Operation> ops = support.undoAndReturnOperations(session, 1);\r
144         if(ops.isEmpty())\r
145             return "Undo history is empty.";        \r
146         \r
147         Operation mainOperation = ops.get(0);\r
148         \r
149         String msg = null;\r
150         long csId = mainOperation.getCSId();\r
151 \r
152         ManagementSupport management = session.getService(ManagementSupport.class);\r
153         for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))\r
154             if(msg == null)\r
155                 msg = "Undo reverted: " + getComment(session, id);\r
156 \r
157         if (DEBUG)\r
158             System.out.println(msg);\r
159         return msg;\r
160     }\r
161 \r
162 }\r