Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e85c9e01106a1d0ecad2a2693944612fe9f9eb75 (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;

import java.lang.reflect.*;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
//import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.internal.ui.actions.TeamAction;

public class SelectionPropertyTester extends PropertyTester {

	/**
	 * 
	 */
	public SelectionPropertyTester() {
		// TODO Auto-generated constructor stub
	}

	public boolean test(Object receiver, String property, Object[] args,
			Object expectedValue) {
		
		// only 'isEnabled' property is allowed at the moment
		if (property == null || !property.equals("isEnabled")) { //$NON-NLS-1$
			return false;
		}
		
		if (!(receiver instanceof ISelection)) {
			return false;
		}

		if (args == null || args.length != 1) {
			return false;
		}

		Object obj = receiver;
		if (obj instanceof IStructuredSelection) {
			obj = ((IStructuredSelection) receiver).getFirstElement();
		}

		if (obj == null) {
			return false;
		}

		Object invoke;
		try {
			Class<?> clazz = Class.forName((String) args[0]);
			Object instance = clazz.getDeclaredConstructor().newInstance();
			
//			Field fld = clazz.getDeclaredField(SELECTION_FIELDNAME);
//			fld.set(instance, (ISelection) receiver);
//			((TeamAction) instance).selectionChanged((IAction)instance,
			// called only to set the selection
			((TeamAction) instance).selectionChanged(null,
					(ISelection) receiver);			

			Method method = findMethod(clazz, property);
			if (method != null) {
				invoke = method.invoke(instance, (Object[])null);
				return ((Boolean) invoke).booleanValue();
			}
		} catch (IllegalArgumentException e) {
			CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
		} catch (IllegalAccessException e) {
			CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
		} catch (InvocationTargetException e) {
			CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
			CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e.getTargetException()));			
		} catch (ClassNotFoundException e) {
			CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
		} catch (Exception e) {
			CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
		}
		return false;
	}

	private static Method findMethod(Class clazz, String method)
			throws Exception {
		Method[] methods = clazz.getMethods();
		for (int i = 0; i < methods.length; i++) {
			if (methods[i].getName().equals(method))
				return methods[i];
		}
		return null;
	}
}

Back to the top