]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/SessionRedoHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / e4 / 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.e4;
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.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;
38
39 /**
40  * @author Kalle Kondelin
41  */
42 public class SessionRedoHandler {
43
44     private final boolean DEBUG = false;
45
46     @CanExecute
47     public boolean canExecute() {
48         return UndoRedoTester.canRedo();
49     }
50     
51     @Execute
52     public void execute(@Active MPart activePart) throws ExecutionException {
53         if (DEBUG)
54             System.out.println("--\nRedo handler called.");
55
56         if (activePart == null)
57             return;
58         final Session session = Simantics.peekSession();
59         if (session == null)
60             return;
61
62         try {
63             final AtomicReference<String> msg = new AtomicReference<String>();
64             IRunnableWithProgress redo = new IRunnableWithProgress() {
65                 @Override
66                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
67                     try {
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);
74                     } finally {
75                         monitor.done();
76                     }
77                 }
78             };
79
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);
85             
86             //
87             // TODO Not the Eclipse 4 way of using IStatusLineManager!
88             // See:
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
91             //
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);
100         }
101         return;
102     }
103
104     private String redo(Session session) throws DatabaseException {
105         UndoRedoSupport support = session.getService(UndoRedoSupport.class);
106         List<Operation> ops = support.redo(session, 1);
107         if(ops.isEmpty())
108             return "Nothing to redo.";
109         
110         Operation mainOperation = ops.get(0);
111         
112         String msg = null;
113         long csId = mainOperation.getCSId();
114
115         ManagementSupport management = session.getService(ManagementSupport.class);
116         for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
117             if(msg == null)
118                 msg = "Redid: " + SessionUndoHandler.getComment(session, id);
119
120         if (DEBUG)
121             System.out.println(msg);
122         return msg;
123     }
124
125 }