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;
\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.AbstractHandler;
\r
18 import org.eclipse.core.commands.ExecutionEvent;
\r
19 import org.eclipse.core.commands.ExecutionException;
\r
20 import org.eclipse.core.runtime.IProgressMonitor;
\r
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
\r
22 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
23 import org.eclipse.swt.widgets.Shell;
\r
24 import org.eclipse.ui.IActionBars;
\r
25 import org.eclipse.ui.IWorkbenchPart;
\r
26 import org.eclipse.ui.PlatformUI;
\r
27 import org.eclipse.ui.handlers.HandlerUtil;
\r
28 import org.simantics.Simantics;
\r
29 import org.simantics.db.ChangeSetIdentifier;
\r
30 import org.simantics.db.Operation;
\r
31 import org.simantics.db.Session;
\r
32 import org.simantics.db.exception.DatabaseException;
\r
33 import org.simantics.db.exception.NoHistoryException;
\r
34 import org.simantics.db.service.ManagementSupport;
\r
35 import org.simantics.db.service.UndoRedoSupport;
\r
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;
\r
39 * @author Kalle Kondelin
\r
41 public class SessionRedoHandler extends AbstractHandler {
\r
43 private final boolean DEBUG = false;
\r
46 public Object execute(ExecutionEvent e) throws ExecutionException {
\r
48 System.out.println("--\nRedo handler called.");
\r
50 IWorkbenchPart part = HandlerUtil.getActivePart(e);
\r
53 IActionBars actionBars = WorkbenchUtils.getActionBars(part);
\r
54 final Session session = Simantics.peekSession();
\r
55 if (session == null)
\r
59 final AtomicReference<String> msg = new AtomicReference<String>();
\r
60 IRunnableWithProgress redo = new IRunnableWithProgress() {
\r
62 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
64 monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);
\r
65 msg.set( redo( session ) );
\r
66 } catch (NoHistoryException e) {
\r
67 msg.set("Nothing to redo.");
\r
68 } catch (DatabaseException e) {
\r
69 throw new InvocationTargetException(e);
\r
76 // busyCursorWhile does not work because locking the session for undo
\r
77 // will also lock the UI completely.
\r
78 //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
\r
79 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
80 new ProgressMonitorDialog(shell).run(true, false, redo);
\r
81 actionBars.getStatusLineManager().setMessage( msg.get() );
\r
82 } catch (InvocationTargetException e1) {
\r
83 throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());
\r
84 } catch (InterruptedException e1) {
\r
85 throw new ExecutionException("Redo failed, interrupted.", e1);
\r
90 private String redo(Session session) throws DatabaseException {
\r
91 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
\r
92 List<Operation> ops = support.redo(session, 1);
\r
94 return "Nothing to redo.";
\r
96 Operation mainOperation = ops.get(0);
\r
99 long csId = mainOperation.getCSId();
\r
101 ManagementSupport management = session.getService(ManagementSupport.class);
\r
102 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
\r
104 msg = "Redid: " + SessionUndoHandler.getComment(session, id);
\r
107 System.out.println(msg);
\r