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.ExecutionException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.e4.core.contexts.Active;
20 import org.eclipse.e4.core.di.annotations.CanExecute;
21 import org.eclipse.e4.core.di.annotations.Execute;
22 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23 import org.eclipse.jface.action.IStatusLineManager;
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.IWorkbenchPart;
28 import org.eclipse.ui.PlatformUI;
29 import org.simantics.Simantics;
30 import org.simantics.db.ChangeSetIdentifier;
31 import org.simantics.db.Operation;
32 import org.simantics.db.Session;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.exception.NoHistoryException;
35 import org.simantics.db.service.ManagementSupport;
36 import org.simantics.db.service.UndoRedoSupport;
37 import org.simantics.utils.ui.workbench.WorkbenchUtils;
40 * @author Kalle Kondelin
42 public class SessionRedoHandler {
44 private final boolean DEBUG = false;
47 public boolean canExecute() {
48 return UndoRedoTester.canRedo();
52 public void execute(@Active MPart activePart) throws ExecutionException {
54 System.out.println("--\nRedo handler called.");
56 if (activePart == null)
58 final Session session = Simantics.peekSession();
63 final AtomicReference<String> msg = new AtomicReference<String>();
64 IRunnableWithProgress redo = new IRunnableWithProgress() {
66 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
68 monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);
69 msg.set( redo( session ) );
70 } catch (NoHistoryException e) {
71 msg.set("Nothing to redo.");
72 } catch (DatabaseException e) {
73 throw new InvocationTargetException(e);
80 // busyCursorWhile does not work because locking the session for undo
81 // will also lock the UI completely.
82 //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
83 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
84 new ProgressMonitorDialog(shell).run(true, false, redo);
87 // TODO Not the Eclipse 4 way of using IStatusLineManager!
89 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=332499
90 // https://www.eclipse.org/forums/index.php?t=msg&th=367379&goto=941232&#msg_941232
92 IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();
93 IStatusLineManager manager = WorkbenchUtils.getStatusLine(part);
94 manager.setMessage(msg.get());
95 // actionBars.getStatusLineManager().setMessage( msg.get() );
96 } catch (InvocationTargetException e1) {
97 throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());
98 } catch (InterruptedException e1) {
99 throw new ExecutionException("Redo failed, interrupted.", e1);
104 private String redo(Session session) throws DatabaseException {
105 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
106 List<Operation> ops = support.redo(session, 1);
108 return "Nothing to redo.";
110 Operation mainOperation = ops.get(0);
113 long csId = mainOperation.getCSId();
115 ManagementSupport management = session.getService(ManagementSupport.class);
116 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
118 msg = "Redid: " + SessionUndoHandler.getComment(session, id);
121 System.out.println(msg);