1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.lang.reflect.*; 14 15 import org.eclipse.core.expressions.PropertyTester; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.viewers.ISelection; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.team.internal.ui.actions.TeamAction; 22 23 public class SelectionPropertyTester extends PropertyTester { 24 25 28 public SelectionPropertyTester() { 29 } 31 32 38 public boolean test(Object receiver, String property, Object [] args, 39 Object expectedValue) { 40 41 if (property == null || !property.equals("isEnabled")) { return false; 44 } 45 46 if (!(receiver instanceof ISelection)) { 47 return false; 48 } 49 50 if (args == null || args.length != 1) { 51 return false; 52 } 53 54 Object obj = receiver; 55 if (obj instanceof IStructuredSelection) { 56 obj = ((IStructuredSelection) receiver).getFirstElement(); 57 } 58 59 if (obj == null) { 60 return false; 61 } 62 63 Object invoke; 64 try { 65 Class clazz = Class.forName((String ) args[0]); 66 Object instance = clazz.newInstance(); 67 68 ((TeamAction) instance).selectionChanged(null, 73 (ISelection) receiver); 74 75 Method method = findMethod(clazz, property); 76 if (method != null) { 77 invoke = method.invoke(instance, null); 78 return ((Boolean ) invoke).booleanValue(); 79 } 80 } catch (IllegalArgumentException e) { 81 CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e)); 82 } catch (IllegalAccessException e) { 83 CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e)); 84 } catch (InvocationTargetException e) { 85 CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e)); 86 CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e.getTargetException())); 87 } catch (ClassNotFoundException e) { 88 CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e)); 89 } catch (Exception e) { 90 CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e)); 91 } 92 return false; 93 } 94 95 private static Method findMethod(Class clazz, String method) 96 throws Exception { 97 Method[] methods = clazz.getMethods(); 98 for (int i = 0; i < methods.length; i++) { 99 if (methods[i].getName().equals(method)) 100 return methods[i]; 101 } 102 return null; 103 } 104 } 105 | Popular Tags |