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 org.netbeans.modules.gsf.api.CompilationInfo;
045: import org.netbeans.modules.gsf.api.Completable.QueryType;
046: import org.netbeans.modules.gsf.api.HtmlFormatter;
047: import org.netbeans.modules.gsf.api.NameKind;
048: import org.netbeans.modules.php.model.SourceElement;
049:
050: /**
051: *
052: * @author Victor G. Vasilyev
053: */
054: public class CodeCompletionContext {
055:
056: private SourceElement sourceElement;
057: private CompilationInfo compilationInfo;
058: private int caretOffset;
059: private String prefix;
060: private NameKind nameKind;
061: private QueryType queryType;
062: private boolean caseSensitive;
063: private HtmlFormatter formatter;
064:
065: private SourceElement currentSourceElement;
066:
067: /**
068: * Creates code completion context.
069: * @param sourceElement current model element (under caret offset)
070: * @param compilationInfo
071: * @param caretOffset
072: * @param prefix
073: * @param nameKind
074: * @param queryType
075: * @param caseSensitive
076: * @param formatter
077: */
078: public CodeCompletionContext(SourceElement sourceElement,
079: CompilationInfo compilationInfo, int caretOffset,
080: String prefix, NameKind nameKind, QueryType queryType,
081: boolean caseSensitive, HtmlFormatter formatter) {
082: this .sourceElement = sourceElement;
083: this .compilationInfo = compilationInfo;
084: this .caretOffset = caretOffset;
085: this .prefix = prefix;
086: this .nameKind = nameKind;
087: this .queryType = queryType;
088: this .caseSensitive = caseSensitive;
089: this .formatter = formatter;
090:
091: }
092:
093: public SourceElement getSourceElement() {
094: return sourceElement;
095: }
096:
097: public CompilationInfo getCompilationInfo() {
098: return compilationInfo;
099: }
100:
101: public int getCaretOffset() {
102: return caretOffset;
103: }
104:
105: public String getPrefix() {
106: return prefix;
107: }
108:
109: public NameKind getNameKind() {
110: return nameKind;
111: }
112:
113: public QueryType getQueryType() {
114: return queryType;
115: }
116:
117: public boolean isCaseSensitive() {
118: return caseSensitive;
119: }
120:
121: public HtmlFormatter getFormatter() {
122: return formatter;
123: }
124:
125: public SourceElement getCurrentSourceElement() {
126: return currentSourceElement;
127: }
128:
129: public void setCurrentSourceElement(
130: SourceElement currentSourceElement) {
131: this .currentSourceElement = currentSourceElement;
132: }
133:
134: public boolean isEmptyPrefix() {
135: return prefix == null || prefix.trim().length() == 0;
136: }
137:
138: public int getInsertOffset() {
139: return isEmptyPrefix() ? caretOffset : caretOffset
140: - prefix.length();
141: }
142:
143: public static CodeCompletionContext changePrefix(
144: CodeCompletionContext c, String newPrefix) {
145: CodeCompletionContext newContext = new CodeCompletionContext(
146: c.sourceElement, c.compilationInfo, c.caretOffset,
147: newPrefix, c.nameKind, c.queryType, c.caseSensitive,
148: c.formatter);
149: newContext.setCurrentSourceElement(c.currentSourceElement);
150: return newContext;
151: }
152: }
|