Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 10a375d3eacc3578993c07e150264ac82cc42b23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved.   This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* 
* Contributors:
*   IBM - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.sse.ui.internal.extension;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.sse.core.IModelManager;
import org.eclipse.wst.sse.core.IModelManagerPlugin;
import org.eclipse.wst.sse.core.IStructuredModel;
import org.eclipse.wst.sse.core.undo.StructuredTextUndoManager;
import org.eclipse.wst.sse.ui.Logger;
import org.eclipse.wst.sse.ui.extension.IExtendedSimpleEditor;
import org.eclipse.wst.sse.ui.extensions.ISelfValidateEditAction;


/**
 */
public class DropActionProxy implements InvocationHandler {
	public static Object newInstance(Object obj) {
		Object instance = null;
		try {
			Set set = new HashSet();
			Class clazz = obj.getClass();
			while (clazz != null) {
				Class[] interfaces = clazz.getInterfaces();
				for (int i = 0; i < interfaces.length; i++) {
					set.add(interfaces[i]);
				}
				clazz = clazz.getSuperclass();
			}
			Class[] classes = new Class[set.size()];
			Iterator itr = set.iterator();
			int i = 0;
			while (itr.hasNext()) {
				classes[i] = (Class) itr.next();
				i++;
			}
			instance = Proxy.newProxyInstance(obj.getClass().getClassLoader(), classes, new DropActionProxy(obj));
		}
		catch (Error e) {
			Logger.logException("Exception while proxying a drop action", e); //$NON-NLS-1$
			instance = obj;
		}
		return instance;
	}

	private IExtendedSimpleEditor editor = null;
	private IStructuredModel fRecorder;
	private Object obj;

	private DropActionProxy(Object obj) {
		this.obj = obj;
	}

	private void beginRecording() {
		IDocument document = null;
		if (editor != null) {
			document = editor.getDocument();
			if (document != null)
				fRecorder = getModelManager().getExistingModelForEdit(document);
			// Prepare for Undo
			if (fRecorder != null) {
				StructuredTextUndoManager um = fRecorder.getUndoManager();
				if (um != null) {
					if (this.obj instanceof IAction)
						um.beginRecording(this, ((IAction) this.obj).getText(), ((IAction) this.obj).getDescription());
					else
						um.beginRecording(this);
				}
			}
		}
	}

	private void endRecording() {
		if (fRecorder != null) {
			StructuredTextUndoManager um = fRecorder.getUndoManager();
			if (um != null)
				um.endRecording(this);
			fRecorder.releaseFromEdit();
			fRecorder = null;
		}
	}

	/**
	 *  
	 */
	private IModelManager getModelManager() {
		IModelManagerPlugin plugin = (IModelManagerPlugin) Platform.getPlugin(IModelManagerPlugin.ID);
		return plugin.getModelManager();
	}

	/**
	 * @see java.lang.reflect.InvocationHandler#invoke(Object, Method,
	 *      Object[])
	 */
	public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
		Object result;
		String name = m.getName();
		try {
			if (name.equals("equals")) { //$NON-NLS-1$
				// Workaround for JDK's bug 4652876
				// "equals" always returns false even if both InvocationHandler
				// class
				// hold the same objects
				// See
				// http://developer.java.sun.com/developer/bugParade/bugs/4652876.html
				// This problem is in the IBM SDK 1.3.1
				// but I don't see the bug in Sun's JDK 1.4.1 (beta)
				Object arg = args[0];
				return (proxy.getClass() == arg.getClass() && equals(Proxy.getInvocationHandler(arg))) ? Boolean.TRUE : Boolean.FALSE;
			}
			else if (name.equals("run")) { //$NON-NLS-1$
				if (args[1] instanceof IExtendedSimpleEditor) {
					editor = (IExtendedSimpleEditor) args[1];
				}
				beginRecording();
				if ((editor != null) && !(obj instanceof ISelfValidateEditAction)) {
					IStatus status = editor.validateEdit(getDisplay().getActiveShell());
					if (!status.isOK()) {
						return null;
					}
				}
			}
			result = m.invoke(obj, args);
		}
		catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
		catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		finally {
			if (name.equals("run")) { //$NON-NLS-1$
				endRecording();
			}
		}
		return result;
	}

	private Display getDisplay() {
		return PlatformUI.getWorkbench().getDisplay();
	}
}

Back to the top