KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > completion > XMLResultItem


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.xml.text.completion;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.swing.text.*;
30 import javax.swing.Icon JavaDoc;
31
32 import org.netbeans.editor.*;
33 //import org.netbeans.editor.ext.*;
34
import org.netbeans.editor.Utilities;
35 import javax.swing.JLabel JavaDoc;
36 import org.netbeans.api.editor.completion.Completion;
37 import org.netbeans.spi.editor.completion.CompletionItem;
38
39 import org.netbeans.editor.ext.CompletionQuery.ResultItem;
40 import org.netbeans.editor.ext.ExtFormatter;
41 import org.netbeans.spi.editor.completion.CompletionTask;
42 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
43
44 /**
45  * This class carries result information required by NetBeans Editor module.
46  *
47  * @author Petr Kuzel
48  * @author Sandeep Randhawa
49  */

50 class XMLResultItem implements ResultItem, CompletionItem {
51     
52     private static final int XML_ITEMS_SORT_PRIORITY = 20;
53     // text to be diplayed to user
54
public String JavaDoc displayText;
55     private String JavaDoc replacementText;
56     // icon to be diplayed
57
public javax.swing.Icon JavaDoc icon;
58     public Color JavaDoc foreground = Color.black;
59     public Color JavaDoc background = Color.white;
60     public Color JavaDoc selectionForeground = Color.black;
61     public Color JavaDoc selectionBackground = new Color JavaDoc(204, 204, 255);
62     private static JLabel JavaDoc rubberStamp = new JLabel JavaDoc();
63     private XMLCompletionResultItemPaintComponent component;
64     private boolean shift = false;
65     
66     static {
67         rubberStamp.setOpaque( true );
68     }
69     
70     /**
71      *
72      * @param replacementText replacement text that is used as display name too
73      */

74     public XMLResultItem(String JavaDoc replacementText){
75         this(replacementText, null);
76     }
77     
78     /**
79      * @param displayText text to display or null if replacementText is OK
80      */

81     public XMLResultItem(String JavaDoc replacementText, String JavaDoc displayText) {
82         this.replacementText = replacementText;
83         this.displayText = displayText != null ? displayText : replacementText;
84     }
85     
86     /** Creates new XMLResultItem
87      * @param displayText The string value that will be displayed in the completion window and will hence
88      * be the replacement text if selected.
89      * @param icon The icon that will be displayed for this element.
90      * @param foreground The foreground color of the text
91      * @param background The background color of the text
92      * @param selectionForeground The foreground color of the selected text
93      * @param selectionBackground The background color of the selected text
94      */

95     public XMLResultItem(String JavaDoc displayText, javax.swing.Icon JavaDoc icon, Color JavaDoc foreground, Color JavaDoc background, Color JavaDoc selectionForeground, Color JavaDoc selectionBackground) {
96         this.displayText = displayText;
97         this.icon = icon;
98         this.foreground = foreground;
99         this.background = background;
100         this.selectionForeground = selectionForeground;
101         this.selectionBackground = selectionBackground;
102     }
103     
104     /**
105      * Insert following text into document.
106      */

107     public String JavaDoc getReplacementText(int modifiers){
108         return displayText;
109     }
110     
111     
112     protected Icon JavaDoc getIcon(){
113         return icon;
114     }
115     
116     /**
117      * Actually replaces a piece of document by passes text.
118      * @param component a document source
119      * @param text a string to be inserted
120      * @param offset the target offset
121      * @param len a length that should be removed before inserting text
122      */

123     boolean replaceText( JTextComponent component, String JavaDoc text, int offset, int len) {
124         BaseDocument doc = (BaseDocument)component.getDocument();
125         doc.atomicLock();
126         try {
127             String JavaDoc currentText = doc.getText(offset, (doc.getLength() - offset) < text.length() ? (doc.getLength() - offset) : text.length()) ;
128             if(!text.equals(currentText)) {
129                 doc.remove( offset, len );
130                 doc.insertString( offset, text, null);
131             } else {
132                 int newCaretPos = component.getCaret().getDot() + text.length() - len;
133                 //#82242 workaround - the problem is that in some situations
134
//1) result item is created and it remembers the remove length
135
//2) document is changed
136
//3) RI is substituted.
137
//this situation shouldn't happen imho and is a problem of CC infrastructure
138
component.setCaretPosition(newCaretPos < doc.getLength() ? newCaretPos : doc.getLength());
139             }
140             //reformat the line
141
//((ExtFormatter)doc.getFormatter()).reformat(doc, Utilities.getRowStart(doc, offset), offset+text.length(), true);
142
} catch( BadLocationException exc ) {
143             return false; //not sucessfull
144
// } catch (IOException e) {
145
// return false;
146
} finally {
147             doc.atomicUnlock();
148         }
149         return true;
150     }
151     
152     public boolean substituteCommonText( JTextComponent c, int offset, int len, int subLen ) {
153         return replaceText( c, getReplacementText(0).substring( 0, subLen ), offset, len );
154     }
155     
156     /**
157      * Just translate <code>shift</code> to proper modifier
158      */

159     public final boolean substituteText( JTextComponent c, int offset, int len, boolean shift ) {
160         int modifier = shift ? java.awt.event.InputEvent.SHIFT_MASK : 0;
161         return substituteText(c, offset, len, modifier);
162     }
163     
164     public boolean substituteText( JTextComponent c, int offset, int len, int modifiers ){
165         return replaceText(c, getReplacementText(modifiers), offset, len);
166     }
167     
168     /** @return Properly colored JLabel with text gotten from <CODE>getPaintText()</CODE>. */
169     public java.awt.Component JavaDoc getPaintComponent( javax.swing.JList JavaDoc list, boolean isSelected, boolean cellHasFocus ) {
170         // The space is prepended to avoid interpretation as HTML Label
171
if (getIcon() != null) rubberStamp.setIcon(getIcon());
172         
173         rubberStamp.setText( displayText );
174         if (isSelected) {
175             rubberStamp.setBackground(selectionBackground);
176             rubberStamp.setForeground(selectionForeground);
177         } else {
178             rubberStamp.setBackground(background);
179             rubberStamp.setForeground(foreground);
180         }
181         return rubberStamp;
182     }
183     
184     public final String JavaDoc getItemText() {
185         return replacementText;
186     }
187     
188     public String JavaDoc toString() {
189         return getItemText();
190     }
191     
192     Color JavaDoc getPaintColor() { return Color.BLUE; }
193     
194     ////////////////////////////////////////////////////////////////////////////////
195
///////////////////methods from CompletionItem interface////////////////////////
196
////////////////////////////////////////////////////////////////////////////////
197
public CompletionTask createDocumentationTask() {
198         return null; //no documentation supported for now
199
//return new AsyncCompletionTask(new DocQuery(this));
200
}
201     
202     public CompletionTask createToolTipTask() {
203         return null;
204     }
205     
206     public void defaultAction(JTextComponent component) {
207         int substOffset = getSubstituteOffset();
208         if (substOffset == -1)
209             substOffset = component.getCaretPosition();
210         
211         if(!shift) Completion.get().hideAll();
212         substituteText(component, substOffset, component.getCaretPosition() - substOffset, shift);
213     }
214     
215     static int substituteOffset = -1;
216     
217     public int getSubstituteOffset() {
218         return substituteOffset;
219     }
220     
221     public CharSequence JavaDoc getInsertPrefix() {
222         return getItemText();
223     }
224     
225     public Component JavaDoc getPaintComponent(boolean isSelected) {
226         XMLCompletionResultItemPaintComponent component =
227                 new XMLCompletionResultItemPaintComponent.StringPaintComponent(getPaintColor());
228         component.setSelected(isSelected);
229         component.setString(getItemText());
230         return component;
231     }
232     
233     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
234         Component JavaDoc renderComponent = getPaintComponent(false);
235         return renderComponent.getPreferredSize().width;
236     }
237     
238     public int getSortPriority() {
239         return XML_ITEMS_SORT_PRIORITY;
240     }
241     
242     public CharSequence JavaDoc getSortText() {
243         return getItemText();
244     }
245     
246     public boolean instantSubstitution(JTextComponent component) {
247         defaultAction(component);
248         return true;
249     }
250     
251     public void processKeyEvent(KeyEvent JavaDoc e) {
252         shift = (e.getKeyCode() == KeyEvent.VK_ENTER && e.getID() == KeyEvent.KEY_PRESSED && e.isShiftDown());
253     }
254     
255     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont,
256             Color JavaDoc defaultColor, Color JavaDoc backgroundColor,
257             int width, int height, boolean selected) {
258         Component JavaDoc renderComponent = getPaintComponent(selected);
259         renderComponent.setFont(defaultFont);
260         renderComponent.setForeground(defaultColor);
261         renderComponent.setBackground(backgroundColor);
262         renderComponent.setBounds(0, 0, width, height);
263         ((XMLCompletionResultItemPaintComponent)renderComponent).paintComponent(g);
264     }
265     
266 }
267
Popular Tags