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.mobility.editor;
043:
044: import org.netbeans.spi.editor.completion.CompletionItem;
045: import org.netbeans.spi.editor.completion.CompletionTask;
046: import org.netbeans.spi.editor.completion.support.CompletionUtilities;
047: import org.netbeans.api.editor.completion.Completion;
048: import org.openide.util.Utilities;
049: import javax.swing.text.JTextComponent;
050: import javax.swing.text.Document;
051: import javax.swing.text.BadLocationException;
052: import javax.swing.*;
053: import java.awt.event.KeyEvent;
054: import java.awt.*;
055:
056: /**
057: * Created by IntelliJ IDEA.
058: * User: bohemius
059: * Date: Jul 18, 2005
060: * Time: 5:49:10 PM
061: * To change this template use File | Settings | File Templates.
062: */
063: public class PPDirectiveCompletionItem implements CompletionItem {
064:
065: private static ImageIcon icon;
066: final private String name;
067: final private String description;
068:
069: public PPDirectiveCompletionItem(String name, String desc) {
070: this .name = name;
071: this .description = desc;
072: }
073:
074: public void defaultAction(final JTextComponent component) {
075: substitute(component, this .name);
076:
077: Completion.get().hideAll();
078: }
079:
080: public void processKeyEvent(final KeyEvent event) {
081: if (event.getKeyChar() == ' ' && !event.isControlDown()) {
082: Completion.get().hideCompletion();
083: }
084: }
085:
086: public int getPreferredWidth(final Graphics graphics,
087: final Font font) {
088: return CompletionUtilities.getPreferredWidth(getLeftText(),
089: getRightText(), graphics, font);
090: }
091:
092: public void render(final Graphics g, final Font defaultFont,
093: final Color defaultColor, final Color backgroundColor,
094: final int width, final int height, final boolean selected) {
095:
096: synchronized (this ) {
097: if (icon == null) {
098: icon = new ImageIcon(
099: Utilities
100: .loadImage("org/netbeans/modules/mobility/editor/resources/d.png"));
101: }
102: }
103:
104: if (selected) {
105: g.setColor(backgroundColor);
106: g.fillRect(0, 0, width, height);
107: g.setColor(defaultColor);
108: }
109: CompletionUtilities.renderHtml(icon, getLeftText(),
110: getRightText(), g, defaultFont, defaultColor, width,
111: height, selected);
112: }
113:
114: public CompletionTask createDocumentationTask() {
115: return null;
116: }
117:
118: public CompletionTask createToolTipTask() {
119: return null;
120: }
121:
122: public boolean instantSubstitution(@SuppressWarnings("unused")
123: final JTextComponent jTextComponent) {
124: return false;
125: }
126:
127: public int getSortPriority() {
128: return 50;
129: }
130:
131: public CharSequence getSortText() {
132: return name;
133: }
134:
135: private String leftText;
136:
137: private String getLeftText() {
138: if (leftText == null) {
139: leftText = "<b>" + toHtmlText(this .name) + "</b> ";
140: }
141: return leftText;
142: }
143:
144: private String getRightText() {
145: return toHtmlText(this .description);
146: }
147:
148: private void substitute(final JTextComponent component,
149: final String directive) {
150: final int offset = component.getCaret().getDot();
151: final Document doc = component.getDocument();
152:
153: try {
154: int wordStart = org.netbeans.editor.Utilities.getWordStart(
155: component, offset);
156: final int wordEnd = org.netbeans.editor.Utilities
157: .getWordEnd(component, offset);
158:
159: if (wordStart > 0) {
160: String word = doc.getText(wordStart, wordEnd
161: - wordStart);
162: if (word.startsWith("//#")) {
163: word = word.substring(3);
164: wordStart += 3;
165: }
166: doc.remove(wordStart, word.length());
167: doc.insertString(wordStart, directive, null);
168:
169: } else {
170: doc.insertString(offset, directive, null);
171: }
172: } catch (BadLocationException ble) {
173: }
174: }
175:
176: private static String toHtmlText(final String text) {
177: StringBuffer htmlText = null;
178: for (int i = 0; i < text.length(); i++) {
179: String rep; // replacement string
180: final char ch = text.charAt(i);
181: switch (ch) {
182: case '<':
183: rep = "<";
184: break;
185: case '>':
186: rep = ">";
187: break;
188: case '\n':
189: rep = "<br>";
190: break;
191: default:
192: rep = null;
193: break;
194: }
195:
196: if (rep != null) {
197: if (htmlText == null) {
198: // Expect 20% of text to be html tags text
199: htmlText = new StringBuffer(
200: 120 * text.length() / 100);
201: if (i > 0) {
202: htmlText.append(text.substring(0, i));
203: }
204: }
205: htmlText.append(rep);
206:
207: } else { // no replacement
208: if (htmlText != null) {
209: htmlText.append(ch);
210: }
211: }
212: }
213: return (htmlText != null) ? htmlText.toString() : text;
214: }
215:
216: public CharSequence getInsertPrefix() {
217: return name;
218: }
219: }
|