KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > completion > ResultItem


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.core.syntax.completion;
21
22 import java.awt.Font JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.net.URL JavaDoc;
25 import org.netbeans.api.editor.completion.Completion;
26 import org.netbeans.editor.BaseDocument;
27 import org.netbeans.editor.ext.CompletionQuery;
28 import org.netbeans.spi.editor.completion.*;
29 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
30
31 import java.awt.Color JavaDoc;
32 import java.awt.Component JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.JTextComponent JavaDoc;
36 import javax.swing.*;
37
38
39 /**
40  * Code completion result item base class
41  *
42  * @author Dusan Balek, Marek Fukala
43  */

44
45 public abstract class ResultItem implements CompletionQuery.ResultItem, CompletionItem {
46     
47     static String JavaDoc toAdd;
48     public static final String JavaDoc COMPLETION_SUBSTITUTE_TEXT= "completion-substitute-text"; //NOI18N
49

50     protected int selectionStartOffset = -1;
51     protected int selectionEndOffset = -1;
52     private int substituteOffset = -1; //stores the substituteOffset
53

54     protected boolean shift = false;
55     
56     public int getSubstituteOffset() {
57         return substituteOffset;
58     }
59     
60     public void setSubstituteOffset(int offset) {
61         this.substituteOffset = offset;
62     }
63     
64     public abstract String JavaDoc getItemText();
65     
66     public abstract Component JavaDoc getPaintComponent(boolean isSelected);
67     
68     public abstract int getSortPriority();
69     
70     public CharSequence JavaDoc getSortText() {
71         return getItemText();
72     }
73     
74     public void processKeyEvent(KeyEvent JavaDoc e) {
75         shift = (e.getKeyCode() == KeyEvent.VK_ENTER && e.getID() == KeyEvent.KEY_PRESSED && e.isShiftDown());
76     }
77     
78     public boolean substituteCommonText(JTextComponent JavaDoc c, int offset, int len, int subLen) {
79         // [PENDING] not enough info in parameters...
80
// commonText
81
// substituteExp
82
return false;
83     }
84     
85     //afaik called only from abbrevs result item
86
public boolean substituteText(JTextComponent JavaDoc c, int offset, int len, boolean shift) {
87         BaseDocument doc = (BaseDocument)c.getDocument();
88         String JavaDoc text = getItemText();
89         
90         if (text != null) {
91             if (toAdd != null && !toAdd.equals("\n")) // NOI18N
92
text += toAdd;
93             // Update the text
94
doc.atomicLock();
95             try {
96                 String JavaDoc textToReplace = doc.getText(offset, len);
97                 if (text.equals(textToReplace)) return false;
98                 
99                 doc.remove(offset, len);
100                 doc.insertString(offset, text, null);
101                 if (selectionStartOffset >= 0) {
102                     c.select(offset + selectionStartOffset,
103                             offset + selectionEndOffset);
104                 }
105             } catch (BadLocationException JavaDoc e) {
106                 // Can't update
107
} finally {
108                 doc.atomicUnlock();
109             }
110         }
111         
112         return true;
113     }
114     
115     public Component JavaDoc getPaintComponent(javax.swing.JList JavaDoc list, boolean isSelected, boolean cellHasFocus) {
116         Component JavaDoc ret = getPaintComponent(isSelected);
117         if (ret==null) return null;
118         if (isSelected) {
119             ret.setBackground(list.getSelectionBackground());
120             ret.setForeground(list.getSelectionForeground());
121         } else {
122             ret.setBackground(list.getBackground());
123             ret.setForeground(list.getForeground());
124         }
125         ret.getAccessibleContext().setAccessibleName(getItemText());
126         ret.getAccessibleContext().setAccessibleDescription(getItemText());
127         return ret;
128     }
129     
130     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
131         Component JavaDoc renderComponent = getPaintComponent(false);
132         return renderComponent.getPreferredSize().width;
133     }
134     
135     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor,
136             Color JavaDoc backgroundColor, int width, int height, boolean selected) {
137         Component JavaDoc renderComponent = getPaintComponent(selected);
138         renderComponent.setFont(defaultFont);
139         renderComponent.setForeground(defaultColor);
140         renderComponent.setBackground(backgroundColor);
141         renderComponent.setBounds(0, 0, width, height);
142         ((ResultItemPaintComponent)renderComponent).paintComponent(g);
143     }
144     
145     public boolean instantSubstitution(JTextComponent JavaDoc c) {
146         defaultAction(c);
147         return true;
148     }
149     
150     public CompletionTask createDocumentationTask() {
151         return new AsyncCompletionTask(new JspCompletionProvider.DocQuery(this));
152     }
153     
154     public abstract URL JavaDoc getHelpURL();
155     
156     /** Returns help for the item. It can be only url. If the item doesn't have a help
157      * than returns null. The class can overwrite this method and compounds the help realtime.
158      */

159     public abstract String JavaDoc getHelp();
160     
161     /** Returns whether the item has a help. */
162     public abstract boolean hasHelp();
163     
164     public CompletionTask createToolTipTask() {
165         return null;
166     }
167     
168     public int getImportance() {
169         return 0;
170     }
171     
172     public void defaultAction(JTextComponent JavaDoc component) {
173         int substOffset = getSubstituteOffset();
174         if (substOffset == -1)
175             substOffset = component.getCaret().getDot();
176         
177         if(!shift) Completion.get().hideAll();
178         substituteText(component, substOffset, component.getCaret().getDot() - substOffset, shift);
179     }
180     
181 }
182
Popular Tags