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