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.e4;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.List;
15 import java.util.concurrent.atomic.AtomicReference;
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;
49 * @author Kalle Kondelin
50 * @author Jani Simomaa
52 public class SessionUndoHandler {
54 private final static boolean DEBUG = false;
55 private final static boolean ENABLED = true;
58 public boolean canExecute() {
59 return UndoRedoTester.canUndo();
63 public void execute(@Active MPart activePart, MApplication mApplication, EModelService modelService) throws ExecutionException {
65 System.out.println("--\nUndo handler called.");
67 if (activePart == null)
69 final Session session = Simantics.peekSession();
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 );
77 boolean sessionUndoEnabled = true;
78 Object value = state.getValue();
79 if (value != null && value instanceof Boolean)
80 sessionUndoEnabled = (Boolean) value;
83 if (sessionUndoEnabled) {
85 final AtomicReference<String> msg = new AtomicReference<String>();
86 IRunnableWithProgress undo = new IRunnableWithProgress() {
88 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
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);
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);
109 // TODO Not the Eclipse 4 way of using IStatusLineManager!
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
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);
125 ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);
130 public static String getComment(Session session, ChangeSetIdentifier id) {
131 byte[] data = id.getMetadata().get(CommentMetadata.class.getName());
133 return "Undescribed operation.";
134 String comment = CommentMetadata.deserialise(session, data).toString().trim();
135 if(comment.isEmpty())
136 return "Undescribed operation.";
140 private String undo(Session session) throws DatabaseException {
141 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
143 List<Operation> ops = support.undoAndReturnOperations(session, 1);
145 return "Undo history is empty.";
147 Operation mainOperation = ops.get(0);
150 long csId = mainOperation.getCSId();
152 ManagementSupport management = session.getService(ManagementSupport.class);
153 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
155 msg = "Undo reverted: " + getComment(session, id);
158 System.out.println(msg);