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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.ui.workbench.handler;
\r
13 import java.lang.reflect.InvocationTargetException;
\r
14 import java.util.List;
\r
15 import java.util.concurrent.atomic.AtomicReference;
\r
17 import org.eclipse.core.commands.AbstractHandler;
\r
18 import org.eclipse.core.commands.Command;
\r
19 import org.eclipse.core.commands.ExecutionEvent;
\r
20 import org.eclipse.core.commands.ExecutionException;
\r
21 import org.eclipse.core.commands.State;
\r
22 import org.eclipse.core.runtime.IProgressMonitor;
\r
23 import org.eclipse.core.runtime.IStatus;
\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.IActionBars;
\r
28 import org.eclipse.ui.IWorkbenchPart;
\r
29 import org.eclipse.ui.PlatformUI;
\r
30 import org.eclipse.ui.commands.ICommandService;
\r
31 import org.eclipse.ui.handlers.HandlerUtil;
\r
32 import org.simantics.Simantics;
\r
33 import org.simantics.db.ChangeSetIdentifier;
\r
34 import org.simantics.db.Operation;
\r
35 import org.simantics.db.Session;
\r
36 import org.simantics.db.common.CommentMetadata;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.exception.NoHistoryException;
\r
39 import org.simantics.db.service.ManagementSupport;
\r
40 import org.simantics.db.service.UndoRedoSupport;
\r
41 import org.simantics.ui.states.TrackedTextState;
\r
42 import org.simantics.utils.ui.ErrorLogger;
\r
43 import org.simantics.utils.ui.workbench.WorkbenchUtils;
\r
46 * @author Kalle Kondelin
\r
48 public class SessionUndoHandler extends AbstractHandler {
\r
50 private final boolean DEBUG = false;
\r
51 private final boolean ENABLED = true;
\r
54 public Object execute(ExecutionEvent e) throws ExecutionException {
\r
56 System.out.println("--\nUndo handler called.");
\r
58 IWorkbenchPart part = HandlerUtil.getActivePart(e);
\r
61 IActionBars actionBars = WorkbenchUtils.getActionBars(part);
\r
62 final Session session = Simantics.peekSession();
\r
63 if (session == null)
\r
66 ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
\r
67 Command command = service.getCommand( TrackedTextState.COMMAND_ID );
\r
68 State state = command.getState( TrackedTextState.STATE_ID );
\r
70 boolean sessionUndoEnabled = true;
\r
71 Object value = state.getValue();
\r
72 if (value != null && value instanceof Boolean)
\r
73 sessionUndoEnabled = (Boolean) value;
\r
76 if (sessionUndoEnabled) {
\r
78 final AtomicReference<String> msg = new AtomicReference<String>();
\r
79 IRunnableWithProgress undo = new IRunnableWithProgress() {
\r
81 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
83 monitor.beginTask("Undo", IProgressMonitor.UNKNOWN);
\r
84 msg.set( undo( session ) );
\r
85 } catch (NoHistoryException e) {
\r
86 msg.set("Nothing to undo.");
\r
87 } catch (DatabaseException e) {
\r
88 throw new InvocationTargetException(e);
\r
95 // busyCursorWhile does not work because locking the session for undo
\r
96 // will also lock the UI completely.
\r
97 //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
\r
98 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
99 new ProgressMonitorDialog(shell).run(true, false, undo);
\r
100 actionBars.getStatusLineManager().setMessage( msg.get() );
\r
101 } catch (InvocationTargetException e1) {
\r
102 throw new ExecutionException("Undo failed, database failure.", e1.getCause());
\r
103 } catch (InterruptedException e1) {
\r
104 throw new ExecutionException("Undo failed, interrupted.", e1);
\r
108 ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);
\r
113 public static String getComment(Session session, ChangeSetIdentifier id) {
\r
114 byte[] data = id.getMetadata().get(CommentMetadata.class.getName());
\r
116 return "Undescribed operation.";
\r
117 String comment = CommentMetadata.deserialise(session, data).toString().trim();
\r
118 if(comment.isEmpty())
\r
119 return "Undescribed operation.";
\r
123 private String undo(Session session) throws DatabaseException {
\r
124 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
\r
126 List<Operation> ops = support.undoAndReturnOperations(session, 1);
\r
128 return "Undo history is empty.";
\r
130 Operation mainOperation = ops.get(0);
\r
133 long csId = mainOperation.getCSId();
\r
135 ManagementSupport management = session.getService(ManagementSupport.class);
\r
136 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
\r
138 msg = "Undo reverted: " + getComment(session, id);
\r
141 System.out.println(msg);
\r