4352cc84a6fe5a147dcbfc6db3b976bdfaf53d17
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / viewsupport / MemberFilter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
12
13 import net.sourceforge.phpdt.core.Flags;
14 import net.sourceforge.phpdt.core.IField;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IMember;
17 import net.sourceforge.phpdt.core.IType;
18 import net.sourceforge.phpdt.core.JavaModelException;
19
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.jface.viewers.ViewerFilter;
22
23 /**
24  * Filter for the methods viewer. Changing a filter property does not trigger a
25  * refiltering of the viewer
26  */
27 public class MemberFilter extends ViewerFilter {
28
29         public static final int FILTER_NONPUBLIC = 1;
30
31         public static final int FILTER_STATIC = 2;
32
33         public static final int FILTER_FIELDS = 4;
34
35         private int fFilterProperties;
36
37         /**
38          * Modifies filter and add a property to filter for
39          */
40         public final void addFilter(int filter) {
41                 fFilterProperties |= filter;
42         }
43
44         /**
45          * Modifies filter and remove a property to filter for
46          */
47         public final void removeFilter(int filter) {
48                 fFilterProperties &= (-1 ^ filter);
49         }
50
51         /**
52          * Tests if a property is filtered
53          */
54         public final boolean hasFilter(int filter) {
55                 return (fFilterProperties & filter) != 0;
56         }
57
58         /*
59          * @see ViewerFilter@isFilterProperty
60          */
61 //      public boolean isFilterProperty(Object element, Object property) {
62 //              return false;
63 //      }
64
65         /*
66          * @see ViewerFilter@select
67          */
68         public boolean select(Viewer viewer, Object parentElement, Object element) {
69                 try {
70                         if (hasFilter(FILTER_FIELDS) && element instanceof IField) {
71                                 return false;
72                         }
73                         if (element instanceof IMember) {
74                                 IMember member = (IMember) element;
75                                 if (member.getElementName().startsWith("<")) { // filter out
76                                                                                                                                 // <clinit>
77                                                                                                                                 // //$NON-NLS-1$
78                                         return false;
79                                 }
80                                 int flags = member.getFlags();
81                                 if (hasFilter(FILTER_STATIC)
82                                                 && (Flags.isStatic(flags) || isFieldInInterface(member))
83                                                 && member.getElementType() != IJavaElement.TYPE) {
84                                         return false;
85                                 }
86                                 if (hasFilter(FILTER_NONPUBLIC) && !Flags.isPublic(flags)
87                                                 && !isMemberInInterface(member)
88                                                 && !isTopLevelType(member)) {
89                                         return false;
90                                 }
91                         }
92                 } catch (JavaModelException e) {
93                         // ignore
94                 }
95                 return true;
96         }
97
98         private boolean isMemberInInterface(IMember member)
99                         throws JavaModelException {
100                 IType parent = member.getDeclaringType();
101                 return parent != null && parent.isInterface();
102         }
103
104         private boolean isFieldInInterface(IMember member)
105                         throws JavaModelException {
106                 return (member.getElementType() == IJavaElement.FIELD)
107                                 && member.getDeclaringType().isInterface();
108         }
109
110         private boolean isTopLevelType(IMember member) throws JavaModelException {
111                 IType parent = member.getDeclaringType();
112                 return parent == null;
113         }
114 }