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