001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.php.editor.completion;
043:
044: import java.util.List;
045: import java.util.LinkedList;
046: import org.netbeans.modules.gsf.api.CompletionProposal;
047: import org.netbeans.modules.php.model.Attribute;
048: import org.netbeans.modules.php.model.AttributesDeclaration;
049: import org.netbeans.modules.php.model.ClassBody;
050: import org.netbeans.modules.php.model.ClassConst;
051: import org.netbeans.modules.php.model.ClassDefinition;
052: import org.netbeans.modules.php.model.ClassFunctionDefinition;
053: import org.netbeans.modules.php.model.ConstDeclaration;
054: import org.netbeans.modules.php.model.FunctionDeclaration;
055: import org.netbeans.modules.php.model.Modifier;
056: import org.netbeans.modules.php.model.SourceElement;
057:
058: /**
059: * An abstraction to support basic methods of the code completion in the scope
060: * of the member access expressions, like this:
061: * <code>
062: * Class1::$attr
063: * Class1::CONST1
064: * $instanceRef->func1();
065: * <code>
066: *
067: * @author Victor G. Vasilyev
068: */
069: public abstract class MemberAccessExpressionScope extends
070: ASTBasedProvider {
071: protected List<CompletionProposal> proposals;
072: protected String prefix;
073: protected int insertOffset;
074:
075: protected ClassDefinition referencedClass;
076:
077: protected abstract class Filter<T extends SourceElement> {
078: public abstract boolean isApplicable(T e);
079: }
080:
081: protected void init(CodeCompletionContext context) {
082: proposals = new LinkedList<CompletionProposal>();
083: assert context != null;
084: myContext = context;
085: prefix = context.getPrefix();
086: insertOffset = calcInsertOffset();
087: }
088:
089: protected abstract int calcInsertOffset();
090:
091: protected abstract String getOperator();
092:
093: protected void addConstants() {
094: ClassBody cb = referencedClass.getBody();
095: List<ConstDeclaration> cdList = cb
096: .getChildren(ConstDeclaration.class);
097: for (ConstDeclaration cDecl : cdList) {
098: for (ClassConst cc : cDecl.getDeclaredConstants()) {
099: String name = cc.getName();
100: if (getOperator().equals(prefix)
101: || startsWith(name, prefix)) {
102: proposals.add(new VariableItem(name, insertOffset,
103: VariableItem.VarTypes.CONSTANT, myContext
104: .getFormatter(), false));
105: }
106: }
107: }
108: }
109:
110: protected void addMethods(Filter<ClassFunctionDefinition> f) {
111: ClassBody cb = referencedClass.getBody();
112: List<ClassFunctionDefinition> fdList = cb
113: .getChildren(ClassFunctionDefinition.class);
114: for (ClassFunctionDefinition fd : fdList) {
115: // TODO ??? select public static only
116: // int actualFlags = Modifier.toFlags(fd.getModifiers());
117: List<Modifier> modifiers = fd.getModifiers();
118: if (f != null) {
119: if (!f.isApplicable(fd)) {
120: continue;
121: }
122: }
123: FunctionDeclaration decl = fd.getDeclaration();
124: String name = decl.getName();
125: if (isApplicableIncompleteExpression()
126: || startsWith(name, prefix)) {
127: proposals.add(new UserDefinedMethodItem(decl,
128: insertOffset, myContext.getFormatter()));
129: }
130: }
131: }
132:
133: /**
134: * Adds properties.
135: */
136: protected void addProperties(Filter<AttributesDeclaration> f) {
137: ClassBody cb = referencedClass.getBody();
138: List<AttributesDeclaration> adList = cb
139: .getChildren(AttributesDeclaration.class);
140: for (AttributesDeclaration ad : adList) {
141: List<Modifier> modifiers = ad.getModifiers();
142: if (f != null) {
143: if (!f.isApplicable(ad)) {
144: continue;
145: }
146: }
147: for (Attribute at : ad.getDeclaredAttributes()) {
148: String name = at.getName();
149: if (getOperator().equals(prefix)
150: || startsWith(name, prefix)) {
151: proposals.add(new VariableItem(name, insertOffset,
152: VariableItem.VarTypes.CONSTANT, myContext
153: .getFormatter(), false));
154: }
155: }
156: }
157: }
158:
159: private static boolean startsWith(String name, String prefix) {
160: if (name == null || prefix == null) {
161: return false;
162: }
163: return name.toLowerCase().startsWith(prefix.toLowerCase());
164: }
165:
166: /**
167: *
168: * @return <code>true</code> if a constant value is not specified after
169: * scope resolution operator.
170: */
171: protected boolean isApplicableIncompleteExpression() {
172: if (prefix != null && prefix.endsWith(getOperator())) {
173: return true;
174: }
175: return false;
176: }
177:
178: }
|