]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.text/src/org/simantics/views/text/internal/TextViewerUndoHandler.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.views.text / src / org / simantics / views / text / internal / TextViewerUndoHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - (#7066) initial API and implementation
11  *******************************************************************************/
12 package org.simantics.views.text.internal;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.concurrent.Callable;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExecutableExtension;
23 import org.eclipse.jface.text.IUndoManager;
24 import org.eclipse.jface.text.TextViewer;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Listener;
30
31 /**
32  * Handles the undo/redo command for {@link TextViewer}s through
33  * {@link IUndoManager}.
34  * 
35  * <p>
36  * The implementation looks for an IUndoManager from the current focus control
37  * using the {@link TextViewerConstants#KEY_UNDO_MANAGER} data key. Its
38  * existence determines whether this handler {@link #isHandled()} returns
39  * <code>true</code> or <code>false</code>.
40  *
41  * <p>
42  * The handler expects to receive a single string as an argument through the
43  * extension definitions ({@link IExecutableExtension}) that determines which
44  * method is invoked from IUndoManager (<code>undo</code> or <code>redo</code>).
45  * 
46  * <p>
47  * Implementation is partially copied from
48  * <code>org.eclipse.ui.internal.handlers.WidgetMethodHandler</code>.
49  * 
50  * @since 1.28.0
51  */
52 public class TextViewerUndoHandler extends AbstractHandler implements IExecutableExtension {
53
54         /**
55          * The parameters to pass to the method this handler invokes. This handler
56          * always passes no parameters.
57          */
58         protected static final Class<?>[] NO_PARAMETERS = new Class[0];
59
60         public TextViewerUndoHandler() {
61                 display = Display.getCurrent();
62                 if (display != null) {
63                         focusListener = new Listener() {
64                                 @Override
65                                 public void handleEvent(Event event) {
66                                         updateEnablement();
67                                 }
68                         };
69                         display.addFilter(SWT.FocusIn, focusListener);
70                 }
71         }
72
73         void updateEnablement() {
74                 boolean rc = isHandled();
75                 if (rc != isEnabled()) {
76                         setBaseEnabled(rc);
77                 }
78         }
79
80         /**
81          * The name of the method to be invoked by this handler. This value should
82          * never be <code>null</code>.
83          */
84         protected String methodName;
85         private Listener focusListener;
86         private Display display;
87
88         @Override
89         public Object execute(final ExecutionEvent event) throws ExecutionException {
90                 Callable<?> runnable = getMethodToExecute();
91                 if (runnable != null) {
92                         try {
93                                 runnable.call();
94                         } catch (ExecutionException e) {
95                                 throw e;
96                         } catch (Exception e) {
97                                 throw new ExecutionException("Unexpected failure executing method " + methodName + " through " + runnable);
98                         }
99                 }
100                 return null;
101         }
102
103         @Override
104         public final boolean isHandled() {
105                 return getMethodToExecute() != null;
106         }
107
108         /**
109          * Looks up the method on the focus control.
110          *
111          * @return The method on the focus control; <code>null</code> if none.
112          */
113         protected Callable<Boolean> getMethodToExecute() {
114                 Display display = Display.getCurrent();
115                 if (display == null)
116                         return null;
117
118                 Control focusControl = display.getFocusControl();
119                 if (focusControl == null)
120                         return null;
121
122                 IUndoManager undoManager = (IUndoManager) focusControl.getData(TextViewerConstants.KEY_UNDO_MANAGER);
123                 if (undoManager == null)
124                         return null;
125
126                 try {
127                         Method method = undoManager.getClass().getMethod(methodName, NO_PARAMETERS);
128                         if (method != null)
129                                 return runner(undoManager, method);
130                 } catch (NoSuchMethodException e) {
131                         //      Fall through...
132                 }
133
134                 return null;
135         }
136
137         protected Callable<Boolean> runner(IUndoManager undoManager, Method method) {
138                 return () -> {
139                         try {
140                                 method.invoke(undoManager);
141                                 return true;
142                         } catch (IllegalAccessException e) {
143                                 // The method is protected, so do nothing.
144                                 return false;
145                         } catch (InvocationTargetException e) {
146                                 throw new ExecutionException(
147                                                 "An exception occurred while executing " //$NON-NLS-1$
148                                                                 + method.getName(), e
149                                                                 .getTargetException());
150
151                         }
152                 };
153         }
154
155         @Override
156         public void setInitializationData(IConfigurationElement config, String propertyName, Object data) {
157                 methodName = data.toString();
158         }
159
160         @Override
161         public void dispose() {
162                 if (display != null && !display.isDisposed()) {
163                         display.removeFilter(SWT.FocusIn, focusListener);
164                 }
165                 display = null;
166                 focusListener = null;
167         }
168
169 }