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