]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/SessionRedoHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / SessionRedoHandler.java
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.ui.workbench.handler;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.List;
15 import java.util.concurrent.atomic.AtomicReference;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.simantics.Simantics;
29 import org.simantics.db.ChangeSetIdentifier;
30 import org.simantics.db.Operation;
31 import org.simantics.db.Session;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.exception.NoHistoryException;
34 import org.simantics.db.service.ManagementSupport;
35 import org.simantics.db.service.UndoRedoSupport;
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;
37
38 /**
39  * @author Kalle Kondelin
40  */
41 public class SessionRedoHandler extends AbstractHandler {
42
43     private final boolean DEBUG = false;
44
45     @Override
46     public Object execute(ExecutionEvent e) throws ExecutionException {
47         if (DEBUG)
48             System.out.println("--\nRedo handler called.");
49
50         IWorkbenchPart part = HandlerUtil.getActivePart(e);
51         if (part == null)
52             return null;
53         IActionBars actionBars = WorkbenchUtils.getActionBars(part);
54         final Session session = Simantics.peekSession();
55         if (session == null)
56             return null;
57
58         try {
59             final AtomicReference<String> msg = new AtomicReference<String>();
60             IRunnableWithProgress redo = new IRunnableWithProgress() {
61                 @Override
62                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
63                     try {
64                         monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);
65                         msg.set( redo( session ) );
66                     } catch (NoHistoryException e) {
67                         msg.set("Nothing to redo.");
68                     } catch (DatabaseException e) {
69                         throw new InvocationTargetException(e);
70                     } finally {
71                         monitor.done();
72                     }
73                 }
74             };
75
76             // busyCursorWhile does not work because locking the session for undo
77             // will also lock the UI completely.
78             //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
79             Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
80             new ProgressMonitorDialog(shell).run(true, false, redo);
81             actionBars.getStatusLineManager().setMessage( msg.get() );
82         } catch (InvocationTargetException e1) {
83             throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());
84         } catch (InterruptedException e1) {
85             throw new ExecutionException("Redo failed, interrupted.", e1);
86         }
87         return null;
88     }
89
90     private String redo(Session session) throws DatabaseException {
91         UndoRedoSupport support = session.getService(UndoRedoSupport.class);
92         List<Operation> ops = support.redo(session, 1);
93         if(ops.isEmpty())
94             return "Nothing to redo.";
95         
96         Operation mainOperation = ops.get(0);
97         
98         String msg = null;
99         long csId = mainOperation.getCSId();
100
101         ManagementSupport management = session.getService(ManagementSupport.class);
102         for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
103             if(msg == null)
104                 msg = "Redid: " + SessionUndoHandler.getComment(session, id);
105
106         if (DEBUG)
107             System.out.println(msg);
108         return msg;
109     }
110
111 }