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