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.e4;
\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.Command;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.core.commands.State;
\r
20 import org.eclipse.core.runtime.IProgressMonitor;
\r
21 import org.eclipse.core.runtime.IStatus;
\r
22 import org.eclipse.e4.core.contexts.Active;
\r
23 import org.eclipse.e4.core.di.annotations.CanExecute;
\r
24 import org.eclipse.e4.core.di.annotations.Execute;
\r
25 import org.eclipse.e4.ui.model.application.MApplication;
\r
26 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
\r
27 import org.eclipse.e4.ui.workbench.modeling.EModelService;
\r
28 import org.eclipse.jface.action.IStatusLineManager;
\r
29 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
\r
30 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
31 import org.eclipse.swt.widgets.Shell;
\r
32 import org.eclipse.ui.IWorkbenchPart;
\r
33 import org.eclipse.ui.PlatformUI;
\r
34 import org.eclipse.ui.commands.ICommandService;
\r
35 import org.simantics.Simantics;
\r
36 import org.simantics.db.ChangeSetIdentifier;
\r
37 import org.simantics.db.Operation;
\r
38 import org.simantics.db.Session;
\r
39 import org.simantics.db.common.CommentMetadata;
\r
40 import org.simantics.db.exception.DatabaseException;
\r
41 import org.simantics.db.exception.NoHistoryException;
\r
42 import org.simantics.db.service.ManagementSupport;
\r
43 import org.simantics.db.service.UndoRedoSupport;
\r
44 import org.simantics.ui.states.TrackedTextState;
\r
45 import org.simantics.utils.ui.ErrorLogger;
\r
46 import org.simantics.utils.ui.workbench.WorkbenchUtils;
\r
49 * @author Kalle Kondelin
\r
50 * @author Jani Simomaa
\r
52 public class SessionUndoHandler {
\r
54 private final static boolean DEBUG = false;
\r
55 private final static boolean ENABLED = true;
\r
58 public boolean canExecute() {
\r
59 return UndoRedoTester.canUndo();
\r
63 public void execute(@Active MPart activePart, MApplication mApplication, EModelService modelService) throws ExecutionException {
\r
65 System.out.println("--\nUndo handler called.");
\r
67 if (activePart == null)
\r
69 final Session session = Simantics.peekSession();
\r
70 if (session == null)
\r
73 ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
\r
74 Command command = service.getCommand( TrackedTextState.COMMAND_ID );
\r
75 State state = command.getState( TrackedTextState.STATE_ID );
\r
77 boolean sessionUndoEnabled = true;
\r
78 Object value = state.getValue();
\r
79 if (value != null && value instanceof Boolean)
\r
80 sessionUndoEnabled = (Boolean) value;
\r
83 if (sessionUndoEnabled) {
\r
85 final AtomicReference<String> msg = new AtomicReference<String>();
\r
86 IRunnableWithProgress undo = new IRunnableWithProgress() {
\r
88 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
90 monitor.beginTask("Undo", IProgressMonitor.UNKNOWN);
\r
91 msg.set( undo( session ) );
\r
92 } catch (NoHistoryException e) {
\r
93 msg.set("Nothing to undo.");
\r
94 } catch (DatabaseException e) {
\r
95 throw new InvocationTargetException(e);
\r
102 // busyCursorWhile does not work because locking the session for undo
\r
103 // will also lock the UI completely.
\r
104 //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
\r
105 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
106 new ProgressMonitorDialog(shell).run(true, false, undo);
\r
109 // TODO Not the Eclipse 4 way of using IStatusLineManager!
\r
111 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332499
\r
112 // https://www.eclipse.org/forums/index.php?t=msg&th=367379&goto=941232&#msg_941232
\r
114 IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();
\r
115 IStatusLineManager manager = WorkbenchUtils.getStatusLine(part);
\r
116 manager.setMessage(msg.get());
\r
117 // statusManager.setMessage( msg.get() );
\r
118 } catch (InvocationTargetException e1) {
\r
119 throw new ExecutionException("Undo failed, database failure.", e1.getCause());
\r
120 } catch (InterruptedException e1) {
\r
121 throw new ExecutionException("Undo failed, interrupted.", e1);
\r
125 ErrorLogger.getDefault().log(IStatus.INFO, 0, "The undo command is disabled", null);
\r
130 public static String getComment(Session session, ChangeSetIdentifier id) {
\r
131 byte[] data = id.getMetadata().get(CommentMetadata.class.getName());
\r
133 return "Undescribed operation.";
\r
134 String comment = CommentMetadata.deserialise(session, data).toString().trim();
\r
135 if(comment.isEmpty())
\r
136 return "Undescribed operation.";
\r
140 private String undo(Session session) throws DatabaseException {
\r
141 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
\r
143 List<Operation> ops = support.undoAndReturnOperations(session, 1);
\r
145 return "Undo history is empty.";
\r
147 Operation mainOperation = ops.get(0);
\r
150 long csId = mainOperation.getCSId();
\r
152 ManagementSupport management = session.getService(ManagementSupport.class);
\r
153 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
\r
155 msg = "Undo reverted: " + getComment(session, id);
\r
158 System.out.println(msg);
\r