KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > SelectionPropertyTester


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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.action.IAction;
19
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     /**
26      *
27      */

28     public SelectionPropertyTester() {
29         // TODO Auto-generated constructor stub
30
}
31
32     /*
33      * (non-Javadoc)
34      *
35      * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object,
36      * java.lang.String, java.lang.Object[], java.lang.Object)
37      */

38     public boolean test(Object JavaDoc receiver, String JavaDoc property, Object JavaDoc[] args,
39             Object JavaDoc expectedValue) {
40         
41         // only 'isEnabled' property is allowed at the moment
42
if (property == null || !property.equals("isEnabled")) { //$NON-NLS-1$
43
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 JavaDoc 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 JavaDoc invoke;
64         try {
65             Class JavaDoc clazz = Class.forName((String JavaDoc) args[0]);
66             Object JavaDoc instance = clazz.newInstance();
67             
68 // Field fld = clazz.getDeclaredField(SELECTION_FIELDNAME);
69
// fld.set(instance, (ISelection) receiver);
70
// ((TeamAction) instance).selectionChanged((IAction)instance,
71
// called only to set the selection
72
((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 JavaDoc) invoke).booleanValue();
79             }
80         } catch (IllegalArgumentException JavaDoc e) {
81             CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
82         } catch (IllegalAccessException JavaDoc 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 JavaDoc e) {
88             CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
89         } catch (Exception JavaDoc e) {
90             CVSUIPlugin.log(new Status(IStatus.ERROR, CVSUIPlugin.ID, null, e));
91         }
92         return false;
93     }
94
95     private static Method findMethod(Class JavaDoc clazz, String JavaDoc method)
96             throws Exception JavaDoc {
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