1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.tester;
14 import org.eclipse.core.expressions.PropertyTester;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.ui.PlatformUI;
17 import org.eclipse.ui.contexts.IContextActivation;
18 import org.eclipse.ui.contexts.IContextService;
19 import org.simantics.DatabaseJob;
20 import org.simantics.Simantics;
21 import org.simantics.db.Session;
22 import org.simantics.db.UndoContext;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.management.ISessionContext;
25 import org.simantics.db.service.UndoRedoSupport;
26 import org.simantics.ui.SimanticsUI;
27 import org.simantics.utils.ui.ErrorLogger;
28 import org.simantics.utils.ui.SWTUtils;
31 * An eclipse property tester for org.simantics.db database undo/redoability.
33 * @author Kalle Kondelin
35 public class UndoPropertyTester extends PropertyTester implements UndoRedoSupport.ChangeListener {
37 private static final String PROP_CAN_REDO = "canRedo";
39 private static final String PROP_CAN_UNDO = "canUndo";
41 private static final String SIMANTICS_UNDO_CONTEXT = "org.simantics.ui.undoContext";
43 public static final boolean DEBUG = false;
45 public static final String UNDO_ENABLED = "org.simantics.undo.enabled";
47 UndoRedoSupport undoSupport = null;
48 IContextActivation activation = null;
49 public UndoPropertyTester() {
51 System.out.println("UndoPropertyTester: " + this);
54 public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
56 System.out.println("UndoPropertyTester: receiver=" + receiver);
58 String undoEnabled = System.getProperty(UNDO_ENABLED);
59 if(undoEnabled != null && "false".equals(undoEnabled))
62 if (property.matches(PROP_CAN_UNDO))
64 else if (property.matches(PROP_CAN_REDO))
69 private boolean canUndo() {
70 ISessionContext ctx = Simantics.getSessionContext();
73 System.out.println("UndoPropertyTester: no can do undo.");
77 Session s = ctx.peekSession();
80 System.out.println("UndoPropertyTester: session is null, no can do undo.");
83 if (DatabaseJob.inProgress())
85 if (null == undoSupport) {
86 undoSupport = s.getService(UndoRedoSupport.class);
87 undoSupport.subscribe(this);
89 UndoContext uc = undoSupport.getUndoContext(s);
92 boolean ret = !uc.getAll().isEmpty();
94 System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do undo.");
96 } catch (Exception e) {
97 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
99 System.out.println("UndoPropertyTester: no can do undo");
103 private boolean canRedo() {
104 ISessionContext ctx = Simantics.getSessionContext();
107 System.out.println("UndoPropertyTester: no can do redo.");
111 Session s = ctx.peekSession();
114 System.out.println("UndoPropertyTester: session is null, no can do redo.");
117 if (DatabaseJob.inProgress())
119 if (null == undoSupport) {
120 undoSupport = s.getService(UndoRedoSupport.class);
121 undoSupport.subscribe(this);
123 UndoContext uc = undoSupport.getUndoContext(s);
126 boolean ret = !uc.getRedoList().isEmpty();
128 System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do redo.");
130 } catch (Exception e) {
131 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
133 System.out.println("UndoPropertyTester: no can do redo.");
138 public void onChanged() {
140 System.out.println("UndoPropertyTester: on change.");
141 Display display = PlatformUI.getWorkbench().getDisplay();
142 SWTUtils.asyncExec(display, new Runnable() {
149 private int oldUndo = 0;
150 private int oldRedo = 0;
151 private void handleChange() {
152 int newUndo = oldUndo;
153 int newRedo = oldRedo;
155 ISessionContext ctx = SimanticsUI.getSessionContext();
157 System.out.println("UndoPropertyTester: handle change, ctx=" + ctx);
160 Session session = ctx.peekSession();
162 System.out.println("UndoPropertyTester: handle change, session=" + session);
165 UndoContext uc = undoSupport.getUndoContext(session);
168 newUndo = uc.getAll().size();
169 newRedo = uc.getRedoList().size();
171 System.out.println("on undo change: " + oldUndo + "->" + newUndo);
172 System.out.println("on redo change: " + oldRedo + "->" + newRedo);
174 boolean undoOn = oldUndo == 0 && newUndo == 1;
175 boolean undoOff = oldUndo > 0 && newUndo == 0;
176 boolean redoOn = oldRedo == 0 && newRedo == 1;
177 boolean redoOff = oldRedo > 0 && newRedo == 0;
178 if (undoOn || undoOff || redoOn || redoOff)
180 } catch (DatabaseException e) {
181 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
186 private void toggleContext() {
187 IContextService contextService = (IContextService)PlatformUI.getWorkbench().getService(IContextService.class);
188 if (null != activation) {
190 System.out.println("UndoPropertyTester: deactivate.");
192 contextService.deactivateContext(activation);
193 } catch (Throwable t) {
194 ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);
198 System.out.println("UndoPropertyTester: deactivated.");
201 System.out.println("UndoPropertyTester: activate.");
203 activation = contextService.activateContext(SIMANTICS_UNDO_CONTEXT);
204 } catch (Throwable t) {
205 ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);
209 System.out.println("UndoPropertyTester: activated=" + activation);