]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/handler/e4/UndoRedoTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / handler / e4 / UndoRedoTester.java
1 package org.simantics.ui.workbench.handler.e4;\r
2 \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
17 \r
18 public class UndoRedoTester {\r
19 \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
23     \r
24     private static UndoRedoSupport undoSupport = null;\r
25     private static UndoChangeListener changeListener = new UndoChangeListener();\r
26     private static IContextActivation activation = null;\r
27     \r
28     private static class UndoChangeListener implements UndoRedoSupport.ChangeListener {\r
29 \r
30         private int oldUndo = 0;\r
31         private int oldRedo = 0;\r
32         \r
33         @Override\r
34         public void onChanged() {\r
35             if (DEBUG)\r
36                 System.out.println("UndoPropertyTester: on change.");\r
37             Display display = PlatformUI.getWorkbench().getDisplay();\r
38             SWTUtils.asyncExec(display, new Runnable() {\r
39                 @Override\r
40                 public void run() {\r
41                     handleChange();\r
42                 }\r
43             });\r
44         }\r
45         \r
46         private void handleChange() {\r
47             int newUndo = oldUndo;\r
48             int newRedo = oldRedo;\r
49             try {\r
50                 ISessionContext ctx = SimanticsUI.getSessionContext();\r
51                 if (DEBUG)\r
52                     System.out.println("UndoPropertyTester: handle change, ctx=" + ctx);\r
53                 if (ctx == null)\r
54                     return;\r
55                 Session session = ctx.peekSession();\r
56                 if (DEBUG)\r
57                     System.out.println("UndoPropertyTester: handle change, session=" + session);\r
58                 if (session == null)\r
59                     return;\r
60                 UndoContext uc = undoSupport.getUndoContext(session);\r
61                 if (uc == null)\r
62                     return;\r
63                 newUndo = uc.getAll().size();\r
64                 newRedo = uc.getRedoList().size();\r
65                 if (DEBUG) {\r
66                     System.out.println("on undo change: " + oldUndo + "->" + newUndo);\r
67                     System.out.println("on redo change: " + oldRedo + "->" + newRedo);\r
68                 }\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
74                     toggleContext();\r
75             } catch (DatabaseException e) {\r
76                 ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);\r
77             }\r
78             oldUndo = newUndo;\r
79             oldRedo = newRedo;\r
80         }\r
81         private void toggleContext() {\r
82             IContextService contextService = (IContextService)PlatformUI.getWorkbench().getService(IContextService.class);\r
83             if (null != activation) {\r
84                 if (DEBUG)\r
85                     System.out.println("UndoPropertyTester: deactivate.");\r
86                 try {\r
87                     contextService.deactivateContext(activation);\r
88                 } catch (Throwable t) {\r
89                     ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);\r
90                 }\r
91                 activation = null;\r
92                 if (DEBUG)\r
93                     System.out.println("UndoPropertyTester: deactivated.");\r
94             } else {\r
95                 if (DEBUG)\r
96                     System.out.println("UndoPropertyTester: activate.");\r
97                 try {\r
98                     activation = contextService.activateContext(SIMANTICS_UNDO_CONTEXT);\r
99                 } catch (Throwable t) {\r
100                     ErrorLogger.getDefault().logError("Undo/Redo support failed.", t);\r
101                     activation = null;\r
102                 }\r
103                 if (DEBUG)\r
104                     System.out.println("UndoPropertyTester: activated=" + activation);\r
105             }\r
106         }\r
107         \r
108     }\r
109     \r
110     private UndoRedoTester() {\r
111     }\r
112     \r
113     public static boolean canUndo() {\r
114         String undoEnabled = System.getProperty(UNDO_ENABLED);\r
115         if(undoEnabled != null && "false".equals(undoEnabled))\r
116             return false;\r
117         \r
118         ISessionContext ctx = Simantics.getSessionContext();\r
119         if (ctx == null) {\r
120             if (DEBUG)\r
121                 System.out.println("UndoPropertyTester: no can do undo.");\r
122             return false;\r
123         }\r
124         try {\r
125             Session s = ctx.peekSession();\r
126             if (null == s) {\r
127                 if (DEBUG)\r
128                     System.out.println("UndoPropertyTester: session is null, no can do undo.");\r
129                 return false;\r
130             }\r
131             if (DatabaseJob.inProgress())\r
132                 return true;\r
133             if (null == undoSupport) {\r
134                 undoSupport = s.getService(UndoRedoSupport.class);\r
135                 undoSupport.subscribe(changeListener);\r
136             }\r
137             UndoContext uc = undoSupport.getUndoContext(s);\r
138             if (uc == null)\r
139                 return false;\r
140             boolean ret = !uc.getAll().isEmpty();\r
141             if (DEBUG)\r
142                 System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do undo.");\r
143             return ret;\r
144         } catch (Exception e) {\r
145             ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);\r
146             if (DEBUG)\r
147                 System.out.println("UndoPropertyTester: no can do undo");\r
148             return false;\r
149         }\r
150     }\r
151     \r
152     public static boolean canRedo() {\r
153         ISessionContext ctx = Simantics.getSessionContext();\r
154         if (ctx == null) {\r
155             if (DEBUG)\r
156                 System.out.println("UndoPropertyTester: no can do redo.");\r
157             return false;\r
158         }\r
159         try {\r
160             Session s = ctx.peekSession();\r
161             if (null == s) {\r
162                 if (DEBUG)\r
163                     System.out.println("UndoPropertyTester: session is null, no can do redo.");\r
164                 return false;\r
165             }\r
166             if (DatabaseJob.inProgress())\r
167                 return true;\r
168             if (null == undoSupport) {\r
169                 undoSupport = s.getService(UndoRedoSupport.class);\r
170                 undoSupport.subscribe(changeListener);\r
171             }\r
172             UndoContext uc = undoSupport.getUndoContext(s);\r
173             if (uc == null)\r
174                 return false;\r
175             boolean ret = !uc.getRedoList().isEmpty();\r
176             if (DEBUG)\r
177                 System.out.println("UndoPropertyTester: " + (ret ? "" : "no ")+ "can do redo.");\r
178             return ret;\r
179         } catch (Exception e) {\r
180             ErrorLogger.getDefault().logError("Undo/Redo support failed.", e);\r
181             if (DEBUG)\r
182                 System.out.println("UndoPropertyTester: no can do redo.");\r
183             return false;\r
184         }\r
185     }\r
186 \r
187 }\r