1 package org.simantics.ui.workbench.handler.e4;
3 import org.eclipse.swt.widgets.Display;
4 import org.eclipse.ui.PlatformUI;
5 import org.eclipse.ui.contexts.IContextActivation;
6 import org.eclipse.ui.contexts.IContextService;
7 import org.simantics.DatabaseJob;
8 import org.simantics.Simantics;
9 import org.simantics.db.Session;
10 import org.simantics.db.UndoContext;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.management.ISessionContext;
13 import org.simantics.db.service.UndoRedoSupport;
14 import org.simantics.ui.SimanticsUI;
15 import org.simantics.utils.ui.ErrorLogger;
16 import org.simantics.utils.ui.SWTUtils;
18 public class UndoRedoTester {
20 private static final boolean DEBUG = false;
21 private static final String SIMANTICS_UNDO_CONTEXT = "org.simantics.ui.undoContext";
22 public static final String UNDO_ENABLED = "org.simantics.undo.enabled";
24 private static UndoRedoSupport undoSupport = null;
25 private static UndoChangeListener changeListener = new UndoChangeListener();
26 private static IContextActivation activation = null;
28 private static class UndoChangeListener implements UndoRedoSupport.ChangeListener {
30 private int oldUndo = 0;
31 private int oldRedo = 0;
34 public void onChanged() {
36 System.out.println("UndoPropertyTester: on change.");
37 Display display = PlatformUI.getWorkbench().getDisplay();
38 SWTUtils.asyncExec(display, new Runnable() {
46 private void handleChange() {
47 int newUndo = oldUndo;
48 int newRedo = oldRedo;
50 ISessionContext ctx = Simantics.getSessionContext();
52 System.out.println("UndoPropertyTester: handle change, ctx=" + ctx);
55 Session session = ctx.peekSession();
57 System.out.println("UndoPropertyTester: handle change, session=" + session);
60 UndoContext uc = undoSupport.getUndoContext(session);
63 newUndo = uc.getAll().size();
64 newRedo = uc.getRedoList().size();
66 System.out.println("on undo change: " + oldUndo + "->" + newUndo);
67 System.out.println("on redo change: " + oldRedo + "->" + newRedo);
69 boolean undoOn = oldUndo == 0 && newUndo == 1;
70 boolean undoOff = oldUndo > 0 && newUndo == 0;
71 boolean redoOn = oldRedo == 0 && newRedo == 1;
72 boolean redoOff = oldRedo > 0 && newRedo == 0;
73 if (undoOn || undoOff || redoOn || redoOff)
75 } catch (DatabaseException e) {
76 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
81 private void toggleContext() {
82 IContextService contextService = (IContextService)PlatformUI.getWorkbench().getService(IContextService.class);
83 if (null != activation) {
85 System.out.println("UndoPropertyTester: deactivate.");
87 contextService.deactivateContext(activation);
88 } catch (Throwable t) {
89 ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);
93 System.out.println("UndoPropertyTester: deactivated.");
96 System.out.println("UndoPropertyTester: activate.");
98 activation = contextService.activateContext(SIMANTICS_UNDO_CONTEXT);
99 } catch (Throwable t) {
100 ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);
104 System.out.println("UndoPropertyTester: activated=" + activation);
110 private UndoRedoTester() {
113 public static boolean canUndo() {
114 String undoEnabled = System.getProperty(UNDO_ENABLED);
115 if(undoEnabled != null && "false".equals(undoEnabled))
118 ISessionContext ctx = Simantics.getSessionContext();
121 System.out.println("UndoPropertyTester: no can do undo.");
125 Session s = ctx.peekSession();
128 System.out.println("UndoPropertyTester: session is null, no can do undo.");
131 if (DatabaseJob.inProgress())
133 if (null == undoSupport) {
134 undoSupport = s.getService(UndoRedoSupport.class);
135 undoSupport.subscribe(changeListener);
137 UndoContext uc = undoSupport.getUndoContext(s);
140 boolean ret = !uc.getAll().isEmpty();
142 System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do undo.");
144 } catch (Exception e) {
145 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
147 System.out.println("UndoPropertyTester: no can do undo");
152 public static boolean canRedo() {
153 ISessionContext ctx = Simantics.getSessionContext();
156 System.out.println("UndoPropertyTester: no can do redo.");
160 Session s = ctx.peekSession();
163 System.out.println("UndoPropertyTester: session is null, no can do redo.");
166 if (DatabaseJob.inProgress())
168 if (null == undoSupport) {
169 undoSupport = s.getService(UndoRedoSupport.class);
170 undoSupport.subscribe(changeListener);
172 UndoContext uc = undoSupport.getUndoContext(s);
175 boolean ret = !uc.getRedoList().isEmpty();
177 System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do redo.");
179 } catch (Exception e) {
180 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
182 System.out.println("UndoPropertyTester: no can do redo.");