-/*******************************************************************************\r
- * Copyright (c) 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui.tester;\r
-\r
-import org.eclipse.core.expressions.PropertyTester;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.contexts.IContextActivation;\r
-import org.eclipse.ui.contexts.IContextService;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.UndoContext;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.db.service.UndoRedoSupport;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.SWTUtils;\r
-\r
-/**\r
- * An eclipse property tester for org.simantics.db database undo/redoability.\r
- * \r
- * @author Kalle Kondelin\r
- */\r
-public class UndoPropertyTester extends PropertyTester implements UndoRedoSupport.ChangeListener {\r
-\r
- private static final String PROP_CAN_REDO = "canRedo";\r
-\r
- private static final String PROP_CAN_UNDO = "canUndo";\r
-\r
- private static final String SIMANTICS_UNDO_CONTEXT = "org.simantics.ui.undoContext";\r
-\r
- public static final boolean DEBUG = false;\r
-\r
- public static final String UNDO_ENABLED = "org.simantics.undo.enabled";\r
- \r
- UndoRedoSupport undoSupport = null;\r
- IContextActivation activation = null;\r
- public UndoPropertyTester() {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: " + this);\r
- }\r
- @Override\r
- public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: receiver=" + receiver);\r
- \r
- String undoEnabled = System.getProperty(UNDO_ENABLED);\r
- if(undoEnabled != null && "false".equals(undoEnabled))\r
- return false;\r
- \r
- if (property.matches(PROP_CAN_UNDO))\r
- return canUndo();\r
- else if (property.matches(PROP_CAN_REDO))\r
- return canRedo();\r
- else\r
- return false;\r
- }\r
- private boolean canUndo() {\r
- ISessionContext ctx = Simantics.getSessionContext();\r
- if (ctx == null) {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: no can do undo.");\r
- return false;\r
- }\r
- try {\r
- Session s = ctx.peekSession();\r
- if (null == s) {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: session is null, no can do undo.");\r
- return false;\r
- }\r
- if (DatabaseJob.inProgress())\r
- return true;\r
- if (null == undoSupport) {\r
- undoSupport = s.getService(UndoRedoSupport.class);\r
- undoSupport.subscribe(this);\r
- }\r
- UndoContext uc = undoSupport.getUndoContext(s);\r
- if (uc == null)\r
- return false;\r
- boolean ret = !uc.getAll().isEmpty();\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do undo.");\r
- return ret;\r
- } catch (Exception e) {\r
- ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: no can do undo");\r
- return false;\r
- }\r
- }\r
- private boolean canRedo() {\r
- ISessionContext ctx = Simantics.getSessionContext();\r
- if (ctx == null) {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: no can do redo.");\r
- return false;\r
- }\r
- try {\r
- Session s = ctx.peekSession();\r
- if (null == s) {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: session is null, no can do redo.");\r
- return false;\r
- }\r
- if (DatabaseJob.inProgress())\r
- return true;\r
- if (null == undoSupport) {\r
- undoSupport = s.getService(UndoRedoSupport.class);\r
- undoSupport.subscribe(this);\r
- }\r
- UndoContext uc = undoSupport.getUndoContext(s);\r
- if (uc == null)\r
- return false;\r
- boolean ret = !uc.getRedoList().isEmpty();\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do redo.");\r
- return ret;\r
- } catch (Exception e) {\r
- ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: no can do redo.");\r
- return false;\r
- }\r
- }\r
- @Override\r
- public void onChanged() {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: on change.");\r
- Display display = PlatformUI.getWorkbench().getDisplay();\r
- SWTUtils.asyncExec(display, new Runnable() {\r
- @Override\r
- public void run() {\r
- handleChange();\r
- }\r
- });\r
- }\r
- private int oldUndo = 0;\r
- private int oldRedo = 0;\r
- private void handleChange() {\r
- int newUndo = oldUndo;\r
- int newRedo = oldRedo;\r
- try {\r
- ISessionContext ctx = SimanticsUI.getSessionContext();\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: handle change, ctx=" + ctx);\r
- if (ctx == null)\r
- return;\r
- Session session = ctx.peekSession();\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: handle change, session=" + session);\r
- if (session == null)\r
- return;\r
- UndoContext uc = undoSupport.getUndoContext(session);\r
- if (uc == null)\r
- return;\r
- newUndo = uc.getAll().size();\r
- newRedo = uc.getRedoList().size();\r
- if (DEBUG) {\r
- System.out.println("on undo change: " + oldUndo + "->" + newUndo);\r
- System.out.println("on redo change: " + oldRedo + "->" + newRedo);\r
- }\r
- boolean undoOn = oldUndo == 0 && newUndo == 1;\r
- boolean undoOff = oldUndo > 0 && newUndo == 0;\r
- boolean redoOn = oldRedo == 0 && newRedo == 1;\r
- boolean redoOff = oldRedo > 0 && newRedo == 0;\r
- if (undoOn || undoOff || redoOn || redoOff)\r
- toggleContext();\r
- } catch (DatabaseException e) {\r
- ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);\r
- }\r
- oldUndo = newUndo;\r
- oldRedo = newRedo;\r
- }\r
- private void toggleContext() {\r
- IContextService contextService = (IContextService)PlatformUI.getWorkbench().getService(IContextService.class);\r
- if (null != activation) {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: deactivate.");\r
- try {\r
- contextService.deactivateContext(activation);\r
- } catch (Throwable t) {\r
- ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);\r
- }\r
- activation = null;\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: deactivated.");\r
- } else {\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: activate.");\r
- try {\r
- activation = contextService.activateContext(SIMANTICS_UNDO_CONTEXT);\r
- } catch (Throwable t) {\r
- ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);\r
- activation = null;\r
- }\r
- if (DEBUG)\r
- System.out.println("UndoPropertyTester: activated=" + activation);\r
- }\r
- }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.ui.tester;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.contexts.IContextActivation;
+import org.eclipse.ui.contexts.IContextService;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.Session;
+import org.simantics.db.UndoContext;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.db.service.UndoRedoSupport;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.SWTUtils;
+
+/**
+ * An eclipse property tester for org.simantics.db database undo/redoability.
+ *
+ * @author Kalle Kondelin
+ */
+public class UndoPropertyTester extends PropertyTester implements UndoRedoSupport.ChangeListener {
+
+ private static final String PROP_CAN_REDO = "canRedo";
+
+ private static final String PROP_CAN_UNDO = "canUndo";
+
+ private static final String SIMANTICS_UNDO_CONTEXT = "org.simantics.ui.undoContext";
+
+ public static final boolean DEBUG = false;
+
+ public static final String UNDO_ENABLED = "org.simantics.undo.enabled";
+
+ UndoRedoSupport undoSupport = null;
+ IContextActivation activation = null;
+ public UndoPropertyTester() {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: " + this);
+ }
+ @Override
+ public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: receiver=" + receiver);
+
+ String undoEnabled = System.getProperty(UNDO_ENABLED);
+ if(undoEnabled != null && "false".equals(undoEnabled))
+ return false;
+
+ if (property.matches(PROP_CAN_UNDO))
+ return canUndo();
+ else if (property.matches(PROP_CAN_REDO))
+ return canRedo();
+ else
+ return false;
+ }
+ private boolean canUndo() {
+ ISessionContext ctx = Simantics.getSessionContext();
+ if (ctx == null) {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: no can do undo.");
+ return false;
+ }
+ try {
+ Session s = ctx.peekSession();
+ if (null == s) {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: session is null, no can do undo.");
+ return false;
+ }
+ if (DatabaseJob.inProgress())
+ return true;
+ if (null == undoSupport) {
+ undoSupport = s.getService(UndoRedoSupport.class);
+ undoSupport.subscribe(this);
+ }
+ UndoContext uc = undoSupport.getUndoContext(s);
+ if (uc == null)
+ return false;
+ boolean ret = !uc.getAll().isEmpty();
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do undo.");
+ return ret;
+ } catch (Exception e) {
+ ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: no can do undo");
+ return false;
+ }
+ }
+ private boolean canRedo() {
+ ISessionContext ctx = Simantics.getSessionContext();
+ if (ctx == null) {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: no can do redo.");
+ return false;
+ }
+ try {
+ Session s = ctx.peekSession();
+ if (null == s) {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: session is null, no can do redo.");
+ return false;
+ }
+ if (DatabaseJob.inProgress())
+ return true;
+ if (null == undoSupport) {
+ undoSupport = s.getService(UndoRedoSupport.class);
+ undoSupport.subscribe(this);
+ }
+ UndoContext uc = undoSupport.getUndoContext(s);
+ if (uc == null)
+ return false;
+ boolean ret = !uc.getRedoList().isEmpty();
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do redo.");
+ return ret;
+ } catch (Exception e) {
+ ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: no can do redo.");
+ return false;
+ }
+ }
+ @Override
+ public void onChanged() {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: on change.");
+ Display display = PlatformUI.getWorkbench().getDisplay();
+ SWTUtils.asyncExec(display, new Runnable() {
+ @Override
+ public void run() {
+ handleChange();
+ }
+ });
+ }
+ private int oldUndo = 0;
+ private int oldRedo = 0;
+ private void handleChange() {
+ int newUndo = oldUndo;
+ int newRedo = oldRedo;
+ try {
+ ISessionContext ctx = SimanticsUI.getSessionContext();
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: handle change, ctx=" + ctx);
+ if (ctx == null)
+ return;
+ Session session = ctx.peekSession();
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: handle change, session=" + session);
+ if (session == null)
+ return;
+ UndoContext uc = undoSupport.getUndoContext(session);
+ if (uc == null)
+ return;
+ newUndo = uc.getAll().size();
+ newRedo = uc.getRedoList().size();
+ if (DEBUG) {
+ System.out.println("on undo change: " + oldUndo + "->" + newUndo);
+ System.out.println("on redo change: " + oldRedo + "->" + newRedo);
+ }
+ boolean undoOn = oldUndo == 0 && newUndo == 1;
+ boolean undoOff = oldUndo > 0 && newUndo == 0;
+ boolean redoOn = oldRedo == 0 && newRedo == 1;
+ boolean redoOff = oldRedo > 0 && newRedo == 0;
+ if (undoOn || undoOff || redoOn || redoOff)
+ toggleContext();
+ } catch (DatabaseException e) {
+ ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);
+ }
+ oldUndo = newUndo;
+ oldRedo = newRedo;
+ }
+ private void toggleContext() {
+ IContextService contextService = (IContextService)PlatformUI.getWorkbench().getService(IContextService.class);
+ if (null != activation) {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: deactivate.");
+ try {
+ contextService.deactivateContext(activation);
+ } catch (Throwable t) {
+ ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);
+ }
+ activation = null;
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: deactivated.");
+ } else {
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: activate.");
+ try {
+ activation = contextService.activateContext(SIMANTICS_UNDO_CONTEXT);
+ } catch (Throwable t) {
+ ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);
+ activation = null;
+ }
+ if (DEBUG)
+ System.out.println("UndoPropertyTester: activated=" + activation);
+ }
+ }
+}