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: package org.netbeans.modules.php.editor.completion;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Set;
047:
048: import java.util.StringTokenizer;
049: import javax.swing.ImageIcon;
050:
051: import org.netbeans.modules.gsf.api.Element;
052: import org.netbeans.modules.gsf.api.ElementKind;
053: import org.netbeans.modules.gsf.api.HtmlFormatter;
054: import org.netbeans.modules.gsf.api.Modifier;
055: import org.netbeans.modules.php.doc.FunctionDoc;
056:
057: /**
058: * This is method completion proposal based on existed PHP function
059: * ( from extensions or existed by default ). They are described
060: * in PHP manual.
061: *
062: * It differs from user defined function completion proposal.
063: * @author ads
064: *
065: */
066: class BuiltinMethodItem extends CompletionItem {
067:
068: private static final String COMMA = ", "; // NOI18N
069: private static final String RIGHT_PARENS = ")"; // NOI18N
070: private static final String LEFT_PARENS = "("; // NOI18N
071: private static final String OPTIONAL_PARAM_PREFIX = "["; // NOI18N
072:
073: BuiltinMethodItem(FunctionDoc doc, int caretOffset,
074: HtmlFormatter formatter) {
075: super (caretOffset, formatter);
076: myFunction = doc;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.netbeans.modules.gsf.api.CompletionProposal#getIcon()
081: */
082: public ImageIcon getIcon() {
083: // TODO An Icon for User Defined Method is required.
084: return null;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.netbeans.modules.gsf.api.CompletionProposal#getInsertParams()
089: */
090: @Override
091: public List<String> getInsertParams() {
092: List<String> args = new ArrayList<String>();
093: for (String arg : myFunction.getArguments()) {
094: if (arg.startsWith(OPTIONAL_PARAM_PREFIX)) {
095: args.add(arg);
096: } else {
097: StringTokenizer tokenizer = new StringTokenizer(arg);
098: if (tokenizer.hasMoreTokens()) {
099: String paramType = tokenizer.nextToken();
100: }
101: if (tokenizer.hasMoreTokens()) {
102: String paramName = tokenizer.nextToken();
103: String insertArgName = "$" + paramName.trim();
104: args.add(insertArgName);
105: }
106: }
107: }
108: return args;
109: }
110:
111: /* (non-Javadoc)
112: * @see org.netbeans.modules.gsf.api.CompletionProposal#getInsertPrefix()
113: */
114: public String getInsertPrefix() {
115: return myFunction.getName();
116: }
117:
118: /* (non-Javadoc)
119: * @see org.netbeans.modules.gsf.api.CompletionProposal#getKind()
120: */
121: public ElementKind getKind() {
122: return ElementKind.METHOD;
123: }
124:
125: /* (non-Javadoc)
126: * @see org.netbeans.modules.gsf.api.CompletionProposal#getLhsHtml()
127: */
128: public String getLhsHtml() {
129: HtmlFormatter formatter = getFormatter();
130: formatter.reset();
131: formatter.name(getKind(), true);
132: formatter.appendText(myFunction.getFullName());
133: formatter.name(getKind(), false);
134:
135: List<String> args = myFunction.getArguments();
136: if ((args != null) && (args.size() > 0)) {
137:
138: formatter.appendText(LEFT_PARENS);
139:
140: Iterator<String> it = args.iterator();
141:
142: while (it.hasNext()) {
143: formatter.parameters(true);
144: formatter.appendHtml(it.next());
145: formatter.parameters(false);
146:
147: if (it.hasNext()) {
148: formatter.appendText(COMMA);
149: }
150: }
151:
152: formatter.appendText(RIGHT_PARENS);
153: }
154:
155: return formatter.getText();
156:
157: }
158:
159: /* (non-Javadoc)
160: * @see org.netbeans.modules.gsf.api.CompletionProposal#getModifiers()
161: */
162: public Set<Modifier> getModifiers() {
163: // TODO Auto-generated method stub
164: return null;
165: }
166:
167: /* (non-Javadoc)
168: * @see org.netbeans.modules.gsf.api.CompletionProposal#getName()
169: */
170: public String getName() {
171: return myFunction.getName();
172: }
173:
174: /* (non-Javadoc)
175: * @see org.netbeans.modules.gsf.api.CompletionProposal#getRhsHtml()
176: */
177: public String getRhsHtml() {
178: return myFunction.getReturnType();
179: }
180:
181: /* (non-Javadoc)
182: * @see org.netbeans.modules.gsf.api.CompletionProposal#isSmart()
183: */
184: public boolean isSmart() {
185: return true;
186: }
187:
188: @Override
189: public String getCustomInsertTemplate() {
190: List<String> params = getInsertParams();
191: String[] delimiters = getParamListDelimiters();
192: assert delimiters.length == 2;
193: int paramCount = params.size();
194:
195: StringBuilder sb = new StringBuilder();
196: sb.append(getInsertPrefix());
197: sb.append(delimiters[0]);
198: int id = 1;
199: for (int i = 0; i < paramCount; i++) {
200: String paramDesc = params.get(i);
201: if (paramDesc.startsWith(OPTIONAL_PARAM_PREFIX)) {
202: // This and all the next params are optional.
203: sb.append("${"); //NOI18N
204: sb.append("php-func-"); // NOI18N
205: sb.append(getInsertPrefix()); // NOI18N
206: sb.append("-optArgs"); // NOI18N
207: sb.append(" default=\""); // NOI18N
208: // for (; i < paramCount; i++) {
209: // sb.append(paramDesc);
210: // if (i < paramCount - 1) {
211: // sb.append(", "); //NOI18N
212: // }
213: // }
214: sb.append("\""); // NOI18N
215: sb.append("}"); //NOI18N
216: break;
217: }
218: if (i != 0 && i < paramCount) {
219: sb.append(", "); //NOI18N
220: }
221: sb.append("${"); //NOI18N
222: sb.append("php-func-"); // NOI18N
223: sb.append(getInsertPrefix()); // NOI18N
224: sb.append("-arg-"); // NOI18N
225: sb.append(Integer.toString(id++));
226: sb.append(" default=\""); // NOI18N
227: sb.append(paramDesc);
228: sb.append("\""); // NOI18N
229: sb.append("}"); //NOI18N
230:
231: }
232: sb.append(delimiters[1]);
233: sb.append("${cursor}"); // NOI18N
234: return sb.toString();
235: }
236:
237: /* (non-Javadoc)
238: * @see org.netbeans.modules.gsf.api.CompletionProposal#getElement()
239: */
240: @Override
241: public Element getElement() {
242: return new DocumentableElement() {
243:
244: public String getIn() {
245: // TODO Auto-generated method stub
246: return null;
247: }
248:
249: public ElementKind getKind() {
250: return ElementKind.METHOD;
251: }
252:
253: public Set<Modifier> getModifiers() {
254: // TODO Auto-generated method stub
255: return null;
256: }
257:
258: public String getName() {
259: return myFunction.getFullName();
260: }
261:
262: public String getDocumentation() {
263: return myFunction.getDocumentation();
264: }
265: };
266: }
267:
268: private FunctionDoc myFunction;
269: }
|