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