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.gsfret.editor.codetemplates;
042:
043: import java.io.IOException;
044: import java.util.*;
045:
046: import javax.swing.text.JTextComponent;
047:
048: import org.netbeans.modules.gsf.api.CancellableTask;
049: import org.netbeans.modules.gsf.api.Completable;
050: import org.netbeans.napi.gsfret.source.CompilationController;
051: import org.netbeans.napi.gsfret.source.CompilationInfo;
052: import org.netbeans.napi.gsfret.source.Phase;
053: import org.netbeans.napi.gsfret.source.Source;
054: import org.netbeans.lib.editor.codetemplates.spi.*;
055: import org.netbeans.modules.gsfret.editor.completion.GsfCompletionProvider;
056: import org.openide.util.Exceptions;
057:
058: /**
059: * Code template processor for GSF: Delegates mostly to the plugin
060: * to resolve parameters.
061: *
062: * @author Tor Norbye
063: */
064: public class GsfCodeTemplateProcessor implements CodeTemplateProcessor {
065: private CodeTemplateInsertRequest request;
066: private CompilationInfo cInfo = null;
067:
068: private GsfCodeTemplateProcessor(CodeTemplateInsertRequest request) {
069: this .request = request;
070: }
071:
072: public synchronized void updateDefaultValues() {
073: boolean cont = true;
074:
075: while (cont) {
076: cont = false;
077:
078: for (Object p : request.getMasterParameters()) {
079: CodeTemplateParameter param = (CodeTemplateParameter) p;
080: String value = getProposedValue(param);
081:
082: if ((value != null) && !value.equals(param.getValue())) {
083: param.setValue(value);
084: cont = true;
085: }
086: }
087: }
088: }
089:
090: public void parameterValueChanged(
091: CodeTemplateParameter masterParameter, boolean typingChange) {
092: if (typingChange) {
093: for (Object p : request.getMasterParameters()) {
094: CodeTemplateParameter param = (CodeTemplateParameter) p;
095:
096: if (!param.isUserModified()) {
097: String value = getProposedValue(param);
098:
099: if ((value != null)
100: && !value.equals(param.getValue())) {
101: param.setValue(value);
102: }
103: }
104: }
105: }
106: }
107:
108: public void release() {
109: }
110:
111: private String getProposedValue(CodeTemplateParameter param) {
112: String variable = param.getName();
113: JTextComponent c = request.getComponent();
114: int caretOffset = c.getCaret().getDot();
115:
116: String resolved = delegatedResolve(caretOffset,
117: param.getName(), variable, param.getHints());
118:
119: return resolved;
120: }
121:
122: /** Delegate to the language plugin to indicate if it cares about this variable or not */
123: private String delegatedResolve(int caretOffset, String name,
124: String variable, Map params) {
125: try {
126: if (initParsing()) {
127:
128: Completable completer = GsfCompletionProvider
129: .getCompletable(cInfo, caretOffset);
130:
131: if (completer == null) {
132: return null;
133: }
134:
135: return completer.resolveTemplateVariable(variable,
136: cInfo, caretOffset, name, params);
137: }
138: } catch (Exception e) {
139: }
140:
141: return null;
142: }
143:
144: private boolean initParsing() {
145: if (cInfo == null) {
146: JTextComponent c = request.getComponent();
147:
148: //final int caretOffset = c.getCaret().getDot();
149: Source js = Source.forDocument(c.getDocument());
150:
151: if (js != null) {
152: try {
153: js
154: .runUserActionTask(
155: new CancellableTask<CompilationController>() {
156: public void cancel() {
157: }
158:
159: public void run(
160: final CompilationController controller)
161: throws IOException {
162: controller
163: .toPhase(Phase.RESOLVED);
164: cInfo = controller;
165: }
166: }, false);
167: } catch (IOException ioe) {
168: Exceptions.printStackTrace(ioe);
169: }
170: }
171: }
172:
173: return cInfo != null;
174: }
175:
176: public static final class Factory implements
177: CodeTemplateProcessorFactory {
178: public CodeTemplateProcessor createProcessor(
179: CodeTemplateInsertRequest request) {
180: return new GsfCodeTemplateProcessor(request);
181: }
182: }
183: }
|