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