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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.ui.workbench.handler;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.List;
15 import java.util.concurrent.atomic.AtomicReference;
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;
46 * @author Kalle Kondelin
48 public class SessionUndoHandler extends AbstractHandler {
50 private final boolean DEBUG = false;
51 private final boolean ENABLED = true;
54 public Object execute(ExecutionEvent e) throws ExecutionException {
56 System.out.println("--\nUndo handler called.");
58 IWorkbenchPart part = HandlerUtil.getActivePart(e);
61 IActionBars actionBars = WorkbenchUtils.getActionBars(part);
62 final Session session = Simantics.peekSession();
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 );
70 boolean sessionUndoEnabled = true;
71 Object value = state.getValue();
72 if (value != null && value instanceof Boolean)
73 sessionUndoEnabled = (Boolean) value;
76 if (sessionUndoEnabled) {
78 final AtomicReference<String> msg = new AtomicReference<String>();
79 IRunnableWithProgress undo = new IRunnableWithProgress() {
81 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
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);
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);
108 ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);
113 public static String getComment(Session session, ChangeSetIdentifier id) {
114 byte[] data = id.getMetadata().get(CommentMetadata.class.getName());
116 return "Undescribed operation.";
117 String comment = CommentMetadata.deserialise(session, data).toString().trim();
118 if(comment.isEmpty())
119 return "Undescribed operation.";
123 private String undo(Session session) throws DatabaseException {
124 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
126 List<Operation> ops = support.undoAndReturnOperations(session, 1);
128 return "Undo history is empty.";
130 Operation mainOperation = ops.get(0);
133 long csId = mainOperation.getCSId();
135 ManagementSupport management = session.getService(ManagementSupport.class);
136 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
138 msg = "Undo reverted: " + getComment(session, id);
141 System.out.println(msg);