KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > manipulation > JavaElementPropertyTester


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.jdt.internal.core.manipulation;
13
14 import java.util.regex.Pattern JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.core.expressions.PropertyTester;
19
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 /**
26  * A property tester for various properties of IJavaElements.
27  * Might be moved down to jdt.core. See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=127085
28  *
29  * @since 3.3
30  */

31 public class JavaElementPropertyTester extends PropertyTester {
32
33     /**
34      * A property indicating the file name (value <code>"name"</code>). Regular expressions are supported.
35      */

36     public static final String JavaDoc NAME = "name"; //$NON-NLS-1$
37

38     /**
39      * A property indicating if the element is in a open and existing Java project (value <code>"isInJavaProject"</code>).
40      */

41     public static final String JavaDoc IS_IN_JAVA_PROJECT = "isInJavaProject"; //$NON-NLS-1$
42

43     /**
44      * A property indicating if the element is in a open and existing Java project that also implements the given nature (value <code>"isInJavaProjectWithNature"</code>).
45      */

46     public static final String JavaDoc IS_IN_JAVA_PROJECT_WITH_NATURE = "isInJavaProjectWithNature"; //$NON-NLS-1$
47

48     /**
49      * A property indicating if the element is on the classpath (value <code>"isOnClasspath"</code>).
50      */

51     public static final String JavaDoc IS_ON_CLASSPATH = "isOnClasspath"; //$NON-NLS-1$
52

53     /**
54      * A property indicating if the a type of the given qualified name is on the classpath (value <code>"hasTypeOnClasspath"</code>).
55      */

56     public static final String JavaDoc HAS_TYPE_ON_CLASSPATH = "hasTypeOnClasspath"; //$NON-NLS-1$
57

58     /**
59      * A property indicating if the element is a source folder or is inside a source folder. (value <code>"inSourceFolder"</code>).
60      * <code>false</code> is returned if the element does not exist.
61      */

62     public static final String JavaDoc IN_SOURCE_FOLDER = "inSourceFolder"; //$NON-NLS-1$
63

64     /**
65      * A property indicating if the element is an archive or is inside an archive. (value <code>"inArchive"</code>).
66      * <code>false</code> is returned if the element does not exist.
67      */

68     public static final String JavaDoc IN_ARCHIVE = "inArchive"; //$NON-NLS-1$
69

70     /**
71      * A property indicating if the element is an archive (value <code>"inExternalArchive"</code>).
72      * <code>false</code> is returned if the element does not exist.
73      */

74     public static final String JavaDoc IN_EXTERNAL_ARCHIVE = "inExternalArchive"; //$NON-NLS-1$
75

76     /**
77      * A property indicating a option in the Java project of the selected element
78      * (value <code>"projectOption"</code>). If two arguments are given,
79      * this treats the first as the option name, and the second as the option
80      * property value. If only one argument (or just the expected value) is
81      * given, this treats it as the property name, and simply tests if the option is
82      * avaiable in the project specific options.
83      */

84     public static final String JavaDoc PROJECT_OPTION = "projectOption"; //$NON-NLS-1$
85

86
87     /* (non-Javadoc)
88      * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
89      */

90     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc expectedValue) {
91         if (!(receiver instanceof IJavaElement)) {
92             return false;
93         }
94         IJavaElement res = (IJavaElement) receiver;
95         if (method.equals(NAME)) {
96             return Pattern.matches(toString(expectedValue), res.getElementName());
97         } else if (method.equals(IS_IN_JAVA_PROJECT)) {
98             IJavaProject javaProject= res.getJavaProject();
99             return javaProject != null && javaProject.exists() && javaProject.getProject().isOpen();
100         } else if (method.equals(IS_IN_JAVA_PROJECT_WITH_NATURE)) {
101             IJavaProject javaProject= res.getJavaProject();
102             if (javaProject != null && javaProject.exists() && javaProject.getProject().isOpen() ) {
103                 if (expectedValue != null) {
104                     try {
105                         return javaProject.getProject().hasNature(toString(expectedValue));
106                     } catch (CoreException e) {
107                         return false;
108                     }
109                 }
110             }
111             return false;
112         } else if (method.equals(IS_ON_CLASSPATH)) {
113             IJavaProject javaProject= res.getJavaProject();
114             if (javaProject != null && javaProject.exists()) {
115                 return javaProject.isOnClasspath(res);
116             }
117             return false;
118         } else if (method.equals(IN_SOURCE_FOLDER)) {
119             IJavaElement root= res.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
120             if (root != null) {
121                 try {
122                     return ((IPackageFragmentRoot) root).getKind() == IPackageFragmentRoot.K_SOURCE;
123                 } catch (JavaModelException e) {
124                     // ignore
125
}
126             }
127             return false;
128         } else if (method.equals(IN_ARCHIVE)) {
129             IJavaElement root= res.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
130             if (root != null) {
131                 return ((IPackageFragmentRoot) root).isArchive();
132             }
133             return false;
134         } else if (method.equals(IN_EXTERNAL_ARCHIVE)) {
135             IJavaElement root= res.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
136             if (root != null) {
137                 return ((IPackageFragmentRoot) root).isExternal();
138             }
139             return false;
140         } else if (method.equals(PROJECT_OPTION)) {
141             IJavaProject project= res.getJavaProject();
142             if (project != null) {
143                 if (args.length == 2) {
144                     String JavaDoc current= project.getOption(toString(args[0]), true);
145                     return current != null && current.equals(args[1]);
146                 } else if (args.length == 1) {
147                     return project.getOption(toString(args[0]), false) != null;
148                 }
149             }
150             return false;
151         } else if (method.equals(HAS_TYPE_ON_CLASSPATH)) {
152             IJavaProject javaProject= res.getJavaProject();
153             if (javaProject != null && javaProject.exists()) {
154                 try {
155                     return javaProject.findType(toString(expectedValue)) != null;
156                 } catch (JavaModelException e) {
157                     return false;
158                 }
159             }
160         }
161         return false;
162     }
163
164     private String JavaDoc toString(Object JavaDoc expectedValue) {
165         return expectedValue == null ? "" : expectedValue.toString(); //$NON-NLS-1$
166
}
167 }
168
Popular Tags